为什么在调用 jnz 之前要与自身 OR 值? [英] Why would you OR a value with itself before a call to jnz?

查看:24
本文介绍了为什么在调用 jnz 之前要与自身 OR 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一些执行以下操作的代码:

I'm reading some code that does the following:

OR al, al
JNZ loc_123456

如果我没看错,OR 命令只会设置一个标志位,然后测试该寄存器是否没有任何非零值.

If I am reading this correctly, the OR command just sets a flag bit, which is then tested that the register doesn't have any nonzero values.

你为什么要这样写,而不是

Why would you write it that way, and not

CMP al, 0
JNE loc_123456

哪个更具可读性?我假设硬件在每种情况下都在做类似的事情......

which is much more readable? I assume the hardware is doing something similar in each case...

推荐答案

正如您所说,OR 操作的执行会设置状态标志,您可以使用以下 JNZ(若非零则跳转)条件分支进行测试.

As you state, the execution of the OR operation sets the status flags, which you test with the following JNZ (Jump if Not Zero) conditional branch.

许多具有隐式寄存器寻址的处理器上的 OR 操作码(虽然不是在 x86 上)可以编码为单个操作码.

The OR opcode on many processors with implicit register addressing (though not on the x86) can be encoded into a single opcode.

使用 CMP al,0 使用操作码并为立即参数使用额外的内存.所以基本上只是为了节省一点内存并可能获得一点速度.

Using CMP al, 0 takes the opcode plus uses additional memory for the immediate parameter. So it is basically just to save a little memory and possibly gain a bit of speed.

当内存不足时,这些是早期汇编语言开发中的常见做法.但这种做法一直延续到今天.

Those were common practices in early assembly language development when memory was scarce. But the practices have carried through to even today.

这篇关于为什么在调用 jnz 之前要与自身 OR 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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