Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testing if a register equals itself in ia32

(ia32) for example,

test $eax, $eax

why would you ever want to do that? it does $eax & $eax, right? shouldn't this always set the flag register to say that they are equal..?

addendum: so test will set the ZF (as mentioned below) if the register is zero. so is test (as used above) mainly just used to tell if a register is empty? and ZF is set if so?

like image 577
Tony Stark Avatar asked May 10 '10 23:05

Tony Stark


People also ask

How can we check whether two pieces of data each from a register are equal?

One of the first relational operators any programmer comes across is the equality/equivalence operator or ==. It is used to evaluate whether any given two operands are equal to each other or not.

What does test %eax %eax do?

This checks if EAX is zero. The instruction test does bitwise AND between the arguments, and if EAX contains zero, the result sets the ZF, or ZeroFlag. This is the standard way to do it.

What is the point of Test EAX and EAX?

Note that eax is used to store the return code of a function, the test test eax, eax is very often used to check this return code after a call (usually, this is the converse and eax == 0 means that an error occurred).

What is difference between CMP and test instruction?

The TEST instruction performs an implied AND operation between corresponding bits in the two operands and sets the flags without modifying either operand. reg, mem, and immed can be 8, 16, or 32 bits. The CMP instruction sets the flags as if it had performed subtraction on the operand.


1 Answers

It'll set ZF (the zero flag) if the register is zero. That's probably what it's most commonly used to test. It'll also set other flags appropriately, but there's probably far less use for those.

Also, I should mention that test doesn't really perform a comparison - it performs a bitwise and operation (throwing away the result, except for the flags).

To perform a comparison of the operands, the cmp instruction would be used, which performs a sub operation, throwing away the results except for the flags. You are correct that a

cmp $eax, $eax

would not have much point, as the flags would be set according to a zero result every time.

like image 106
Michael Burr Avatar answered Oct 17 '22 12:10

Michael Burr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!