Sie sind auf Seite 1von 30

Lecture 5

Computer Arithmetic
Part II: Integer Arithmetic & Floating Point

Zelalem Birhanu, AAiT 1


Last lecture:

Integer Representation
Unsigned integer
Signed integer -> 2s complement

Integer Arithmetic
Addition/subtraction
Unsigned integer multiplication

Zelalem Birhanu, AAiT 2


In this lecture:

Integer Arithmetic (Multiplication & Division)


Floating Point Representation

Zelalem Birhanu, AAiT 3


Signed Integer Multiplication

e.g. 1001 0011


Unsigned:1001 9 0011 3 = 11011 27
Signed:1001 7 0011 3 = 11011(5) Error

Straightforward multiplication wouldnt work if either the


multiplicand or the multiplier is negative

Possible solution
Convert both multiplier and multiplicand to positive numbers, perform
unsigned integer multiplication, and then take the 2s complement of
the result if signs of the two original numbers differed

Zelalem Birhanu, AAiT 4


Booths Algorithm

e.g. 1001(710) 0011(310)


= 11111001 21 + 20
= 11111001 22 20
= 11111001 22 11111001 20
Shift 2 bits to the left Shift 0 bits to the left

= 11100100 11111001
= 11101011(2110)

Zelalem Birhanu, AAiT 5


Booths Algorithm

2 + 21 + 22 + + 2 = 2+1 2

Sequence of 1s in the multiplier can be replaced by one


addition and one subtraction
e.g. 0110 = 610 = 23 21
1011 = 510 = 23 + 22 20

Booths algorithm exploits this characteristics to make


faster multiplier with add, subtract and shift operations

Check current LSB and previous LSB of multiplier to determine whether to


add, subtract or just shift 6
Booths Algorithmcntd
Q0 LSB-1 Operation
1001(710) 0011(310) 0 0 Shift left
Assume initially previous LSB (LSB-1) is 0 0 1 Add, Shift left
1 0 Subtract, Shift
Product(P) Multiplicand(M) Multiplier(Q) LSB-1 Initial left
00000000 11111001 0011 0 Values 1 1 Shift left

00000111 11111001 0011 0 PP-M


1
00000111 11110010 0011 0 Shift M left
00000111 11110010 0001 1 Shift (Q, LSB-1) right

00000111 11100100 0001 1 Shift M left


2
00000111 11100100 0000 1 Shift (Q, LSB-1) right

11101011 11100100 0000 1 PP+M


3 11101011 11001000 0000 1 Shift M left
11101011 11001000 0000 0 Shift (Q, LSB-1)right 7
Booths Algorithmcntd

A,M,Q: n-bits
Q-1: 1-bit, used to hold
previous LSB of multiplier
Product: A,Q

Arithmetic Shift
Shift operation that
preserves the sign bit
e.g. X = 11001101
Arithmetic shift right:
1 1 0 0 1 1 0 1

1 1 1 0 0 1 1 0

8
Booths Algorithmcntd

E.g. Use Booths algorithm to calculate the


following:
i) 1001 1100
A Q Q-1(LSB-1) M Initial
0000 1100 0 1001 Values

1 0000 0110 0 1001 Arithmetic Shift Right

2 0000 0011 0 1001 Arithmetic Shift Right

0111 0011 0 1001 AA-M


3
0011 1001 1 1001 Arithmetic Shift Right

4 0001 1100 1 1001 Arithmetic Shift Right


Zelalem Birhanu, AAiT 9
Booths Algorithmcntd

Q. Use Booths algorithm to calculate the


following:
i) 0010 1011
A Q Q-1(LSB-1) M Initial
0000 1011 0 0010 Values

1110 1011 0 0010 AA-M


1
1111 0101 1 0010 Arithmetic Shift Right
2 1111 1010 1 0010 Arithmetic Shift Right

3 0001 1010 1 0010 AA+M


0000 1101 0 0010 Arithmetic Shift Right

Zelalem Birhanu, AAiT 10


Booths Algorithmcntd

1110 1101 0 0010 AA-M


4
1111 0110 1 0010 Arithmetic Shift Right

Zelalem Birhanu, AAiT 11


Integer Division

= +
Unsigned division
e.g. 1001/0010
0 01
0010 1001 0010 1001 0010 1001
10
00

0100 010
0010 1001 0010
10 1001
10
0001 000
Q. Compute 1011/0011 12
Integer Divisioncntd

13
Integer Divisioncntd

Q. Compute 1001/0010
A Q M Initial
0000 1001 0010 Values

0001 0010 0010 Shift Left (A,Q)


1111 0010 0010 AA-M
1
1111 0010 0010 Q00
0001 0010 0010 AA+M

0010 0100 0010 Shift Left (A,Q)


