Unit - 5 Computer Arithmetic: 5.1 Addition and Subtraction
Unit - 5 Computer Arithmetic: 5.1 Addition and Subtraction
Unit - 5 Computer Arithmetic: 5.1 Addition and Subtraction
Computer Arithmetic
The four basic arithmetic operations are addition, subtraction, multiplication and division. In
this chapter we develop the various arithmetic algorithms and show the procedure for
implementing them with digital hardware. We consider addition, subtraction,
m u l t i p l i c a t i o n , and division for the following types of data,
1. Fixed-point binary data in signed-magnitude representation
2. Fixed-point binary data in signed-2’s complement represent ation
3. Floating-point binary data
4. Binary-coded decimal (BCD) data
We designate the magnitude of the two numbers by A and B. When the signed
numbers are added or subtracted, we find that there are eight different conditions to
consider, depending on the sign of the numbers and the operation performed.
These conditions are listed in the first column of Table 10-1. The other columns in
the table show the actual operation to be performed with magnitude of the numbers.
The last column is needed to prevent a negative zero. In other words, when two
equal numbers are subtracted, the result should be +0 not -0.
Addition (subtraction) algorithm: when the signs of A and B are identical
(different), add the two magnitudes and attach the sign of A to the result.
When the signs of A and B are different (identical), compare the magnitudes
and subtract the smaller number from the larger. Choose the sign of the result
to be the same as A if A > B or the complement of the sign of A if A < B. If the
two magnitudes are equal, subtract B from A and make the sign of the result
positive.
Use the terminology given in bracket for subtraction
Hardware Implementation
To implement the addition and subtraction operations with hardware, it is first
necessary that the two numbers be stored in registers. Let A and B be two
registers that hold the magnitudes of the numbers, and As and Bs be two
flip-flops that hold the corresponding signs. The result of the operation may
be transferred to a third register or to one of the source register (A).
Figure 10-1 shows a block diagram of the hardware for implementing the
addition and subtraction operations. It consists of registers A and B and
sign flip-flops As and Bs. Subtraction is done by adding A to the 2's
complement of B. The output carry is transferred to flip-flop E, where it
can be checked to determine the relative magnitudes of the two numbers.
The add-overflow flip-flop AVF holds the overflow bit when A and B are
added. The A register provides other micro operations that may be needed
when we specify the sequence of steps in the algorithm.
The addition of A plus B is done through the parallel adder. The S (sum)
output of the adder is applied to the input of the A register. The complementer
provides an output of B or the complement of B depending on the state of
the mode control M. The complementer consists of Exclusive-OR gates and
parallel adder consists of Full adder circuits.
The M signal is also applied to the input carry of the adder. When M = 0,
the output of B is transferred to the adder, the input carry is 0, and the
output of the adder is equal to the sum A + B. When M= 1 , the l's
complement of B is applied to the adder, the input carry is 1, and output S
A + B + 1. This is equal to A plus the 2's complement of B, which is
equivalent to the subtraction A – B.
Hardware Algorithm
The flowchart for the hardware al gori t hm is presented in Fig 10-2. The two signs As
and Bs are compared by an exclusive-OR gate. If the output of the gate is 0, the signs are
identical; if it is 1, signs are different.
For an add operation, identical signs dictate that magnitudes be added.
For a subtract operation, different signs dictate that the magnitudes be added.
The magnitudes are added with a micro operation EA=A + B, where EA is a register that
combines E and A. The value of E is transferred into the add-overflow flip-flop AVF.
The two magnitudes are subtracted if the signs are different for an add operation
or identical for a subtract operation. The magnitudes are subtracted by adding A to the
2's complement of B.
The leftmost bit of a binary number represents the sign bit: 0 for positive and 1 for negative.
If the sign bit is 1, the entire number is represented in 2' s complement form Thus + 33 is
represented as 00100001and -33 as 11011111.Note that 11011111is the 2's complement of
00100001, and vice versa
We name the A register AC (accumulator) and the B register BR. The leftmost bit in
AC and BR represent the sign bits of the numbers.
The two sign bits are added or subtracted together with the other bits in the
complementer and parallel adder
The overflow flip-flop V is set to 1 if there is an overflow. The output carry in this
case is discarded.
The sum is obtained by adding the contents of AC and BR (including their sign bits).
The overflow bit V is set to 1 if the exclusive-OR of the last two carries is 1, and it is
cleared to 0 otherwise.
The subtraction operation is accomplished by adding the content of AC to the 2's
complement of BR. Taking the 2's complement of BR has the effect of changing a
positive number to negative, and vice versa.
An overflow must be checked during this operation because the two numbers added
could have the same sign.
5.2 Multiplication Algorithms
Booth algorithm gives a procedure for multiplying binary integers in signed-2' s complement
representation.
As in all multiplication schemes, Booth algorithm requires examination of the
multiplier bits and shifting of the partial product. Prior to the shifting, the multiplicand may
be added to the partial product, subtracted from the partial product, or left unchanged
according to the following rules:
1. The multiplicand is subtracted from the partial product upon first least significant 1 in a
string of l's in the multiplier. (Q Qn+1 = 10)
2. The multiplicand is added to the partial product upon the first 0 in a string of 0's in the
multiplier. (Q Qn+1 = 01)
3. The partial product does not change when the multiplier bit is identical to the previous
multiplier bit. (Qn Qn+1 = 00 or 11)
We rename registers A, B, and Q as AC, BR, and QR respectively.
Qn designates the least significant bit of the multiplier in register QR.
An extra flip-flop Qn+1 is appended to QR to facilitate a double bit inspection of
the multiplier.
AC and the appended bit Qn+l are initially cleared to 0 and the sequence counter SC
is set to a number n equal to the number of bits in the multiplier.
The two bits of the multiplier in Qn and Qn+1 are inspected.
If the two bits are equal to 10, it means that the first 1 in a string of l's has
been encountered. This requires a subtraction of the multiplicand from the
partial product in AC.
If the two bits are equal to 01, it means that the first 0 in a string of 0's has been
encountered. This requires the addition of the multiplicand to the partial product
in AC.
When the two bits are equal, the partial product does not change. An overflow
cannot occur because the addition and subtraction of the multiplicand follow each
other.
The next step is to shift right the partial product and the multiplier (including bit
Qn+1)· This is an arithmetic shift right (ashr) operation which shifts AC and QR to
the right and leaves the sign bit in AC unchanged.
5.2.3 Array Multiplier
As shown in the diagram, the first partial product is formed by means of two AND
gates. The second partial product is formed by multiplying a1 by b1 b0 and is shifted
one position to the left. The two partial products are added with two half-adder (HA)
circuits.
Usually, there are more bits in the partial products and it will be necessary to use
full-adders to produce the sum.
The hardware for implementing the division operation is identical to that required for
multiplication.
Register EAQ is shifted to the left with 0 inserted into Q and the previous value of E
lost.
The divisor is stored in the B register and the double-length dividend is stored in
registers A and Q.
The dividend is shifted to the left and the divisor is subtracted by adding its 2' s
complement value.
The information about the relative magnitude is available in E. If E = 1, it signifies
that A>=B. A quotient bit 1 is inserted into Q. and the partial remainder is shifted
to the left to repeat the process.
If E = 0, it signifies that A < B so the quotient in Q remains a 0 (inserted during the
shift). The value of B is then added to restore the partial remainder in A to its
previous value.
The partial remainder is shifted to the left and the process is repeated again until
all five quotient bits are formed. Note that while the partial remainder is shifted
left, the quotient bits are also shifted and after five shifts, the quotient is in Q and
the final remainder is in A.
The hardware divide algorithm is shown in the flowchart of Fig. 10-13. The
dividend is in A and Q and the divisor in B. The sign of the result is transferred into
Qs to be part of the quotient. A constant is set into the sequence counter SC to
specify the number of bits in the quotient.
Since we are considering only magnitude SC will consist of n -1 bits.
A divide-overflow condition is tested by subtracting the divisor in B from half of the
bits of the dividend stored in A.
If A>=B, the divide-overflow flip-flop DVF is set and the operation is terminated
prematurely.
If A < B, no divide overflow occurs so the value of the dividend is restored by adding
B to A.
The division of the magnitudes starts by shifting the dividend in AQ to the left with
the high-order bit shifted into E. If the bit shifted into E is 1, we know that EA> B
because EA consists of a 1 followed by n-1 bits while B consists of only n-1 bits.
In this case, B must be subtracted from EA and 1 inserted into Qn for the quotient bit.
If the shift-left operation inserts a 0 into E, the divisor is subtracted by adding its 2's
complement value and the carry is transferred into E. If E = 1, it signifies that A >= B;
therefore, Qn is set to 1. If E = 0, it signifies that A < B and the original number is
restored by adding B to A. In the latter case we leave a 0 in Q. (0 was inserted during
the shift).
This process is repeated again with register A holding the partial remainder. After n -
1 times, the quotient magnitude is formed in register Q and the remainder is found in
register A. The quotient sign is in Qs and the sign of the remainder in As is the same
as the original sign of the dividend.
Divide Overflow