美文网首页
深入理解 lea 和 mov 指令的区别

深入理解 lea 和 mov 指令的区别

作者: onedam | 来源:发表于2020-11-04 10:52 被阅读0次

重点在 lea 是在执行期间由cpu计算(似乎是一个硬件函数了.)
mov 的是汇编的时候编译器算的

指令Leal实际上是movl指令的变形。 (lea 可以让cpu计算!)
MOV将抢在数据括号内的地址和位置数据到目标操作数。
LEA将在括号内执行地址的计算,并将计算出的地址放入目标操作数中。
发生这种情况时,并没有实际访问内存并获取数据。所做的工作LEA是在“有效地址”的计算中。
MOV可以执行与LEA [label]相同的操作,但是MOV指令包含指令内部的有效地址作为立即数常量(由汇编程序预先计算)。
LEA使用PC相对值来计算指令执行期间的有效地址。

在某些情况下MOV和LEA可以重叠并且做完全相同的事情 –

https://stackoverflow.com/questions/1658294/whats-the-purpose-of-the-lea-instruction

For me, it just seems like a funky MOV. What's its purpose and when should I use it?

lea 是在出现了更高级的c语言,为了更好的支持c语言. 8086版本 cpu电路技术进步后 新加入的一条命令

See also Using LEA on values that aren't addresses / pointers?: LEA is just a shift-and-add instruction. It was probably added to 8086 because the hardware is already there to decode and calculate addressing modes, not because it's "intended" only for use with addresses. Remember that pointers are just integers in assembly.

824

As others have pointed out, LEA (load effective address) is often used as a "trick" to do certain computations, but that's not its primary purpose. The x86 instruction set was designed to support high-level languages like Pascal and C, where arrays—especially arrays of ints or small structs—are common. Consider, for example, a struct representing (x, y) coordinates:

struct Point
{
     int xcoord;
     int ycoord;
};

Now imagine a statement like:

int y = points[i].ycoord;

where points[] is an array of Point. Assuming the base of the array is already in EBX, and variable i is in EAX, and xcoord and ycoord are each 32 bits (so ycoord is at offset 4 bytes in the struct), this statement can be compiled to:

MOV EDX, [EBX + 8*EAX + 4]    ; right side is "effective address"

which will land y in EDX. The scale factor of 8 is because each Point is 8 bytes in size. Now consider the same expression used with the "address of" operator &:

int *p = &points[i].ycoord;

In this case, you don't want the value of ycoord, but its address. That's where LEA (load effective address) comes in. Instead of a MOV, the compiler can generate

LEA ESI, [EBX + 8*EAX + 4]
(feng: 为什么 MOV EDX, EBX + 8*EAX + 4  这个不行. 因为mov指令不支持计算功能)

which will load the address in ESI.

相关文章

  • 深入理解 lea 和 mov 指令的区别

    重点在 lea 是在执行期间由cpu计算(似乎是一个硬件函数了.)mov 的是汇编的时候编译器算的 指令Leal实...

  • 5.汇编中中括号[]作用以及lea和mov指令的区别

    现在总结一下:其中牵扯到lea指令,mov指令,[] 一.lea指令:对于寄存器来说:第二个操作数是寄存器必须要加...

  • Lea和mov

    lea指令是将源操作数、即存储单元的有效单元地址存储到目的操作数mov将源操作数传输到目的操作数。lea eax,...

  • 汇编lea命令

    lea命令为加载有效地址(load dffective address) 和mov用法一样。指令并不是从制定的位置...

  • 汇编语言--汇编指令系统总结

    [数据传送指令] 一、通用数据传送指令 1、传送指令 MOV (move) 指令的汇编格式:MOV DST,SRC...

  • 汇编语言-第七章总结

    更灵活的定位内存地址的方法 and和or指令 and指令: 逻辑与指令、按位进行与运算。Example:mov a...

  • 汇编的数据处理指令

    mov指令 这个指令是最常用的数据转移指令,表示将原操作数转移到目的操作数,指令的格式为。mov 目的操作数,原操...

  • ARM汇编

    汇编 索引 [###MOV][###指令后面加S][###条件执行和标志位][###指令后面加条件][@str@s...

  • X86 Assemble指令--LEA

    LEA指令描述 LEA指令 LEA指令用来计算第二个操作数(源操作数)的有效地址,并且将该地址保存到第一个操作数(...

  • 14. ARM 汇编指令集2

    常用ARM指令1:s数据处理指令 数据传输指令 mov mvn 算术指令 ...

网友评论

      本文标题:深入理解 lea 和 mov 指令的区别

      本文链接:https://www.haomeiwen.com/subject/smzwvktx.html