News for hackers. Stuff that matters.
Re:Why do arrays start at 0? by kitkat_new on Wednesday August 24, 2022 @06:20PM (#32586565)
would it? or would it be just a differently wired circuit?
Re:Why do arrays start at 0? by fsckboy on Wednesday August 24, 2022 @07:33PM (#32587313)
the "differently wired circuit" would be an extra stage of logic computing a carry all the way from lsb to msb (an ALU outside the ALU?) and would contribute a fair bit of extra time. Easier to just use the ALU to do it, which is inserting an extra instruction, also a time waster.
Re:Why do arrays start at 0? by codebje on Wednesday August 24, 2022 @08:47PM (#32587896)
If you're already doing base index addressing (eg, [SI + DI]) you're already using the ALU to compute your memory address, and it's not _that_ much extra wiring to have the ALU support either a constant power-of-two displacement or even a three-way addition; a constant displacement for a basic ripple adder is a XOR and an AND NOT, eg. The x86 family does three-way addition (and a shift) to support "[ESI] [EBX * scale + displacement]".
Re:Why do arrays start at 0? by TuringTest on Thursday August 25, 2022 @02:32AM (#32590315)
It's not just the extra wiring, but the extra time; each additional computation layer in an integrated circuit introduced delays that would made the operation noticeably slower.
Re:Why do arrays start at 0? by codebje on Wednesday August 24, 2022 @08:30PM (#32587776)
Well, maybe, if the specific use case of 1-based arrays was all that got supported.

It'd be a pretty huge waste of processor design space to do that, though: if an HLL wants to support n-based arrays for n != 0 it can just store the base pointer with the initial index offset already applied.

Early 8-bit processors had a pretty poor set of addressing modes: you couldn't even expect [R1 + R2] let alone [R1 + R2 * scale + displacement], so random access into an array would be a multi-instruction task. By the time of the 80386 base scaled index addressing with displacement was in the CPU core, but it's not _free_ - there's an additional byte in the instruction encoding for the displacement, so even if the CPU computes the displacement with no additional clock cycles you'll have a latency cost for fetching the byte.