什么是LEA指令的目的是什么? [英] What's the purpose of the LEA instruction?
问题描述
对于我来说,这似乎只是一个时髦的MOV。什么是它的目的,当我应该使用它?
For me, it just seems like a funky MOV. What's its purpose and when should I use it?
推荐答案
正如其他人所指出的那样,LEA(装载有效地址)经常被用来作为一个绝招做某些计算,但这并不是它的主要目的。 x86指令集被设计为支持高级语言如Pascal和C,其中阵列与MDASH;特别是整数或小的结构&MDASH的阵列,是常见的。举个例子,一个结构重新presenting(X,Y)坐标:
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;
其中,点[]
是点
的数组。假设数组的基本已在 EBX
和变量 I
是 EAX
和 XCOORD
和 YCOORD
均为32位(因此 YCOORD
以抵消结构4个字节),这句话可以被编译为:
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"
将登陆是
在 EDX
。 8的比例因子是因为每个点
的大小8个字节。现在考虑与运营商和放大器地址所用的相同的前pression;
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;
在这种情况下,你不希望 YCOORD
的价值,但它的地址。这就是 LEA
(加载有效地址)的用武之地。取而代之的是 MOV
,编译器可以生成
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]
将在 ESI
加载地址。
这篇关于什么是LEA指令的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!