Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [79]
Notice that the first Load instruction refers to the memory location 0013h, which is where the result of the first calculation was stored.
So now we have some instructions starting at address 0000h, some data starting at 0010h, some more instructions at 0020h, and some more data at 0030h. We want to let the automated adding machine start at 0000h and execute all the instructions.
We know we must remove that Halt instruction at address 000Ch, and by remove I really mean replace it with something else. But is that sufficient? The problem is that whatever we replace the Halt instruction with is going to be interpreted as an instruction byte. And so will the bytes stored every 3 bytes after that—at 000Fh, and 0012h, and 0015h, and 0018h, and 001Bh, and 001Eh. What if one of these bytes just happens to be an 11h? That's a Store instruction. And what if the 2 bytes following that Store instruction happened to refer to address 0023h? That would cause the machine to write the contents of the accumulator to that address. But that address contains something important already! And even if nothing like this happened, the next instruction byte that the adder retrieves from memory after the one at 001Eh will be at address 0021h, not 0020h, which is where our next real instruction happens to be.
Are we all in agreement that we can't just remove the Halt instruction at address 000Ch and hope for the best?
But what we can replace it with is a new instruction called Jump. Let's add that to our repertoire.
Operation
Code
Load
10h
Store
11h
Add
20h
Subtract
21h
Add with Carry
22h
Subtract with Borrow
23h
Jump
30h
Halt
FFh
Normally, this automated adder addresses the RAM array sequentially. A Jump instruction causes the machine to alter that pattern. Instead, it begins addressing the RAM array at a different specified address. Such an instruction is sometimes also called a Branch instruction, or Goto, as in "go to another place."
In the preceding example, we can replace the Halt instruction at address 000Ch with a Jump instruction:
The 30h byte is the code for a Jump instruction. The 16-bit address that follows indicates the address of the next instruction that the automated adder is to read.
So in the preceding example, the automated adder begins at 0000h, as usual, and does a Load instruction, an Add, a Subtract, and a Store. It then does the Jump instruction and continues at address 0020h with a Load, two Add instructions, a Store, and finally Halt.
The Jump instruction affects the 16-bit counter. Whenever the automated adder encounters a Jump instruction, the counter must somehow be forced to output that new address that follows the Jump instruction code. This is implemented by using the Preset and Clear inputs of the edge-triggered D-type flip-flops that make up the 16-bit counter:
You'll recall that the Preset and Clear inputs should both be 0 for normal operation. But if Preset is 1, Q becomes 1. And if Clear is 1, Q becomes 0.
If you want to load a single flip-flop with a new value (which I'll call A for address), you can wire it like this:
Normally the Set It signal is 0. In that case, the Preset input to the flip-flop is 0. The Clear input is also 0 unless the Reset signal is 1. This allows the flip-flop to be cleared independently of the Set It signal. When the Set It signal is 1, the Preset input will be 1 and the Clear input will be 0 if A is 1. If A is 0, the Preset input will be 0 and the Clear input will be 1. This means that Q will be set to the value of A.
We need one of these for each bit of the 16-bit counter. Once loaded with a particular value, the counter will continue counting from that value on.
Otherwise, the changes aren't severe. The 16-bit address that's latched from the RAM array is an input to both the 2-to-1 Selector (which allows this address to be an address input to the RAM array) and the 16-bit counter for the Set It function:
Obviously, we must ensure that the Set It signal is 1 only if the instruction code