2 0000 0100 0010 AA-M
0000 0101 0010 Q01

Zelalem Birhanu, AAiT 14


Integer Divisioncntd

0000 1010 0010 Shift Left (A,Q)


1110 1010 0010 AA-M
3 1110 1010 0010 Q00
0000 1010 0010 AA+M

0001 0100 0010 Shift Left (A,Q)


1111 0100 0010 AA-M
4 1110 0100 0010 Q00
0001 0100 0010 AA+M

Remainder

Zelalem Birhanu, AAiT 15


Floating-Point

Zelalem Birhanu, AAiT 16


Real Numbers

Numbers with fractional part

e.g. 12.25
Integer Decimal Fractional
Part (radix) Part
Point
Can be represented using:
Fixed-point representation
Floating-point representation
Zelalem Birhanu, AAiT 17
Fixed-point representation

Divide the bits into integer and fractional part and


fix the binary point

Assume we have an 8-bit system


Let us use 4-bits for the integer part and 4-bits for the
fractional part
e.g. 12.25 = 1 101 + 2 100 + 2 101 + 5 102
= 1100.0100 = 1 23 + 1 22 + 1 22
Binary (radix) Point

. 0.125 = 0000.0010 = 23
Zelalem Birhanu, AAiT 18
Fixed-point representationcntd

Advantage
Integer representation is fixed-point representation with the
binary point positioned to the far right
Therefore, hardware for integer arithmetic can be reused for
fixed-point arithmetic
Drawback
Loss of range and precision
For the previous example the fractional part is only precise to
multiples of 0.0625 (e.g. can not represent 0.1)
Increase precision=>lose range, Increase range=>lose
precision
(Very large numbers and very small fractions can not be
represented)
Zelalem Birhanu, AAiT 19
Floating-point

For decimal numbers scientific notation is used to


represent very large numbers and very small
fractions
e.g. 976,000,000,000,000 = 9.67 1014
0.0000000000000976 = 9.67 1014

Same approach can be taken for binary representation


by changing the base to 2
Zelalem Birhanu, AAiT 20
Floating-pointcntd

General binary representation


2
e.g.
0.110 25
110 22
0.0110 26
The binary point can be moved to a convenient
position (floating point) by changing the value of the
exponent

To simplify operations on floating-point numbers, they


are normalized Zelalem Birhanu, AAiT 21
Floating-pointcntd

Normalized numbers
The MSB of the significand is 1, the binary point comes
after the MSB
1. 2 = 0 1
e.g. the normalized form of 0.110 25 is 1.10 24

Q. Change into normalized form


i) 0.001 23 : 1.000 26
ii) 110.1 27 : -1.101 29

Zelalem Birhanu, AAiT 22


Floating-point representation

2
Only need to represent sign, signifiand and
exponent
IEEE 754
32-bits (single precision )
Sign bit 8 bits 23 bits

The number should be normalized first


Since the MSB is always 1, it needs not be represented
(S(24-bits) = 1:Fraction)
Zelalem Birhanu, AAiT 23
IEEE 754

Exponent
8-bits (can be positive or negative)
Uses biased base 127 encoding (-127 to 128)
Add 127 to the exponent value to encode
-127 00000000 1 10000000
-126 00000001 2 10000001

0 01111111 128 11111111

How to represent 0
Exponent: -127 (all zeros), Fraction:0 (all zeros), sign: + or -
Zelalem Birhanu, AAiT 24
IEEE 754cntd

Example
Find the IEEE754 representation of 10.62510
. To binary . [ ]

.

Sign bit: 0
Exponent: 3+127=130=10000010
Fraction: 01010100000000000000000

0 10000010 01010100000000000000000
25
IEEE 754cntd

Exercise
Find the IEEE754 representation of 7.7510
. To binary . [ ]

.

Sign bit: 1
Exponent: 2+127=129=10000001
Fraction: 11110000000000000000000

1 10000001 11110000000000000000000
26
IEEE 754cntd

Exercise
What is the value (in decimal) of the following IEEE 754
floating point encoded number?

1 01111110 01000000000000000000000

Sign: Negative
Exponent:126-127 = -1
Significand:1.01

. = + = .

Zelalem Birhanu, AAiT 27


IEEE 754cntd

64-bits (double precision )

Sign bit 11 bits 52 bits

Range
32-bits -----1.18 1038 3.4 1038
64-bits -----2.23 10308 1.80 10308

Zelalem Birhanu, AAiT 28


Next Class

Floating-point arithmetic

Zelalem Birhanu, AAiT 29


More Readings

1. Computer Architecture and Organization,


William Stallings, 8th edition (chapter 9)
2. Computer Organization and Design, David A.
Patterson, 4th edition (section 3.5)

Zelalem Birhanu, AAiT 30

Das könnte Ihnen auch gefallen