Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LEA in x86 assembly [duplicate]

Tags:

x86

assembly

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.

like image 813
user1090614 Avatar asked Mar 04 '14 00:03

user1090614


People also ask

What does Lea do in assembly?

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.

What is assembly code Lea?

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.

What is Lea in 8086 assembly language?

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.

What is difference between MOV and Lea?

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.


2 Answers

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
like image 93
higaki Avatar answered Oct 20 '22 19:10

higaki


It stores esp + 0x18 in eax. In other words, it's just addition. LEA is frequently used to perform basic arithmetic.

like image 20
StilesCrisis Avatar answered Oct 20 '22 17:10

StilesCrisis