简体   繁体   English

位运算符(x86 汇编):移位时二进制数据 go 的 rest 在哪里?

[英]Bitwise operators (x86 Assembly): Where does the rest of the binary data go when shifting it?

I know that when I execute the following line of Assembly x86 code:我知道,当我执行以下汇编 x86 代码行时:

shrl $31, %eax

If EAX has the following 32-bit data:如果 EAX 有以下 32 位数据:

10000010000000000000000000000000

Then executing the command shrl $31, %eax results in:然后执行命令shrl $31, %eax结果:

00000000000000000000000000000001

However, where do the other bits ( 000001 ) go?但是,其他位 ( 000001 ) go 在哪里? Where exactly are they located in memory?它们究竟位于 memory 的什么位置?

Those bits are discarded because that's what shifting them out means, and that's what you asked the machine to do.这些位被丢弃,因为这就是将它们移出的意思,这就是您要求机器做的事情。 Just like in C, u32_var >>= 31;就像在 C 中一样, u32_var >>= 31; doesn't magically update some other location with the bits shifted out.不会神奇地用移出的位来更新其他位置。

If you wanted them to go somewhere, use shrd (2-register shift, although it only updates one of the registers. To do a uint64_t shift like >>= 31 on a pair of 32-bit registers (EDX:EAX), you need shrd $31, %edx, %eax / shr $31, %eax )如果您希望它们在某处使用 go,请使用shrd (2 寄存器移位,尽管它只更新其中一个寄存器。要在一对 32 位寄存器 (EDX:EAX) 上执行类似>>= 31uint64_t移位,您需要shrd $31, %edx, %eax / shr $31, %eax )

There's zero reason that shifting a register would result in a store to memory, that would be an insane design.移位寄存器会导致存储到 memory 的原因为零,这将是一个疯狂的设计。 Even shifting them into some temporary register would be weird, and would require a wider barrel shifter to implement shifts efficiently.即使将它们转移到一些临时寄存器中也会很奇怪,并且需要更宽的桶形移位器才能有效地实现移位。 (Which x86 needs for shrd , but other ISAs with shift instructions don't always have double-precision shifts. Of course, 8086 didn't have 386 shrd, but it also didn't have a barrel shifter: each shift count cost an extra cycle.) (其中 x86 需要shrd ,但其他带有移位指令的 ISA 并不总是具有双精度移位。当然,8086 没有 386 shrd,但它也没有桶形移位器:每个移位计数花费额外的循环。)


x86 instruction are not all reversible. x86 指令并非全部可逆。 Many (like shifts by more than 1) discard some information.许多人(比如超过 1 的班次)会丢弃一些信息。 You could just as easily ask where the bits went after sub %eax, %eax or xor %eax, %eax zeros the register, or after or $-1, %eax set it to all-ones.您可以很容易地询问sub %eax, %eaxxor %eax, %eax将寄存器归零,或者在or $-1, %eax将其设置为全一。 Some like not , neg , or xor (with different registers) are reversible by repeating, or add / sub are reversible by doing the other one.有些类似notnegxor (使用不同的寄存器)通过重复是可逆的,或者add / sub通过执行另一个是可逆的。 (Still destroying the old FLAGS, though.) (不过,仍在销毁旧的 FLAGS。)

Shifts leave the last bit shifted out in CF , so they always destroy the old contents of CF (unless the shift count is 0, then FLAGS are unmodified. x86 is so CISC it hurts. That's why variable-count shifts cost 3 uops on Intel CPUs. >.<)移位将最后一位移出 CF ,因此它们始终会破坏 CF 的旧内容(除非移位计数为 0,否则 FLAGS 未修改。x86 是如此的 CISC 伤害。这就是为什么可变计数移位在英特尔上花费 3 uops CPU。>.<)

Shifts by 1 leave the all the original bits of the input register around, and can be undone by a rotate-through-carry ( rcl or rcr ) in the other direction.移位 1 保留输入寄存器的所有原始位,并且可以通过另一个方向的循环进位( rclrcr )撤消。


related: https://en.wikipedia.org/wiki/Reversible_computing - information theory and thermodynamics.相关: https://en.wikipedia.org/wiki/Reversible_computing - 信息论和热力学。 Specifically the Logical reversibility section discusses how you might compute things without discarding any bits.具体来说, 逻辑可逆性部分讨论了如何在不丢弃任何位的情况下计算事物。

Of course, our current technology for digital logic in silicon uses vastly more power than the information-theoretical minimums even for the amount of information they destroy during computation (by many orders of magnitude).当然,我们目前的硅数字逻辑技术使用的功率远远超过信息理论最小值,即使它们在计算过程中破坏的信息量(多个数量级)也是如此。 So destroying information or not with your choice of instructions is completely irrelevant to power consumption.因此,通过您选择的指令是否破坏信息与功耗完全无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM