Our calculator can run simple programs composed of ADD
and SUBTRACT
instructions.
Let’s extend the instruction set to add some state. Currently, any results are produced in a stream and discarded. We’re going to add a SAVE
instruction to store this data.
operation | description |
---|---|
ADD | Adds two numbers |
SUBTRACT | Subtracts two numbers |
SAVE | Saves the result of the last addition/subtraction to a register |
We’re going to need to proceed carefully, as there are some tripwires here.
First, note that some time will pass between an ADD
/SUBTRACT
and the SAVE
instruction arriving. We need to make sure that the result is temporarily stored somewhere in the meantime, so that the SAVE
instruction can grab it.
Let’s use a looped register from our lessons on state:
The plan is to dump the results of each ADD
/SUBTRACT
immediately into this block of memory, erasing whatever was previously there. It then keeps on cycling until the next ADD
/SUBTRACT
. In the meantime, if a SAVE
comes along, we can copy the results from this block of memory to more permanent storage.
We’ll call this block of memory the output register. This kind of thing is exactly what registers are intended for in computing — storing small, low-level, temporary results.
We’ll create a second register called the save register. When we SAVE
, we’ll copy the output register into the save register. The save register can only be overwritten by a subsequent SAVE
.
writing to the register
We need to make sure that every time an ADD/SUBTRACT happens, we trigger write mode on the output register at the same time as the result arrives.
reading from the register
Whenever we hit a SAVE, we want to trigger read mode
on the output register, move the results to the A register, and trigger write mode
to insert them.
next
We’ve built a calculator that can add, subtract, and save its results. In the next sequence, we’ll start extending this into a more general-purpose computer.
finish