4 ms·
Kinda weird that the author couldn't think of a use for base+index addressing. Doesn't it seem like the obvious application? Anyway, the tone of the article is
by jeffbee 6y ago
Kinda weird that the author couldn't think of a use for base+index addressing. Doesn't it seem like the obvious application?
Anyway, the tone of the article is unnecessary, IMHO. These addressing modes are useful and easy to understand, and the address generation units do double-duty as low-latency, high-throughput add-and-shift units, via the LEA instruction. CISC is useful, after all.
- saagarjha 6y ago> This is obvious in retrospect Otherwise smart people miss simple things all the time.
- userbinator 6y agoand the address generation units do double-duty as low-latency, high-throughput add-and-shift units, via the LEA instruction. For example, lea eax, [ecx+ecx*8+42] is equivalent to eax = ecx * 9 + 42, so you get a multiplication, an addition, and a move (without destroying the source register) in one instruction.
- akira2501 6y ago> without destroying the source register or the FLAGS register. Which can be very useful in some spectacular corner cases.
- woodruffw 6y agoAuthor here: it's an extremely obvious application! 'saagarjha points out that I chastise myself later on for missing it while trying to contrive samples. I'll blame that one on writing this post in the middle of the night :-) With respect to the tone: it's a little flippant, sure. I work professionally on research programs that involve binary translating x86 (and other CISCs) into various representations for program analysis; what you're seeing is some of my frustration there bubble up.
- nkurz 6y ago> These addressing modes are useful and easy to understand, and the address generation units do double-duty as low-latency, high-throughput add-and-shift units, via the LEA instruction. While I think you are right that LEA exists because of the memory addressing modes, at least on Intel (and I'm pretty sure AMD) it's been a long time since it's actually been executed on the address generation units. Instead, it's [mostly] treated as just another arithmetic instruction and executed on the same integer ALU's that execute all the other simple integer math. According to Agner (https://www.agner.org/optimize/instruction_tables.pdf https://www.agner.org/optimize/instruction_tables.pdf) it's been this way at least since the original Pentium. Does anyone know what the last mainstream processor was that actually executed LEA on the AGU? And whether there any less mainstream processors that still do? [mostly] I say mostly because there are a few odd addressing modes that have longer than usual latencies, and a because the 3 argument form also takes longer than the usual 1 cycle latency. It's still executed on a standard integer port, though, and not on the AGU.
- jeffbee 6y agoSure, but there's still something special/magic about LEA. For one the ports on which it dispatches are different from the ones available for ADD or SHL, even though is it effectively capable of SHL with register and immediate operands. And it's odd that 2- and 3-component address is 1 uop. Even though it's in the kitchen sink with all the other arithmetic, it's still a special case, rather than being decomposed into several adds, shifts, and moves.
- spc476 6y agoGenerally speaking, the LEA instruction will never modify the flags, while ADD and SHL will.
- jabl 6y agoMany RISC architectures have base + index*scale addressing modes.