Some of this has been covered, but I'll fill in a little more detail.
The general purpose of the test reg,mask
instruction tests a register value against a mask (register value is internally ANDed with mask) and then sets the status flags SF, ZF, and PF according to the result. [EDIT per comment from @ChrisDodd] It also unconditionally clears the O
(overflow) and C
(carry) status bits.[/EDIT]
SF = sign flag (1 if sign bit is set (a 2's complement negative value))
ZF = zero flag (1 if result is 0)
PF = parity flag (1 if result has an even number of 1 bits)
In this specific example (test eax,eax
), the instruction is taking eax AND eax
. When this is done, the bits will be:
SF = 1 if EAX has a negative value (since sign bit will not change when ANDed with itself)
ZF = 1 if EAX is zero (the only value that yields a zero when ANDed with itself is zero)
PF = 1 if EAX has an even number of 1 bits
In other words, it's a simple test for zero or negative. It's a very common pattern in compiler code generation.