MOV and data transfer

Basic moves

MOV copies data between registers, between a register and memory, or from a constant into a register or memory. It never moves memory to memory directly:

mov rax, rbx ; register to register mov rax, 42 ; constant to register mov [buf], rax ; register to memory mov rax, [buf] ; memory to register

The rules

Both operands must be the same size. The destination cannot be a constant. And a segment register needs a special move, not a plain MOV:

mov eax, ebx ; ok: both 32-bit ; mov eax, bx ; error: invalid combination of opcode and operands

Common traps

MOV is not assignment in C: it copies bits, it does not evaluate anything. MOV [mem], [mem] is illegal, so a memory-to-memory copy goes through a register. And MOV to a 32-bit register clears the upper half of the 64-bit register, which surprises people porting 32-bit code.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does mov rax, rbx do?

    Answer

    It copies rbx into rax

    mov copies the source into the destination and leaves the source unchanged.

  2. Which move is impossible with a plain MOV?

    Answer

    Memory to memory

    x86 has no memory-to-memory move; one register must carry the value in between.

  3. What happens to the upper 32 bits of rax after mov eax, 1?

    Answer

    They are cleared

    A write to a 32-bit register zero-extends into the 64-bit register.

  4. Can the destination operand of MOV be a constant?

    Answer

    No, the destination must be a register or memory

    A constant has no address to store into, so the assembler rejects it.

  5. What does mov eax, ebx require?

    Answer

    Both operands to be 32-bit

    The two operands must have the same size, so eax and ebx match and eax and bx do not.

  6. What does this program print? default rel section .data msg db "M", 10 section .text global _start _start: mov rsi, msg mov rdx, 2 mov rax, 1 mov rdi, 1 syscall mov rax, 60 xor rdi, rdi syscall

    Answer

    M

    mov copies the address of msg into rsi and the count into rdx, so write prints M.

  7. What exit status does this program return? section .text global _start _start: mov rax, 60 mov rdi, 3 syscall

    Answer

    3

    The status is the value in rdi, which is 3.

  8. Why is mov [dest], [src] rejected?

    Answer

    The two operands are memory

    The CPU has no instruction that copies from one memory location straight to another.

  9. What does this program print? default rel section .data buf db "xy", 10 section .text global _start _start: mov al, [buf] mov [buf], al mov rax, 1 mov rdi, 1 mov rsi, buf mov rdx, 3 syscall mov rax, 60 xor rdi, rdi syscall

    Answer

    xy

    Reading the first byte into al and writing it back changes nothing, so the output is xy.

  10. MOV from a segment register to a general-purpose register needs what?

    Answer

    A special move instruction or a memory temporary

    A segment register cannot be a plain MOV operand like a general-purpose one.

The snippets on this page are fragments, not whole programs: they show MOV and its rules. They were assembled with NASM 3.01 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev