Professional Documents
Culture Documents
CS 45101
CS 55101
CHAPTER 4
Arithmetic for Computers
H. Peyravi
Department of Computer Science
Kent State University
Fall 2001
Computer Architecture
Fall 2001
Contents
Introduction
Arithmetic Operations
Constructing an ALU
A 32-bit ALU
Addition
Multiplication
Computer Architecture
Fall 2001
Introduction
Introduction
Goals
To introduce 2s complement numbers and their addition and
subtraction
To introduce basic logic operations (AND, OR, logical shift), and
hardware building blocks (AND, OR, NOT gates and multiplexor)
To explain the construction and operation of a 32-bit ALU that
performs AND, OR, add, and slt
To show algorithms that perform multiplication and division and
hardware that implements these algorithms
To demonstrate floating point representation and arithmetic
operations
To introduce MIPS instructions for unsigned operands, for logic
operations, and for floating point numbers
Numbers
In binary system how do we represent negative numbers?
What is the largest or smallest number that can fit into a word?
What would happen to overflow and underflow case?
How fractions and real numbers are represented?
Computer Architecture
Fall 2001
1 of 61
Introduction
Possible Representation
Sign
000
001
010
011
Magnitude
=
+0
=
+1
=
+2
=
+3
Ones Complement
000 =
+0
001 =
+1
010 =
+2
011 =
+3
Twos Complement
000 =
+0
001 =
+1
010 =
+2
011 =
+3
100
101
110
111
=
=
=
=
100
101
110
111
100
101
110
111
-0
-1
-2
-3
=
=
=
=
-3
-2
-1
-0
=
=
=
=
-4
-3
-2
-1
Issues:
Balanced ?
Number of zeros ?
Ease of Operations ?
Which one is best ?
Computer Architecture
Fall 2001
2 of 61
Computer Architecture
Fall 2001
3 of 61
0000
0000
0000
0111
0111
0111
1000
1000
1000
1111
1111
1111
1111
1111
1111
0000
0000
0000
1111
1111
1111
0000
0000
0000
1111
1111
1111
0000
0000
0000
1111
1111
1111
0000
0000
0000
1111
1111
1111
0000
0000
0000
1101
1110
1111
0000
0001
0010
010
110
210
=
2, 147, 483, 64510
=
2, 147, 483, 64610
=
2, 147, 483, 64710 Max int
= 2, 147, 483, 64810 Min int
= 2, 147, 483, 64710
= 2, 147, 483, 64610
Fall 2001
4 of 61
Computer Architecture
Fall 2001
5 of 61
Arithmetic Operations
Arithmetic Operations
Addition
Except the right most digits, we have a carry (0 or 1) for the
subsequent digits and hence 3 operands
(1)
(0)
(0)
(0)
(0)
(1)
(1)
Carries
(1) 0
(1) 0
(0)1
(0)1
(0)1
(0)1
(1)0
(1) 0
1100 = -4
0100 = +4
0000 =
0
0011 = +3
0100 = +4
0111 = +7
0101 = +5
0100 = +4
1001 = OF
1001 = -7
1010 = -6
0011 = OF
Computer Architecture
Fall 2001
6 of 61
Arithmetic Operations
Subtraction
To subtract one number(subtrahend) from another (minuend),
take 2s complement of the subtrahend and add it to the minuend
2
0010
1001
1011
7
= +2
= 7
= 5
5
0101
0010
0111
7
0111
0111
1110
(
=
=
=
52
1011 = 5
1110 = 2
1001 = 7
7)
+7
+7
OF
(
=
=
=
2)
+5
+2
+7
5
0101
1110
0011
2
= +5
= 2
= +3
64
1010 = 6
1100 = 4
0110 = OF
Computer Architecture
Fall 2001
7 of 61
Arithmetic Operations
Effects of Overflow
An exception (interrupt) occurs
Control jumps to predefined address for exception
Address of the offending instruction is saved for possible
resumption
MIPS saves the address of the instruction that overflowed in a
register called Exception Program Counter, EPC, and an interrupt
handler routine is called
Details based on software system/language
flight control vs. homework assignment
MIPS uses register $k0 and $k1 to save the return address
from EPC
Unsigned integers could cause overflow, but they are not
considered overflow since they are commonly used for memory
address
Note that overflow term is somehow misleading, it doesnt mean a
a carry overflowed
In MIPS
add, addi and sub cause exceptions for overflow
addu, addiu and subu do not cause exceptions for overflow
Computer Architecture
Fall 2001
8 of 61
Arithmetic Operations
Supporting slt
MIPS offers two sets of set-on-less-than
For signed intergers
Set-on-less-than, slt
Set-on-less-than-immediate slti
For unsigned intergers
Set-on-less-than-unsigned, slu
Set-on-less-than-immediate-unsigned sltiu
Example: Suppose
$s0 = 1111 1111 1111 1111 1111 1111 1111 1111 =
1signed
2
unsigned
= 4, 294, 967, 29510
$s1 = 0000 0000 0000 0000 0000 0000 0000 0001 =
1signed
2
=
1unsigned
10
What would the following do
slt
sltu
# singend comparison
# unsigned comparison
Computer Architecture
Fall 2001
9 of 61
Computer Architecture
Fall 2001
10 of 61
Constructing an ALU
Constructing an ALU
ALUs are constructed from four hardware building blocks, AND,
OR, Inverter, and Multiplexor(MUX)
1. AND gate (c = a b)
a
2. OR gate (c = a + b)
a
3. Inverter (c =a)
c
4. Multiplexor
(if d = = 0, c = a;
else c = b)
d
a
c=ab
0
0
1
1
0
1
0
1
0
0
0
1
c=a+b
0
0
1
1
0
1
0
1
0
1
1
1
c=a
0
1
1
0
0
1
a
b
c
b
Computer Architecture
Fall 2001
11 of 61
Constructing an ALU
A 1-bit ALU
A 1-bit logical unit can be constructed by an AND, and OR and a
MUX
Operation
a
0
Result
A 1-bit half adder consists of two input operands, the sum, and
one output carryOut
Inputs
Outputs
SOP
a b CarryOut Sum CarryOut Sum
0 0
0
0
a+b
a+b
0 1
0
1
a + b
ab
1 0
0
1
a + b
ab
1 1
1
0
ab
a + b
SumSOP = ab + ab
CSOP = ab
Computer Architecture
Fall 2001
12 of 61
Constructing an ALU
CarryIn
a
Sum
b
CarryOut
a
0
0
0
0
1
1
1
1
Inputs
Outputs
b CarryIn CarryOut Sum
0
0
0
0
0
1
0
1
1
0
0
1
1
1
1
0
0
0
0
1
0
1
1
0
1
0
1
0
1
1
1
1
Outputs
Co Sum
Comments
0+0+0=00
0+0+1=01
abci
0+1+0=01
ab
ci
0+1+1=10 abci
1+0+0=01
abci
1+0+1=10 abci
1+1+0=10 ab
ci
1+1+1=11 abc abc
be expressed as SOP
abci + abci + abci + abc
abci + abci + abci + abci
abci + abci + abci + abci + abci + abci
ab + aci + bci
Fall 2001
13 of 61
Constructing an ALU
b
CarryOut
0
1
Result
CarryOut
Computer Architecture
Fall 2001
14 of 61
Constructing an ALU
Subtraction
Subtractions can be performed using 2s complement and a 2:1
Mux
To compute 2s complement the carrayIn of the LSB is set to 1
A 1-bit ALU that performs and, or, addition and subtraction
Binvert
Operation
CarryIn
0
1
Result
CarryOut
Computer Architecture
Fall 2001
15 of 61
Constructing an ALU
A 32-bit Adder
A 32-bit adder can be constructed by replication of 32 full adders,
(ripple carry adder)
CarryIn
a0
b0
a1
b1
a2
b2
a31
b31
Computer Architecture
Operation
CarryIn
Result0
ALU0
CarryOut
CarryIn
Result1
ALU1
CarryOut
CarryIn
Result2
ALU2
CarryOut
CarryIn
Result31
ALU31
Fall 2001
16 of 61
A 32-bit ALU
A 32-bit ALU
Tailoring the ALU to the MIPS
Need to support the set-on-less-than instruction (slt)
Remember: slt is an arithmetic instruction
It produces a 1 if rs < rt = (rs rt < 0) and 0 otherwise
Use subtraction: (a b) < 0 = a < b
Computer Architecture
Fall 2001
17 of 61
Computer Architecture
A 32-bit ALU
Fall 2001
18 of 61
A 32-bit ALU
Binvert
CarryIn
a0
b0
CarryIn
ALU0
Less
CarryOut
a1
b1
0
CarryIn
ALU1
Less
CarryOut
a2
b2
0
CarryIn
ALU2
Less
CarryOut
Operation
Result0
Result1
Result2
CarryIn
a31
b31
0
Computer Architecture
CarryIn
ALU31
Less
Fall 2001
Set
Result31
Overflow
19 of 61
A 32-bit ALU
Bnegate
Operation
a0
b0
CarryIn
ALU0
Less
CarryOut
Result0
a1
b1
0
CarryIn
ALU1
Less
CarryOut
Result1
a2
b2
0
CarryIn
ALU2
Less
CarryOut
Result2
a31
b31
0
CarryIn
ALU31
Less
Zero
Result31
Set
Overflow
ALU operation
a
ALU
Zero
Result
Overflow
b
CarryOut
Computer Architecture
Fall 2001
20 of 61
A 32-bit ALU
=
=
=
=
=
and
or
add
subtract
slt
A Few Conclusions
We can build an ALU that supports basic MIPS instruction
Key ideas
Use multiplexor to select the output we want
Use twos complement to perform subtraction efficiently
Replicate a 1-bit ALU to produce a 32-bit ALU
Important points about hardware
All of the gates are always working
The speed of a gate is affected by the number of inputs to the
gate
The speed of a circuit is affected by the number of gates in
series (critical path)
Our focus: comprehension, however,
Clever changes to organization can improve performance
Similar to using better algorithms in software
We will look at two examples for addition and multiplications
in the next section
Computer Architecture
Fall 2001
21 of 61
Addition
Addition
Problem
Ripple carry adder is too slow
Is a 32-bit ALU as fast as a 1-bit ALU?
Is there more than one way to do addition?
Two extremes:
Ripple carry
Not feasible! why?
SOP using infinite hardware
Not feasible! why?
Carry Lookahead
Motivation:
If we didnt know the value of CarryIn, what could we do?
When would we always generate a carry? gi = ai bi
When would we propagate the carry? pi = ai + bi
c1
c2
c3
c4
Computer Architecture
=
=
=
=
g 0 + p0 c 0
g 1 + p1 c 1
g 2 + p2 c 2
g 3 + p3 c 3
Fall 2001
22 of 61
Addition
Computer Architecture
=
=
=
=
g 0 + p0 c 0
g 1 + p 1 g 0 + p 1 p0 c 0
g 2 + p 2 g 1 + p 2 p1 g 0 + p 2 p1 p0 c 0
g 3 + p 3 g 2 + p 3 p2 g 1 + p 3 p2 p1 g 0 + p 3 p2 p1 p0 c 0
Fall 2001
23 of 61
Addition
a0
b0
a1
b1
a2
b2
a3
b3
CarryIn
Result0--3
ALU0
P0
G0
pi
gi
C1
a4
b4
a5
b5
a6
b6
a7
b7
a8
b8
a9
b9
a10
b10
a11
b11
a12
b12
a13
b13
a14
b14
a15
b15
Carry-lookahead unit
ci + 1
CarryIn
Result4--7
ALU1
P1
G1
pi + 1
gi + 1
C2
ci + 2
CarryIn
Result8--11
ALU2
P2
G2
pi + 2
gi + 2
C3
ci + 3
CarryIn
ALU3
P3
G3
Result12--15
C4
pi + 3
gi + 3
ci + 4
CarryOut
Computer Architecture
Fall 2001
24 of 61
Multiplication
Multiplication
Multiplication is performed as the sum of partial products
The partial product is 0 when the multiplier bit is 0, and the
partial product is the multiplicand when multiplier bit is 1
The total product is the sum of left shifted of partial products
The multiplication of two n-bit binary integer results in a product
of up to 2n bit is length
1011
x 1101
------1011
0000
1011
1011
-----------10001111
Computer Architecture
Fall 2001
25 of 61
Multiplication
Multiplicand
Shift left
64 bits
Multiplier
Shift right
64-bit ALU
32 bits
Product
Write
Control test
64 bits
Computer Architecture
Fall 2001
26 of 61
Multiplication
Start
Multiplier0 = 1
1. Test
Multiplier0
Multiplier0 = 0
32nd repetition?
Yes: 32 repetitions
Done
Second Version
In average half of the bits of the multiplicand are 0, therefore
64-bit ALU seemed wasteful
Instead of shifting the multiplicand left, we shift the product to
the right, after adding multiplicand to the left half of the product
The algorithm uses 32-bit multiplicand, 32-bit multiplier, 32-bit
adder, and 64-bit product
Computer Architecture
Fall 2001
27 of 61
Multiplication
Multiplicand
32 bits
Multiplier
Shift right
32-bit ALU
32 bits
Product
Shift right
Write
Control test
64 bits
Start
Multiplier0 = 1
1. Test
Multiplier0
Multiplier0 = 0
32nd repetition?
Yes: 32 repetitions
Done
Computer Architecture
Fall 2001
28 of 61
Multiplication
Third Version
The right half of the product can be used to store the multiplier
the procedure, and the example, respectively
Multiplicand
32 bits
32-bit ALU
Product
Shift right
Write
Control
test
64 bits
Computer Architecture
Fall 2001
29 of 61
Multiplication
Start
Product0 = 1
1. Test
Product0
Product0 = 0
32nd repetition?
Yes: 32 repetitions
Done
The above algorithms will not work if both numbers are not
positive
1001 9
x 0011 3
00001001
00010010
00011011 27
Unsigned
Computer Architecture
1001 -7
x 0011
3
11111001
11110010
11101011 -21
2s complement
Fall 2001
30 of 61
Multiplication
Signed Multiplication
Consider the following of two unsigned integers yielding an 8-bit
result
1011
x 1101
00001011
00000000
00101100
01011000
10001111
1011 1 20
1011 0 21
1011 1 22
1011 1 23
143
Each partial product (other than that for 20) consists of the 4-bit
multiplicand (1011) shifted to the left with the unoccupied
positions on the right filled with 0s
However, this straightforward multiplication will not work if the
multiplicand is negative
Each contribution of the negative multiplicand as a partial
product must be a negative number on a 2n-bit (64-bit) field
The sign bits of the partial products must line up
Unsigned Integers
1001
9
x 1101
3
00001001
1001 1 20
00010010
1001 1 21
00100100
1001 0 22
01001000
1001 0 23
00011011
27
Computer Architecture
2s Complement Integers
1001
7
x 0011
3
11111001
7 = 1001 1 20
11110010
7 = 1001 1 21
00000000
7 = 1001 0 22
00000000
7 = 1001 0 23
21
Fall 2001
= 7
= 14
=0
=0
31 of 61
Multiplication
Booths Algorithm
First Consider a positive multiplier consisting of one block of 1s
surrounded by 0s
M (00011110) = M (24 + 23 + 22 + 21)
= M (16 + 8 + 4 + 2)
= M 30
(1)
Computer Architecture
Fall 2001
32 of 61
Multiplication
6 = 11111010 = 27 + 26 + 25 + 24 + 23 + 21
Computer Architecture
Fall 2001
33 of 61
Multiplication
The Algorithm:
Trace 2 bits of the multiplier at a time, and if it is
00 Middle of a string of 0, no arithmetic operation.
01 End of a string of 1, add the multiplicand to the left half of
the product.
10 Beginning of a string of 1, subtract the multiplicand from the
left half of the product.
11 Middle of a string of 1, no arithmetic operation.
Computer Architecture
Fall 2001
34 of 61
Multiplication
Computer Architecture
Fall 2001
35 of 61
Multiplication
Computer Architecture
Fall 2001
36 of 61
Multiplication
Division
It is somewhat more complex than multiplication
Procedure:
1. The bits of the dividend are examined from left to right,
until the set of bits examined represents a number divisor.
until this event occur, 0s are placed in the quotient from left
to right
2. When the event occurs, a 1 is placed in the quotient and a
divisor is subtracted from the partial dividend
3. Append additional bits from the dividend (if any) to the new
partial remainder. If the result divisor, go to 1
An example of long division of unsigned binary integers
Divisor ---> 1 0 1 1
0 0 0 0 1 1 0 1 <--- Quotient
_______________
)1 0 0 1 0 0 1 1 <--- Dividend
1 0 1 1
| |
___________ | |
0 0 1 1 1 0 | |
1 0 1 1 | |
___________ v v
0 0 1 1 1 1
1 0 1 1
___________
1 0 0 <--- Reminder
Fall 2001
37 of 61
Multiplication
The size of the dividend is limited by the sum of the sizes of the
divisor and quotient
First Version
A 32-bit Quotient register, initially set to 0
The divisor is placed in the left half of the 64-bit Divisor register
At each step, the Divisor register is shifted to the right (1 bit) to
be aligned with the Dividend.
Divisor
Shift right
64 bits
Quotient
Shift left
64-bit ALU
32 bits
Remainder
Write
Control
test
64 bits
Computer Architecture
Fall 2001
38 of 61
Multiplication
Start
Remainder> 0
Test Remainder
Remainder < 0
33rd repetition?
Yes: 33 repetitions
Done
Computer Architecture
Fall 2001
39 of 61
Multiplication
Second Version
By switching the order of the operation to shift and then subtract,
One iteration of the algorithm can be removed
The ALU and Divisor register can be cut in half
Shifting the remainder to the left instead of shifting the divisor to
the right produces the same alignment
Divisor
32 bits
Quotient
Shift left
32-bit ALU
32 bits
Remainder
Shift left
Write
Control
test
64 bits
Computer Architecture
Fall 2001
40 of 61
Multiplication
Third Version
The Quotient register could eliminated by shifting the bits of the
quotient into the Reminder instead of shifting in 0s
Both the remainder and the quotient will be shifted when a shift is
made on the the remainder register,
This causes the remainder to be shifted an extra position to the
left and correction is required,
Divisor
32 bits
32-bit ALU
Remainder
Shift right
Shift left
Write
Control
test
64 bits
Computer Architecture
Fall 2001
41 of 61
Multiplication
Start
Remainder>
0
Test Remainder
Remainder < 0
32nd repetition?
Yes: 32 repetitions
Done. Shift left half of Remainder right 1 bit
Computer Architecture
Fall 2001
42 of 61
Multiplication
Signed Division
For sign numbers, the sign of the divisor and dividend should be
saved
Sign of Quotient = Sign of Dividend Sign of Divisor
For a nonzero remainder,
Sign of Remainder = Sign of Dividend
The same hardware can be used for both multiply and divide using
a 64-bit shift register for product (in multiply) or remainder (in
divide)
MIPS has two multiply instructions: multiply(mult) and
multiply unsigned(multu)
MIPS has two divide instructions: divide(dvi) and
divide unsigned(dviu)
To fetch the 32-bit product (remainder), MIPS has
move f rom lo(mflo) instruction
To avoid overflow, Hi must be 0 for multu or must be the
replicated sign of Lo for mult
The instruction move f rom hi transfers Hi to a register to test
for overflow
MIPS divide instructions ignore overflow
Both overflow and dived-by-0 are handled by MIPS software
Computer Architecture
Fall 2001
43 of 61
Biased Exponent
8-bits
Significand(or mantissa)
23-bits
Computer Architecture
Fall 2001
44 of 61
0.1bbb b 2E
210100
210100
210100
210100
=
=
=
=
0
1
0
1
10010100 10100010000000000000000
10010100 10100010000000000000000
01101100 10100010000000000000000
01101100 10100010000000000000000
Fall 2001
45 of 61
Computer Architecture
Fall 2001
46 of 61
Biased Exponent
8-bits
Significand(or mantissa)
23-bits
Biased Exponent
11-bits
Significand(or mantissa)
52-bits
Computer Architecture
Fall 2001
47 of 61
0111 1111
0
127
1000 0000
1
128
1111 1101
+ 126
253
1111 1101
+ 127
254
1111 1111
+ 128
255
Computer Architecture
Fall 2001
48 of 61
Ye
X + Y = (XS b
+ Ys ) b
X Y = (XS bXeYe + Ys) bYe
if Xe Ye
Computer Architecture
Fall 2001
49 of 61
Computer Architecture
Fall 2001
50 of 61
Guard Bits
Loading mantissa or exponent into ALU registers results in some
extra bits called guard bits
They affect the precision of the result
Consider X = 1.000 000 21 and Y = 1.111 111 20
What is Z = X Y ?
X Y
X =
Y =
Z =
=
X =
Y =
Z =
=
Computer Architecture
Fall 2001
51 of 61
Rounding
The result of an ALU operation on significands is put back into a
floating point format
Extra bits must be disposed
Some techniques:
Round
Round
Round
Round
Computer Architecture
to
to
to
to
Fall 2001
52 of 61
Round to Nearest
If the extra bits amount more than one-half of the last
representable bit, add 1 to lo the LSB of the significand, otherwise
drop them
Extra bits 10010 add 1 to LSB of the significand
Extra bits 01111 drop the extra bit
If the extra bits amount exactly halfway between two representable
values randomly round up or down to avoid cumulative bias
In IEEE, if the result of a computation is exactly midway between
two representable numbers:
Round up if the last representable bit is 1
Truncate if the last representable bit is 0
Computer Architecture
Fall 2001
53 of 61
Examples
157.3 + 257.1, assume 6-bit bias (excess 32) and 10-bit mantissa
Convert to binary and normalize
157.3 = 10011101.01001 = 0.1001110101001 28
257.1 = 100000001.0001 = 0.1000000010001 29
Round the mantissa to ten bits
157.3 = 0.1001110101001 28 = 0.100111011 28
257.1 = 0.1000000010001 29 = 0.100000001 29
Make the smaller exponent equal to the larger exponent and add
157.3 = 0.010011101 29
257.1 = 0.100000001 29
414.0 = 0.110011110 29
The result is already normalized. The result differs from the exact
answer (414.4) by 0.4
Computer Architecture
Fall 2001
54 of 61
Floating-Point Arithmetic
For addition and subtraction, it is necessary to ensure that both
operands have the same exponent. This may require shifting the
radix point on one of the operands to achieve alignment
Problems may arise as the result of these operations
Exponent overflow: A positive exponent exceeds the limit In
some system this may be designated as + or
Computer Architecture
Fall 2001
55 of 61
Example:0.5 0.4375
1. 0.5 = 1.0001
2
0.4375 = 1.1102
2
1
1
2. 1.0002 0.1112
1
3. 1.00021 0.1111
2 = 0.0012 2
4. 0.0012 21 = 1.0002 24
Computer Architecture
Fall 2001
56 of 61
Start
Overflow or
underflow?
Yes
No
Exception
No
Still normalized?
Yes
Done
Computer Architecture
Fall 2001
57 of 61
Sign
Exponent
Significand
Sign
Exponent
Significand
Compare
exponents
Small ALU
Exponent
difference
0
Control
Shift smaller
number right
Shift right
Big ALU
Increment or
decrement
Computer Architecture
Exponent
Add
Rounding hardware
Sign
Normalize
Round
Significand
Fall 2001
58 of 61
Computer Architecture
Fall 2001
59 of 61
Computer Architecture
Fall 2001
60 of 61
Example
0.5 0.4375 = (1.0002 21) (1.110 22 )
1. New exponent= -1 + (-2) = -3. or using the biased
representation: (-1+127)+(-2+127)-127= 124
2. Multiplying the significand
1.0002 1.110 = 11100002 = 1.1100002 23 = 1.1102 23
3. The product is already normalized
4. No rounding is required,
5. The sign of the result is negative
Computer Architecture
Fall 2001
61 of 61