I am learning x86 assembly, and have some troubles with the lea instruction.
0x080486f7 <+21>: lea eax,[esp+0x18]
Can anybody explain what happens in this line? In my understanding, it takes the value at [esp+0x18] and interprets the value as an address, and puts the value of what is int the address into eax.
The lea instruction places the address specified by its first operand into the register specified by its second operand. Note, the contents of the memory location are not loaded, only the effective address is computed and placed into the register.
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.
LEA − Used to load the address of operand into the provided register. LES − Used to load ES register and other provided register from the memory.
The lea instruction copies an “effective address” from one place to another. Unlike mov, which copies data at the address src to the destination, lea copies the value of src itself to the destination. The syntax for the destinations is the same as mov.
Basically
mov eax, [esp+0x18]
means
mov eax, esp
add eax, 0x18
mov eax, [eax]
and in C that would look like
eax = *(unsigned int*)(esp + 0x18)
Meanwhile
lea eax, [esp+0x18]
means
mov eax, esp
add eax, 0x18
and in C that would look like
eax = esp + 0x18
It stores esp + 0x18
in eax
. In other words, it's just addition. LEA is frequently used to perform basic arithmetic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With