Addressing Modes
Any instruction that touches memory has to say where in memory to look. x86-64 hands you a generous toolkit for that, from bare constants up to expressions involving several registers and a scale factor. The modes matter because compilers map every variable, array element, and struct field straight onto them.
For background on how the CPU interacts with memory, see x86 Instruction Set: Addressing Modes.
Direct (Absolute) Addressing
The simplest form: use a label or numeric address directly.
section .data value dq 42
section .text mov rax, [value] ; load the 8 bytes at address 'value' into rax mov [value], rbx ; store rbx into the memory at 'value'The square brackets do the heavy lifting. Drop them and mov rax, value loads the address of value into rax, not the data living there. With brackets, you dereference and grab the contents.
One catch: without default rel, NASM encodes [value] as an absolute 32-bit address. That links fine into a plain static (non-PIE) executable, the way this course builds with ld, but the linker rejects it in a position-independent executable, which is what gcc produces by default. Write mov rax, [rel value] (or put default rel at the top of the file) and the same load becomes RIP-relative and works in both. More on that below.
Register Indirect Addressing
Instead of a fixed label, use a register that holds an address.
lea rsi, [value] ; rsi = address of value mov rax, [rsi] ; load the 8 bytes at the address in rsi mov [rsi], rdx ; store rdx at the address in rsiThis is pointer-based access in its purest form. Every *p in C lowers to register indirect addressing.
Register + Displacement (Base + Offset)
Tack a constant onto a register's value. This is how struct field access works under the hood.
; Imagine rsi points to a struct: ; offset 0: int id (4 bytes) ; offset 4: int score (4 bytes) ; offset 8: long timestamp (8 bytes) mov eax, [rsi] ; load id (offset 0) mov ebx, [rsi + 4] ; load score (offset 4) mov rcx, [rsi + 8] ; load timestamp (offset 8)The displacement is a signed 32-bit constant baked into the instruction; the CPU adds it to the base register at execution time. There's no cycle penalty over plain register indirect.
Base + Index * Scale
The workhorse for array access. The CPU computes base + index * scale with scale in .
section .data arr dq 100, 200, 300, 400, 500 ; array of 5 quadwords (8 bytes each)
section .text lea rbx, [arr] ; rbx = base address of array mov rcx, 3 ; index = 3 mov rax, [rbx + rcx*8] ; load arr[3] -> rax = 400Scale matches element size: 1 for bytes, 2 for words, 4 for dwords, 8 for qwords. The multiplication happens in hardware via the SIB (Scale-Index-Base) byte.
The Full Form: Base + Index * Scale + Displacement
The full beast — every component on at once:
; base = rbx, index = rcx, scale = 4, displacement = 12 mov eax, [rbx + rcx*4 + 12]In one instruction, you've expressed struct_array[i].field: base names the array, index*scale picks the struct, displacement picks the field within it. The CPU encodes the whole thing in a ModR/M byte plus a SIB byte.
Every data structure in a compiled program collapses into some combination of these modes. Plain variable? Direct addressing. Pointer dereference? Register indirect. Struct field? Base + displacement. Array element? Base + index*scale. A field inside an array of structs uses all four pieces at once. Once you see addressing modes clearly, you see how the CPU sees your data.
The SIB Byte Encoding
When the assembler sees [rbx + rcx*4 + 12], it emits a SIB (Scale-Index-Base) byte to spell the address out for the CPU:
; Encoding breakdown:; ModR/M byte: signals SIB is present + displacement size; SIB byte: scale (2 bits) | index reg (3 bits) | base reg (3 bits); scale=2 (means *4) | index=rcx(001) | base=rbx(011); Displacement: 0x0C (12 in hex)Scale gets 2 bits: 00 is ×1, 01 ×2, 10 ×4, 11 ×8. That's why only those four values work. Want a scale of 3 or 5? You'll be reaching for imul or some lea chicanery to compute the offset by hand.
RIP-Relative Addressing
64-bit mode adds one more trick: an addressing mode where the instruction pointer itself is the base.
; In NASM with 'default rel': default rel mov rax, [value] ; encoded as [rip + offset_to_value] lea rsi, [value] ; rsi = address of value, position-independentRIP-relative addressing is what makes position-independent code possible. Compilers use it by default for globals on x86-64, but NASM only does so when you ask with default rel or [rel label]. The position-independent code lesson digs deeper.
Practical: Accessing a 2D Array
Stack the addressing components and you can index straight into multi-dimensional arrays.
section .data ; 3x4 matrix of dwords (row-major order) matrix dd 1, 2, 3, 4 dd 5, 6, 7, 8 dd 9, 10, 11, 12
section .text ; Access matrix[row][col] where row=1, col=2 ; address = base + (row * cols_per_row + col) * element_size lea rbx, [matrix] mov rcx, 1 ; row imul rcx, 4 ; row * cols_per_row (4 columns) add rcx, 2 ; + col mov eax, [rbx + rcx*4] ; element_size = 4 bytes -> loads 7Here's the matrix in memory, one row of four dwords per line, at an illustrative address. Step through a few [row][col] lookups and check each effective address by hand. That final mov eax, [rbx + rcx*4] assembles to 8b 04 8b: opcode 8b, a ModR/M byte of 04 that says "a SIB byte follows", and a SIB byte of 8b, which is scale 10 (×4), index 001 (rcx), base 011 (rbx).
matrix[row][col] = [rbx + rcx*4]
Starts at 0x402000; small numbers under each byte are the last two hex digits of its address.
matrix[0][0]: rcx = 0 × 4 + 0 = 0.
Security Perspective: Out-of-Bounds Addressing
The CPU does no bounds checking on any addressing mode. If rcx holds 100 in our five-element array example, the CPU cheerfully computes rbx + 100*8 and reads whatever happens to live at that address — secret data, stack canaries, return addresses, take your pick.
This is the hardware reality that makes buffer overflows possible. mov rax, [rbx + rcx*8] has zero concept of array length. The CPU has no idea your array only holds five entries. It'll read or write any address you ask for, as long as the page permissions allow.
Every out-of-bounds read or write in a compiled program ultimately runs as one of these addressing modes with an index or displacement that overshoots. Reading that behaviour at the instruction level is the first rung on the ladder toward memory-corruption bugs.
For more on how out-of-bounds access becomes an exploitation primitive, see Memory Corruption Classes.
What is Next
With the addressing modes mapped out, the next lesson puts them to work on arrays — defining them, iterating over them, and seeing first-hand why bounds checking earns its keep.