lea 和 offset 之间的区别

发布于 2024-09-01 00:55:14 字数 176 浏览 8 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风轻花落早 2024-09-08 00:55:14

在此用例中,LEA 和 MOV 执行相同的操作。如果您想以更复杂的方式计算地址,LEA 比 MOV 更强大。

举例来说,假设您想要获取数组中第 n 个字符的地址,并且 n 存储在 bx 中。使用 MOV,您必须编写以下两条指令:

Mov dx, offset ar
add dx, bx

使用 lea,您只需一条指令即可完成:

lea dx, [ar + bx]

这里要考虑的另一件事: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:

Mov dx, offset ar
add dx, bx

With lea you can do it with just one instruction:

lea dx, [ar + bx]

Another thing to consider here: the add dx,bx instruction will change the status flags of the CPU. The addition done inside the lea 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.

池木 2024-09-08 00:55:14

引自 x86 处理器的汇编语言,7e,KIP R. IRVINE

不可能使用 OFFSET 来获取堆栈参数的地址,因为 OF​​FSET 仅适用于编译时已知的地址。以下语句无法汇编:

mov esi,OFFSET [ebp-30]        ; error

Quote from Assembly Language for x86 Processors, 7e, KIP R. IRVINE

It is not possible to use OFFSET to get the address of a stack parameter because OFFSET only works with addresses known at compile time. The following statement would not assemble:

mov esi,OFFSET [ebp-30]        ; error
花开浅夏 2024-09-08 00:55:14

您可以使用它们来做同样的事情,但是如果地址有点复杂,LEA 总是会更好。

假设您有以下字符数组:

ASC_TBL DB '0','1','2','3','4','5','6','7','8' ,'9','A','B','C','D','E','F'

如果你想获得第 6 个元素 '5' 你会这样做offset

mov ax, offset ASC_TBL

add ax, 5h; flags [PF] will be affected 

另一方面,如果您使用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:

mov ax, offset ASC_TBL

add ax, 5h; flags [PF] will be affected 

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 using offset, it's more efficient to use offset 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文