简体   繁体   中英

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:

shrl $31, %eax

If EAX has the following 32-bit data:

10000010000000000000000000000000

Then executing the command shrl $31, %eax results in:

00000000000000000000000000000001

However, where do the other bits ( 000001 ) go? Where exactly are they located in 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; 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 )

There's zero reason that shifting a register would result in a store to memory, that would be an insane design. 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 instruction are not all reversible. Many (like shifts by more than 1) discard some information. 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. Some like not , neg , or xor (with different registers) are reversible by repeating, or add / sub are reversible by doing the other one. (Still destroying the old FLAGS, though.)

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

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.


related: https://en.wikipedia.org/wiki/Reversible_computing - information theory and thermodynamics. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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