TactivergeLite

Lite / Engineering Complex Systems / 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 · Full interactive version

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.

Predict

Multiplying x by y by repeated addition, roughly how does the number of instructions grow with y?

  1. It stays constant
  2. In proportion to y
  3. In proportion to y²
  4. In proportion to log y
Guess first, then reveal

Each pass of the loop adds x once and counts y down once, so instructions ≈ (loop body) × y + a little setup: proportional to y. That linear cost is what you will try to beat.

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.

What the interactive shows (text version)

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.

You can also open the interactive on the full site when you have a faster connection.

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

Intuitive

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.

Technical

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.

Mathematical

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).

Applied

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

instructions

Practice 3. Why state a loop invariant before writing the program?

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.

Reflect

Which edge case surprised you most, and how did you explain it?

Write your answer on paper or in your own notes. Lite never stores what you write.

← How fast can the clock tick?Project: then speed it up →