In computer programming and computer architecture, an index register is an area of memory usually built into the central processing unit (CPU) to be used as a very fast counter for stepping through memory addresses or to keep track of operations such as looping. Depending on the type of system architecture, an index register can be a defined and dedicated register among other processor registers, or it can be any general-purpose register. Most commonly, an index register holds the current offset of a memory location, with another register holding the base address, so the combination of the two registers creates a completed memory address. One of the special functions of an index register, when one is specially designated by a CPU, is that it can be used to easily step through memory addresses either by being incremented or decremented as needed so data structures such as arrays and stacks can be traversed.
The index registers on the CPU of a computer are incredibly low-level areas of memory that usually are only directly accessible by a programmer through the use of assembly language or a similar low-level programming language. In some of the more common types of processors, two separate registers are defined as index registers, namely the source index (SI) and destination index (DI) registers. Other processors do not specifically have index registers or supporting operators that require them, meaning any general-purpose register of the appropriate size can be used.
One of the most frequent uses for an index register is to act as a pointer to a memory location that holds a stream of data that needs to be accessed sequentially. An example can be seen when using an array of data in which all the elements are arranged consecutively in memory. If the index register is used to access an array, then it can hold the corresponding value of the offset of the element currently being accessed when added to another register that holds a base address, such as the data segment register. This can make it very easy to complete procedures such as copying the contents of a null-terminated string from a source location to a destination string.
Another use for an index register can be to hold information about loops and other counters. Some system architectures prefer to use the index register to hold the number of iterations that have occurred in a loop, although other times any register can be used. Additionally, some assembly instructions rely specifically on source and destination index registers to perform certain operations, such as block memory reading or writing, as might be done to send information to a screen.