为什么大多数的x64指令为零的32位寄存器的上半部分寄存器、指令、为零、部分

2023-09-07 17:24:07 作者:゛_不撞南墙丶不回头。

今天,我了解的x64组件(资料来源:http://x86asm.net/articles/x86-64-tour-of-intel-manuals/)这

Today I learnt about x64 assembly (Source: http://x86asm.net/articles/x86-64-tour-of-intel-manuals/) that

也许最令人惊讶的事实是,一个指令,如MOV EAX,EBX自动归零高32位的RAX寄存器中。

Perhaps the most surprising fact is that an instruction such as MOV EAX, EBX automatically zeroes upper 32 bits of RAX register.

英特尔文档(手动基本架构64位模式3.4.1.1通用寄存器)在同一来源引述告诉我们:

The Intel documentation (3.4.1.1 General-Purpose Registers in 64-Bit Mode in manual Basic Architecture) quoted at the same source tells us:

  在64位运算生成一个64位的结果到目标通用寄存器。    32位操作数产生一个32位结果,零扩展到64位结果在目标通用寄存器。    8位和16位操作数产生一个8位或16位的结果。上部56位或48位的目的地通用寄存器的(分别地)未由操作进行修改。如果一个8位或16位操作的结果是用于64位地址计算,明确地符号扩展寄存器的全64位。    64-bit operands generate a 64-bit result in the destination general-purpose register. 32-bit operands generate a 32-bit result, zero-extended to a 64-bit result in the destination general-purpose register. 8-bit and 16-bit operands generate an 8-bit or 16-bit result. The upper 56 bits or 48 bits (respectively) of the destination general-purpose register are not be modified by the operation. If the result of an 8-bit or 16-bit operation is intended for 64-bit address calculation, explicitly sign-extend the register to the full 64-bits.

在X32汇编,16位指令,如

In x32 assembly, 16 bit instructions such as

mov ax, bx

没有显示出这种奇怪行为EAX的高字被清零。

don't show this kind of "strange" behaviour that the upper word of eax is zeroed.

因此​​:究竟是为什么这种行为被引入的原因是什么?乍一看,这似乎不合逻辑(但原因可能是因为我习惯了X32汇编的怪癖)。

Thus: what is the reason why this behaviour was introduced? At a first glance it seems illogical (but the reason might be that I am used to the quirks of x32 assembly).

推荐答案

我不是AMD或讲话他们,但我会做同样的方式。由于归零高一半不创建于previous值的依赖,该CPU将不得不等待。如果它不能那么做的寄存器重命名机制,将从根本上被击败。这样,您可以写快32位code。在64位模式,而无需显式地打破依赖所有的时间。如果没有这种行为,在64位模式下的每一个32位指令将不得不等待事情以前发生过,即使是高的部分会几乎从来没有被使用。

I'm not AMD or speaking for them, but I would have done it the same way. Because zeroing the high half doesn't create a dependency on the previous value, that the cpu would have to wait on. The register renaming mechanism would essentially be defeated if it wasn't done that way. This way you can write fast 32bit code in 64bit mode without having to explicitly break dependencies all the time. Without this behaviour, every single 32bit instruction in 64bit mode would have to wait on something that happened before, even though that high part would almost never be used.

为16位指令的行为是奇怪的。依赖关系是疯狂的原因之一是16位的指令,现在可以避免的。

The behaviour for 16bit instructions is the strange one. The dependency madness is one of the reasons that 16bit instructions are avoided now.