School/□ Systems Engineer/Week 27
Project: program the processor
The Nibble machine can't multiply. By the end of this lesson it will, using only adding, storing and one conditional jump.
About 60 min · 3 goals · Checked by automated tests
1.Why should I care?
This is the first half of the term project: make a real program work on a machine with almost nothing in its instruction set, then measure it like an engineer. The measurement is what makes the second half, speeding it up, possible.
- ◇ Design, write and trace a looping program for a minimal ISA.
- ◇ Test it on edge cases including zero and overflow.
- ◇ Measure its cost in instructions and clock cycles as a function of its inputs.
2.Discover
Do Level 4: write the multiply program (M0 × M1 into M2, with M15 = 15). Plan it as a flowchart first, then write it, step through it for 3 × 4, and run the server's tests. Record how many steps it took for each test pair.
Can't use the interactive? Read a description instead
Level 4 of the Virtual Computer provides a program editor with LOAD, ADD, STORE, JNZ and HALT, a step-by-step trace, and server tests on six pairs including zeros. The program must leave M0 × M1 (4-bit) in M2 using M15 = 15 as −1.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
Step counts are 25 for y = 3 and 41 for y = 5. Assuming N = b·y + s, what is b?
Practice 2
The Nibble machine multiplies 5 × 4 in 4-bit arithmetic. What value ends up in the answer cell?
4.Apply it
Deliverable 1 of the term project: a working multiply program that passes the build's tests, a trace table for one test pair, a table of instructions executed for at least four values of y, and your fitted formula N(y) = b·y + s with cycles at 3 per instruction.
5.Understand
Plan with a loop invariant
Before coding, state what stays true each time round the loop, for example: 'M2 + x × (remaining y) = x × original y'. Each pass adds x to M2 and takes 1 off y; when y reaches 0 the invariant gives M2 = x × y. Invariants turn 'I think it works' into an argument.
Test the edges
Test small ordinary cases (3 × 4), the identities (1 × 7), and the zeros (0 × 6, 6 × 0). In 4 bits, results above 15 wrap, and a loop that counts y down with ADD 15 behaves surprisingly when y starts at 0: trace it and explain what happens. The build's own notes describe the behaviour you should be able to justify.
Measure, don't guess
Record instructions executed for several y and fit a formula: instructions = b·y + s, where b is the loop body length and s the setup and HALT. Multiply by cycles per instruction (3 in the stepper's model) for cycles. This table is your 'before' measurement for the speed-up.
Explain it another way
You are teaching a very literal helper to multiply using only 'add this', 'remember this' and 'if not zero, go back'. Every detail has to be spelled out.
Repeated-addition multiply is O(y) time. Shift-and-add multiplication is O(number of bits), which is why real ALUs or ISAs include shifts.
If the loop body has b instructions and runs y times, with s other instructions: N(y) = b·y + s and cycles = CPI · N(y).
Small microcontrollers without a hardware multiplier still multiply in software routines written exactly this way, carefully optimised because they run constantly in control loops.
Worked example 1
A different looping program's measured step counts are 14 for y = 2 and 26 for y = 4. Find b and s.
Each extra y costs (26 − 14)/2 = 6 instructions, so b = 6; s = 14 − 2 × 6 = 2. N(y) = 6y + 2, and at 3 cycles per instruction, cycles = 18y + 6.
Common mix-ups
- If it works for 3 × 4 it works.
✓ Zeros, ones and overflow are where loops fail; test them deliberately.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 3
Why state a loop invariant before writing the program?
Practice 4 · a fresh version every time
A program spends 2 cycles on setup, then runs a loop 15 times with 8 cycles per pass. A new instruction cuts the loop body to 6 cycles. What is the speed-up (old cycles ÷ new cycles), to 2 decimal places?
Practice 5 · a fresh version every time
Two 8-bit registers start at A = 0, B = 4. On every clock edge both transfers happen at once: A ← B and B ← (A + B) mod 256. What does B hold after 6 clock cycles?
7.Predict
Multiplying x by y by repeated addition, roughly how does the number of instructions grow with y?
Guess first. Being wrong here is how the surprise works.
8.Reflect
Concepts: Instruction set architecture · Algorithms, searching and complexity · Fixed-width integers, bits and registers