Professional Documents
Culture Documents
Computer Architecture
Lecture 8 & 9
Fall 2012
l
Topics:
l
l
l
Integer Arithmetic
l
l
l
l
l
0 to 65 535
(2 bytes)
(4 bytes)
l
l
l
l
l
l
l
6 0110
- 5 0101
---------------
1
0001
8
1000
+ 10 1010
---------------
18 1 0010
2
0010
of
ux;
uy;
ans;
Guess
In C, y is represented as 2s complement
Overflow
l
l
l
An overflow occurs when the result cannot fit within the word-size
limits of the result data type
When executing C programs, overflows are not signaled as errors!
It is onus on the programmer to determine if an overflow has
occurred or make sure that overflow does not occur by limiting the
ranges of the operands
When two unsigned numbers s, x are added
l
2s complement addition
l
l
l
l
l
l
-7
1001
+5
0101
---------------
-2
1110
-5 1011
+ -2 1110
---------------
-7 1 1001 ignore carry out
4
0100
- (-2)
0010
---------------
6
0110
Overflow
l
When two numbers of w bits are added, the result could need w+1
bits.
Since, MSB of 1 represents negative numbers, a carry into the
MSB creates problems
Overflow
l
Normal case
3
0011
+2
0010
---------------
+5
0101
Carry out 0
Carry in 0
l
l
l
5
0101
-3
1101
---------------
+2
1 0010
Carry out 1
Carry in 1
-4
1100
5
0101
---------------
1
1 0001
Carry out 1
Carry in 1
-2
1110
-3
1101
---------------
-5
1 1011
Carry out 1
Carry in 1
Detecting overflow
l
Detecting overflow
Opera&on
First operand
Second operand
X+Y
Overow condi&on
<0
X+Y
<0
<0
X-Y
<0
<0
<0
X-Y
Actions on overflow
Detect in hardware
l Raise flags or special bits to indicate overflow
l
Unsigned multiplication
When two numbers, each w bits long are multiplied, the result
can be 2*w bits long
If the result also needs to be w bits long, then w bits need to
be discarded
The w Most Significant Bits (MSBs) are discarded
Unsigned Multiplication
up = ux * uy
Truncates product to w-bit number up = UMultw(ux, uy)
Modular arithmetic: up = (ux uy) mod 2w
Unsigned Multiplication in C
Operands: w bits"
True Product: 2*w bits" u v
Discard w bits: w bits"
UMultw(u , v)
= u v mod 2w
Decimal Multiplication
Multiplicand
Multiplier
Partial products
Final sum
Each digit in the multiplier has
different place values
The partial product is shifted
left for each consecutive digit
in the multiplier
123 x 101
123
000
123
-----------------------
12423
1
x
Algorithm
Step1: Check LSB of multiplier
l Decision Step 2: If 1, then add multiplicand to
result
l
If 0, add 0 to result
l Step 3: Shift multiplicand left by 1 bit
l
Multiply by 2
Binary multiplication
M=#bits in multiplier
RESULT=0
LSBPTR=LSB[MULTIPLIER]
Y
IS LBSPTR=0?
STOP
Y
IS M=0?
Binary multiplier
16 bits (note: multiplicand is 8 bits but needs 16 bits for shifts)
Multiplicand
Shift left
8 bits
ADDER
Result
16 bits
Control
Multiplier
Shift right
Signed Multiplication
l
Hence
l
l
l
ANS=X*Y
ANS is +ve if X and Y have the same sign else ANS is ve
Convert multiplier, multiplicand to positive numbers
Multiply unsigned numbers as before
Compute sign, convert product accordingly
Sign extension
l
l
Signed integer
4-bit representa&on
8-bit representa&on
16-bit representa&on
+1
0001
00000001
0000000000000001
-1
1111
11111111
1111111111111111
2s complement multiplication
l
l
l
l
l
l
l
l
l
l
l
111101 x
111101
1
1
1
1
0
1
0
0
0
0
0
0
1
1
1
1
0
1
1
1
1
1
0
1
1
1
1
1
0
1
1
1
1
1
0
1
. . . . . 0 0 0 1 0 0 1
Another 2s Complement
multiplier
Perform sign extension on shifts
l Use a combined register for multiplier and
result
l
l 101 x 010
l
l
l
l
l
000
000
101
110
111
010
001
001
100
010
-3 x 2
LSB is 0, add 0, and shift right 1 bit
LSB is 1, add multiplicand and
shift right 1 bit (with sign extension)
LSB is 0, add 0, and shift right 1 bit
STOP
ADDER
Result
Multiplier
16 bits
Shift right
Control
Fast Multipliers
Number of tricks used to improve
multiplication
l Booth multiplication algorithm
l
Integer division
21
12
262
240
22
12
10
l
l
l
l
l
l
l
l
l
l
27
Integer division
Repeated subtraction
l Shift and subtract
l
28
Repeated subtraction
Q=0; R=Dividend, D=Divisor!
l
While (D<=R)!
l
{Q++; R=R-D;}!
l
printf(Q(in binary),R)!
l 0111 divided by 11!
l Q=0; R=0111, D=11!
l I1:
Q=1; R=0111-11=0100!
l I2;
Q=2; R=0100-11=0001!
l Exit!
l Q=0010, R=0001!
l
29
1
1
1
0
0
0
1
1
0
0
1
0
1
0
1
0
0
0
1
1
0
0
0
1
0
30
Integer Division
l
31
N
Reminder<0
Restore Remainder
By adding Divisor
R=R+D
Shift left 1 bit Quotient
Set LSB(quotient)=0
Shift left q bit quotient
Set LSB(quotient)=1
Shift right 1 bit divisor
Y
STOP
N
#of itertation>remainder size in bits
32
Step
Divisor
Remainder
Initial values
0010 0000
0000 0111
0010 0000
1110 1110
0001 0000
0000 0111
0001 0000
1111 0111
Q=0000
0000 0111
0000 1000
00001000
1111 1111
00001000
0000 0111
00000100
0000 0111
33
Step
Divisor
Remainder
0000 0100
0000 0011
0000 0100
0000 0011
00000010
0000 0011
0000 0010
0000 0001
0000 0010
0000 0001
00000001
Q=0011, R=0001
34
Quotient
Shift left
64-bit ALU
32 bits
Remainder
Write
Control
test
64 bits
35