For me, it just seems like a funky MOV. What's its purpose and when should I use it?
lea
is an abbreviation of "load effective address". It loads the address of the location reference by the source operand to the destination operand. For instance, you could use it to:
lea ebx, [ebx+eax*8]
to move ebx
pointer eax
items further (in a 64-bit/element array) with a single instruction. Basically, you benefit from complex addressing modes supported by x86 architecture to manipulate pointers efficiently.
From the "Zen of Assembly" by Abrash:
LEA, the only instruction that performs memory addressing calculations but doesn't actually address memory. LEA accepts a standard memory addressing operand, but does nothing more than store the calculated memory offset in the specified register, which may be any general purpose register.
What does that give us? Two things that ADD doesn't provide:
- the ability to perform addition with either two or three operands, and
- the ability to store the result in any register; not just one of the source operands.
Maybe just another thing about LEA instruction. You can also use LEA for fast multiplaing registers by 3, 5 or 9.
LEA EAX, [EAX * 2 + EAX] //EAX = EAX * 3
LEA EAX, [EAX * 4 + EAX] //EAX = EAX * 5
LEA EAX, [EAX * 8 + EAX] //EAX = EAX * 9