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.
The code on this page was assembled with NASM 3.01 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev