Skip to content

Lesson 2 of 9 · 26m

Memory Addressing and Data Movement Without Mysticism

Decode effective addresses, operand sizes, endianness, loads, stores, and the difference between moving values and moving addresses.

Most beginners treat memory operands as incantations. Then they meet [rip+0x2012] in real disassembly and freeze. Memory addressing is arithmetic with a fixed shape. Once you can compute the address by hand, half of reverse engineering stops looking adversarial.

The effective-address formula

x86-64 memory operands commonly use:

[base + index*scale + displacement]

Examples:

mov rax, [rdi]           ; load 8 bytes from address in rdi
mov rax, [rdi+8]         ; load from rdi + 8
mov rax, [rdi+rcx*4]     ; array indexing, 4-byte elements
mov rax, [rbx+rdx*8+16]  ; base + scaled index + offset

Brackets mean memory access. Without brackets, you usually mean the value in the register itself. lea is the exception: it calculates the address arithmetic but does not dereference it.

Operand size determines what moves

The register name or a size prefix defines the transfer width:

mov byte [rdi], 1        ; one byte
mov word [rdi], 1        ; two bytes
mov dword [rdi], 1       ; four bytes
mov qword [rdi], 1       ; eight bytes

x86 is little-endian. If rax contains 0x1122334455667788 and you store it at 0x1000, memory contains:

1000: 88 77 66 55 44 33 22 11

This matters when inspecting network packets, file formats, shellcode, or a partially overwritten pointer.

mov, movzx, and movsx are different promises

movzx eax, byte [rdi]    ; zero-extend unsigned byte
movsx eax, byte [rdi]    ; sign-extend signed byte

A byte 0xff becomes 255 with zero extension and -1 with sign extension. If you misread one in a parser, length checks and bounds checks stop making sense.

lea is often just arithmetic

Compilers use lea because it can combine addition and multiplication without touching flags:

lea rax, [rdi+rsi*4]     ; rdi + rsi*4
lea eax, [rcx-1]         ; often just ecx - 1

Do not assume every lea means an object address. Sometimes it is the cheapest arithmetic instruction available.

Field notes

  • [rip+disp32] means position-independent access relative to the next instruction.
  • Alignment affects performance and sometimes correctness; do not assume every address is legal for every width.
  • A null register used as a base faults when dereferenced, not when the address is calculated.
  • mov never dereferences twice. [rax] is one memory access, not a pointer chain by itself.
  • In debuggers, verify memory with x/8gx or equivalent instead of trusting what a local variable name suggests.

Was this lesson helpful?

Sign in to rate