lea 和 offset 之间的区别
ar db "Defference $"
区别
mov dx,offset ar
我认为
lea dx,ar
两者都在做相同的工作,但这两者之间有什么
ar db "Defference $"
What's the difference between
mov dx,offset ar
and
lea dx,ar
I think both are doing same work, but what is the difference between these two
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在此用例中,LEA 和 MOV 执行相同的操作。如果您想以更复杂的方式计算地址,LEA 比 MOV 更强大。
举例来说,假设您想要获取数组中第 n 个字符的地址,并且 n 存储在 bx 中。使用 MOV,您必须编写以下两条指令:
使用 lea,您只需一条指令即可完成:
这里要考虑的另一件事:
add dx,bx
指令将更改 CPU 的状态标志。另一方面,在 lea dx, [ar + bx] 指令内完成的加法不会以任何方式更改标志,因为它不被视为算术指令。如果您想在进行一些简单计算时保留标志(地址计算非常常见),这有时会很有帮助。存储和恢复标志寄存器是可行的,但操作很慢。
In this use-case LEA and MOV do the same thing. LEA is more powerful than MOV if you want to calculate an address in a more complex way.
Lets for example say you want to get the address of the n'th character in your array, and the n is stored in bx. With MOV you have to write the following two instructions:
With lea you can do it with just one instruction:
Another thing to consider here: the
add dx,bx
instruction will change the status flags of the CPU. The addition done inside thelea dx, [ar + bx]
instruction on the other hand does not change the flags in any way because it is not considered an arithmetic instruction.This is sometimes helpful if you want to preserve the flags while doing some simple calculations (address calculations are very common). Storing and restoring the flag-register is doable but a slow operation.
引自 x86 处理器的汇编语言,7e,KIP R. IRVINE
Quote from Assembly Language for x86 Processors, 7e, KIP R. IRVINE
您可以使用它们来做同样的事情,但是如果地址有点复杂,
LEA
总是会更好。假设您有以下字符数组:
ASC_TBL DB '0','1','2','3','4','5','6','7','8' ,'9','A','B','C','D','E','F'
如果你想获得第 6 个元素 '5' 你会这样做
offset
:另一方面,如果您使用
LEA
指令,您可以简单地使用one指令,如下所示:LEA ax, [ASC_TBL + 5h];没有标志受到影响
请注意:
虽然使用
LEA
证明它比使用offset
具有优势,但使用offset 效率更高
如果地址写起来不是很复杂,或者它们都将使用相同数量的指令执行相同的操作。原因是
offset ASC_TBL
是在翻译过程中计算的 - 就像被预处理一样 - 但是,LEA
是一条实际的处理器指令。您不能使用
offset
指令来获取编译之前未知的地址。You can use them to do the same thing but,
LEA
will always be better if the address is a little complex.consider that you have the following array of chars:
ASC_TBL DB '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
if you want to get the 6th element '5' you would do something like this using
offset
:On the other hand, if you are using the
LEA
instruction you can simply use one instruction like this:LEA ax, [ASC_TBL + 5h]; no flags are affected
Notice that:
Although using
LEA
proved that it has an advantage over usingoffset
, it's more efficient to useoffset
if the address isn't very complex to write or both of them will do the same thing with the same number of instructions.The reason is that
offset ASC_TBL
is calculated during translation - like being be preprocessed- but,LEA
is an actual processor instruction.you can't use the
offset
instruction to get the addresses that aren't known before the compilation time.