3

How can I load a single byte from address? I thought it would be something like this:

mov      rax, byte[rdi]
Peter Cordes
  • 286,368
  • 41
  • 520
  • 731
user2798943
  • 197
  • 2
  • 11
  • A duplicate with more detailed answers: [Why can't I move directly a byte to a 64 bit register?](https://stackoverflow.com/q/22621340) – Peter Cordes Dec 16 '21 at 23:26

1 Answers1

9
mov al, [rdi]

Merge a byte into the low byte of RAX.


Or better, avoid a false dependency on the old value of RAX by zero-extending into a 32-bit register (and thus implicitly to 64 bits) with MOVZX:

movzx  eax, byte [rdi]          ; most efficient way to load one byte on modern x86

Or if you want sign-extension into a wider register, use MOVSX. (On some CPUs this is just as efficient as MOVZX.)

movsx  eax, byte [rdi]    ; sign extend to 32-bit, zero-extend to 64
movsx  rax, byte [rdi]    ; sign extend to 64-bit

The MASM equivalent replaces byte with byte ptr.

A mov load doesn't need a size specifier (al destination implies byte operand-size). movzx always does for a memory source because a 32-bit destination doesn't disambiguate between 8 vs. 16-bit memory sources.

Peter Cordes
  • 286,368
  • 41
  • 520
  • 731
ikh
  • 9,611
  • 1
  • 30
  • 64