Thread: lea

  1. #1
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    lea

    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?
    Grrr

  2. #2
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    I thought lea took the contents of that specific pointer and stored them into EAX.
    "What are you after - the vague post of the week award?" - Salem
    IPv6 Ready.
    Travel the world, meet interesting people...kill them.
    Trying to fix or change something, only guaruntees and perpetuates its existence.
    I don't know about angels, but it is fear that gives men wings.
    The problem with wanting something is the fear of losing it, or never having it. The thought makes you weak.

    E-Mail Xei

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    No, it says right here the lea instruction never reads memory.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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)

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    > 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.
    Last edited by Shiro; 06-09-2003 at 03:05 AM.

  6. #6
    Eibro@School
    Guest
    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/p...pcasm-book.pdf

  7. #7
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    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.

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    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.

  9. #9
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    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
    use:
    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.

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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)

  11. #11
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    If the calculations are done at run-time, where are they done?

  12. #12
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    That's poppycock, they're not computed at runtime they're computed by the assembler. Hence the need for the lea instruction.

  13. #13
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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

    and

    mov esi, [esp + 4]

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

  14. #14
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    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].
    Code:
    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/Encodin...structions.htm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. swapping pointers
    By csvraju in forum C Programming
    Replies: 17
    Last Post: 04-01-2009, 03:18 AM
  2. Two basic questions about generated assembly
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 07-16-2008, 03:19 AM
  3. Getting position from game..
    By brietje698 in forum C++ Programming
    Replies: 1
    Last Post: 10-26-2007, 12:15 PM
  4. Storing String (Texts) :: ASM
    By kuphryn in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-15-2002, 05:21 PM
  5. Replies: 5
    Last Post: 09-17-2001, 06:18 AM