WEBVTT

1
00:01.550 --> 00:03.140
Hello and welcome back.

2
00:03.140 --> 00:07.280
In this lesson, we are going to study the compare instruction.

3
00:07.760 --> 00:13.910
So the compare instruction is also used in the same way as the TEST instruction.

4
00:14.960 --> 00:22.340
Like in the TEST instruction, we use it to test for bits, but it can also be used for testing the result

5
00:22.340 --> 00:25.220
of some kind of function return.

6
00:25.490 --> 00:27.650
So the same thing with compare.

7
00:27.650 --> 00:28.490
It can also do that.

8
00:28.490 --> 00:30.500
But compare cannot test bits.

9
00:30.590 --> 00:40.070
Basically, the compare instruction is another way to check if a register's value, such as -, is zero.

10
00:40.460 --> 00:47.540
The compare instruction is specifically designed for comparing two values, and is essentially a subtraction

11
00:47.540 --> 00:51.890
operation that affects the flags but doesn't store the result.

12
00:52.280 --> 00:56.900
It is often used to set up conditional jumps based on the comparison.

13
00:58.060 --> 01:05.350
To check if - is zero using compare, you would compare - with zero.

14
01:05.740 --> 01:07.930
So here is how it looks like.

15
01:08.290 --> 01:09.820
So let's try this now.

16
01:10.630 --> 01:14.770
Let's open template2.exe in x64dbg.

17
01:15.640 --> 01:17.740
Move down and put a breakpoint here.

18
01:18.760 --> 01:22.330
Let's do our code for compare.

19
01:23.710 --> 01:29.710
So let's do a compare here, CMP - with zero.

20
01:31.240 --> 01:32.170
Click okay.

21
01:36.280 --> 01:38.440
Now we're going to run to our breakpoint.

22
01:40.440 --> 01:45.060
It hits our breakpoint as we step over it until we come here.

23
01:45.510 --> 01:50.250
So now over here, it's going to compare - with zero.

24
01:50.250 --> 01:55.500
That means it's going to take whatever in - and minus zero and see the result.

25
01:55.650 --> 01:59.820
If the result is indeed zero, then the zero flag will be set.

26
02:01.010 --> 02:04.070
If the result is non-zero, the zero flag will be cleared.

27
02:04.370 --> 02:06.170
So let us put something in.

28
02:08.420 --> 02:12.050
Modify the value here and put zero in.

29
02:14.330 --> 02:15.380
So now we have zero.

30
02:15.380 --> 02:18.890
And so it's going to take zero minus zero.

31
02:18.890 --> 02:20.270
And the result will be zero.

32
02:20.270 --> 02:22.220
So the zero flag should be set.

33
02:22.490 --> 02:24.860
So let's step over that now and see it happening.

34
02:25.580 --> 02:31.580
And as you see, the result of the subtraction is zero.

35
02:31.610 --> 02:34.400
Therefore, the zero flag is set.

36
02:35.480 --> 02:43.490
Now this instruction, compare - to zero, is essentially subtracting zero from - and sets the flag based

37
02:43.490 --> 02:47.450
on this operation, and the flag that is set is the zero flag.

38
02:48.360 --> 02:52.560
If - is indeed zero, the subtraction result is zero,

39
02:52.560 --> 02:54.180
setting the zero flag.

40
02:56.280 --> 03:04.170
So the choice between TEST and compare for checking if a value is zero can depend on the context and

41
03:04.170 --> 03:12.540
the programmer's preference. TEST is generally used for bitwise tests, like checking specific bits, while

42
03:12.540 --> 03:16.770
compare is a more general-purpose comparison instruction.

43
03:17.190 --> 03:25.590
Both will effectively set the zero flag if the register being tested or compared holds a zero value.

44
03:26.400 --> 03:28.470
So that's all for this video.

45
03:28.470 --> 03:30.180
Thank you for watching.