CPU Architecture and the Memory Hierarchy

IB Computer Science · Computer Organization

Inside the processor. The central processing unit contains a control unit that fetches and decodes instructions and issues control signals, an arithmetic and logic unit that performs calculations and comparisons, and a small set of very fast registers. Four registers matter for examination answers: the program counter holds the address of the next instruction; the memory address register holds the address currently being accessed; the memory data register holds the data or instruction just read from or about to be written to memory; and the accumulator holds the result of the most recent operation.

The buses. The address bus carries an address from the CPU to memory and is unidirectional, because memory never originates an address. Its width determines the maximum number of addressable locations: an n-bit address bus can address 2 to the power n locations. The data bus is bidirectional, because data is both read and written, and its width determines how much data moves per transfer. The control bus carries signals such as read, write and interrupt requests.

The machine instruction cycle. Fetch: the program counter's value is copied to the memory address register; the address travels on the address bus; the control unit asserts a read signal; the contents return on the data bus into the memory data register; the program counter is incremented. Decode: the control unit separates the instruction into an opcode saying what to do and an operand saying where the data is. Execute: the arithmetic and logic unit performs the operation, or data is transferred, with the result typically left in the accumulator.

The memory hierarchy. Memory technologies trade speed against cost and capacity, so machines use several levels. Registers are fastest and tiny. Cache is small, fast and expensive per byte; it works because of the locality of reference — programs tend to reuse the same data soon (temporal locality) and to use data stored nearby (spatial locality), so keeping recently used blocks close to the processor pays off. Primary memory (RAM) is larger, slower and volatile: contents are lost when power is removed. ROM is non-volatile and holds the bootstrap program that starts the machine, which is why a computer can start at all when RAM is empty. Secondary storage is non-volatile, far larger and far slower, and is not directly addressable by the CPU: data must be loaded into primary memory before it can be processed. This last point is the reason a program with insufficient RAM slows down dramatically rather than simply failing.

THE FETCH — DECODE — EXECUTE CYCLE CPU PC — program counter MAR — address register MDR — data register CU — control unit ALU — arithmetic and logic MAIN MEMORY (RAM) cell 100: LOAD 200 cell 101: ADD 201 cell 102: STORE 202 instructions AND data in the same memory ADDRESS BUS — one-way DATA BUS — two-way CONTROL BUS — read/write signals 1 FETCH PC → MAR · read memory → MDR · PC increases by 1 2 DECODE CU separates the opcode from the operand address 3 EXECUTE ALU computes or moves data result goes to the accumulator
A question about this cycle almost always appears, and the answer that loses marks is the one that names the three steps without saying which registers the data passes through. Learn it by the path of the signals. The fetch step. The program counter holds the address of the NEXT instruction. That address is copied to the memory address register, then goes out to memory over the address bus. The control unit sends a read signal on the control bus; the contents of the memory cell come back over the data bus and sit in the memory data register. The program counter is incremented by one to point to the following instruction. The decode step. The control unit splits the contents of the instruction into two parts: the opcode says what operation to perform, and the operand says where the data is. No calculation happens in this step. The execute step. The arithmetic and logic unit performs the calculation, or data is moved between registers and memory; the result usually lies in the accumulator. Three details worth remembering because they are often asked separately. One, the address bus is one-way — the CPU is always the one that states the address, and memory never sends an address by itself; while the data bus is two-way because data is both read in and written out. Two, the width of the address bus determines the maximum memory capacity the machine can address, while the width of the data bus determines how much data can be transferred per cycle. Three, instructions and data share one memory and travel over one bus, so the bus becomes a bottleneck — and this bottleneck is the reason the cache exists.

Worked example: Why adding a cache is much faster than you might predict

A system has a cache access time of 2 ns and a main memory access time of 60 ns. The cache hit rate is 95%. (a) Calculate the average access time. (b) Calculate the average time if the hit rate falls to 80%. (c) Explain why locality of access makes a high hit rate normal rather than lucky.

Solution

(a) Hit rate of 95%. The average time equals the hit rate times the cache time plus the miss rate times the main memory time. Substituting: 0.95 times 2 plus 0.05 times 60 equals 1.9 plus 3.0 equals 4.9 ns. Compared with 60 ns without a cache, the system is about 12 times faster.

