0
\$\begingroup\$

In a cpu, why are/were Index Registers needed? You can of course live without them, but why would you want them? Wikipedia says that they are used for vector/array operations, but I'm not really sure what that means. (I am deciding on the architecture of my home-brew cpu)

\$\endgroup\$
  • \$\begingroup\$ Downvoter care to explain? \$\endgroup\$ – akaltar May 31 '15 at 19:39
  • \$\begingroup\$ As with a lot of features, the are not needed, but experience has learend that the ability to index (add two registers or a register + an offset from the instruction to form a memory address) can increase the effectiveness of a CPU. \$\endgroup\$ – Wouter van Ooijen May 31 '15 at 20:44
  • \$\begingroup\$ They aren't needed: you can implement an entire processor with a single instruction. They're just a major use-case that's worth providing a processor idiom for. \$\endgroup\$ – user207421 May 31 '15 at 20:53
0
\$\begingroup\$
variable = array[index];

(where index is a variable who's value is unknown at compile time) is a common thing to do in C. You can do something similar in x86 assembly:

mov edx, [ebx + 4 * eax]

Here, eax holds the value of the index variable, and we call eax an index register in this instruction. If ebx holds the address of array, and array is of a type which has a size of 4 bytes, then eax = n will store array[n] into edx.

Without index variables, this would be cumbersome to do.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Thanks, that makes sense :) This sounds like it could be used in a register machine, but I have seen index registers in stack machines too. That does not make sense to me, because you can only acces the newest register i a stack, as far as I know..? \$\endgroup\$ – Christian Bertram May 31 '15 at 19:38
  • \$\begingroup\$ Im not too sure, but i think that depends on the implementation. While most opcodes can be based on the stack, I think most (sufficiently advanced) stack machines have a way to access the memory as well, and this might be done with an index 'register' (by this i mean just a value that specifies which word in memory to load). \$\endgroup\$ – Ruben May 31 '15 at 19:59
  • \$\begingroup\$ ah, looking closer i think it switches between (mux) the stack pointer and a index register address \$\endgroup\$ – Christian Bertram May 31 '15 at 20:03
1
\$\begingroup\$

Observe:

     LDX #0
     LDB #5
     LDA #0
loop STA buf,X
     INX
     DEB
     BNZ loop
     RTS

buf  RESB 5

This simple example clears out 5 bytes in a buffer. Without indexing the operation STA buf,X would require modifying the code itself, which as any seasoned programmer knows is evil, and isn't even possible on some platforms.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.