x86 assembly, mov vs lea

This is a discussion on x86 assembly, mov vs lea within the Tech Board forums, part of the Community Boards category; I am following a tutorial on x86 assembly, and for accessing arguments passed on the stack, it says - If ...

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

    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
    Portugal
    Posts
    7,458
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,183
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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,183
    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, 03:48 PM
  2. Inline asm
    By brietje698 in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2007, 01: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, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21