重点在 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
.
网友评论