1

I'm debugging the following function:

_print_func:
    mov rdx, 0xFFFFFFFFFFFFFFFF
    mov rax, 0x01
    mov rdi, 0x01
    mov rsi, str
    movzx dx, byte [str_len] ; <--- Here
    syscall
    ret

The function was compiled with

nasm -g -f elf64 2.asm

The issue I faced with is that after stepping the line movzx dx, byte [str_len] the rdx content was:

rdx            0xffffffffffff000d       -65523

And it was reasonable. Now, replacing the instruction as this:

_print_func:
    mov rdx, 0xFFFFFFFFFFFFFFFF
    mov rax, 0x01
    mov rdi, 0x01
    mov rsi, str
    movzx edx, byte [str_len] ; dx replaced with edx
    syscall
    ret

Now the register content is this:

rdx            0xd      13

It looks like moving to 32-bit register zero extending it's 64 bit high part. Why is that happening?

Why did not we zero extend eax when movzx dx, byte [str_len]?

St.Antario
  • 26,175
  • 41
  • 130
  • 318

1 Answers1

2

When you write to a 16 bit register, only these 16 bits of the corresponding 64 bit register are changed. However, when you write to a 32 bit register, the other 32 bit of the corresponding 64 bit register are cleared. That's one of the quirks introduced in long mode (64 bit mode).

fuz
  • 88,405
  • 25
  • 200
  • 352