Hello and welcome.
In this lesson, we are going to take a look at the TEST instruction.
The TEST instruction takes the format TEST register, immediate or TEST register, register.
These are the two most common formats being used by the TEST instruction.
The purpose of the TEST instruction is to test the bits.
What it essentially does is perform an AND logical operation between two values.
For example, if you have the first value of this bit binary number, and if we wanted to test whether
the fourth bit and the second bit is one, then we will use a test in this way: 1010.
So if any of these bits here are non-zero, then it will result in the zero flag being cleared.
The zero flag here being cleared.
However, if any of these tests, any of the bits here results in a zero, let's say both are zeros,
then the zero flag will be set to one.
So let's take a look at a practical example.
So I've opened the template2.exe file in x64dbg and put the breakpoint here.
So now let us move this value to the - register.
And then we will do a test.
So what we are doing is we are moving this value into the - register, and then we are doing a test
for the second and the fourth bit.
Let us now run to our breakpoint.
And then step over it.
Now we have this in the - register, hex 88.
So let us check what is hex 88 using our programmer's calculator.
Let's look for the calculator and make sure it is set to programmer.
And here we set it to byte.
Now we can select hex and then key in 88.
And we check to make sure that the binary is the same as the one we input.
So it is the same.
Next, we are going to test our - with A.
So A should be 1010.
So we clear this and we check what is A.
And indeed it is 1010.
Now we are going to step over both of these and check the zero flag.
The zero flag is cleared.
So it is correct.
The zero flag is cleared because the result of this binary AND here and AND binary AND over here will yield
a one, a non-zero.
That's why the flag is cleared.
If both of these yield zero, then the zero flag will be set.
So let's try another test now.
We are going to test our -.
And see what we get.
So this test is to test whether or not - is zero.
So we can do that now.
We test - with -.
So we step over this and see what happens to the zero flag.
Now the - has got the value 88 like this.
So when we step over,
it still remains the same.
And the zero flag is still clear because the value in - is a non-zero.
That is why the zero flag is clear.
It is not set.
However, let us now modify the - value to make it zero.
Now it is zero.
And now we perform another test.
- -.
So go with it and watch what happens to the zero flag.
This time the zero flag is set to one because the value in - is zero.
So this is a very common way to use the test in order to check for the value, whether or not a register
is zero.
So whenever you perform a test, if the value in - is zero, then the zero flag will be set.
However, if you perform the test and you find that - is a non-zero, the zero flag will be cleared.
Now, this is very commonly used when testing for the return value from a function.
Whenever we call a function, the function normally returns the result in the - register.
The register value will either be zero, one, negative one, or some other non-zero value.
So by using TEST -, we can quickly determine the result of the function call.
So normally if a function call returns zero, it would mean that the function successfully
ran.
And if it returns negative one,
it means it could mean that the function somehow failed.
The test failed in its function. And in programming in assembly,
this is a normal way for returning values from a function.
So whenever a function returns a value, it will return it in -.
And we use TEST - to check for the value of the function
call, the return value of the function call.
So
TEST has two uses.
One is to check for the individual bits.
And the second use of the TEST is to test whether a register is zero by using a test on itself.
-.
If we wanted to test whether the value in - is zero, then you will use TEST
- -.
Now, one thing to note is that the TEST performs an AND operation, logical AND.
And the good thing about this TEST is that it does not affect the result of the register.
The register's original value is always preserved, unlike an AND operation.
If you do an AND operation, it will alter the result in the - register or - register.
But if you use a TEST, it will not affect the original values in those registers.
So that's why TEST is the preferred method
if we wanted to test for the register value, whether the value is zero or non-zero.
So that's all for this lesson.
Thank you for watching.