Hızlı yanıt: kod örneği
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:Now imagine a statement like:where is an array of . Assuming the base of the array is already in , and variable is in , and and are each 32 bits (so is at offset 4 bytes in the struct), this statement can be compiled to:which will land in . The scale factor of 8 is because each is 8 bytes in size. Now consider the same expression used with the "address of" operator &:In this case, you don't want the value of , but its address. That's where (load effective address) comes in. Instead of a , the compiler can generatewhich will load the address in .
struct Point{ int xcoord; int ycoord;};
int y = points[i].ycoord;
points[]
Point
EBX
i
EAX
xcoord
ycoord
ycoord
MOV EDX, [EBX + 8*EAX + 4] ; right side is "effective address"
y
EDX
Point
int *p = &points[i].ycoord;
ycoord
LEA
MOV
LEA ESI, [EBX + 8*EAX + 4]
ESI