(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?
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.
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With