Skip to content
systemcoding.dev
Assembly

x86-64 Addressing Modes: Base, Index, Scale and Displacement

Learn the x86-64 memory operand form [base + index*scale + displacement], what each part may be, which combinations are illegal, and how arrays map onto it.

6 min read
Summarize in AI
Flat illustration of a database beside the table it holds, with the Intel, C++ and Linux logos.

Every load and store on x86-64 names its memory address with the same compact formula, and once you can read that formula you can read the memory access of any disassembly. The formula is [base + index*scale + displacement]. Each of the four parts is optional, and a single instruction computes the whole sum in the address generation hardware before touching memory. This post defines the four parts, lists the restrictions the encoding imposes, and then maps array and struct accesses onto them, using NASM syntax and the Intel manuals as the reference.

The four parts

A memory operand is written in square brackets. The brackets mean "the memory at the address inside", not "the number inside". The address is computed from:

  • base: any 64-bit general-purpose register (rax to r15, including rsp and rbp).

  • index: any 64-bit general-purpose register except rsp. It is multiplied by the scale.

  • scale: the constant 1, 2, 4 or 8.

  • displacement: a signed constant, either 8 bits (-128 to 127) or 32 bits.

All of these are legal operands for a 32-bit load into eax:

asm
        mov     eax, [rdi]                 ; base only
        mov     eax, [rdi+16]              ; base + displacement
        mov     eax, [rdi+rsi]             ; base + index
        mov     eax, [rdi+rsi*4]           ; base + index * scale
        mov     eax, [rdi+rsi*4+16]        ; all four parts
        mov     eax, [rsi*4+table]         ; index * scale + displacement, no base
        mov     eax, [rel table]           ; rip-relative

The scales 1, 2, 4 and 8 are not arbitrary. They match the sizes of char, short, int and long (or a pointer), so an array element a[i] costs nothing extra to address. The displacement carries the constant offset of a struct field or the distance from the start of an array.

Worked example: indexing an array

Suppose rdi holds the address of an array of 32-bit integers and rsi holds an index. The C expression a[i] becomes one instruction:

asm
; int get(const int *a, long i)   a in rdi, i in rsi
get:
        mov     eax, [rdi+rsi*4]
        ret

Compute the address by hand. If rdi is 0x1000 and rsi is 3, the address is 0x1000 + 3*4 = 0x100c. With the memory contents below, the function returns the value stored at 0x100c:

Text
address   0x1000  0x1004  0x1008  0x100c  0x1010
value       10      20      30      40      50

get(a, 3) -> 40

The 4 is the element size, not the index. A common slip is to write [rdi+rsi+4], which reads the wrong place entirely, and the instruction assembles without complaint because it is valid, just not what you meant.

Structs and arrays together

Fields of a struct use a displacement. Take a struct whose size is a power of two so the scale can reach it:

point.cc
struct point {
    int x;      /* offset 0 */
    int y;      /* offset 4 */
};              /* size 8 */

An array of struct point has elements 8 bytes apart, so p[i].y is the address p + i*8 + 4:

asm
; int get_y(const struct point *p, long i)
get_y:
        mov     eax, [rdi+rsi*8+4]
        ret

With p at 0x2000, i equal to 2 and the second element's y equal to 90, the address is 0x2000 + 16 + 4 = 0x2014, and the function returns whatever y holds there.

If the element size is not 1, 2, 4 or 8, the scale cannot express it. A 12-byte struct needs the index multiplied first:

asm
; struct of 12 bytes: address = p + i*12
        lea     rax, [rsi+rsi*2]       ; rax = i * 3
        mov     eax, [rdi+rax*4]       ; element at p + i*3*4 = p + i*12

That is why you often see lea used purely as an arithmetic instruction: it evaluates a memory operand without reading memory, so [rsi+rsi*2] becomes "multiply by 3". A later post in this series explores that trick.

Restrictions that the encoding imposes

Four rules cause most assembler errors.

**rsp cannot be an index.** The encoding of the index field uses the value for "no index" in the position that would mean rsp. NASM reports:

Text
error: invalid effective address

for mov eax, [rdi+rsp*2]. The reverse is fine: rsp as a base ([rsp+8]) is the standard way to reach stack variables, though it needs an extra byte in the encoding called the SIB byte.

Only one index, and the scale is a constant. [rdi+rsi*rdx] and [rdi+rsi*3] are invalid. The scale 3 trick is [rsi+rsi*2], which is base plus index times 2, not a scale of 3.

You cannot write two displacements, and a 64-bit absolute address does not fit in a displacement. A displacement is at most 32 bits, sign-extended. Accessing a symbol at an arbitrary 64-bit address needs a register, or the rip-relative form, which is a 32-bit signed offset from the next instruction.

Mixed address sizes are not allowed. [rdi+esi] is rejected because the base and index must be the same width. You can use all 32-bit registers ([edi+esi*4]), which adds an address-size prefix byte and truncates the address to 32 bits, and you almost never want it.

RIP-relative and absolute addressing

In 64-bit mode the processor adds a form that 32-bit x86 lacked: [rip + disp32]. NASM spells it with the rel keyword:

asm
section .data
counter: dq 0

section .text
        mov     rax, [rel counter]     ; load 8 bytes at counter
        inc     rax
        mov     [rel counter], rax     ; store back

The processor computes the address as the address of the next instruction plus a fixed displacement. Since code and data move together when a program is relocated, no relocation is needed, which makes this the standard way to reach globals in shared libraries and position-independent executables. The post on RIP-relative addressing covers why in depth.

A true absolute 64-bit address works only with the special mov forms that take a 64-bit offset (moffs), and compilers almost never emit them.

Operand size when no register tells it

When one operand is a register, its width sets the width of the access. When both are a memory operand and a constant, the assembler cannot guess and you must say:

asm
        mov     dword [rdi], 5         ; store 4 bytes
        mov     qword [rdi+8], 0       ; store 8 bytes

Leave out the size and NASM stops:

Text
error: operation size not specified

The size keywords are byte, word, dword and qword. An instruction can still never take two memory operands (apart from the string instructions), so mov [rdi], [rsi] is invalid and a register has to carry the value.

Mistakes that assemble but read the wrong memory

Because every combination inside the brackets is legal as long as the encoding allows it, the assembler cannot tell you that the address is wrong. Three habits catch most of them. Say the formula out loud as "base plus index times size plus offset" and check each term against the C expression. Check that the register holding the index is a full 64-bit value, because an index loaded with mov esi, [rdx] is zero-extended and a negative int index becomes a large positive one. And check that the displacement is in bytes, never in elements, since [rdi+4] is the second int and [rdi+16] is the fifth.

What this means when reading disassembly

A few patterns are worth recognizing immediately:

PatternTypical source
[rdi+rsi*4]int array element a[i]
[rbp-20] or [rsp+12]a local variable on the stack
[rax+8]a struct field at offset 8
[rip+0x2ee4]a global or a string constant
[rax+rax*4]arithmetic through lea, here multiplying by 5

In objdump's Intel syntax a negative displacement prints as [rbp-0x14], and a missing base prints as ds:0x... for an absolute address.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.