为什么 lea 指令在执行算术时以内存寻址命名? [英] Why is lea instruction named for memory addressing when it just performs arithmetic?

查看:33
本文介绍了为什么 lea 指令在执行算术时以内存寻址命名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

lea 指令(加载有效地址")获取第一个值的内存地址并将其添加到第二个值 - 可以相乘.然后将该内存地址加载到给定的寄存器中.

The lea instruction ("load effective address") takes the memory address of the first value and adds it to the second value - which may be multiplied. It then loads that memory address into a given register.

让我举个例子来澄清我的困惑:

Let me make an example to clarify my confusion:

eax = 2
leal (%eax, %eax, 4), %edx

我相信这样做的结果是 edx 里面会有值 10 (2 + 2 * 4 == 10).但是如果它只是对整数进行算术运算,那么为什么这条指令是针对内存地址的?

I believe the result of this is that edx will have the value 10 inside of it (2 + 2 * 4 == 10). But why is this instruction for memory addresses if it's just doing arithmetic on integers?

我已阅读其他回复,但他们都谈到内存地址是唯一涉及的内容.有人可以帮助我了解 leal 在做什么吗?

I have read other responses, but they all talk about memory addresses being the only thing involved. Can someone help me understand what leal is doing?

推荐答案

它以其用途命名.

大多数指令都包含相同的寻址模式.CPU架构师将确定寻址方式选择的内存地址的工作称为计算有效地址".

Most instructions include the same addressing modes. The CPU architects call the work of determining the memory address selected by the addressing modes as "computing the effective address".

指令的目的是将有效地址放入寄存器.因此,加载有效地址".

The instruction purpose is to put the effective address into a register. Hence, "load effective address".

是的,它只是执行算术"是真的.如果您仔细想想,这几乎就是 CPU 的全部功能,因此该短语并不能很好地描述任何特定指令或 CPU 活动.

Yes, it is true "it just performs arithmetic". If you think about it, that's pretty much all a CPU does, so that phrase isn't very descriptive of any particular instruction or CPU activity.

如果您想了解有多少指令获得它们的名称(更不用说指令的目的是什么),最好参加计算机体系结构课程.

If you want to understand how many instructions get their names (let alone what the purpose of an instruction is), it is a good idea to take a computer architecture class.

[经过长时间的评论互动后编辑]:

这里的大部分答案(包括我的)都挂在计算有效地址"上,其中指令用于形成内存地址,该指令具有当之无愧的名字.

Most of the answers here (including mine) were hung up on "computing effective addresses", in which the instruction is used to form a memory address, where the instruction has a well-deserved name.

然而,由于指令实际上没有使用计算的地址",另一个LEA 指令的极其常见的用途是简单地执行它所做的算术运算.实际上,从这个角度来看,LEA 是用一些特殊的小常数计算和或乘积的组合,并将结果存储到另一个寄存器而不影响条件位.与进行真正的乘法相比,它也恰好可以非常快地完成此操作.这在实际程序中的实用性出奇地高;获得一些在 x86 上编写汇编代码的经验,您就会相信这一点.

However, since the instruction doesn't actually use the computed "address", another extremely common use of the LEA instruction is simply do the arithmetic that it does. In effect, LEA viewed from this perspective is a combination of compute sums or products with some special small constants, and store the result to another register without affecting the condition bits. It also happens to do this very quickly compared to doing a real multiply. The utility of this in real programs is surprisingly high; get some experience writing assembly code on x86 and you will believe this.

例如,LEA 可用于将寄存器乘以 5 并添加一个大常数.这里的指令名称只会混淆;不幸的是,它仍然必须有一个名字.

So LEA can be used, for example, to multiply a register by 5 and add a big constant. Here the instruction name only confuses; unfortunately, it still has to have a name.

欢迎来到汇编代码领域,在那里设计人员发明指令来实现一个目的,而编码人员发现他们可以使用该指令来计算设计人员显然没有考虑过的事情.[作为另一个例子,AND-immediate 指令对于计算二次幂的模非常方便].因此,合理指令集中的每条指令都由指令架构师放置在那里,因为它有一些有用的目的.当编码人员发现聪明的应用程序时,它会被用于这一点和其他事情.

Welcome to assembly-code land, where designers invent instructions to achieve one purpose, and coders discover that they can use the instruction to compute things the designers didn't obviously consider. [The AND-immediate instruction is pretty handy for computing modulo some-power-of-two, as another example]. So every instruction in a rational instruction set was placed there by the instruction architect becuase it serves some useful purpose. And it gets use for that, and other things, as coders discover clever applications.

这篇关于为什么 lea 指令在执行算术时以内存寻址命名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