Testing if case is equal to itself in ia32

(ia32) for example

test $eax, $eax 

why do you have to do this? he does $eax & $eax , right? shouldn't this always set the flag case to say that they are equal ..?

Application: therefore, the test will set ZF (as indicated below) if the register is zero. so the test (as used above) is mainly used only to determine if the register is empty? and ZF is installed if so?

+7
x86 cpu-registers testing
source share
4 answers

It will set ZF (zero flag) if the register is zero. This is probably what he most often tested. He will also set other flags accordingly, but for them, probably much less.

In addition, I should mention that test does not really perform the comparison - it performs the bitwise operation and (discarding the result, with the exception of flags).

To compare the operands, the cmp command will be used, which performs the sub operation, discarding the results, with the exception of flags. You are right that

 cmp $eax, $eax 

will not make much sense, since flags will be set in accordance with a zero result each time.

+9
source share

This sets the zero flag, just as with or $eax,$eax can non-destructively check the zero flag.

+1
source share

It sets the Z and S flags on the processor, so you can say "test eax, eax" (or any other register 8, 16, 32 and I think 64 bits) if the value is zero, or if the character bit is set using " je / jne / jz / jnz "or" js / jns "respectively. Over 30 years of programming for the x80 / โ€‹โ€‹x86 architecture, I have done this a huge number of times, with most combinations of registers. (You do not put this into practice on ESP!)

IIRC, the parity bit is also calculated there, therefore, by performing this test, you can determine whether the number of bits set in the register is even or odd using "jp / jnp". I have never used this.

+1
source share

This command is not intended to be checked only if the value of %eax is zero. It can generally be used to check if the %eax value is zero, positive, or negative. The biggest advantage of using this method is that it does not change the %eax valleys (after executing %eax & %eax it simply discards the value) and sets the condition flags as follows.

if the value of% eax is zero, OF, CF, ZF = 0 (set to zero)
else SF = MSB of the result (here the result is %eax & %eax ). Therefore, if the number is negative, we get SF = 1 , otherwise SF = 0 .

+1
source share

All Articles