x86汇编:CMP REG,0 VS或REG,REG [英] x86 assembly: CMP REG, 0 vs OR REG, REG

查看:608
本文介绍了x86汇编:CMP REG,0 VS或REG,REG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有用下面的code的执行速度区别:

Is there any execution speed difference using the following code:

cmp al, 0
je done

和以下内容:

or al, al
jz done

我知道JE和JZ指令是相同的,并且也在使用或提供一个字节的大小的改善。不过,我也关注与code的速度。看来,逻辑运算符会比一个SUB或CMP更快,但我只是想确认一下。这可能是规模和速度,或者一个双赢的(当然code将会更加不透明)。

I know that the JE and JZ instructions are the same, and also that using OR gives a size improvement of one byte. However, I am also concerned with code speed. It seems that logical operators will be faster than a SUB or a CMP, but I just wanted to make sure. This might be a trade-off between size and speed, or a win-win (of course the code will be more opaque).

推荐答案

这取决于确切code序列,它是特定的CPU,以及其他因素。

It depends on the exact code sequence, which specific CPU it is, and other factors.

或人,人,的主要问题是,它修改 EAX ,这意味着使用 EAX 在某种程度上后续指令可能会停止,直到该指令完成。的请注意,条件分支( JZ ),还取决于指令,但CPU厂商做了很多工作(分支prediction和推测执行),以减轻那。还要注意,在理论上是可能的CPU制造商设计一个可以识别CPU EAX 在这种特定情况没有改变,但也有数以百计的这些特殊情况并认识到他们最的好处是太少。

The main problem with or al, al, is that it "modifies" EAX, which means that a subsequent instruction that uses EAX in some way may stall until this instruction completes. Note that the conditional branch (jz) also depends on the instruction, but CPU manufacturers do a lot of work (branch prediction and speculative execution) to mitigate that. Also note that in theory it would be possible for a CPU manufacturer to design a CPU that recognises EAX isn't changed in this specific case, but there are hundreds of these special cases and the benefits of recognising most of them are too little.

CMP人的主要问题,0 的是它的稍大,这可能意味着较慢取指令/更多的缓存pressure,并且(如果它是一个环)可能意味着code不再在一些CPU的循环缓冲区适合。

The main problem with cmp al,0 is that it's slightly larger, which might mean slower instruction fetch/more cache pressure, and (if it is a loop) might mean that the code no longer fits in some CPU's "loop buffer".

由于小丑在评论中指出, 测试人,人避免了这两个问题 - 它比 CMP人小,0 并且不修改 EAX

As Jester pointed out in comments; test al,al avoids both problems - it's smaller than cmp al,0 and doesn't modify EAX.

当然(取决于特定序列)中 AL 的值一定是从什么地方来,并且,如果它从该设置的标志的指令来适当它可能是可以修改code,以避免使用其他指令后重新设置标志。

Of course (depending on the specific sequence) the value in AL must've come from somewhere, and if it came from an instruction that set flags appropriately it might be possible to modify the code to avoid using another instruction to set flags again later.

这篇关于x86汇编:CMP REG,0 VS或REG,REG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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