Assembly Register Arithmetic?

This question already has an answer here:

  • What's the purpose of the LEA instruction? 14 answers

  • lea does not move addresses, it computes the effective address and stores the resulting address into the destination register. Address computations are performed in machine language with regular arithmetic, not C pointer arithmetic.

    lea eax,[ebp-40]
    

    subtracts 40 from the value in register ebp and stores the result in register eax .

    mov DWORD PTR [esp+4], 0x80484c
    

    Computes the destination address by adding 4 to the value contained in register esp and stores the value 0x80484c , the integer 8407116 , as a 32 bit integer occupying 4 bytes at that address, least significant byte first.


    lea just calculates an expression and saves it to variable. It doesn't actually do anything with memory or memory addresses even through it is named 'load effective address'.

    For example lea eax,[ebp-40] means eax = ebp - 40 .

    It can be used to calculate expressions which would otherwise take several instructions, for example:

    lea eax, [8 * eax + ebx + 10]
    

    calculates eax = 8 * eax + ebx + 10 with one instruction.

    On the other hand mov , when used with [...] , read/writes something from/to memory, so it's a bit like using C pointers.

    mov DWORD PTR [esp+4], 0x80484c
    

    This saves 32-bit unsigned integer ( DWORD ) value 0x80484c to memory location esp + 4 , ie same as following pseudo-C:

    *((uint32_t*)(esp + 4)) = 0x80484c;
    
    链接地址: http://www.djcxy.com/p/72374.html

    上一篇: movl / leal,在这种情况下的区别

    下一篇: 汇编寄存器算术?