The point of test %eax %eax [duplicate]

or test %rdi, %rdi , etc. etc. I'm very confused as to what this does. Isn't the values in %eax, %eax the same? What is it testing? I read somewhere that it is doing the AND operation. but since they are the same value, wouldn't it just return %eax ? The following is just one instance where I found this usage:

 400e6e: 85 c0 test %eax,%eax 400e70: 74 05 je 400e77

I thought je jumps if the two values being compared are equal. well, because %eax is well, itself, in what situation would we NOT jump? I'm a beginner to programming in general, so I'd appreciate it very much if someone could explain this to me. Thanks!

47.2k 9 9 gold badges 115 115 silver badges 210 210 bronze badges asked Oct 25, 2012 at 8:43 6,605 11 11 gold badges 43 43 silver badges 52 52 bronze badges

Since some answers seem a little bit unclear about it, let me point out that TEST updates other flags apart from ZF as well. See the instruction set reference.

Commented Oct 25, 2012 at 12:15 @Jester fixed (in my answer), sorry. Commented Aug 19, 2014 at 16:45 Another possible duplicate: What does the test instruction do? Commented Oct 16, 2015 at 0:56

5 Answers 5

CMP subtracts the operands and sets the flags. Namely, it sets the zero flag if the difference is zero (operands are equal).

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.

JE [Jump if Equals] tests the zero flag and jumps if the flag is set. JE is an alias of JZ [Jump if Zero] so the disassembler cannot select one based on the opcode. JE is named such because the zero flag is set if the arguments to CMP are equal.

TEST %eax, %eax JE 400e77

jumps if the %eax is zero.

answered Oct 25, 2012 at 8:53 John Dvorak John Dvorak 27.2k 13 13 gold badges 71 71 silver badges 85 85 bronze badges where can I find information like this? Commented Aug 19, 2014 at 15:14

namely? The list of x86 instructions is on Wikipedia, which also links a spec by Intel as well as another (quite readable) reference on wayback machine. A tutorial is also available on Wikibooks.

Commented Aug 19, 2014 at 16:31 yes, this is the information I was looking for Commented Aug 19, 2014 at 16:39 Jumps if %eax is zero, that's what I was looking for. Commented Apr 3, 2017 at 18:56 Commented Apr 21, 2017 at 9:04

Some x86 instructions are designed to leave the content of the operands (registers) as they are and just set/unset specific internal CPU flags like the zero-flag (ZF). You can think at the ZF as a true/false boolean flag that resides inside the CPU.

in this particular case, TEST instruction performs a bitwise logical AND, discards the actual result and sets/unsets the ZF according to the result of the logical and: if the result is zero it sets ZF = 1, otherwise it sets ZF = 0.

Conditional jump instructions like JE are designed to look at the ZF for jumping/notjumping so using TEST and JE together is equivalent to perform a conditional jump based on the value of a specific register:

TEST EAX,EAX JE some_address 

the CPU will jump to "some_address" if and only if ZF = 1, in other words if and only if AND(EAX,EAX) = 0 which in turn it can occur if and only if EAX == 0

the equivalent C code is: