Accessing Data in M emory— Addressing M odes

CHAPT ER 4 T HE INST RUCT ION SET ARCHIT ECT URE 135

4.5 Accessing Data in M emory— Addressing M odes

Up to this point, we have seen four ways of computing the address of a value in memory: 1 a constant value, known at assembly time, 2 the contents of a reg- ister, 3 the sum of two registers, and 4 the sum of a register and a constant. Table 4.1 gives names to these addressing modes, and shows a few others as well. Notice that the syntax of the table differs from that of the ARC. T his is a com- mon, unfortunate feature of assembly languages: each one differs from the rest in its syntax conventions. T he notation M[x] in the Meaning column assumes memory is an array, M, whose byte index is given by the address computation in brackets. T here may seem to be a bewildering assortment of addressing modes, but each has its usage: • Immediate addressing allows a reference to a constant that is known at as- sembly time. • Direct addressing is used to access data items whose address is known at as- sembly time. • Indirect addressing is used to access a pointer variable whose address is known at compile time. T his addressing mode is seldom supported in mod- ern processors because it requires two memory references to access the op- erand, making it a complicated instruction. Programmers who wish to access data in this form must use two instructions, one to access the pointer and another to access the value to which it refers. T his has the beneficial side effect of exposing the complexity of the addressing mode, perhaps dis- couraging its use. Addressing Mode Syntax Meaning Immediate K K Direct K M[K] Indirect K M[M[K]] Register Indirect Rn M[Rn] Register Indexed Rm + Rn M[Rm + Rn] Register Based Rm + X M[Rm + X] Register Based Indexed Rm + Rn + X M[Rm + Rn + X] Table 4.1 Addressing Modes 136 CHAPT ER 4 T HE INST RUCT ION SET ARCHIT ECT URE • Register indirect addressing is used when the address of the operand is not known until run time. Stack operands fit this description, and are accessed by register indirect addressing, often in the form of push and pop instruc- tions that also decrement and increment the register respectively. • Register indexed, register based, and register based indexed addressing are used to access components of arrays such as the one in Figure 4-14, and components buried beneath the top of the stack, in a data structure known as the stack frame , which is discussed in the next section.

4.6 Subroutine Linkage and Stacks