Assembly Language DISCUSSION

What is the LEA instruction for in x86 assembly if it never reads memory?

Started by tiago LEA instructionx86 addressing modeseffective addressMOV vs LEAcompiler output
4 replies 248 views 5 participants
Latest activity · 30 Sep 2026

What is the LEA instruction for in x86 assembly if it never reads memory?

tiago Assembly Language Forum
#1

Looking at compiler output for a simple function I see lines like lea eax, [rdi+rdi*4] and lea rax, [rsp+8]. The brackets made me think memory is being read, as in mov eax, [rdi], but stepping through in the debugger shows that nothing is loaded from memory.

What does LEA actually compute, how is it different from MOV with the same operand, and why would a compiler use it for plain arithmetic instead of ADD or IMUL?

Community replies 4

Re: What is the LEA instruction for in x86 assembly if it never reads memory?

#2

LEA stands for load effective address. It performs the address calculation of its memory operand and writes the resulting address to the destination register, without touching memory. mov eax, [rdi+8] computes rdi+8 and then loads four bytes from that address; lea rax, [rdi+8] computes rdi+8 and stops there.

In C terms, MOV with brackets is x = a[i] and LEA is p = &a[i]. Because nothing is read or written, LEA can never fault, even when the computed value is not a valid address.

Re: What is the LEA instruction for in x86 assembly if it never reads memory?

#3

Compilers like it because an x86 addressing mode is a small arithmetic expression: base + index × scale + displacement, with a scale of 1, 2, 4 or 8. One LEA can add two registers and a constant and put the result in a third register: lea eax, [rdi+rsi+3] leaves both inputs intact, where ADD would overwrite one of them.

Multiplication by small constants falls out of the same form. lea eax, [rdi+rdi*2] is ×3, lea eax, [rdi+rdi*4] is ×5 and lea eax, [rdi+rdi*8] is ×9. For ×10 a compiler typically emits the ×5 form followed by add eax, eax.

Re: What is the LEA instruction for in x86 assembly if it never reads memory?

#4

Another property matters to compilers: LEA does not modify the flags, while ADD, SUB and IMUL do. That lets arithmetic be placed between a compare and the conditional jump that consumes its flags. The other side of that is that you cannot test for carry or overflow after an LEA.

Its original purpose is still in daily use as well. lea rax, [rsp+8] is the address of a local variable, as needed when &local is passed to a function. In 64-bit position-independent code the address of a global is formed relative to the instruction pointer, written lea rax, [rel msg] in NASM syntax.

Re: What is the LEA instruction for in x86 assembly if it never reads memory?

#5

A few limits when writing it by hand. The only scale factors are 1, 2, 4 and 8, with one base and one index register. With a 32-bit destination the result is truncated to 32 bits and the upper half of the 64-bit register is cleared, as for any 32-bit register write, which is why lea eax, [rdi+rdi*4] is correct for int arithmetic.

On speed, a two-component LEA costs about the same as an ADD on current cores. The three-component form, base plus index plus displacement, has been slower on several Intel generations, which is why compilers sometimes split it into an LEA and an ADD. Those details vary by microarchitecture, so consult the vendor's optimisation manual rather than assuming.

TEP COMMUNITY