Chapter10 Arithmetic
Chapter10 Arithmetic
Computer Arithmetic
Chapter 10
Arithmetic -- The heart of instruction execution
Instruction
Fetch
Instruction
Decode
operation
Operand
Fetch
a
Execute 32 ALU
result
32
Result b
Store 32
Next
Instruction
Arithmetic & Logic Unit
• Does the calculations
• Everything else in the computer is there
to service this unit
• Handles integers
• May handle floating point (real) numbers
• May be separate FPU (maths co-
processor)
• May be on chip separate FPU (486DX +)
ALU Inputs and Outputs
Positional Number Systems
Different Representations of Natural Numbers
Characters
Most Least
Significant Bit Significant Bit
Floating-point numbers
Example:
Solution:
E B 1 6 A 7 9 4
1110 1011 0001 0110 1010 0111 1001 0100
Integer Storage Sizes
Byte 8
Double Word 64
0 = positive 1 1 1 1 0 1 1 0
Negative
0 0 0 0 1 0 1 0 Positive
Practice: What is the range of signed values that may be stored in 20 bits?
Geometric Depiction of Twos
Complement Integers
Two's Complement Special Cases
Case 1
0= 00000000
Bitwise not 11111111
Add 1 to LSB +1
Result 1 00000000
Overflow is ignored, so:
-0=0
-128 = 10000000
bitwise not 01111111
Add 1 to LSB +1
Result 10000000
Monitor MSB (sign bit)
It should change during negation
Two’s Compliment - Summery
Benefits:
•One representation of zero
•Arithmetic works easily (see later)
Character Storage
Character sets
Standard ASCII: 7-bit character codes (0 – 127)
Extended ASCII: 8-bit character codes (0 – 255)
Unicode: 16-bit character codes (0 – 65,535)
Unicode standard represents a universal character set
Defines codes for characters used in all major languages
Used in Windows-XP: each character is encoded as 16 bits
UTF-8: variable-length encoding used in HTML
Encodes all Unicode characters
Uses 1 byte for ASCII, but multiple bytes for other characters
Null-terminated String
Array of characters followed by a NULL character
Binary Addition
Start with the least significant bit (rightmost bit)
Add each pair of bits
Include the carry in the addition, if present
carry 1 1 1 1
0 0 1 1 0 1 1 0
(54)
+
0 0 0 1 1 1 0 1
0 1 0 1 0 0 1 1
(29)
bit position: 7 6 5 4 3 2 1 0 (83)
Binary Subtraction
When subtracting A – B, convert B to its 2's complement
Add A to (–B)
borrow: 1 1 1 carry: 1 1 1 1
01001101 01001101
– +
00111010 11000110 (2's
complement)
0 0 0 0 1 1 1 1 15 0 0 0 0 1 1 1 1 15
+ +
0 0 0 0 1 0 0 0 8 1 1 1 1 1 0 0 0 248 (-8)
0 0 0 1 0 1 1 1 23 0 0 0 0 0 1 1 1 7
1 1 1 1
0 1 0 0 1 1 1 1 79 1 1 0 1 1 0 1 0 218 (-
+ + 38)
0 1 0 0 0 0 0 0 64 1 0 0 1 1 1 0 1 157 (-
99)
1 0 0 0 1 1 1 1 143 0 1 1 1 0 1 1 1 119
(-113)
Carry = 0 Overflow = 1 Carry = 1 Overflow = 1
Addition of Numbers in Twos Complement
Representation
Subtraction of Numbers in Twos Complement
Representation (M – S)
Unsigned Multiplication
Paper and Pencil Example:
Multiplicand 11002 = 12
Multiplier × 11012 = 13
1100 Binary multiplication is easy
0000 0 × multiplicand = 0
1100 1 × multiplicand = multiplicand
1100
Product 100111002 = 156
m-bit multiplicand × n-bit multiplier = (m+n)-bit product
Accomplished via shifting and addition
Consumes more time and more chip area
Unsigned Binary Multiplication
Flowchart for Unsigned Binary
Multiplication
Execution of Example
Multiplying Negative Numbers
• This does not work!
• Solution 1
—Convert to positive if required
—Multiply as above
—If signs were different, negate answer
• Solution 2
—Booth’s algorithm
Booth’s Algorithm
Example of Booth’s Algorithm
Examples Using Booth's Algorithm
How it works
• Consider a positive multiplier consisting of a block
of 1s surrounded by 0s. For example, 00111110.
The product is given by :
• Note that:
2n + 2n-1 +…+2n-k = 2n+1 – 2n-k
How it works
• So, the product can be generated by one
addition and one subtraction
• In Booth’s algorithm
—perform subtraction when the first 1 of the
block is encountered (1 - 0)
—perform addition when the last 1 of the block
is encountered (0 - 1)
00001101 Quotient
Divisor 1011 10010011 Dividend
1011
Partial 001110
1011
Remainders
001111
1011
100 Remainder
Real Numbers
• Numbers with fractions
• Could be done in pure binary
—1001.1010 = 23 + 20 +2-1 + 2-3 =9.625
• Where is the binary point?
• Fixed?
—Very limited
• Moving?
—How do you show where it is?
Exponential Notation
123,400.0 x 10-2
12,340.0 x 10-1 The representations differ
1,234.0 x 100 in that the decimal place –
123.4 x 101 the “point” -- “floats” to
the left or right (with the
12.34 x 102 appropriate adjustment in
1.234 x 103 the exponent).
0.1234 x 104 p. 122
Parts of a Floating Point Number
Exponent
-0.9876 x 10 -3
Sign of
Sign of Location of exponent
Significand
mantissa decimal point
or Mantissa Base
32 bits
Exponent (8 bits)
C17B000016
• Step 1
—Express in binary and find S, E, and M
C17B000016 =
1 10000010 111101100000000000000002
S E M
1 = negative
0 = positive
• Step 2
—Find “real” exponent, n
—n = E – 127
= 100000102 – 127
= 130 – 127
=3
• Step 3
—Put S, M, and n together to form binary result
—(Don’t forget the implied “1.” on the left of the
mantissa.)
-1.11110112 x 2n =
-1.11110112 x 23 =
-1111.10112
• Step 4
—Express result in decimal
-1111.10112
-15 2-1 = 0.5
2-3 = 0.125
2-4 = 0.0625
0.6875
Answer: -15.6875
Converting to Floating Point
• E.g., Express 36.562510 as a 32-bit
floating point number (in hexadecimal)
• Step 1
—Express original value in binary
36.562510 =
100100.10012
• Step 2
—Normalize
100100.10012 =
1.0010010012 x 25
• Step 3
—Determine S, E, and M
+1.0010010012 x 25
S M n E = n + 127
= 5 + 127
= 132
= 100001002
0 10000100 001001001000000000000002
S E M
• Step 5
—Express in hexadecimal
0 10000100 001001001000000000000002 =
4 2 1 2 4 0 0 016
Answer: 4212400016
FP Ranges
• For a 32 bit number
—8 bit exponent
—+/- 2256 1.5 x 1077
• Accuracy
—The effect of changing lsb of mantissa
—23 bit mantissa 2-23 1.2 x 10-7
—About 6 decimal places
Expressible Numbers
Density of Floating Point Numbers
IEEE 754
• Standard for floating point storage
• 32 and 64 bit standards
• 8 and 11 bit exponent respectively
• Extended formats (both mantissa and
exponent) for intermediate results
IEEE 754 Formats
FP Arithmetic +/-
• Check for zeros
• Align significands (adjusting exponents)
• Add or subtract significands
• Normalize result
FP Addition & Subtraction Flowchart
Z=X±Y
FP Arithmetic x/
• Check for zero
• Add/subtract exponents
• Multiply/divide significands (watch sign)
• Normalize
• Round
• All intermediate results should be in
double length storage
Floating Point Multiplication
Z=X x Y
Floating Point Division
Z=X/Y