Ch#3 Part 1 2 3
Ch#3 Part 1 2 3
Ch#3 Part 1 2 3
and Architecture
Chapter # 3
Arithmetic For Computers
Today’s Topic
Introduction
Numerical Representation
2
Introduction
• Computer words are composed of bits; thus, words can
be represented as binary numbers.
•Largest Number ? 4
ASCII Vs. Binary
ambiguous zero
001 = +1 001 = +1 001 = +1
010 = +2 010 = +2 010 = +2
011 = +3 011 = +3 011 =
+3
100 = 0 100 = -3 100 = -
4
101 = -1 101 = -2 101 = -
3
110 = -2 110 = -1 110 = -
2
111 = -3 111 = 0 111 = -1
6
Negative Numbers
0000 0000 0000 0000 0000 0000 0000 0000 two = 0ten
0000 0000 0000 0000 0000 0000 0000 0001 two = 1ten
…
0111 1111 1111 1111 1111 1111 1111 1111 two = 231-1
1000 0000 0000 0000 0000 0000 0000 0000 two = -231
1000 0000 0000 0000 0000 0000 0000 0001 two = -(231 – 1)
1000 0000 0000 0000 0000 0000 0000 0010 two = -(231 – 2)
…
1111 1111 1111 1111 1111 1111 1111 1110 two = -2
1111 1111 1111 1111 1111 1111 1111 1111 two = -1 7
MIPS – 2’s complement
11
Example
12
Signed / Unsigned
14
MIPS Instructions
15
Sign Extension
17
Addition and Subtraction
18
Two’s Complement Addition
Perform addition
Examples (4-bits):
0101 0110 1011 1001 1111
0001 0101 0111 1010 1110
Do these sums now!! Remember all registers are 4-bit including result register!
So you have to throw away the carry-out from the msb!!
Have to beware of overflow : if the fixed number of bits (4, 8, 16,
32, etc.) in a register cannot represent the result of the operation
Overflow does not mean there was a carry-out from the msb that
we lost (though it sounds like that!) – it means simply that the
result in the fixed-sized register is incorrect
As can be seen from the above examples there are cases when
the result is correct even after losing the carry-out from the
msb 19
Detecting Overflow
No overflow when adding a positive and a negative number
No overflow when subtracting numbers with the same sign
Overflow occurs when the result has “wrong” sign (verify!):
A + B 0 0 0
A + B 0 0 0
A – B 0 0 0
A – B 0 0 0
20
Overflows
• For an unsigned number, overflow happens when the last carry (1)
cannot be accommodated
• For a signed number, overflow happens when the most significant bit
is not the same as every bit to its left
when the sum of two positive numbers is a negative result
when the sum of two negative numbers is a positive result
• MIPS allows addu and subu instructions that work with unsigned
integers and never flag an overflow – to detect the overflow, other
instructions will have to be executed
21
Effects of Overflow
If an exception (interrupt) occurs
control jumps to predefined address for exception
interrupted address is saved for possible resumption
22
Some Terms
23
Multiplication Example
Multiplicand 1000ten
Multiplier x 1001ten
---------------
1000 Binary makes it easy
0000 multiplier bit 1 => copy multiplicand
(1 x multiplicand)
0000 multiplier bit 0 => place 0
1000 (0 x multiplicand)
----------------
Product 1001000ten
In every step
• multiplicand is shifted
• next bit of multiplier is examined (also a shifting step)
• if this bit is 1, shifted multiplicand is added to the product
24
HW Algorithm 1
Start
Multiplier
64-bit ALU Shift right
2. Shift the Multiplicand register left 1 bit
32 bits
Product
Control test
Write 3. Shift the Multiplier register right 1 bit
64 bits
Done 25
Shift-add Multiplier Version1
Start
Yes: 32 repetitions
Done
26
Algorithm
Observation- Shift-add Multiplier Version1
1 step per clock cycle nearly 100 clock cycles to multiply two
32-bit numbers
Half the bits in the multiplicand register always 0
64-bit adder is wasted
0’s inserted to right as multiplicand is shifted left
least significant bits of product never
change once formed
27
HW Algorithm 2
Start
Multiplier
32-bit ALU Shift right
32 bits 2. Shift the Product register right 1 bit
Shift right
Product Control test
Write
3. Shift the Multiplier register right 1 bit
64 bits
Yes: 32 repetitions
Done Algorithm
28
Shift-add Multiplier Version2
Start
Multiplier0 = 1 Multiplier0 = 0
1. Test
Multiplier0 Example: 0010 * 0011:
Yes: 32 repetitions
Done
29
Algorithm
Observation- Shift-add Multiplier Version 2
Each step the product register wastes space that exactly matches
the current size of the multiplier
30
HW Algorithm 3
• 32-bit ALU and multiplicand is untouched Start
32 bits
1a. Add multiplicand to the left half of
the product and place the result in
the left half of the Product register
32-bit ALU
33
Signed Number Multiplication
35
Hardware Comparison
1001ten Quotient
Divisor 1000ten | 1001010ten Dividend
-1000
10
101
1010
-1000
10ten Remainder
At every step,
• shift divisor right and compare it with current dividend
• if divisor is larger, shift 0 as the next bit of the quotient
• if divisor is smaller, subtract to get new dividend and shift 1
as the next bit of the quotient 38
Division(2)
39
Division(3)
1001ten Quotient
Divisor 1000ten | 1001010ten Dividend
At every step,
• shift divisor right and compare it with current dividend
• if divisor is larger, shift 0 as the next bit of the quotient
• if divisor is smaller, subtract to get new dividend and shift 1
as the next bit of the quotient 40
Divide Example
• Divide 7ten (0000 0111two) by 2ten (0010two)
41
Divide Example
• Divide 7ten (0000 0111two) by 2ten (0010two)
Iter Step Quot Divisor Remainder
0 Initial values 0000 0010 0000 0000 0111
1 Rem = Rem – Div 0000 0010 0000 1110 0111
Rem < 0 +Div, shift 0 into Q 0000 0010 0000 0000 0111
Shift Div right 0000 0001 0000 0000 0111
2 Same steps as 1 0000 0001 0000 1111 0111
0000 0001 0000 0000 0111
0000 0000 1000 0000 0111
3 Same steps as 1 0000 0000 0100 0000 0111
4 Rem = Rem – Div 0000 0000 0100 0000 0011
Rem >= 0 shift 1 into Q 0001 0000 0100 0000 0011
Shift Div right 0001 0000 0010 0000 0011
5 Same steps as 4 0011 0000 0001 0000 0001
42
Divide Example
• Divide 7ten (0000 0111two) by 2ten (0010two)
43
Hardware for Division
Quotient
64-bit ALU Shift left
2a. Shift the Quotient register to the left, 2b. Restore the original value by adding
32 bits setting the new rightmost bit to 1 the Divisor register to the Remainder
register and place the sum in the
Remainder Control Remainder register. Also shift the
Write test Quotient register to the left, setting the
new least significant bit to 0
64 bits
Yes: 33 repetitions
Algorithm 45
Done
Hardware for Division-Basic Algorithm
Start
Yes: 33 repetitions
Done
46
Efficient Division
47
Divisions Involving Negatives
48
Divisions involving Negatives
49
Floating Point(1)
50
Floating Point(2)
52
Sign and Magnitude Representation
54
Sign and Magnitude Representation
Sign Exponent Fraction
1 bit 8 bits 23 bits
S E F
56
Bias and Fraction field
Example(continue)
Example (continue)
Examples
Single: (1 + 8 + 23)
Double: (1 + 11 + 52)
61
Examples
Single: (1 + 8 + 23)
1 0111 1110 1000…000
Double: (1 + 11 + 52)
1 0111 1111 110 1000…000