Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access higher order bytes of a register in assembly [duplicate]

Tags:

x86

assembly

In x86 assembly language, is there any way to obtain the upper half of the EAX register? I know that the AX register already contains the lower half of the EAX register, but I don't yet know of any way to obtain the upper half.

I know that mov bx, ax would move the lower half of eax into bx, but I want to know how to move the upper half of eax into bx as well.

like image 269
Anderson Green Avatar asked Dec 29 '25 00:12

Anderson Green


2 Answers

If you want to preserve EAX and the upper half of EBX:

rol eax, 16
mov bx, ax
rol eax, 16

If have a scratch register available, this is more efficient (and doesn't introduce extra latency for later instructions that read EAX):

mov ecx, eax
shr ecx, 16
mov  bx, cx

If you don't need either of those, mov ebx, eax / shr ebx, 16 is the obvious way and avoids any partial-register stalls or false dependencies.

like image 163
Alexey Frunze Avatar answered Dec 31 '25 00:12

Alexey Frunze


If you don't mind shifting the original value of bx (low 16 bits of ebx) to high 16 bits of ebx, you need only 1 instruction:

shld ebx,eax,16

This does not modify eax.

like image 24
nrz Avatar answered Dec 30 '25 23:12

nrz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!