Get the upper half of the EAX register

前端 未结 4 1615
旧巷少年郎
旧巷少年郎 2020-12-20 19:52

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 hal

相关标签:
4条回答
  • 2020-12-20 20:04

    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.

    0 讨论(0)
  • 2020-12-20 20:10

    I would do it like this:

    mov ebx,eax
    shr ebx, 16
    

    ebx now contains the top 16-bits of eax

    0 讨论(0)
  • 2020-12-20 20:25

    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.

    0 讨论(0)
  • 2020-12-20 20:25

    For 16-bit mode, this is the smallest (not fastest) code: only 4 bytes.

    push eax  ; 2 bytes: prefix + opcode 
    pop ax    ; 1 byte: opcode
    pop bx    ; 1 byte: opcode
    

    It's even more compact than single instruction shld ebx,eax,16 which occupies 5 bytes of memory.

    0 讨论(0)
提交回复
热议问题