Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [52]
Step 4. Subtract 100000000 (which equals 256):
The result is equivalent to 77 in decimal.
Let's try it again with the two numbers reversed. In decimal, the subtraction problem is
and in binary it looks like this:
Step 1. Subtract the subtrahend from 11111111. You get the ones' complement:
Step 2. Add the ones' complement of the subtrahend to the minuend:
Now 11111111 must be subtracted from the result in some way. When the original subtrahend is smaller than the minuend, you accomplish this task by adding 1 and subtracting 100000000. But you can't subtract this way without borrowing. So instead, we subtract this result from 11111111:
Again, this strategy really means that we're just inverting all the bits to get the result. The answer again is 77, but really –77.
At this point, we have all the knowledge we need to modify the adding machine developed in the last chapter so that it can perform subtraction as well as addition. So that this doesn't become too complex, this new adding and subtracting machine will perform subtractions only when the subtrahend is less than the minuend, that is, when the result is a positive number.
The core of the adding machine was an 8-Bit Adder assembled from logic gates:
As you probably recall, the inputs A0 through A7 and B0 through B7 were connected to switches that indicated two 8-bit values to be added. The Carry In input was connected to ground. The S0 through S7 outputs were connected to eight lightbulbs that displayed the result of the addition. Because the addition could result in a 9-bit value, the Carry Out output was also connected to a ninth lightbulb.
The control panel looked like this:
In this diagram, the switches are set to add 183 (or 10110111) and 22 (00010110), producing the result of 205, or 11001101 as shown in the row of lightbulbs.
The new control panel for adding and subtracting two 8-bit numbers is just slightly modified. It includes an extra switch to indicate whether we want to add or subtract.
You turn this switch off for addition and on for subtraction, as labeled. Also, only the rightmost eight lightbulbs are used to display results. The ninth lightbulb is now labeled "Overflow/Underflow." This lightbulb indicates that a number is being calculated that can't be represented by the eight lightbulbs. This will happen if an addition produces a number greater than 255 (that's called an overflow) or if a subtraction produces a negative number (an underflow). A subtraction will produce a negative number if the subtrahend is larger than the minuend.
The major addition to the adding machine is some circuitry that calculates a ones' complement of an 8-bit number. Recall that the ones' complement is equivalent to inverting bits, so something to calculate the ones' complement of an 8-bit number might look as simple as eight inverters:
The problem with this circuit is that it always inverts the bits that enter into it. We're trying to create a machine that does both addition and subtraction, so the circuitry needs to invert the bits only if a subtraction is being performed. A better circuit looks like this:
A single signal labeled Invert is input to each of eight XOR (exclusive OR) gates. Recall that the XOR exhibits the following behavior:
XOR
0
1
0
0
1
1
1
0
So if the Invert signal is 0, the eight outputs of the XOR gates are the same as the eight inputs. For example, if 01100001 is input, then 01100001 is output. If the Invert signal is 1, the eight input signals are inverted. If 01100001 is input, 10011110 is output.
Let's package these eight XOR gates in a box labeled Ones' Complement:
The Ones' Complement box, the 8-Bit Adder box, and a final exclusive OR gate can now be wired together like this:
Notice the three signals all labeled SUB. This is the Add/Subtract switch. This signal is 0 if an addition is to be performed and 1 if a subtraction is to be performed. For a subtraction, the B inputs (the second row of switches) are all inverted by the Ones' Complement circuit before entering the