WEBVTT

1
00:01.700 --> 00:03.050
Hello and welcome.

2
00:03.050 --> 00:07.670
In this lesson, we are going to take a look at the TEST instruction.

3
00:08.720 --> 00:16.670
The TEST instruction takes the format TEST register, immediate or TEST register, register.

4
00:16.670 --> 00:22.040
These are the two most common formats being used by the TEST instruction.

5
00:22.880 --> 00:27.170
The purpose of the TEST instruction is to test the bits.

6
00:27.350 --> 00:33.050
What it essentially does is perform an AND logical operation between two values.

7
00:33.050 --> 00:40.760
For example, if you have the first value of this bit binary number, and if we wanted to test whether

8
00:40.760 --> 00:50.120
the fourth bit and the second bit is one, then we will use a test in this way: 1010.

9
00:50.930 --> 01:01.940
So if any of these bits here are non-zero, then it will result in the zero flag being cleared.

10
01:03.110 --> 01:04.880
The zero flag here being cleared.

11
01:05.390 --> 01:13.970
However, if any of these tests, any of the bits here results in a zero, let's say both are zeros,

12
01:13.970 --> 01:18.080
then the zero flag will be set to one.

13
01:19.220 --> 01:22.370
So let's take a look at a practical example.

14
01:22.790 --> 01:30.200
So I've opened the template2.exe file in x64dbg and put the breakpoint here.

15
01:30.410 --> 01:34.370
So now let us move this value to the - register.

16
01:46.160 --> 01:47.960
And then we will do a test.

17
02:03.570 --> 02:12.360
So what we are doing is we are moving this value into the - register, and then we are doing a test

18
02:15.840 --> 02:18.270
for the second and the fourth bit.

19
02:25.330 --> 02:27.190
Let us now run to our breakpoint.

20
02:27.820 --> 02:29.080
And then step over it.

21
02:30.610 --> 02:36.280
Now we have this in the - register, hex 88.

22
02:36.280 --> 02:40.990
So let us check what is hex 88 using our programmer's calculator.

23
02:45.430 --> 02:51.280
Let's look for the calculator and make sure it is set to programmer.

24
02:51.910 --> 02:54.250
And here we set it to byte.

25
02:55.330 --> 02:58.630
Now we can select hex and then key in 88.

26
02:58.630 --> 03:05.080
And we check to make sure that the binary is the same as the one we input.

27
03:06.220 --> 03:07.540
So it is the same.

28
03:08.200 --> 03:12.730
Next, we are going to test our - with A.

29
03:12.760 --> 03:14.860
So A should be 1010.

30
03:15.580 --> 03:18.430
So we clear this and we check what is A.

31
03:18.580 --> 03:20.710
And indeed it is 1010.

32
03:22.270 --> 03:27.940
Now we are going to step over both of these and check the zero flag.

33
03:30.070 --> 03:31.180
The zero flag is cleared.

34
03:31.960 --> 03:33.370
So it is correct.

35
03:33.970 --> 03:42.250
The zero flag is cleared because the result of this binary AND here and AND binary AND over here will yield

36
03:42.670 --> 03:45.640
a one, a non-zero.

37
03:45.790 --> 03:48.100
That's why the flag is cleared.

38
03:50.100 --> 03:54.420
If both of these yield zero, then the zero flag will be set.

39
03:54.900 --> 03:58.470
So let's try another test now.

40
03:59.520 --> 04:02.370
We are going to test our -.

41
04:04.830 --> 04:06.300
And see what we get.

42
04:07.560 --> 04:13.080
So this test is to test whether or not - is zero.

43
04:14.250 --> 04:15.930
So we can do that now.

44
04:16.050 --> 04:20.280
We test - with -.

45
04:23.950 --> 04:28.180
So we step over this and see what happens to the zero flag.

46
04:28.960 --> 04:33.250
Now the - has got the value 88 like this.

47
04:34.480 --> 04:36.220
So when we step over,

48
04:38.410 --> 04:39.850
it still remains the same.

49
04:39.850 --> 04:47.320
And the zero flag is still clear because the value in - is a non-zero.

50
04:47.320 --> 04:49.450
That is why the zero flag is clear.

51
04:49.870 --> 04:50.860
It is not set.

52
04:52.000 --> 04:57.640
However, let us now modify the - value to make it zero.

53
05:01.340 --> 05:02.330
Now it is zero.

54
05:02.330 --> 05:04.310
And now we perform another test.

55
05:05.210 --> 05:06.260
- -.

56
05:12.000 --> 05:14.730
So go with it and watch what happens to the zero flag.

57
05:15.870 --> 05:19.770
This time the zero flag is set to one because the value in - is zero.

58
05:20.820 --> 05:28.770
So this is a very common way to use the test in order to check for the value, whether or not a register

59
05:28.770 --> 05:29.520
is zero.

60
05:30.450 --> 05:39.210
So whenever you perform a test, if the value in - is zero, then the zero flag will be set.

61
05:40.440 --> 05:46.800
However, if you perform the test and you find that - is a non-zero, the zero flag will be cleared.

62
05:47.970 --> 05:54.390
Now, this is very commonly used when testing for the return value from a function.

63
05:55.050 --> 06:02.370
Whenever we call a function, the function normally returns the result in the - register.

64
06:03.030 --> 06:09.060
The register value will either be zero, one, negative one, or some other non-zero value.

65
06:09.510 --> 06:15.570
So by using TEST -, we can quickly determine the result of the function call.

66
06:16.140 --> 06:23.820
So normally if a function call returns zero, it would mean that the function successfully

67
06:23.820 --> 06:24.480
ran.

68
06:25.050 --> 06:27.030
And if it returns negative one,

69
06:27.960 --> 06:31.350
it means it could mean that the function somehow failed.

70
06:31.380 --> 06:38.100
The test failed in its function. And in programming in assembly,

71
06:38.130 --> 06:42.540
this is a normal way for returning values from a function.

72
06:42.780 --> 06:46.200
So whenever a function returns a value, it will return it in -.

73
06:46.200 --> 06:52.110
And we use TEST - to check for the value of the function

74
06:52.110 --> 06:54.450
call, the return value of the function call.

75
06:55.590 --> 06:56.550
So

76
06:56.550 --> 06:58.530
TEST has two uses.

77
06:58.530 --> 07:01.200
One is to check for the individual bits.

78
07:01.200 --> 07:08.940
And the second use of the TEST is to test whether a register is zero by using a test on itself.

79
07:08.970 --> 07:09.960
-.

80
07:10.350 --> 07:15.120
If we wanted to test whether the value in - is zero, then you will use TEST

81
07:15.420 --> 07:15.960
- -.

82
07:17.350 --> 07:23.920
Now, one thing to note is that the TEST performs an AND operation, logical AND.

83
07:24.490 --> 07:30.970
And the good thing about this TEST is that it does not affect the result of the register.

84
07:31.330 --> 07:36.490
The register's original value is always preserved, unlike an AND operation.

85
07:36.790 --> 07:45.760
If you do an AND operation, it will alter the result in the - register or - register.

86
07:46.270 --> 07:52.660
But if you use a TEST, it will not affect the original values in those registers.

87
07:53.200 --> 07:57.160
So that's why TEST is the preferred method

88
07:57.160 --> 08:03.730
if we wanted to test for the register value, whether the value is zero or non-zero.

89
08:04.570 --> 08:06.580
So that's all for this lesson.

90
08:06.610 --> 08:08.290
Thank you for watching.