Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [74]
Now I know you're tempted to practice your hexadecimal arithmetic and fill in the little boxes yourself. But that's not the point of this demonstration. We want the automated adder to do the additions for us.
Instead of making the automated adder do just one thing—which in the first version involved adding the contents of a RAM address to the 8-bit latch that I've called the accumulator—we actually want it now to do four different things. To begin an addition, we want it to transfer a byte from memory into the accumulator. I'll call this operation Load. The second operation we need to perform is to Add a byte in memory to the contents of the accumulator. Third, we need to take a sum in the accumulator and Store it in memory. Finally, we need some way to Halt the automated adder.
In gory detail, what we want the automated adder to do in this particular example is this:
Load the value at address 0000h into the accumulator.
Add the value at address 0001h to the accumulator.
Add the value at address 0002h to the accumulator.
Store the contents of the accumulator at address 0003h.
Load the value at address 0004h into the accumulator.
Add the value at address 0005h to the accumulator.
Store the contents of the accumulator at address 0006h.
Load the value at address 0007h into the accumulator.
Add the value at address 0008h to the accumulator.
Add the value at address 0009h to the accumulator.
Store the contents of the accumulator at address 000Ah.
Halt the workings of the automated adder.
Notice that just as in the original automated adder, each byte of memory is still being addressed sequentially beginning at 0000h. The original automated adder simply added the contents of the memory at that address to the contents of the accumulator. In some cases, we still want to do that. But we also sometimes want to Load the accumulator directly with a value in memory or to Store the contents of the accumulator in memory. And after everything is done, we want the automated adder to simply stop so that the contents of the RAM array can be examined.
How can we accomplish this? Well, it's not sufficient to simply key in a bunch of numbers in RAM and expect the automated adder to do the right thing. For each number in RAM, we also need some kind of numeric code that indicates what the automated adder is to do: Load, Add, Store, or Halt.
Perhaps the easiest (but certainly not the cheapest) way to store these codes is in a whole separate RAM array. This second RAM array is accessed at the same time as the original RAM array. But instead of containing numbers to be added, it contains the codes that indicate what the automated adder is supposed to do with the corresponding address in the original RAM array. These two RAM arrays can be labeled Data (the original RAM array) and Code (the new one):
We've already established that our new automated adder needs to be able to write sums into the original RAM array (labeled Data). But the new RAM array (labeled Code) will be written to solely through the control panel.
We need four codes for the four actions we want the new automated adder to do. These codes can be anything we want to assign. Here are four possibilities:
Operation
Code
Load
10h
Store
11h
Add
20h
Halt
FFh
So to perform the three sets of addition in the example I just outlined, you'll need to use the control panel to store the following values in the Code RAM array:
You might want to compare the contents of this RAM array with the RAM array containing the data we want to add (shown on page 211). You'll notice that each code in the Code RAM corresponds to a value in the Data RAM that is to be loaded into or added to the accumulator, or the code indicates that a value is to be stored back in memory. Numeric codes used in such a manner are often called instruction codes, or operation codes, or (most concisely) opcodes. They "instruct" circuitry to perform a certain "operation."
As I mentioned earlier, the output of the 8-bit latch in the original