Test eax eax

broken image

Both instructions modify the OF SF ZF AF PF and CF bits of the flags register. 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.Īs it turns out, both instructions modify the Flags register in an almost identical fashion. Now, what you are asking is whether there is any other difference. 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.

broken image
broken image

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? 最佳答案Īs Zang MingJie has already said in a comment, test eax,eax is almost identical to cmp eax,0, except that it shorter than cmp, because with cmp you have to supply 0 as an argument.

broken image