-2

I am trying to figure out what this in assembly would mean in C:

movq 16(%rdi), %rdx
movq 16(%rsi), %rax
movzbl (%rdx), %edx

I am mostly confused about what the movzbl (%rdx), %edx will do. Thanks!

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
210312312
  • 45
  • 3
  • Well, this is what `movq` does: https://stackoverflow.com/a/3852919/8658157, and this for `movzbl`: https://stackoverflow.com/a/9318005/8658157 – Elliott Oct 15 '20 at 22:09
  • `movzbl (%rdx), %edx` reads a single byte from memory address [rdx] and zero extends the byte so it is 32-bits wide with the upper 24 bits set to zero and stores it in EDX. As well it also zero extends the 32-bit EDX register through the entire RDX register (it sets the upper 32-bits of RDX to 0) because in 64-bit mode when a destination register is a 32-bit register the upper 32-bits of the corresponding 64-bit register are set to zero automatically by the CPU. In essence it reads one byte from [RDX] and sets the low byte of RDX to the byte read and ensures the upper 56 bits are zero. – Michael Petch Oct 15 '20 at 22:09

1 Answers1

1

This is AT&T syntax for the movzx instruction. It fetches one byte from the address contained in %rdx, zero-extends it to 32 bits, and stores the result in %edx.

As with every x86-64 instruction that writes to a 32-bit register, the high half of the corresponding 64-bit register %rdx is also zeroed. So you may also think of this instruction as zero-extending an 8-bit value to 64 bits.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • 2
    I think it is worth bringing up that the upper 32-bits of RDX are also set to zero automatically by the CPU since the destination (EDX) is a 32-bit register. The end result really is that the lower 8 bits of RDX are set to the byte read and the upper 56 bits are set to 0 – Michael Petch Oct 15 '20 at 22:12