2

I read that for example "lea eax, [ebp - 120]" essentially means

mov    eax, ebp
sub     eax, 120

Given the example that the esp is at ebp-200, then later in the function

lea eax, [ebp - 120]
push eax
call xyz

would this mean that the value at ebp-120 is loaded into the eax register, then this 4 byte vaule is pushed onto the stack? Or does it mean that the esp is decremented further by 120, thereby creating a buffer from ebp-200 to ebp-320, with esp now at ebp-320?

Soap
  • 173
  • 1
  • 3
  • 9
  • 1
    Your first interpretation is correct. – 500 - Internal Server Error Feb 06 '14 at 22:04
  • `push eax` pushes the contents of the `eax` register onto the stack. – Kerrek SB Feb 06 '14 at 22:07
  • The code is passing a pointer to a local variable. Stored in the stack frame of the caller at ebp-120. Very common. Doesn't have anything to do with esp, other than at the caller's function entry the esp value needs to be adjusted to make space for the local variables. The way stack frames work is well described in many tutorials and books. – Hans Passant Feb 06 '14 at 23:14
  • Can you guys take a look at this? http://stackoverflow.com/questions/30007738/how-does-lea-instruction-store-address-of-a – committedandroider May 02 '15 at 21:36

1 Answers1

2

What's the purpose of the LEA instruction?

lea looks more fancy than mov, but in reality it only does part of what mov does: mov calculates the address and dereferences memory, lea just calculates the address.

In your example, eax receives the value stored in ebp with 120 subtracted, not the value stored at the address which is stored in ebp with 120 subtracted. This value proceeds to be pushed onto the stack. If this assembly corresponded to C code, eax/stack would contain a pointer to some variable.

There is no direct interaction between lea and esp. Unless esp is one of lea's arguments, lea does not read or modify esp.

Community
  • 1
  • 1
  • 1
    I like to explain it by saying `lea` is a shift-and-add instruction that just happens to use memory-address syntax and machine code. – Peter Cordes Jul 16 '17 at 20:45