Online Book Reader

Home Category

Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [76]

By Root 1512 0
the 2 low-order bytes results in a carry:

This carry must be added to the sum of the 2 high-order bytes:

for a final result of 9A17h.

Can we enhance the circuitry of our automated adding machine to add two 16-bit numbers correctly? Yes, we can. All we need do is save the Carry Out bit from the 8-Bit Adder when the first addition is performed and then use that Carry Out bit as the Carry Input bit to the next addition. How can a bit be saved? By a 1-bit latch, of course; this time, the latch is known as the Carry latch.

To use the Carry latch, another operation code is needed. Let's call it Add with Carry. When you're adding 8-bit numbers together, you use the regular old Add instruction. The carry input to the adder is 0, and the carry output from the adder is latched in the Carry latch (although it need not be used at all).

If you want to add two 16-bit numbers together, you use the regular Add instruction for adding the low-order bytes. The carry input to the adder is 0 and the carry output is latched in the Carry latch. To add the 2 high-order bytes, you use the new Add with Carry instruction. In this case, the two numbers are added using the output of the Carry latch as the carry input to the adder. So if the first addition resulted in a carry, that carry bit is used in the second addition. If no carry resulted, the output from the Carry latch is 0.

If you're subtracting one 16-bit number from another, you need another new instruction; this one is called Subtract with Borrow. Normally, a Subtract instruction requires that you invert the subtrahend and set the carry input of the adder to 1. A carry out of 1 is normal and should usually be ignored. If you're subtracting a 16-bit number, however, that carry output should be saved in the Carry latch. In the second subtraction, the carry input to the adder should be set to the result of the Carry latch.

With the new Add with Carry and Subtract with Borrow operations, we have a total of seven opcodes so far:

Operation

Code

Load

10h

Store

11h

Add

20h

Subtract

21h

Add with Carry

22h

Subtract with Borrow

23h

Halt

FFh

The number sent to the adder is inverted for a Subtract or a Subtract with Borrow operation. The carry output of the adder is the data input to the Carry latch. The latch is clocked whenever an Add, Subtract, Add with Carry, or Subtract with Borrow operation is being performed. The carry input of the 8-Bit Adder is set to 1 when a Subtract operation is performed or when the data output of the Carry latch is 1 and an Add with Carry or Subtract with Borrow operation is being performed.

Keep in mind that the Add with Carry instruction causes the carry input of the 8-Bit Adder to be set to 1 only if the previous Add or Add with Carry instruction resulted in a carry output from the adder. Thus you use the Add with Carry instruction whenever you're adding multibyte numbers whether or not the operation is actually needed. To properly code the 16-bit addition shown earlier, you use

This works correctly regardless of what the numbers are.

With these two new opcodes, we've greatly expanded the scope of the machine. No longer are we restricted to adding 8-bit values. By repeated use of the Add with Carry instruction, we can now add 16-bit values, 24-bit values, 32-bit values, 40-bit values, and so on. Suppose we want to add the 32-bit values 7A892BCDh and 65A872FFh. We need one Add instruction and three Add with Carry instructions:

Of course, actually keying these numbers into memory isn't the most rewarding job around. Not only do you have to use switches to represent binary numbers, but the numbers aren't stored in consecutive addresses. For example, the number 7A892BCDh goes into addresses 0000h, 0003h, 0006h, and 0009h starting with the least-significant byte. To get the final result, you have to examine the values located at addresses 0002h, 0005h, 0008h, and 000Bh.

Moreover, the current design of our automated adder doesn't allow the reuse of results in subsequent calculations. Suppose we want to add three 8-bit numbers

Return Main Page Previous Page Next Page

®Online Book Reader