(b) Hit rate of 80%. 0.8 times 2 plus 0.2 times 60 equals 1.6 plus 12.0 equals 13.6 ns. This result is worth noting: the hit rate fell by only 15 percentage points yet the average time almost tripled. The reason is that the contribution of the misses is multiplied by a very large factor, so it is the miss rate that dominates rather than the hit rate. This is also why processor designers monitor the miss rate rather than the hit rate.

(c) Why a high hit rate is normal. If a program accessed memory randomly, a small cache would be almost useless. But real programs do not access randomly, for two reasons that have their own names.

Temporal locality: a memory cell that has just been used is very likely to be used again soon afterward. A loop counter variable is read and written on every iteration; a total variable is accumulated thousands of times; the very code in the loop body is fetched again on every pass.

Spatial locality: if one cell is used, the neighboring cells are very likely to be used right after. Traversing an array goes through consecutive elements in memory; the instructions of a program lie next to one another so after executing the instruction at address 100, the one at 101 is almost certainly executed next. Because the cache loads a whole block rather than one byte at a time, one miss is followed by several hits in a row.

The consequence to state in an answer. Because the mechanism relies on locality, programs that break locality will have a high miss rate however large the cache is — for example traversing a matrix by column in a row-major language, or following a linked list whose nodes are scattered through memory. This is why contiguous data structures such as arrays are often faster than linked lists in practice, even when the theoretical analysis gives the same complexity.

Worked example: Deducing the maximum memory capacity from the bus width

A processor has a 20-bit address bus and a 16-bit data bus, and each memory cell holds 1 byte. (a) Calculate the maximum number of memory cells and the total addressable capacity. (b) If the address bus is widened to 32 bits, how does the capacity change? (c) What does widening the data bus affect?

Solution

(a) With a 20-bit address bus. Each line of the bus carries one bit, so the total number of distinct address combinations is 2 to the power of 20 equals 1,048,576. Since each cell holds 1 byte, the maximum addressable capacity is 1,048,576 bytes, that is, 1 MiB.

(b) Widening to 32 bits. The number of cells becomes 2 to the power of 32 equals 4,294,967,296, that is, 4 GiB. The point to recognize is that the relationship is exponential: adding 12 wires makes the capacity increase by 2 to the power of 12 equals 4,096 times. This is also the standard explanation of why 32-bit systems cannot use more than 4 GiB of main memory without additional techniques, and why moving to a 64-bit architecture removes that limit entirely.

(c) What the data bus affects. The data bus does NOT affect the maximum memory capacity — this is the point most often confused. It determines how much data can be transferred in one transfer. With a 16-bit data bus, the machine reads 2 bytes per cycle; widening to 32 bits, it reads 4 bytes per cycle, so at the same clock speed, memory bandwidth doubles.

How to tell them apart so as not to confuse them in the exam room. The address bus answers the question how many places are there to refer to; the data bus answers the question how much is fetched each time. An analogy: the address bus is like the maximum number of house numbers a street can have, while the data bus is like the amount of cargo carried per trip of the truck.

Common mistake

Trap 1 — naming the three steps of the instruction cycle without saying which registers the data passes through. Marks are awarded for the path: program counter to address register, out on the address bus, the read signal, data back to the data register, counter incremented by one.
Trap 2 — saying the data bus determines the maximum memory capacity. That is the ADDRESS bus, with capacity equal to 2 to the power of its width. The data bus determines the amount of data transferred each time.
Trap 3 — saying the cache is faster so it should completely replace main memory. The cache is far more expensive per byte, so it must be small; its value comes from locality of access, not from capacity.

Key takeaway

Lesson 1 summary. Four things to take into the exam. One, four registers and three buses, remembering that the address bus is one-way while the data bus is two-way. Two, the fetch-decode-execute cycle described by the path of the data, not by the names of the steps. Three, an n-bit address bus can address 2 to the power of n cells, and this relationship is exponential so adding a few wires makes the capacity grow very fast. Four, temporal locality and spatial locality explain the whole memory hierarchy, and the average access time formula shows that it is the MISS rate that dominates.

← Stakeholders, Documentation, Training and the Social Impact of Systems · All IB Computer Science lessons · Practice IB Computer Science questions (free, instant scoring) · Data Representation, Logic Gates and the Role of the Operating System →