C8 · Lesson 1 · Fetch, decode, execute
How a processor runs a program
A processor is built from the parts of C7. A register called the program counter (PC) holds the address of the next instruction. A memory holds the instructions. A register file holds 32 working values. An ALU adds, subtracts, compares and does bitwise logic. Multiplexers choose which value goes where. Every instruction goes through the same cycle: fetch the instruction word at the PC, decode its fields, compute in the ALU, maybe read or write data memory, write the result back, and move the PC on. Below, a small RISC-V processor runs a short program, and the datapath lights every wire the last instruction used.
Play
- Press Step. Each press runs one instruction, and the datapath shows the one that just ran.
- Compare
addiwithlw(load word) andsw(store word). Which units does each one use? - Watch the PC. How much does it go up by on each step, and when does it jump instead?
Predict
The loop starts at address 0x08, and its last instruction, bnez, sits at 0x18. When that branch is taken, what does the PC hold next?
0x08. A taken branch loads the PC with the address of its label. The instruction stores that as an offset from its own address: here 0x08 − 0x18 = −16 bytes. When the branch isn't taken (on the last pass, when t1 has reached 0), the PC goes on to 0x1C as usual.
Name it
The instruction cycle
Each instruction is one 32-bit word, 4 bytes, and memory is addressed in bytes. So the PC normally goes up by 4.
- Fetch. Read the word at the address in the PC from instruction memory.
- Decode. Split the word into fields: the operation (the opcode), up to three register numbers (5 bits each, enough to pick one of 32 registers) and an immediate, a constant built into the instruction. Control logic turns the opcode into the select lines of the multiplexers and the ALU's operation.
- Execute. The ALU works on two operands: two registers, or a register and the immediate. For a load or store it adds the base register and the offset to get the address. For a branch it compares the two registers.
- Memory. A load reads data memory at that address; a store writes it.
- Write back. The result goes into the destination register.
- Next PC. PC + 4, or the branch target when a branch is taken.
In the simplest design, the single-cycle processor, all of this happens in one clock period. The PC is a register, and everything between its output and its input is combinational logic, the register-and-logic pattern of C7. The time a program takes is
for N instructions, CPI clock cycles per instruction (1 here) and clock frequency f.
Worked example
A program runs 3 million instructions on a single-cycle processor at 50 MHz. How long does it take?
Show the solution
- Cycles per instruction. On a single-cycle processor, CPI is 1.
- Run time. Divide the cycles by the clock rate:
Answer the program takes 0.06 s, which is 60 ms.
Worked example
Follow addi t0, t0, 4 through the cycle.
Show the solution
- Fetch. Fetch reads the word at the PC.
- Decode. Decode finds an I-type arithmetic instruction with rs1 = rd = t0 and the immediate 4, and reads t0.
- Execute. The ALU adds t0 and 4.
- Memory. The memory step does nothing.
- Write back. Write back puts the sum in t0, and the PC moves on by 4.
Answer fetch reads the word, decode reads t0 and the immediate 4, the ALU adds them, the memory step does nothing, and write back puts the sum in t0 while the PC moves on by 4.
Worked example
A multi-cycle processor runs at 200 MHz, taking 5 cycles for loads and 4 for everything else. A program of 3 million instructions is 25% loads. How long does it take, against the single-cycle machine's 60 ms at 50 MHz?
Show the solution
- Average cycles per instruction.
- Run time.
- Compare with the single-cycle machine. It is slightly slower than the 60 ms, despite a clock four times as fast: each instruction takes several short cycles instead of one long one.
- The general rule. The time is always instructions × CPI ÷ clock rate, and improving one factor helps only if the others don't worsen as much.
Answer 63.75 ms, slightly slower than the single-cycle machine's 60 ms.
Check
The PC holds 0x0120. With no branches taken, what does it hold after 2 more instructions?
Check
In the program above, which instructions use the data memory?
Check
A processor has 16 registers. How many bits does an instruction need to name one of them?
Check
Put the steps of a load instruction (lw t2, 0(t0)) in order.
- Read t0 from the register file
- Write the word into t2
- Fetch the instruction word at the address in the PC
- Read data memory at that address
- Decode the opcode and the register fields
- Add t0 and the offset in the ALU
Check
A single-cycle processor's slowest instruction takes 2.5 ns to settle from one clock edge to the next. What's the fastest clock it can use?
Write units the way you normally would: 1.5 MHz, 1500 kHz and 0.0015 GHz are the same answer.
Check
A single-cycle processor (CPI = 1) at 50 MHz runs a program of 500 thousand instructions. How long does it take?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
A multi-cycle processor at 50 MHz takes 6 cycles for loads (30% of instructions) and 3 for the rest, running 5 million instructions.
(a)
What's its average CPI?
(b)
How long does the program take?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
A program of N instructions runs at an average CPI of c on a clock of frequency f.
Step 1
How long does it take?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How many instructions per second?
Step 3
How long would it take if the clock doubled with no change in CPI?
Explain why
Why does a single-cycle processor's clock period have to be long enough for its slowest instruction?
The clock doesn't know which instruction is running, so every instruction gets the same period. Between two edges, the new instruction's signals have to travel from the PC through instruction memory, the register file, the ALU and, for a load, data memory, and then settle at the register file's input before the next edge writes it. If the period were shorter than a load's path, the register would capture a value that hadn't finished arriving. So the slowest path sets the clock for everything, which is the waste that pipelining (lesson 39) recovers.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.