Thread: x86 assembly, mov vs lea

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229

    x86 assembly, mov vs lea

    I am following a tutorial on x86 assembly, and for accessing arguments passed on the stack, it says -
    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 as-
    sembler (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 Ef-
    fective 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.
    So it seems like mov does not work because ebp - 8 is not a constant. Then, is this equivalent to the lea instruction? -
    Code:
    mov    eax, ebp
    sub     eax, 8
    ?

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I would assume so, yes. With the addition of the status register being moved on and off the stack, since LEA doesn't affect the flags, where sub does.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Ah thanks.

    But then what is the purpose of LEA? (that said, x86 is a CISC arch with many duplicate instructions, but the tutorial author actually recommends the use of LEA)

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I know lea would take the address of a variable, eg something on the stack.
    Useful if you need to call a class method and the class is an object on the stack, for example.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cyberfish View Post
    I am following a tutorial on x86 assembly, and for accessing arguments passed on the stack, it says -

    So it seems like mov does not work because ebp - 8 is not a constant. Then, is this equivalent to the lea instruction? -
    Code:
    mov    eax, ebp
    sub     eax, 8
    ?
    Yes, except the lea instruction is obviously faster than the above. lea is used all the time for this purpose. You can perform a multiplication and two additions in a single instruction. For instance, instead of

    Code:
    mov eax, ebx
    imul eax, 4
    add eax, ecx
    sub eax, 100
    You have:

    Code:
    lea eax, [4*ebx+ecx-100]
    Or you can use it to eliminate multiplication altogether. For instance, multiply by 5:

    Code:
    lea eax, [4*eax+eax]
    One of the older tricks in the book.

    As far as the PURPOSE of lea, the purpose of the instruction is to allow you to directly access the addressing mode circuitry of the CPU and obtain the address that would have been used for an indexing operation, instead of actually accessing the value at that address. To map this back to C/C++, it's specialized for pointer manipulation.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Ah thanks! that makes sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  2. Inline asm
    By brietje698 in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2007, 02:54 PM
  3. Getting position from game..
    By brietje698 in forum C++ Programming
    Replies: 1
    Last Post: 10-26-2007, 12:15 PM
  4. Linking C++ and Assembly
    By Warlax in forum C++ Programming
    Replies: 6
    Last Post: 10-19-2007, 04:33 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM