用CMP reg,0和OR reg,reg测试寄存器是否为零? [英] Test whether a register is zero with CMP reg,0 vs OR reg,reg?

查看:140
本文介绍了用CMP reg,0和OR reg,reg测试寄存器是否为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码是否存在执行速度差异:

Is there any execution speed difference using the following code:

cmp al, 0
je done

以及以下内容:

or al, al
jz done

我知道JE和JZ指令是相同的,并且使用OR可以使字节大小增加1个字节.但是,我也担心代码速度.看来逻辑运算符会比SUB或CMP快,但我只是想确定一下.这可能是大小和速度之间的权衡,或者是双赢(当然,代码将更加不透明).

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

推荐答案

这取决于确切的代码序列,它是哪个特定的CPU以及其他因素.

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

or al, al,的主要问题是它会修改" EAX,这意味着以某种方式使用EAX的后续指令可能会暂停,直到该指令完成. 请注意,条件分支(jz)也取决于指令,但是CPU制造商做了很多工作(分支预测和推测执行)来减轻这种情况.还要注意,从理论上讲,CPU制造商有可能设计出可以识别EAX在这种特定情况下没有发生变化的CPU,但是有数百种特殊情况,而识别大多数情况的好处太少了

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 al,0的主要问题是它稍大一些,这可能意味着指令获取速度变慢/高速缓存压力增加,并且(如果是循环)可能意味着该代码不再适合某些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".

就像杰斯特在评论中指出的那样; test al,al避免了这两个问题-它比cmp al,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中的值必须来自某个地方,并且如果它来自正确设置标志的指令,则可能可以修改代码以避免使用另一条指令来稍后再设置标志.

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.

这篇关于用CMP reg,0和OR reg,reg测试寄存器是否为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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