Address Modes
As mentioned on the Instructions page, address modes specify algorithms for finding the
values to be operated on.
Any value that an instruction manipulates is generally either in memory or in a register.
If it is in a register, then register mode is used to access it.
In this case the address mode simply names the register that contains the value.
If it is in memory, then the CPU must calculate the effective address, which is
the address in memory that actually contains the value. Often the instruction will not contain
the effective address, but instead will contain the information needed to calculate it.
We will look at some examples:
- "add $1 $2 $3" has three operands, all of which are specified using register mode
- "addi $4 $5 42" has two operands in register mode and one in immediate mode
- in immediate mode, the value is in the instruction; it is the number 42 in this case
- in this example, the computer will add 42 to the value in register 5 and put the sum into register 4
- "lw $6 x" has two operands; one uses register mode,
and the other uses direct mode (which is a fake)
- "lw $7 24($8)"uses register mode and base-displacement mode (which is not a fake)
- in this example, the effective address is calculated by adding 24 to the contents of register 8
- base-displacement mode has a special case called register indirect mode
- in this case the number before the left parenthesis is zero
- the zero may be omitted; the assembler will insert it for you
- "lw $9 0($10)" and "lw $9 ($10)" are equivalent and are examples of register indirect mode
- PC-relative mode contains an offset that is added
to the program counter to produce the effective address
- when written, PC-relative mode looks like direct mode, but they are easy to distinguish because
PC-relative mode appears only in branch instructions,
and direct mode never appears in branch instructions
- this is a common feature of RISC machines: once you know the operator, you also know what address modes all
the operands will use, so you don't need other bits in the instruction to specify them
- the addi instruction is another example of this feature