View Full Version : lea

06-08-2003, 04:37 PM
I don't understand this instruction. Why exactly is it needed? Why couldn't I just do:
mov eax, ebp - 8
Why do I have to use:
lea eax, [ebp - 8]

ebp, points to the head of the stack. Subtract 8, and don't I have the address of the desired variable?

06-08-2003, 04:40 PM
I thought lea took the contents of that specific pointer and stored them into EAX.

06-08-2003, 04:44 PM
No, it says right here the lea instruction never reads memory.

06-09-2003, 12:48 AM
On assemblers such as MASM you can use both, but many use the mov method as it's believed to be faster (it may have been at some time....but I dont know about now)

06-09-2003, 02:53 AM
> mov eax, ebp - 8
> lea eax, [ebp - 8]

They are equivalent. But the first is faster. An advantage of lea is that it can handle more complex address calculations.

06-09-2003, 07:09 AM
The example was, a local variable x is created on the stack, the address of x is pushed on to the front of the stack and another function is called.
It was said that you couldn't just use mov eax, ebp- 8, that lea eax, [ebp-8] had to be used to calculate the address. If anyone wants to take a look at the example, it's the first occourance of lea in this book: http://www.comsc.ucok.edu/~pcarter/pcasm/mirror/pcasm-book.pdf

06-09-2003, 07:56 AM
A side question...

where are the ebp-8 calculations done? At compile time? I don't understand how they could be done at run-time if every single run-time instruction is represented directly in assembly.

06-09-2003, 08:32 AM
They are done at run-time. Memory addresses are complicated things to convert into machine code. Among the many bytes in the code, those adress calculations are encoded into a SIB byte. You can do addition and scalar multiplication along with a base register. SIB = scalar, index, base

So, you can do this at the same speed as any other mov instruction:

mov eax, [ecx + edx * 2 + 4]

Correct me if I'm wrong, anyone.

06-09-2003, 12:52 PM
ygfperson, the book mentioned something similar to what you're saying:
4.7.4 Calculating addresses of local variables
Finding the address of a label defined in the data or bss segments is
simple. Basically, the linker does this. However, calculating the address
of a local variable (or parameter) on the stack is not as straightforward.
However, this is a very common need when calling subroutines. Consider
the case of passing the address of a variable (letís call it x) to a function
(letís call it foo). If x is located at EBP − 8 on the stack, one cannot just
mov eax, ebp - 8
Why? The value that MOV stores into EAX must be computed by the assembler
(that is, it must in the end be a constant). However, there is an
instruction that does the desired calculation. It is called LEA (for Load Effective
Address). The following would calculate the address of x and store
it into EAX:
lea eax, [ebp - 8]
Now EAX holds the address of x and could be pushed on the stack when
calling function foo. Do not be confused, it looks like this instruction is
reading the data at [EBP−8]; however, this is not true. The LEA instruction
never reads memory! It only computes the address that would be read
by another instruction and stores this address in its first register operand.
Since it does not actually read any memory, no memory size designation
(e.g. dword) is needed or allowed.Perhaps you can decode that and let me know why I need to use the lea instruction.

06-09-2003, 01:31 PM
Actually, looking again, if you want to use an offset to a register and get an address loaded, LEA is the way to go.

mov eax, ebp - 8 ;doesnt even assemble......you can;

mov eax, [ebp - 8] ;that will load the contents of that location

lea eax, [ebp - 8] ;that will give an address

If your just loading an address of a variable;

mov ebx, offset MyVar

is the same as

lea ebx, MyVar

but the former takes less opcodes and is reportedly faster (but as I said.....I'm not too sure of the speed difference these days)

06-09-2003, 09:55 PM
If the calculations are done at run-time, where are they done?

06-09-2003, 10:03 PM
That's poppycock, they're not computed at runtime they're computed by the assembler. Hence the need for the lea instruction.

06-09-2003, 11:37 PM
The calculations are done by the processor as far as I know...

Masm interpret's

lea esi, [esp + 4]

to be "8D7424 04" - I imagine the 4 on the end is the offset


mov esi, [esp + 4]

to be "8B7424 04" - again the 4 as an offset

06-10-2003, 08:34 PM
All memory addressing is done on the cpu. Therefore, [eax + 8] is done on the cpu. Why use the lea instruction? Say you have many operations on some variable located at [eax - 4]. Now you can always write [eax - 4] but that just takes more time. Using lea, you can calculate the address just once and store it back into eax. Now you only need to address it as [eax].

mov [eax - 4], 293
add [eax - 4], 20
;etc etc


lea eax, [eax - 4]
mov [eax], 293
add [eax], 20
;etc etc

Some reference: http://www.cs.uaf.edu/~cs301/Encoding%20instructions.htm