Chapter 04C: Arithmetic For Computers
Chapter 04C: Arithmetic For Computers
C H A P T E R 04C
ARITHMETIC FOR
COMPUTERS
1
Tailoring the ALU
to the MIPS
2
Let us first remember the slt instruction:
6
2. If (A - B) < 0, it implies that A < B.
a. It implies also that the sum of the
subtraction is a negative number
b. the sign-bit of the above sum,
(which is the leftmost bit) = 1.
c. the final result (of slt after subtraction)
should be (thirty-one 0s and one 1:
0000 0000 0000 0000 0000 0000 0000 0001
7
d. the final result of the slt consists of
31 zeros and a 1 at the LSB.
e. In the design of the 32-bit-ALU we
could make all the 1-bit ALUs to
output 0, except the LSB 1-bit-
ALU which should output the
sign-bit (1) of the subtraction A-B
8
3. If (A - B) ≥ 0, it implies that A ≥ B
a
0
Result
b 0 2
Less 3
a. CarryOut
a
0
R e s u lt
b 0 2
1
L e ss 3
Set
O v e r flo w
O v e rfl o w
d e t e c tio n
b.
17
B i n v e rt C a rr yI n O p e r a ti o n
a 0 C a rr yI n
b 0 A L U 0 R e s ult 0
L e s s
C a r r y Out
a 1 C a rry I n
b 1 A L U 1 R e s ult 1
0 L e s s
C a r r y Out
a 2 C a rr yI n
b 2 A L U 2 R e s ult 2
0 L e s s
C a r r y Out
C a rr yI n
a 3 1 C a rr yI n R e s ult 3 1
b 3 1 A L U 3 1 S e t
0 L e s s O v e r fl o w
0 C a rry I n
1 A L U 1
Less = 0
C a r r y Out
Result =
0 C a rr yI n
0 A L U 2
Less = 0
C a r r y Out
C a rr yI n
0 C a rr yI n
0 A L U 3
Less = 0 SET =output of the FA =
Carry Out
0 C a rry I n
1 0 A L U 1 0
Less = 0
C a r r y Out
Result =
0 C a rr yI n
0 1 A L U 2 0
Less = 0
C a r r y Out
C a rr yI n
0 C a rr yI n 0
0 1 A L U 3
Less = 0 SET =output of the FA =
Carry Out
Result =
0 C a rr yI n Result = Less =
0 A L U 2
Less = 0
C a r r y Out
C a rr yI n
0
Result = Less =
C a rr yI n
0 A L U 3
Less = 0 SET =output of the FA =
Carry Out
Result =
0 C a rr yI n Result = Less = 0
0 1 A L U 2
Less = 0
C a r r y Out
C a rr yI n
0
Result = Less = 0
C a rr yI n
0 1 A L U 3
Less = 0 SET =output of the FA =
Carry Out
Example SLT when A>B: Assume a 4-bit computer. slt regC, regA, regB,
Where regA = 0010 and regB = 0001 22
conditional branch:
MIPS must support conditional branch,
depending on whether two registers
contain equal or unequal values.
23
The easiest way to test for equality is to
NOR all the Results of all 1-bit ALUs. (NOR
gate: Inverting the output of the OR gate).
Therefore, if all inputs of the NOR gate are
0, the NOR gate outputs 1.
OR gate NOR gate
a b c=a+b NOR
0 0 0 1 a
0 1 1 0 b
c
1 0 1 0
1 1 1 0
24
Test for equality Diagram: In order to test if two numbers (A and
B) are equal, subtraction is used: If (A - B) = 0 then A = B
B n e g a te O p era ti o n
a2 C a rr yIn R e sult 2
b2 AL U2
0 Le ss
C a rry O ut
R e sult3 1
a 31 C a rr yIn
b 31 AL U31 Set
0 Le ss O v erfl o w
25
If all the Results (from all 1-bit-
ALUs) are 0's then the output of
NOR gate is 1.
If the NOR gate outputs 1, then A –
B = 0 or A = B
Binvert and CarryIn are both 1
when subtracting. They are both 0
when adding. Binvert and CarryIn
are connected together and the
new input is called Bnegate. 26
The following symbol is commonly used to
represent an n-bit ALU. An n-bit ALU
receives, as major inputs, two n-bit
numbers, A and B.
ALU operation
27
Carryout
Multiplication
Multiplication is more
complicated and time consuming
than addition
Multiplication is accomplished
0
via shifting and addition
Multiplication takes more time
and more area (more hardware)
28
Example of multiplication in decimal:
Multiplicand 1 0 0 0 ten
Multiplier x 1 0 0 1 ten
1000 1stpartial product (1x1000)
0000 2ndpartial product (0x1000)
0000 3rdpartial product (0x1000)
1000 4thpartial product (1x1000)
Product 1 0 0 1 0 0 0 ten Final Product
29
Example of multiplication in decimal:
Multiplicand 2 3 9 ten
Multiplier x 1 2 3 ten
717 1st partial product (3x239)
478 2ndpartial product (2x239)
239 3rd partial product (1x239)
Product 29397 ten Final Product
30
The sign of the product
depends on the signs of the
multiplicand and multiplier
If the signs are the same, the
product is positive
If the signs are different, the
product is negative
31
The number of digits in the
product is larger
The length of the product of
an n-bit multiplicand
and an m-bit multiplier
is n+m bits long
32
When two binary numbers are
multiplied, both numbers must
be in true (uncomplemented)
form
33
Steps of the partial product method
of binary multiplication:
37
Solution:
0101 0011 (multiplicand)=
=64+16+2+1= +83
1100 0101 (multiplier) is negative
0011 1011 =32+16+8+2+1=59
(therefore –59)
The answer should be at the end
(+83)x(-59)= -4897
38
Step 1:
40
Step 3 and 4:
The multiplication proceeds by
finding the partial products:
Notice that only the magnitude
bits are used in these steps.
Each successive partial product
is shifted by one bit to the left.
41
bits 6543210
Multiplicand 1010011
Multiplier x0111011
1010011 1st partial product-bit0
+1010011 2nd partial product-bit1
11111001 Sum of the 1st and 2nd
+ 0000000 3rd partial product-bit2
011111001 Sum
+1010011 4th partial product-bit3
1110010001 Sum
+1010011 5th partial product-bit4
100011000001 Sum
+1010011 6th partial product-bit5
1001100100001 Sum
+0000000 7th partial product-bit6
42
1001100100001 Sum
Step 5:
43
Product: 1 0011 0010 0001
2's compliment: 0 1100 1101 1111
44
Confirming that the above product
is equal to (+83) x (-59) = - 4897
45
Example of positive (signed)
number:
214 213 212 211 210 29 28 27 26 25 24 23 22 21 20
+ 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1
0 1 0 0 1 1 0 0 1 0 0 0 0 1
Multiplicand
64 bit Shift left
64 bits
add Multiplier
64-bit ALU 32 bitShift right
32 bits
Product
Control test
64 bit Write
64 bits
48
Figure. First version of multiplication hardware for 32 bit numbers
Shifting occurs in order to align
the multiplicand with the sum
being accumulated in the 64-bit
product register
The control-test-unit has four
outputs, instructing for shift-left,
add, write, shift-right. It has one
input to test the LSB of the
multiplier (if it is 0 or 1). 49
Figure: The first multiplication algorithm, using the
hardware described before
Sta rt
M ultiplier0 = 1 1 . T e st M ultiplier0 = 0
M ultiplier0
Y e s: 3 2 re petitio ns
50
Do ne
The algorithm, above, shows the
three basic steps needed for
each bit
The three basic steps are
repeated 32 times to obtain the
product
51
The LSB of the multiplier
determines whether the
multiplicand is added to the
product register.
Let us call the LSB of the
multiplier Multiplier0
52
Shifting the multiplicand register
to the left (in step 2 of the
algorithm) has the effect of moving
the intermediate operands to the
left, just when multiplying by hand
Shifting the multiplier register to
the right (in step 3 of the
algorithm), provides the next bit to
be multiplied in the following
iteration. 53
Example using the first version
of algorithm and hardware to
multiply:
54
1st version: Solution (2 x 3) or (0010 x 0011):
60
Second version multiplication hardware, being able to
multiply, two, 32-bit numbers
The 32-bit ALU, outputs 32 bits
The result of the ALU, are the left 32 bits, of the product
Multiplicand The 64-bit product register is initialized to zero
32 bits
32-bit multiplicand register
32 bits 32 bits
Multiplier
32 -ALU
32-bit ALU Shift right
32 bits 32-bit multiplier32 bits
register
Shift right
Product Control test
Write
64 bits
32 bits 64-bit product register
Figure. The multiplicand register and the ALU are 32 bits (instead of 64
bits of the first version). The whole product register is shifted to
the right (instead of the multiplicand to the left)
61
Second version multiplication: Minimum hardware required,
for multiplying, two, 4-bit numbers
The 4-bit ALU, outputs 4 bits
The result of the ALU, goes to the left 4 bits, of the 8 bit
Multiplicand product
32
4-bitbits
multiplicand register
4 bits 4 bits
Multiplier
4 bit-ALU
32-bit ALU Shift right
4 bits 4-bit multiplier 32 bits
register
Shift right
Product Control test
Write
64 bits
4 bits 8-bit product register
4 bits 4 bits
0011
Multiplier
4 bit-ALU
32-bit ALU Shift right
4 bits 4-bit multiplier 32 bits
register
4 bits 64 bits
8-bit product register
64
Looking at the first version of the
multiplication algorithm, we
recognize that half of the bits of
the multiplicand were always 0.
(Only half of the bits contained
useful values).
A 64-bit ALU thus seemed
wasteful and slow (since half of
the adder bits were adding 0 to
the intermediate sum. 65
Solution: Instead of shifting
the multiplicand to the left, it is
smarter, faster, and more
economical if we shift the
product to the right.
M ultipli e r 0 = 1 1 . T e st M ultipli e r 0 = 0
M ultipli e r 0
1 a . A d d m ulti plic a n d t o t h e l e ft h a lf o f
th e p ro d u ct a n d pl a c e th e r e sult in
th e le ft h a lf of th e P ro d u ct r e gi st e r
The algorithm is
repeated 32 times. N o : < 3 2 r e p e titio n s
3 2 n d r e p e titi o n ?
The algorithm is
repeated as many Y e s: 3 2 r e p e titio n s
69
Example with the 2nd algorithm : Minimum hardware for 4 bit numbers: multiply
2x3 (0010 x 0011). The LSB of the multiplier is 1 (left half of product) =
(multiplicand) + (left half of product)
Iteration Step Multiplier Multiplicand Product
1 Initial values 0011 0010 0000 0000
2 Prod=Prod+Mlcnd >> >> 0010 0000
Shift right Product >> >> 0001 0000
Shift right Multiplier 0001 >> >>
3
70
Example with the 2nd algorithm : Minimum hardware for 4 bit numbers: multiply
2x3 (0010 x 0011). The LSB of the multiplier is again 1
(left half of product) = (multiplicand) + (left half of product)
Iteration Step Multiplier Multiplicand Product
1 Initial values 0011 0010 0000 0000
2 Prod=Prod+Mlcnd >> >> 0010 0000
Shift right Product >> >> 0001 0000
Shift right Multiplier 0001 >> >>
3 Prod=Prod+Mlcnd >> >> 0011 0000
Shift right Product >> >> 0001 1000
Shift right Multiplier 0000 >> >>
4
71
Example with the 2nd algorithm : Minimum hardware for 4 bit numbers: multiply
2x3 (0010 x 0011). The LSB of the multiplier is 0 No addition occurs and
the Product stays the same
Iteration Step Multiplier Multiplicand Product
1 Initial values 0011 0010 0000 0000
2 Prod=Prod+Mlcnd >> >> 0010 0000
Shift right Product >> >> 0001 0000
Shift right Multiplier 0001 >> >>
3 Prod=Prod+Mlcnd >> >> 0011 0000
Shift right Product >> >> 0001 1000
Shift right Multiplier 0000 >> >>
4 0 no Addition >> >> >>
Shift right Product >> >> 0000 1100
Shift right Multiplier 0000 >> >>
5
72
Example with the 2nd algorithm : Minimum hardware for 4 bit numbers: multiply
2x3 (0010 x 0011). The LSB of the multiplier is 0 No addition occurs and
the Product stays the same
Iteration Step Multiplier Multiplicand Product
1 Initial values 0011 0010 0000 0000
2 Prod=Prod+Mlcnd >> >> 0010 0000
Shift right Product >> >> 0001 0000
Shift right Multiplier 0001 >> >>
3 Prod=Prod+Mlcnd >> >> 0011 0000
Shift right Product >> >> 0001 1000
Shift right Multiplier 0000 >> >>
4 0 no Addition >> >> >>
Shift right Product >> >> 0000 1100
Shift right Multiplier 0000 >> >>
5 0 no Addition >> >> >>
Shift right Product >> >> 0000 0110
73
Shift right Multiplier 0000 >> >>
Final (3rd) version of the multiplication hardware for 32 bit numbers
The left half of the Product register, is initialized to
Multiplicand zero. The right half, of the Product register, is filled
Multiplicand
register: 32 bits
with the 32 bit multiplier number.
32 bits
32 bits The whole Product register is shifted to the right.
32 bits
The multiplier is also shifted to the right, since it is
placed in the Product register. The test, whether the LSB
of the multiplier, is 1 or 0, is done at the LSB of the Product
register.
ALU: 32 bits
32-bit ALU
32 bits
32 bits
4 bits
Product
64 bitsregister: 8 bits,
4 bits (right half: 4 bit
multiplier)
iii y
4 bits
0000
Shift right
Product 0011
Control
add shift
Write test
80
Start
Yes: 32 repetitions
Done
81
Example: using the Final (3rd) version of
algorithm and hardware for multiplication: `
Using 4-bit numbers: 2x3 (0010 x 0011): Initial
values.
Iteration Step Multiplicand Product
1 Initial values 0010 0000 0011
2
82
Example: using the Final (3rd) version of
algorithm and hardware for multiplication: `
Using 4-bit numbers: 2x3 (0010 x 0011): LSB of Product = 1
(new value of left half of product) = multiplicand + (value of left
half of product)
Iteration Step Multiplicand Product
1 Initial values 0010 0000 0011
2 Prod=Prod+Mlcnd >> 0010 0011
Shift right Product >> 0001 0001
3
83
Example: using the Final (3rd) version of
algorithm and hardware for multiplication: `
Using 4-bit numbers: 2x3 (0010 x 0011): LSB of Product = 1
(new value of left half of product) = multiplicand + (value of left
half of product)
Iteration Step Multiplicand Product
1 Initial values 0010 0000 0011
2 Prod=Prod+Mlcnd >> 0010 0011
Shift right Product >> 0001 0001
3 Prod=Prod+Mlcnd >> 0011 0001
Shift right Product >> 0001 1000
4
84
Example: using the Final (3rd) version of
algorithm and hardware for multipliplication: `
Using 4-bit numbers: 2x3 (0010 x 0011): LSB of Product = 0 No
addition left half of product remains the same
85
Example: using the Final (3rd) version of
algorithm and hardware for multipliplication: `
Using 4-bit numbers: A x B = 2 x 3 (0010 x 0011)
divident
quotient
divisor
87
Example (division) in decimal
(using subtraction) 21/7=3, rem 0:
2 1 Divident
- 7 1st subtraction divisor
1 4 1st partial remainder
- 7 2nd subtraction divisor
7 2nd partial remainder
- 7 3rd subtraction of divisor
0 Remainder 88
The sign of the quotient
depends on the sign of the
dividend and the divisor
Step 1:
Initialize the quotient to zero
Determine what the sign of the
quotient will be (from the signs
of dividend and divisor)
90
Step 2:
Subtract the divisor from the
dividend using two's complement
addition, to get the first partial
remainder and add 1 to the quotient.
If the partial remainder is positive go
to step 3
If the partial remainder is zero or
negative, the division is complete
(END) 91
Step 3:
Subtract the divisor from the partial
remainder & add 1 to the quotient
If the result is positive repeat finding
the next partial remainder
If the result is zero or negative, the
division is complete
The quotient is found by counting the
number of times the divisor is
subtracted
92
Example (division) in binary:
Divide the 8 bit signed binary numbers:
0110 0100 (dividend) and 0001 1001 (divisor).
+100/+25
Solution:
0110 0100 (dividend) = 64+32+4 = +100
0001 1001 (divisor) = 16+ 8+1 = + 25
0001 1001 (divisor) 1110 0111
(two's complement of divisor)
93
Step 1:
•Initialize the quotient to zero.
Quotient = 0000 0000
•The sign of the dividend is 0
and the sign of the divisor is 0
•The sign of the quotient will
be 0 (positive)
94
Step 2:
• Subtract the divisor from the dividend using
two's complement addition, to get the first partial
remainder
• (remember that final carries are discarded)
bits 7 6 5 4 3 21 0
dividend 01100100
2’s complement of divisor + 11100111
positive 1st partial remainder 1 0 1 0 0 1 0 1 1
bits 7 6 5 4 3 21 0
2ndpartial remainder 00110010
2’s complement of divisor + 11100111
positive 3rdpartial remainder 1 0 0 0 1 1 0 0 1
bits 7 6 5 4 3 21 0
3rd partial remainder 00011001
2’s complement of divisor + 11100111
zero remainder 100000000
Add 1 to the quotient:
0000 0011 + 0000 0001 = 0000 0100 98
The final quotient is 0000 0100, which is +4
We have reviewed, so far, the basics
of binary division
The following section will present one
algorithm and hardware that
executes division
In this section we will assume
that we divide only positive
numbers
99
• Divisor: takes the left (higher) half of Divisor register (Right half is initialized with zero)
• Dividend: takes the right (lower) half of remainder register (Left half is initialized with zero)
• Initialize the quotient to zero.
•Do subtraction: (Remainder register) = (Remainder register) – (Divisor register)
• If the result of subtraction, in the Remainder register, is negative (a) restore previous
remainder (Rem = Rem + Divisor ). Shift Quotient to the left, with a 0
• If the result of subtraction, in the Remainder register, is positive or 0, keep the new result, in
the Remainder register and Shift Quotient to the left, with a 1
• Shift divisor to the right with a 0. Repeat 33 times (as many as the bits of the hardware, + 1)
64-bits
64-bits
Quotient
64-bit ALU
Shift left
64-bits
100
Figure. 32 bit Division hardware (for two, 32 bit numbers)
• Divisor: takes the left (higher) half of Divisor register. (Right half is initialized with zero)
• Dividend: takes the right (lower) half of remainder register (Left half is initialized with zero)
• Initialize the quotient to zero.
• Do subtraction: (Remainder register) = (Remainder register) – (Divisor register)
• If the result of subtraction, in the Remainder register, is negative (a) restore previous
remainder (Rem = Rem + Divisor ). Shift Quotient to the left, with a 0
• If the result of subtraction, in the Remainder register, is positive or 0, keep the new result, in
the Remainder register and Shift Quotient to the left, with a 1
• Shift divisor to the right with a 0. Repeat 33 times (as many as the bits of the hardware, + 1)
Divisor register 64 bits
Divisor number 32 zeros
32 bits 000000 … right
Shift 000000
64-bits
64-bits
32 zeros
Quotient
64-bit ALU 000000 … 000000
Shift left
Quotient register
64-bits
Remainder register 64 bits 32-bits
Remainder
32 zeros register 64 bits
Dividend number Control
Write
000000 … 000000 Dividend
32 bits Test
64-bits
101
Figure: 32 bit Division hardware (for two, 32 bit numbers), Initial Conditions
• Divisor: takes the left (higher) half of Divisor register. (Right half is initialized with zero)
• Dividend: takes the right (lower) half of remainder register (Left half is initialized with zero)
• Initialize the quotient to zero.
• Do subtraction: (Remainder register) = (Remainder register) – (Divisor register)
• If the result of subtraction, in the Remainder register, is negative (a) restore previous
remainder (Rem = Rem + Divisor ). Shift Quotient to the left, with a 0
• If the result of subtraction, in the Remainder register, is positive or 0, keep the new result, in
the Remainder register and Shift Quotient to the left, with a 1
• Shift divisor to the right with a 0. Repeat 5 times (as many as the bits of the hardware, + 1)
Divisor register 8bits
Divisor number 4 zeros
4 bits 0000right
Shift
8-bits
8-bits
4 zeros
Quotient
8-bit ALU 0000
Shift left
8-bits
102
Figure: Minimum Division hardware required for two, 4 bit numbers and Initial Conditions
• Divisor: takes the left (higher) half of Divisor register. (Right half is initialized with zero)
• Dividend: takes the right (lower) half of remainder register (Left half is initialized with zero)
• Initialize the quotient to zero.
• Do subtraction: (Remainder register) = (Remainder register) – (Divisor register)
• If the result of subtraction, in the Remainder register, is negative (a) restore previous
remainder (Rem = Rem + Divisor ). Shift Quotient to the left, with a 0
• If the result of subtraction, in the Remainder register, is positive or 0, keep the new result, in
the Remainder register and Shift Quotient to the left, with a 1
• Shift divisor to the right with a 0. Repeat 5 times (as many as the bits of the hardware, + 1)
Divisor register 8bits
Divisor number 4 zeros
0010 0000
Shift right
8-bits
8-bits
4 zeros
Quotient
8-bit ALU 0000
Shift left
4 zeros register
Remainder 64 bits
Dividend number Control
0000 0111 Write
Dividend Test
8-bits
103
Figure: Example: Dividend / Divisor, 7/2, 0111 / 0010, Minimum Division hardware required for two, 4
bit numbers and Initial Conditions
Start
Yes: 33 repetiitons
Done
104
Example: using the first version of
algorithm and hardware for DIVISION:
Assume 4-bit numbers. +7 and +2
Divide the signed binary numbers:
7 = 0111 (is the dividend)
2 = 0010 (is the divisor)
7/2 = 3 (is the quotient) and 1 is the remainder
divident 7
quotient 3 remainder1
divisor 2
105
Solution:
0111 (dividend) = +7 Dividend takes the
lower half of the 8-bit remainder register
Remainder register = Dividend=0000 0111
go to step 1)
n = (number of quotient
bits)+1
108
Assume 4-bit numbers and minimum hardware required (therefore do 5
subtractions, 4+1 =5): dividend / divisor, Example: 7/2 = 3 remainder 1, 0111 /
0010 = 0011 remainder 0001,
Solution : Initial values (initial conditions)
0000111 0000
0001000 1 11
2
11110111
9 00000111 0000
00001000 s
109
Iteration Step Divisor Remainder Quotient
(Dividend)
3 9
11111111
9 000
0000111
soon olee as a
4
a 0000 All a
0000000 a 0001
Poll 110