I'm looking at some small assembler codes and I'm having trouble understanding the TEST instruction and its use. I'm looking at the following code at the end of a loop:
8048531: 84 c0 test al,al 8048533: 75 dc jne 8048511 <function+0x2d>
The way i understand TEST is that it works a bit like the AND operator and it sets some flags. I guess I don't really understand how the flags work. test al,al
to me looks like it checks the same lower bits and will always get the same results.
Can someone explain?
In the x86 assembly language, the TEST instruction performs a bitwise AND on two operands. The flags SF , ZF , PF are modified while the result of the AND is discarded. The OF and CF flags are set to 0 , while AF flag is undefined.
eax contains the return value of strcmp. test is like bitwise and except it only sets the flags. Anding a value with itself gives the same value, so test eax, eax sets the flags based on whatever eax contains. ZF is set when the result of an operation is zero.
TEST sets the zero flag, ZF , when the result of the AND operation is zero. If two operands are equal, their bitwise AND is zero when both are zero. TEST also sets the sign flag, SF , when the most significant bit is set in the result, and the parity flag, PF , when the number of set bits is even.
For MOVZBL, the low 8 bits of the destination are replaced by the source operand. the top 24 bits are set to 0. The source operand is unaffected. For MOVZBW, the low 16 bits of the destination are replaced by the source operand.
It tests the register against itself, just to set the flags. The result will be different for a zero and a non-zero value.
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