Chapter 2 Computer Arithmetic
Chapter 2 Computer Arithmetic
Chapter 2 Computer Arithmetic
Computer arithmetic
Integers Arithmetic
Representation
Addition and subtraction
Multiplication and Division
1
Arithmetic & Logic Unit (ALU)
Part of the computer that actually performs arithmetic
Control
Signals Flags
ALU
Operand Result
Registers Registers
3
Integers representation
Integers
-1101.01012 = -13.312510
periods.
Only binary digits (0 and 1) may be used to represent
4 numbers.
Representation
Sign-Magnitude Representation
most significant (leftmost) bit in the word as a sign
bit.
If the sign bit is 0 >> the number is positive;
examples
5
Representation
There are several drawbacks to sign-magnitude
representation.
One is that addition and subtraction require a
of 0
6
Representation
Twos Complement Representation
2’s complement => 1’s complement +1
7
2’s complement
for a positive number,
the sign bit,an-1 is zero.
The remaining bits represent the magnitude of the number
in the same fashion as for sign magnitude
For negative number,
sign bit , an-1 is one.
Magnitude is found applying 2’s complement
Example:
+5:0101
8
- 5:1011
Addition and Subtraction
2’s complement addition
If the operand to add is negative convert to 2’s
complement, then add
If the result of the operation is positive, we get a
positive number in true binary form
If the result of the operation is negative, we get a
negative number in twos complement form
N.B discard carry while addition of MSB
Overflow occurs if sum cannot be expressed with
the same bit of operands including sign bit.
9
OVERFLOW RULE
10
Examples
1001 = –7 1100 = –4
+0101 = 5 +0100 = 4
1110 = –2 10000 = 0
(a) (–7) + (+5) (b) (–4) + (+4)
0011 = 3 1100 = –4
+0100 = 4 +1111 = –1
0111 = 7 11011 = –5
(c) (+3) + (+4) (d) (–4) + (–1)
0101 = 5 1001 = –7
+0100 = 4 +1010 = –6
1001 = Overflow 10011 = Overflow
(e) (+5) + (+4) (f) (–7) + (–6)
Exercise
-4+4
-4+(-1)
11
-7+(-6)
1001 = –7 1100 = –4
+0101 = 5 +0100 = 4
1110 = –2 10000 = 0
(a) (–7) + (+5) (b) (–4) + (+4)
0011 = 3 1100 = –4
+0100 = 4 +1111 = –1
0111 = 7 11011 = –5
(c) (+3) + (+4) (d) (–4) + (–1)
0101 = 5 1001 = –7
+0100 = 4 +1010 = –6
1001 = Overflow 10011 = Overflow
(e) (+5) + (+4) (f) (–7) + (–6)
12
Subtraction
To subtract one number (subtrahend) from another
13
Examples
0010 = 2 0101 = 5
+1001 = –7 +1110 = –2
1011 = –5 10011 = 3
1011 = –5 0101 = 5
+1110 = –2 +0010 = 2
11001 = –7 0111 = 7
0111 = 7 1010 = –6
+0111 = 7 +1100 = –4
1110 = Overflow 10110 = Overflow
Exercise
5-2
-6-4
14
0010 = 2 0101 = 5
+1001 = –7 +1110 = –2
1011 = –5 10011 = 3
1011 = –5 0101 = 5
+1110 = –2 +0010 = 2
11001 = –7 0111 = 7
0111 = 7 1010 = –6
+0111 = 7 +1100 = –4
1110 = Overflow 10110 = Overflow
15
Multiplication
unsigned integers multiplication
Using paper and pencil:
digit in the multiplier. These partial products are then summed to produce
16
Multiplication
flow chart of unsigned number multiplication
17
Multiplication:
Example:
Multiplicand(M) = 1011 (11)
Multiplier(Q)=1101(13)
Solution: ? 1000 1111 (143)
Multiplicand
Mn-1 M0
Shift Right
C An-1 A0 Qn-1 Q0
Multiplier
C A Q M
0 0000 1101 1011 Initial Values
18
multiplication
Efficient paper and pencil multiplication using
HW
19
Signed number multiplication-booth’s
algorism
Straight forward multiplication will not work if
multiplicand is negative:
Note that this is accomplished by padding out each
20
Signed number multiplication-booth’s
algorism
Example: +7*+3
21
Booth’s Algorism-exercise
Use booth’s algorism to find product of:
A) 7*-3
B)-7*3
C)-7*-3
22
0111 0111
´ 0011 (0) ´ 1101 (0)
11111001 1–0 11111001 1–0
0000000 1–1 0000111 0–1
000111 0–1 111001 1–0
00010101 (21) 11101011 (–21)
(a) (7) ´ (3) = (21) (b) (7) ´ (–3) = (–21)
1001 1001
´ 0011 (0) ´ 1101 (0)
00000111 1–0 00000111 1–0
0000000 1–1 1111001 0–1
111001 0–1 000111 1–0
11101011 (–21) 00010101 (21)
(c) (–7) ´ (3) = (–21) (d) (–7) ´ (–3) = (21)
23
Division
Paper and Pencil approach(long division)
First, the bits of the dividend are examined from left to
right, until the set of bits examined represents a number
greater than or equal to the divisor;
this is referred to as the divisor being able to divide the
number.
Until this event occurs, 0s are placed in the quotient
from left to right.
When the event occurs, a 1 is placed in the quotient and
the divisor is subtracted from the partial dividend.
The result is referred to as a partial remainder
24
Division-machine algorism corresponds to
long division
25
Division-Example (7/3)
26
Floating-Point Arithmetic
28
Floating-Point representation
Limitation of fixed point representation:
Very large numbers cannot be represented, can
very small fractions.
Furthermore, the fractional part of the quotient in a
division of two large numbers could be lost.
Decimal system has scientific Notation to overcome
this limitation:
E.g
976,000,000,000,000 =9.76*1014
0.0000000000000976 =9.76*10-14
29
Floating-Point representation
Likewise in binary number we can represent using:
31
Floating-Point representation
The exponent value is stored in the next 8 bits.
The representation used is known as a biased representation.
A fixed value, called the bias, is subtracted from the field to get the true
exponent value
Typical bias value = 2k-1 - 1, where k is the number of bits in the binary
exponent
In 32-bits floating-point representation bits of exponent=8-bits
Bias value = 2 8-1 -1 = 27-1= 128-1=127
Normally exponent has value b/n 0 and 255
E.g if exponent = 10100 = 20, thus
True exponent = 20-127=-107
The final portion of the word (23 bits in this case) is the significand
32
Floating-Point representation
33
Floating-Point representation
number may be normalized by shifting the radix point
to the right of the leftmost 1 bit and adjusting the
exponent accordingly.
Note the following from example below:
The sign is stored in the first bit of the word.
The first bit of the true significand is always 1 and need
not be stored in the significand field.
The value 127 is added to the true exponent to be stored
in the exponent field.
The base is 2.
34
Example
1.1010001*210100 = 0 10010011 101 0001 0000 0000 0000 0000
= 1.6328125*220
–1.1010001*210100 = 1 10010011 101 0001 0000 0000 0000 0000
= –1.6328125*220
1.1010001*2–10100 = 0 01111011 101 0001 0000 0000 0000 0000
= 1.6328125*2–20
–1.1010001*2–10100 = 1 01111011 101 0001 0000 0000 0000 0000
= –1.6328125*2–20
35
IEEE Standard for Binary Floating-Point
Representation
The most important floating-point representation is
defined in IEEE Standard 754, adopted in 1985.
portability of programs from one processor to another
and to encourage the development of sophisticated,
numerically oriented programs.
The standard has been widely adopted and is used on
virtually all contemporary processors and arithmetic
coprocessors.
The IEEE standard defines both a 32-bit single and a 64-
bit double format with 8-bit and 11-bit exponents,
respectively.
36
IEEE 754 format
37
Floating-point arithmetic
addition and subtraction
it is necessary to ensure that both operands have the same
exponent value.
This may require shifting the radix point on one of the
operands to achieve alignment.
Multiplication and division are more straightforward.
A floating-point operation may produce one of these
conditions:
Exponent overflow: A positive exponent exceeds the
maximum possible exponent value. In some systems, this may
be designated as -∞ or + ∞
Exponent underflow: A negative exponent is less than the
minimum possible exponent value
This means that the number is too small to be represented, and it may
be reported as 0.
38
Floating-point arithmetic
A floating-point operation may produce one of these
conditions: cont..
Significand underflow:
In the process of aligning significands, digits may
flow off the right end of the significand.
Some form of rounding is required.
Significand overflow:
The addition of two significands of the same sign
may result in a carry out of the most significant bit.
This can be fixed by realignment.
39
Addition and Subtraction
There are four basic phases of the algorithm for addition
and subtraction:
1. Check for zeros.
2. Align the significands.
3. Add or subtract the significands.
4. Normalize the result.
40
Addition and subtraction
Zero Check:
the result.
41
Addition and subtraction
Significand alignment.
Alignment may be achieved by shifting either the
smaller number to the right (increasing its
exponent) or shifting the larger number to the left.
Because either operation may result in the loss of
digits, it is the smaller number that is shifted; any
digits that are lost are therefore of relatively small
significance
The alignment is achieved by repeatedly shifting
the magnitude portion of the significand right 1
digit and incrementing the exponent until the two
exponents are equal
42
Addition and Subtraction
Addition
two significands are added together, taking into
account their signs.
There is also the possibility of significand overflow
by 1 digit.
If so, the significand of the result is shifted right and
the exponent is incremented.
An exponent overflow could occur as a result; this would
be reported and the operation halted.
43
Addition and Subtraction
Normalization
44
Examples(reading assignment)
Represent 0.5 and 0.25 using the following floating
point representation:
6 bit normalized signed 2’s complement fraction
mantissa(significands), including the sign.
1 bit for the sign, 5 bits for the mantissa.
3 bit biased exponent.
Range of representation is -4 to 3.
Add 4 to bias it.
45
Examples (Contd.)
0.5 0.1 X 20
0 10000 100 (Already normalized).
0.25 0.01 X 20
0 01000 100
0 10000 011 (After normalization).
0 10000 100
+ 0 10000 011
46
Examples (Contd.)
1. Check for Zero.
None of the numbers is 0.
2. Align Mantissas(significands)
0 10000 100
0 10000 100 +
+ 0 10000 011 0 01000 100
3. Add
0 11000
4. Normalize
Already normalized.
47
Examples (Contd.)
Add 0.5 to –0.25
49
Overflow in Floating-Point Operations
What about overflow?
If adding the mantissas produces an overflow,
we shift the result’s mantissa to the right 1 position and
increment the exponent by 1.
50
Examples (Contd.)
Add 0.5 to 0.5
51
Examples (Contd.)
Add 4 to 4
52
Multiplication of Floating-Point Numbers
Multiplication of floating-point numbers is a four step
process:
Check for zeros.
Add the exponents.
Multiply the mantissas.
Normalize the product.
53
Division of Floating-Point Numbers
Division is done by:
Check for zeros.
Align the dividend.
Subtract the exponents.
Divide the mantissas.
54
Quiz 1( 5 or 10 marks) G3&4
For the following numbers, 0.4 and 0.30
55
Quiz 1( 5 or 10 marks)G5&6
For the following numbers, 0.4 and 0.30
56