← again tap: answer good → ↑ swipe up at the end: next
Question
What is the mov instruction and what are its forms?
Answer
mov copies data from a source to a destination — the most common x86 instruction — but it cannot move directly from one memory location to another.
* mov allows every source/destination pair except memory-to-memory, which must be staged through a register. *
mov is deceptively central: most of what code does is shuffle values between registers, memory, and immediates, and mov is the workhorse for all of it.
mov $imm, reg # immediate → register
mov $imm, mem # immediate → memory
mov reg, reg # register → register
mov reg, mem # register → memory
mov mem, reg # memory → register
The one combination that's illegal: memory-to-memory in a single instruction. You must stage through a register:
mov mem, mem # NOT allowed — use a register as intermediate
Like other instructions, mov takes a size suffix (movb, movw, movl, movq).
Two behaviors worth memorizing: movl into a 32-bit register zeros the upper 32 bits of the 64-bit register, and mov with an immediate is limited to a 32-bit (sign-extended) constant — to load a full 64-bit immediate you need movabs.
Go deeper:
Felix Cloutier — MOV — the authoritative operand-form table and the memory-to-memory restriction.
Question
What are the x86-64 memory addressing modes?
Answer
A memory operand has the general form D(Rb, Ri, S), computing the address Rb + Ri×S + D — base register, scaled index register, and a constant displacement.
* The effective address is base + index x scale + displacement; the scale (1/2/4/8) equals the element size, and the index can be any register except %rsp. *
This one flexible form is what lets a single instruction reach an array element, a struct field, or a stack slot. You use whichever pieces you need:
| Mode | Syntax | Address computed |
|---|---|---|
| Absolute | 0x1000 |
0x1000 |
| Indirect | (%rax) |
%rax |
| Base + displacement | 8(%rax) |
%rax + 8 |
| Indexed | (%rax,%rbx) |
%rax + %rbx |
| Scaled indexed | (%rax,%rbx,4) |
%rax + %rbx×4 |
| Full | 8(%rax,%rbx,4) |
%rax + %rbx×4 + 8 |
The scale S can only be 1, 2, 4, or 8 — not by accident, but because those are exactly the sizes of the primitive data types, so array[i] for any type maps onto one addressing mode. The index register can be any register except %rsp.
8(%rbp) # a local variable
(%rax,%rcx,4) # int array: base + index*4
array(,%rdi,8) # 8-byte element array[i] with no base register
Go deeper:
Addressing mode (Wikipedia) — the general base + index x scale + displacement computation.