测试 ia32 中的寄存器是否等于自身

发布于 2024-09-01 04:55:54 字数 251 浏览 3 评论 0原文

(ia32) 例如,

test $eax, $eax

您为什么要这样做?它确实 $eax & $eax,对吗?这不应该总是设置标志寄存器来表示它们是相等的..?

附录: 因此,如果寄存器为零,测试将设置 ZF(如下所述)。那么测试(如上所述)主要用于判断寄存器是否为空?如果是这样,ZF 是否已设置?

(ia32) for example,

test $eax, $eax

why would you ever want to do that? it does $eax & $eax, right? shouldn't this always set the flag register to say that they are equal..?

addendum:
so test will set the ZF (as mentioned below) if the register is zero. so is test (as used above) mainly just used to tell if a register is empty? and ZF is set if so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如何视而不见 2024-09-08 04:55:54

如果寄存器为零,它将设置 ZF(零标志)。这可能是最常用于测试的内容。它还会适当地设置其他标志,但这些标志的用途可能要少得多。

另外,我应该提到 test 并不真正执行比较 - 它执行按位 and 操作(丢弃除标志之外的结果)。

要执行操作数的比较,将使用 cmp 指令,该指令执行 sub 操作,丢弃除标志之外的结果。您是正确的,a

cmp $eax, $eax

没有多大意义,因为每次都会根据零结果设置标志。

It'll set ZF (the zero flag) if the register is zero. That's probably what it's most commonly used to test. It'll also set other flags appropriately, but there's probably far less use for those.

Also, I should mention that test doesn't really perform a comparison - it performs a bitwise and operation (throwing away the result, except for the flags).

To perform a comparison of the operands, the cmp instruction would be used, which performs a sub operation, throwing away the results except for the flags. You are correct that a

cmp $eax, $eax

would not have much point, as the flags would be set according to a zero result every time.

梦回梦里 2024-09-08 04:55:54

它在处理器上设置 Z 和 S 标志,因此您可以在“test eax,eax”(或任何其他寄存器 8、16、32,我认为是 64 位)之后判断该值是否为零,或者符号位是否为分别使用“je/jne/jz/jnz”或“js/jns”进行设置。在 30 年的 x80/x86 架构编码中,我已经对大多数寄存器组合进行了很多次这样的操作。 (您在 ESP 上实际上不会使用它!)

IIRC,也计算了一个奇偶校验位,因此通过执行此测试,您可以使用“jp/jnp”判断寄存器中设置的位数是偶数还是奇数”。我从未用过这个。

It sets the Z and S flags on the processor, so you can tell after "test eax,eax" (or any other register 8, 16, 32 and I think 64 bits) if the value is zero, or if the sign bit is set, by using "je/jne/jz/jnz" or "js/jns" respectively. In 30 years of coding for x80/x86 architectures, I've done this a huge number of times, with most of the register combinations. (You don't use this in practice on ESP!)

IIRC, there's a parity bit that's calculated too, so by do this test you can tell if the number of bits set in the register is even or odd, using "jp/jnp". I've never had a use for this.

油焖大侠 2024-09-08 04:55:54

该指令并非仅检查 %eax 的值是否为零。它可以作为一个整体用于检查值 %eax 是否为零、正数或负数。
这种方式的最大优点是它不会修改 %eax 的值(执行%eax & %eax 后,它只是丢弃该值)并按如下方式设置条件标志。

如果 %eax 值为零,OF、CF、ZF = 0(设置为零)
else SF = 结果的 MSB(此处,结果为 %eax & %eax)。因此,如果数字为负数,则得到 SF = 1,否则得到 SF = 0

This instruction is not meant to check only if the value of %eax is zero. It can be as a whole used to check if the value %eax is zero or positive or negative.
The biggest advantage of using it this way is it doesn't modify the vale of %eax (after performing %eax & %eax, it just discards away the value) and sets the condition flags as follows.

if %eax value is zero, OF, CF, ZF = 0 (set to zero)
else SF = MSB of the result (here, result is %eax & %eax). So if the number is negative, we get SF = 1, otherwise SF = 0.

最舍不得你 2024-09-08 04:55:54

这是设置零标志,与使用 或 $eax,$eax 也可以非破坏性地测试零标志的方式相同。

This is setting the zero flag, the same way that using or $eax,$eax can non-destructively test for the zero flag as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文