Why is lea instruction named for memory addressing

2020-04-21 02:13发布

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

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?

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?

标签: assembly x86
3条回答
看我几分像从前
2楼-- · 2020-04-21 02:17

It is named for its purpose.

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".

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.

[Edit after long comment interaction, below]:

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.

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.

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.

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.

查看更多
▲ chillily
3楼-- · 2020-04-21 02:34

The simple answer for why lea references the concept of effective addresses in its name is that it is intended for calculating effective addresses. Names and intentions often go together (although I wish they did more often - there are endless examples of unnecessarily obscure terminology in this field).

Perhaps the problems people often have understanding the purpose of lea have more to do with the term 'load'. 'Load' suggests that a memory operation is performed, although it is not. This is even more likely to be confusing because there is a conceptual association between effective addresses and memory. Finally, the syntax of the memory operand to lea is that of a operand that in other instructions denotes an actual load. Given all this, some initial confusion over whether or not lea produces a memory access is understandable.

Perhaps a better mnemonic would have been cea, 'calculate effective address'. Oh well.

查看更多
劫难
4楼-- · 2020-04-21 02:37

The reason why the LEA works the way it does is because on the original 8086 the LEA instruction reused the processor's effective address calculation hardware. The effective address hardware calculates the address the memory operand of an instruction acts on. Since there a number of different basic operations that need to be performed to calculate an effective address, this means there was relatively speaking a lot of power packed into a LEA instruction. Most "real" arithmetic instructions only performed one operation at a time, and most require that destination register be one of the source operands. Since it could be implemented a tiny amount additional encoding space and silicon area, it was pretty cheap considering what its capable of doing.

So an instruction like MOV AX,[BX + SI] (I'm using Intel syntax here) loads AX with the 16-bit value stored at the address calculated by adding BX and SI . The instruction LEA AX,[BX + SI] loads AX with the address calculated by adding BX and SI. In other words the LEA instruction treats memory operands differently than other instructions. Instead operating on the memory at the address indicated by the memory operand, it uses the calculated address directly as the operand. The same address encoding is used for both instruction, the LEA instruction just tweaks how the memory operand is interpreted.

In other words, LEA is called that because that's exactly what it does. It loads the effective address given by a memory operand into the destination register. Since the memory operand isn't actually used as memory operand, it does in fact work like an ordinary arithmetic instruction. If ADD is the addition arithmetic instruction, then LEA is the effective address arithmetic instruction.

查看更多
登录 后发表回答