在x86中,"test eax,eax"和"test eax,eax"之间有什么区别?和"cmp eax,0&" [英] In x86 what's difference between "test eax,eax" and "cmp eax,0"

查看:165
本文介绍了在x86中,"test eax,eax"和"test eax,eax"之间有什么区别?和"cmp eax,0&"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

test eax, eax是否比cmp eax, 0更有效?在cmp eax, 0不满足要求的情况下,是否有必要使用test eax, eax?

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?

推荐答案

正如Zang MingJie在评论中所说,test eax,eaxcmp eax,0几乎相同,只是它比cmp短,因为cmp,您必须提供0作为参数.请注意,节省的空间不是很大,因为第二个操作数会进行符号扩展以匹配第一个操作数的大小,因此不一定要用整个4个字节来表示那个零.

As 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. 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.

现在,您要问的是是否还有其他区别.这是一个合理的问题,因为cmp是算术运算(执行减法并丢弃结果),而test是逻辑运算(执行按位与并丢弃结果),因此人们可能会合理地怀疑他们可能会不同地修改Flags寄存器.

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.

事实证明,两条指令都以几乎相同的方式修改Flags寄存器.两条指令都修改了标志寄存器的OF SF ZF AF PF和CF位. test指令始终清除OF和CF,但这也是针对零的cmp的作用.唯一的不同是cmp指令将正确设置晦涩的AF标志,而test指令则使该标志的内容未定义.但是,对于cmp eax,0,无论eax的值如何,都将始终清除AF,因此您无法从cmp eax,0中学到什么,而不会从test eax,eax中学习.

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.

因此,我得出的结论是,在任何情况下test eax,eax都不会给您cmp eax,0不会给您的东西,反之亦然.这两个指令对于任何实际甚至不是实际的目的似乎都是可以互换的,除了保存一个或两个字节的指令代码外.

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.

使用test eax,eax而不是cmp eax,0表示您知道您的程序集.它还表明,与简单易懂的指令相比,您更喜欢隐晦的,性能稍佳的指令.这类东西往往会从其他极客那里获得奖励积分,但是在过去的几十年左右的时间里,它在现实世界中没有任何实际用途.

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.

这篇关于在x86中,"test eax,eax"和"test eax,eax"之间有什么区别?和"cmp eax,0&"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