← From gates to a processor

A tiny processor

A processor repeats one cycle: fetch the instruction the program counter points at, decode it, execute it, one step per clock tick. The program is numbers in the same memory as the data. Step a tiny processor, change its program, and write your own.

Your games, apps and websites all end up as simple instructions like ADD and JUMP. What does a processor actually do with them?

A processor is gates, registers and memory working together. It does one tiny thing per clock tick, again and again: fetch an instruction, decode it, execute it.

PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.

What will it output?

Memory holds: cell 0 LOAD 6, cell 1 ADD 7, cell 2 OUT, cell 3 HALT, cell 6 = 5, cell 7 = 3. What does the program output?

TryTry it on the board and compare the result with your prediction.

Try: tick by tick

Was your prediction right?

Try it yourselfYour edits are saved in this browser.

Press Clock tick (or Whole instruction) until it halts. Watch the PC, the instruction register and the accumulator.

Use the board. This ticks when you get there.

InvestigateWork out why it happens, one step at a time.

Worked example: one instruction, three ticks

Worked exampleBefore each step, say what you think comes next.
  1. fetch: IR=cell 0=22, PC=1\text{fetch: IR} = \text{cell } 0 = 22,\ PC = 1LOAD 6 is stored as 1 × 16 + 6 = 22. The PC moves on, ready for next time.

Now count the ticks for the whole program.

Each instruction takes three clock ticks. How many ticks does the whole program (LOAD, ADD, OUT, HALT) take?

Sam explains the memory. Click the line where it first goes wrong.

A processor runs at 3 GHz. About how many clock ticks is that each second?

ModifyChange the problem on purpose and see what happens.

Vary: change the program

Try it yourselfYour edits are saved in this browser.

Change one data cell so the program outputs 12. Type in the cell, then run it.

Use the board. This ticks when you get there.

Change one instruction so the program outputs 5 − 3.

Use the board. This ticks when you get there.

This program stores its answer. Run it: what ends up in cell 9?

Use the board. This ticks when you get there.

This program counts down from 3 using JUMP and JZ. Make it count down from 5.

Use the board. This ticks when you get there.

PractiseA few on your own, to check you have it.

The cycle

The PC is 4. The processor fetches. What is the PC afterwards (if the instruction isn't a jump)?

Which instruction lets a program repeat steps?

Review: ADD 7 adds two 8-bit numbers. What inside the processor does the adding?

MakeMake your own problem using what you have learned. Expect a few tries; that is normal.

Write a program

Then: can you do it with a loop instead, so it would work for 2, 4, 6, 8, 10 without more cells?

Try it yourselfYour edits are saved in this browser.
Need a hint?

Cell 15 holds 2. Every output needs an OUT just before it.

Another hint

LOAD 15, then OUT, then ADD 15, then OUT…

Another hint

LOAD 15; OUT; ADD 15; OUT; ADD 15; OUT; HALT (seven cells).

Memory is empty except cell 15, which holds 2. Write a program that outputs 2, 4, 6 and then halts.

Use the board. This ticks when you get there.

For teachers

Students step a tiny processor through fetch, decode and execute one clock tick at a time, change its program and data, follow a loop, and write a program of their own (AC9TDI8K04 node 3, extended; links to AC9TDI8K01). About 50 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

Try the boards yourself. The instruction set is deliberately tiny: HALT, LOAD, ADD, SUB, STORE, JUMP, JZ (jump if zero) and OUT. Each instruction is a number: operation × 16 + address, so LOAD 6 is 22. Numbers wrap at 256.

The one idea: a processor repeats one cycle: fetch the instruction the program counter points at, decode it, execute it, one step per clock tick. The program is numbers in the same memory as the data (the stored-program idea).

Simplifications: one register (the accumulator), 16 cells, three ticks per instruction. Real processors overlap the steps (pipelining) and have many registers. It is similar in spirit to the Little Man Computer, which some teachers will know.

Running the lesson

  1. Predict: students who said 53 are joining digits, not adding.
  2. Try: ask what PC shows just after a fetch, and why it is already one ahead.
  3. Worked example: model the three ticks for LOAD 6 aloud.
  4. Vary: in the loop, ask which instruction makes it repeat and which one stops it.
  5. Make: collect programs: who used the fewest cells?

The hard part (on purpose)

Writing the Make program from an empty memory is the hard part. Have students write the outputs they want first, then the instruction before each OUT.

When students get stuck

Stretch and support

Stretch: Write a program that multiplies 3 by 4 using a loop of ADDs. How many clock ticks does it take?

Support: An instruction card: each instruction, what it does, and an example.

Where it leads

Links: Inside a computer: 3 GHz means about 3 billion clock ticks a second, and each core is a processor like this (much bigger). Code pathway: the Python and MakeCode programs students write are turned into instructions like these.

What each step is for

Now you can
You reached the endBack to From gates to a processor