| ▲ | secondcoming 2 hours ago |
| The confusing thing about LEA is that the source operands are within a '[]' block which makes it look like a memory access. I'd love to know why that is. I think the calculation is also done during instruction decode rather than on the ALU, but I could be wrong about that. |
|
| ▲ | pwg 2 hours ago | parent | next [-] |
| It (LEA) does all the work of a memory access (the address computation part) without actually performing the memory access. Instead of reading from memory at "computed address value" it returns "computed address value" to you to use elsewhere. The intent was likely to compute the address values for MOVS/MOVSB/MOVSW/MOVSD/MOVSQ when setting up a REP MOVS (or other repeated string operation). But it turned out they were useful for doing three operand adds as well. |
|
| ▲ | trollbridge 2 hours ago | parent | prev [-] |
| LEA is the equivalent of & in C. It gives you the address of something. Fun question: what does the last line of this do? MOV BP,12
LEA AX,[BP]
MOV BX,34
LEA AX,BX |
| |
| ▲ | hota_mazi an hour ago | parent [-] | | I think OP was just making a comment on the asymmetry of the syntax. Brackets [] are usually used to dereference. Why is this written lea eax, [rdi + rsi]
instead of just lea eax, rdi + rsi
?
| | |
| ▲ | jcranmer 26 minutes ago | parent | next [-] | | When you encode an x86 instruction, your operands amount to either a register name, a memory operand, or an immediate (of several slightly different flavors). I'm no great connoisseur of ISAs, but I believe this basic trichotomy is fairly universal for ISAs. The operands of an LEA instruction are the destination register and a memory operand [1]. LEA happens to be the unique instruction where the memory operand is not dereferenced in some fashion in the course of execution; it doesn't make a lot of sense to create an entirely new syntax that works only for a single instruction. [1] On a hardware level, the ModR/M encoding of most x86 instructions allows you to specify a register operand and either a memory or a register operand. The LEA instruction only allows a register and a memory operand to be specified; if you try to use a register and register operand, it is instead decoded as an illegal instruction. | |
| ▲ | Y_Y 33 minutes ago | parent | prev | next [-] | | The way I rationalize it is that you're getting the address of something. A raw address isn't what you want the address of, so you're doing something like &(*(rdi+rsi)). | |
| ▲ | secondcoming an hour ago | parent | prev [-] | | Yes, that’s what I meant | | |
| ▲ | HarHarVeryFunny 25 minutes ago | parent [-] | | LEA stands for Load Effective Address, so the syntax is as-if you're doing a memory access, but you are just getting the calculated address, not reading or writing to that address. LEA would normally be used for things like calculating address of an array element, or doing pointer math. |
|
|
|