LEA — Load Effective Address. LEA
Description
Computes the effective address of the second operand (the source operand) and stores it in the first operand (destination operand). The source operand is a memory address (offset part) specified with one of the processors addressing modes; the destination operand is a general-purpose register. The address-size and operand-size attributes affect the action performed by this instruction, as shown in the following table. The operand-size attribute of the instruction is determined by the chosen register; the address-size attribute is determined by the attribute of the code segment.
Usage
LEA r16,m ## Store effective address for m in register r16
将有效地址放入寄存器。第二个操作数可以进行算术运算:
LEA EAX, [ EAX + EBX + 1234567 ]
有一个经典的例子:stackoverflow
X86 指令集用于支持高级程序语言如 Pascal 和 C,这种编程语言的数组特别是整形数组或小结构数组在编程中非常常见。
struct Point
{
int xcoord;
int ycoord;
};
考虑一个状态,是一个取值状态:
int y = points[i].ycoord;
这里,points[]
是一个Point 结构体的数组,假设数组的基已经是 EBX
,变量 i
是存于 EAX
,xcoord
和 ycoord
都是 32-bits (因此,ycoord
是处在结构体的4-bytes 偏移),这个状态可以被编译成:
MOV EDX, [EBX + 8*EAX + 4] ; right side is "effective address"
将 y
存于 EDX
。放缩因子8 是因为每个结构体 Point
是 8-bytes 大小,现在考虑相同的表达,取地址运算:
int *p = &points[i].ycoord;
这里,我们需要的是 ycoord
的地址,这就是 LEA
的来源,较之事业 MOV
,编译器可以生成:
LEA ESI, [EBX + 8*EAX + 4]
这里将加载地址到 ESI