Definition
Addressing Mode
An addressing mode is the way an instruction’s operand specifies where to read its data from or write it to — a register, a fixed memory location, an immediate constant, or a memory cell whose address is taken from another register. The same operation behaves differently depending on which addressing mode its operands use; the four common modes on x86-64 are register direct, memory direct, immediate, and memory indirect.
The Four Common Modes
How each mode names its operand
- Register direct —
mov rax, rbx— both operands are registers; the value inrbxis copied intorax.- Memory direct —
mov rax, [0x1234]— the operand is a fixed memory address; the value at address0x1234is loaded intorax.- Immediate —
mov rax, 3— the operand is a constant baked into the instruction; the literal3is written intorax.- Memory indirect —
mov [rax], rbx— the operand is memory, but the address is taken from a register; the square brackets mean “the address held in” ([rax]reads “the address stored inrax”), so the value inrbxis written to the memory cellraxpoints to.
Reading Square-Bracket Operands
Intuition
The single trick that trips people up is that
[rax]does not mean “the value inrax” — it means “the value in the memory cell whose address is inrax”.raxalone is the value;raxinside brackets is a pointer the instruction dereferences. This is exactly the C distinction between a pointer and its dereference, and it is whylea <dst>, <src>(“load effective address”) is so oddly useful:lea rax, [rbp-8]computes the addressrbp-8and puts it inraxwithout dereferencing it, giving you free pointer arithmetic in hardware.