C81 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 addi with lw (load word) and sw (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?

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.

  1. Fetch. Read the word at the address in the PC from instruction memory.
  2. 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.
  3. 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.
  4. Memory. A load reads data memory at that address; a store writes it.
  5. Write back. The result goes into the destination register.
  6. 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

t=N⋅CPIf

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
  1. Cycles per instruction. On a single-cycle processor, CPI is 1.
  2. Run time. Divide the cycles by the clock rate: t=3×10650×106s=0.06s=60ms

Answer the program takes 0.06 s, which is 60 ms.

Worked example

Follow addi t0, t0, 4 through the cycle.

Show the solution
  1. Fetch. Fetch reads the word at the PC.
  2. Decode. Decode finds an I-type arithmetic instruction with rs1 = rd = t0 and the immediate 4, and reads t0.
  3. Execute. The ALU adds t0 and 4.
  4. Memory. The memory step does nothing.
  5. 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
  1. Average cycles per instruction. CPI=0.25×5+0.75×4=4.25
  2. Run time. t=3×106×4.25200×106s=63.75ms
  3. 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.
  4. 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.

  1. Read t0 from the register file
  2. Write the word into t2
  3. Fetch the instruction word at the address in the PC
  4. Read data memory at that address
  5. Decode the opcode and the register fields
  6. 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°.

Explain why

Why does a single-cycle processor's clock period have to be long enough for its slowest instruction?

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.