PROGRAMMING TECHNIQUES
1. INDEX VARIABLES
Many algorithms traverse information structures that are sequentially allocated in memory. Let us assume that a sequence of n data items a0, a1, . . . , an−1 is stored sequentially. Further assume that each data item occupies 8 bytes, and the first element a0 is stored at address A
; the address of ai is then A
+ 8i. To load ai with 0 ≤ i < n from memory into register ai
, we need a suitable base address and so we assume that we have A
= LOC(
a0)
in register a
. Then we can write ‘8ADDU t,i,a; LDO ai,t,0
’ or alternatively ‘SL t,i,3; LDO ai,a,t
’. If this operation is necessary for all i, it is more efficient to maintain a register i
containing 8i as follows:
SET | i,0 | i ← 0. |
LDO | ai,a,i | Load ai. |
ADD | i,i,8 ... |
Get The MMIX Supplement: Supplement to The Art of Computer Programming Volumes 1, 2, 3 by Donald E. Knuth now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.