Hello and welcome back.
In this lesson, we are going to study the compare instruction.
So the compare instruction is also used in the same way as the TEST instruction.
Like in the TEST instruction, we use it to test for bits, but it can also be used for testing the result
of some kind of function return.
So the same thing with compare.
It can also do that.
But compare cannot test bits.
Basically, the compare instruction is another way to check if a register's value, such as -, is zero.
The compare instruction is specifically designed for comparing two values, and is essentially a subtraction
operation that affects the flags but doesn't store the result.
It is often used to set up conditional jumps based on the comparison.
To check if - is zero using compare, you would compare - with zero.
So here is how it looks like.
So let's try this now.
Let's open template2.exe in x64dbg.
Move down and put a breakpoint here.
Let's do our code for compare.
So let's do a compare here, CMP - with zero.
Click okay.
Now we're going to run to our breakpoint.
It hits our breakpoint as we step over it until we come here.
So now over here, it's going to compare - with zero.
That means it's going to take whatever in - and minus zero and see the result.
If the result is indeed zero, then the zero flag will be set.
If the result is non-zero, the zero flag will be cleared.
So let us put something in.
Modify the value here and put zero in.
So now we have zero.
And so it's going to take zero minus zero.
And the result will be zero.
So the zero flag should be set.
So let's step over that now and see it happening.
And as you see, the result of the subtraction is zero.
Therefore, the zero flag is set.
Now this instruction, compare - to zero, is essentially subtracting zero from - and sets the flag based
on this operation, and the flag that is set is the zero flag.
If - is indeed zero, the subtraction result is zero,
setting the zero flag.
So the choice between TEST and compare for checking if a value is zero can depend on the context and
the programmer's preference. TEST is generally used for bitwise tests, like checking specific bits, while
compare is a more general-purpose comparison instruction.
Both will effectively set the zero flag if the register being tested or compared holds a zero value.
So that's all for this video.
Thank you for watching.