Addressing Modes

MEMORY20002001200220032004200520062007MEMORY INTERFACECONTROL UNITALUMARMDRIRA
A word in transitInternal busAddress busData busControl busIR already holds the instruction. Each transfer copies one word. A window flashes when its register is written.
0 steps

Memory, in hex. The shaded slot is the drawer on the bus.

The problem

LOAD brings a value into the accumulator, A. The instruction has to say where that value is. It carries one 16 bit field, and the addressing mode says how to read the field: as the value, as the address of the value, or as the address of the address. Fig 1 runs the same LOAD in each mode. The field is 2000 every time, and A ends up holding 2000, 2003 or 7B10.

16 bit addresses

Every drawer has a number, its address. In this machine an address is 16 bits, so there are 216 = 65,536 drawers. In hex that is 0000 to FFFF, four digits of four bits each. Fig 1 shows eight of them, 2000 to 2007. The address bus has 16 wires, one per bit, and if each drawer held one byte the machine could reach 64 KiB.

A word is also 16 bits, so a drawer can hold an address. Drawer 2000 holds 2003. Whether that is a number or an address depends only on how the CPU reads it. Indirect mode reads it as an address.

The instruction

An instruction takes two words. The first says what to do and how to read the field. The second is the field.

WordBitsHolds
115 to 8Opcode: LOAD, ADD, STORE and so on
17 to 6Mode: 00 immediate, 01 direct, 10 indirect
215 to 0The field: a 16 bit value or address

In assembly the mode is written around the field. The three forms of the LOAD in Fig 1:

ModeWrittenThe field isA getsMemory reads
ImmediateLOAD #2000the value20000
DirectLOAD 2000the address of the valueM[2000] = 20031
IndirectLOAD (2000)the address of the addressM[M[2000]] = M[2003] = 7B102

M[x] means the word in drawer x. Some books write indirect as @2000 or [2000].

Immediate

The value is inside the instruction, so the execute phase is one transfer:

A ← IR[field]

Nothing goes out on the address bus. Use immediate mode for a constant the program knows when it is written: add 1, load 0, compare with 10.

Direct

The field is where the value lives. The CPU reads that drawer once:

MAR ← IR[field] MDR ← M[MAR] A ← MDR

This is the LOAD from the fetch-execute article, with a 16 bit address instead of a 3 bit one. Use direct mode for a variable kept at a fixed address.

Indirect

The field points to a drawer that holds the real address. The CPU reads twice:

MAR ← IR[field] MDR ← M[MAR] MAR ← MDR MDR ← M[MAR] A ← MDR

The middle transfer, MAR ← MDR, is the whole difference: the word that came back is sent out again as an address. Use indirect mode when the address itself changes while the program runs. A program that walks through a list keeps the current item's address in one drawer. The same LOAD (2000) then fetches a different item each time round, because the program changes the word in 2000 between rounds.

Telling the modes apart

Count the memory reads: immediate 0, direct 1, indirect 2. Each read is one trip out on the address bus and back on the data bus, so an indirect LOAD takes about twice as long as a direct one. That is the price of an address that can change.

Check your understanding

  1. In Fig 1, drawer 2007 holds 2005 and drawer 2005 holds 1234. What does A hold after LOAD #2007, LOAD 2007 and LOAD (2007)?
  2. Drawer 2000 is changed to hold 2006. Which of the three LOADs in Fig 1 give a different answer, and what is it?
  3. Why must the field be the same width as an address, 16 bits, for direct and indirect mode to work?
  4. A CPU has a 20 bit address bus. How many drawers can it name, and what is the highest address in hex?

Summary