LEA and addressing modes

LEA basics

LEA (Load Effective Address) computes an address and puts it in a register without reading memory. Its bracket syntax is the full x86 addressing grammar:

lea rax, [buf] ; address of buf lea rax, [buf+8] ; address plus 8 lea rax, [rbx+rcx*4] ; base + index*scale

Addressing modes

A memory operand is base plus index times scale plus displacement: [base + index*scale + disp]. Scale is 1, 2, 4, or 8, and matches the element size of an array:

mov eax, [table + rsi*4] ; table[rsi] of 32-bit ints mov al, [grid + rdi*8 + rsi] ; row rdi, column rsi

The arithmetic trick

Because LEA only computes addresses, it also does fast arithmetic: LEA rax, [rax+rax*2] is rax times 3 in one instruction, with no flags changed. Compilers use this trick constantly, so you will see it in every disassembly.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does LEA put in the destination register?

    Answer

    The computed address

    LEA computes the effective address from the brackets and does not read memory.

  2. Which scales can an index register have in an x86 address?

    Answer

    1, 2, 4, 8

    The scale field has four encodings: times 1, 2, 4 and 8.

  3. What does lea rax, [rbx + rcx*4] compute?

    Answer

    The address rbx + rcx*4

    LEA returns the address itself, which is useful for array indexing.

  4. Which instruction reads the value stored at an address?

    Answer

    mov rax, [buf]

    mov with a bracketed source loads the memory contents; lea only computes the address.

  5. How many bits does [table + rsi*4] add for each step of rsi?

    Answer

    32 bits

    The scale of 4 bytes is 32 bits, the size of one 32-bit element.

  6. Does LEA change the flags?

    Answer

    No, it changes only the destination

    LEA is an address computation, so it leaves the flags untouched.

  7. What does this program print? default rel section .data digits db "0123456789" buf db " ", 10 section .text global _start _start: mov rbx, 3 lea rax, [rbx + rbx*2] mov cl, [digits + rax] mov [buf], cl mov rax, 1 mov rdi, 1 mov rsi, buf mov rdx, 1 syscall mov rax, 60 xor rdi, rdi syscall

    Answer

    9

    LEA computes 3 + 3*2 = 9, which indexes the digit 9.

  8. What exit status does this program return? section .text global _start _start: mov rax, 4 lea rax, [rax + rax*2] mov rdi, rax mov rax, 60 syscall

    Answer

    12

    The address computation gives 4 + 4*2 = 12, which becomes the status.

  9. Which operand form is [rax + rbx*8 + 16]?

    Answer

    Base plus index times scale plus displacement

    The full form is base + index*scale + displacement, with scale 1, 2, 4 or 8.

  10. Why do compilers use lea rax, [rax + rax*2] for multiplication?

    Answer

    It computes times 3 without changing the flags

    The address unit computes the sum, so the multiply is free and the flags stay.

The snippets on this page are fragments, not whole programs: they show LEA and the addressing modes. They were assembled with NASM 3.01 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev