stateful computation

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.

operationdescription
ADDAdds two numbers
SUBTRACTSubtracts two numbers
SAVESaves 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:

00/0000/00
loading…

dynamic register

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.

note

We’re using a second register for simplicity. In practice, we’d want to move this data to a more suitable block of memory, such as RAM.

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.

coming soon
We’re still working on this part of the tutorial — come back later!

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.

coming soon
We’re still working on this part of the tutorial — come back later!

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