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