Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In x86 what's difference between "test eax,eax" and "cmp eax,0"

Tags:

x86

assembly

Is test eax, eax more efficient than cmp eax, 0? Is there any case that the test eax, eax is necessary where cmp eax, 0 doesn't fulfill requirement?

like image 454
Hind Forsum Avatar asked Sep 18 '16 10:09

Hind Forsum


People also ask

What is test EAX EAX?

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.

How does CMP work Assembly?

The CMP instruction compares two operands. It is generally used in conditional execution. This instruction basically subtracts one operand from the other for comparing whether the operands are equal or not. It does not disturb the destination or source operands.

What does x86 test do?

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.


1 Answers

As Zang MingJie has already said in a comment, test eax, eax is almost identical to cmp eax, 0, except that it is shorter than cmp, because with cmp you have to supply 0 as an argument. Note that the savings are not very large, because the 2nd operand gets sign-extended to match the size of the 1st operand, so it does not necessarily take a whole 4 bytes to represent that zero.

Now, what you are asking is whether there is any other difference. This is a reasonable question to ask, because cmp is an arithmetic operation, (it performs a subtraction and discards the result,) while test is a logical operation, (it performs a bitwise AND and discards the result,) so one could reasonably suspect that they may modify the Flags register differently.

As it turns out, both instructions modify the Flags register in an almost identical fashion. Both instructions modify the OF SF ZF AF PF and CF bits of the flags register. The test instruction always clears OF and CF, but that's also what cmp against zero does. The only other difference is that the cmp instruction will properly set the obscure AF flag, while the test instruction leaves the contents of that flag undefined. But in the case of cmp eax,0 the AF will always be cleared regardless of the value of eax, so there is nothing that you can learn from a cmp eax, 0 that you would not learn from a test eax, eax.

Therefore, I would conclude that there is no situation where test eax, eax will give you something that cmp eax, 0 will not, nor vice versa. The two instructions appear to be completely interchangeable for any practical or even not-so-practical purpose, except for saving a byte or two of instruction code.

Using test eax, eax instead of cmp eax, 0 shows that you know your assembly. It also shows that you prefer a slightly cryptic, and marginally better performing instruction over a straightforward, understandable instruction. This is the kind of thing that tends to earn bonus points from other geeks, but it has not had any practical usefulness in the real world in the last couple of decades or so.

like image 74
Mike Nakis Avatar answered Sep 20 '22 18:09

Mike Nakis