Lukas' Notes

computer-architecture

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 directmov rax, rbx — both operands are registers; the value in rbx is copied into rax.
  • Memory directmov rax, [0x1234] — the operand is a fixed memory address; the value at address 0x1234 is loaded into rax.
  • Immediatemov rax, 3 — the operand is a constant baked into the instruction; the literal 3 is written into rax.
  • Memory indirectmov [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 in rax”), so the value in rbx is written to the memory cell rax points to.

Reading Square-Bracket Operands

Intuition

The single trick that trips people up is that [rax] does not mean “the value in rax” — it means “the value in the memory cell whose address is in rax”. rax alone is the value; rax inside brackets is a pointer the instruction dereferences. This is exactly the C distinction between a pointer and its dereference, and it is why lea <dst>, <src> (“load effective address”) is so oddly useful: lea rax, [rbp-8] computes the address rbp-8 and puts it in rax without dereferencing it, giving you free pointer arithmetic in hardware.