Sub topic 6
Addressing modes and why they exist
This chapter assumes you know the fetch-decode-execute cycle, the idea of an instruction with an opcode and address fields, and how registers and memory differ. The next chapter covers effective address arithmetic in detail. This one explains what each mode is and why designers added it.
The problem addressing modes solve
An instruction has a fixed number of bits. Some of those bits hold the opcode. The rest hold the address field, which says where the operand is. Three pressures push on that field:
- Programs need constants, and a constant should not need a memory read.
- Programs need big address spaces, but a short field reaches only a small part of memory. A k-bit field reaches at most 2^k locations.
- Programs use arrays, loops, pointers, and code that may be loaded at different places in memory. A fixed address in the instruction handles none of these well.
An addressing mode is a rule that tells the processor how to turn the address field into the operand. The effective address (EA) is the memory address the rule produces. Some modes produce no EA because the operand is not in memory.
The common modes
| Mode | Operand is | Effective address | Operand memory reads |
|---|---|---|---|
| Immediate | the address field itself | none | 0 |
| Register | in register R | none | 0 |
| Direct (absolute) | at the address field | A | 1 |
| Indirect | at the address stored at A | M[A] | 2 |
| Register indirect | at the address held in R | R | 1 |
| Indexed | at A plus an index register | A + XR | 1 |
| Base register | at a base register plus offset | BR + A | 1 |
| Relative | at PC plus offset | PC + A | 1 |
| Autoincrement | at the address in R, then R is increased | R (before update) | 1 |
| Autodecrement | at R after R is decreased | R - size | 1 |
The read counts exclude the instruction fetch. For a branch, the relative-mode EA is the branch target itself, so no data read happens.
Why each mode exists
Immediate supplies constants such as the 1 in ADD R1, #1. It is fast because the operand arrives with the instruction. The cost is range: the constant must fit in the address field.
Register keeps the operand in a register, so no memory access is needed. A register number needs only a few bits, which also makes instructions short.
Direct is the simplest way to reach a memory variable. The reach is limited to 2^k words for a k-bit field.
Indirect reaches a bigger space. The field points to a memory word, and that word holds the full-width address. With an n-bit memory word, the reach is 2^n locations. The price is an extra memory read.
Register indirect gives the same pointer behavior with one fewer memory read, because the pointer is in a register. It is the basic form for pointers and for walking through data.
Indexed serves arrays. The address field holds the array start and the index register holds the element number or offset. Changing the register moves through the array without changing the instruction.
Base register serves relocation. The program is written with offsets, and the operating system loads a base value into the register. Moving the program to another place in memory then needs one register change, not edits to every address.
Relative serves position-independent code and short branches. A small signed offset is added to the PC, so the instruction works wherever it is loaded and needs few bits.
Autoincrement and autodecrement combine access with a pointer update. They suit loops over arrays and stack push and pop.
Choosing where the operand lives
The diagram shows the decision the processor makes for the data-access modes.
A worked comparison in code
This sketch contrasts direct and indexed access on a three-element array stored at addresses 200 to 202. It also computes a relative branch target using the common convention that the PC already points to the next instruction.
memory = {200: 10, 201: 20, 202: 30}
# Direct mode: one instruction per element, each with a fixed address.
direct_total = memory[200] + memory[201] + memory[202]
# Indexed mode: one instruction in a loop, the index register changes.
indexed_total = 0
for xr in range(3):
indexed_total += memory[200 + xr] # EA = A + XR with A = 200
print(direct_total, indexed_total) # 60 60
def branch_target(instr_addr, instr_len, offset):
"""Relative mode: the PC has already moved past the instruction."""
pc_after_fetch = instr_addr + instr_len
return pc_after_fetch + offset
print(branch_target(1000, 2, -6)) # 996Both array sums give the same answer. Only the indexed version still works if the array grows, because the loop bound changes and the instruction does not.
Trade-offs and common errors
- Speed against flexibility. Immediate and register modes are fastest. Indirect is the most flexible and the slowest, at two operand reads.
- Instruction size. Richer modes need mode bits in the instruction, or they need separate opcodes. Mode bits take space from the address field.
- Hardware cost. Each extra mode adds adders, registers, or control steps. This is one reason RISC designs offer few modes and CISC designs offer many.
Watch for these mistakes:
- Treating the immediate value as an address. In immediate mode there is no EA.
- Forgetting that indirect mode costs two reads. Count the pointer read and the operand read.
- Using the PC value of the instruction itself in relative mode. Use the incremented PC unless the question says otherwise.
- Confusing indexed with base register mode. Mechanically both add a register to the field. The difference is purpose: the index register changes inside a loop, and the base register is set once for relocation.
Exercise
An instruction has a 12-bit address field, and memory words are 32 bits wide. How many distinct memory locations can direct mode reach and how many can indirect mode reach? How many operand memory reads does each mode need?
Show answer
Direct mode reaches 2^12 = 4096 locations and needs 1 operand read. Indirect mode reads a 32-bit pointer from memory, so it can reach up to 2^32 locations. It needs 2 operand reads: one for the pointer and one for the operand.
Exercise
A relative branch is at address 500, takes 2 words, and has offset +10. The PC is incremented after fetch. What is the branch target? What is the target if the offset is -4?
Show answer
The PC after fetch is 500 + 2 = 502. With +10 the target is 512. With -4 the target is 498.