Multiplication and Division CMSC 301 Prof. Szajda
Administrative • Read Ch. 3.1-3.4, C.11 • Program #1 due 10/24 • HW #5 assigned w Due Monday, 10/21, at 5pm
Review • What is slow in a ripple carry adder? • What makes a carry lookahead adder faster than a ripple carry adder?
Multiplication
Multiplication in Decimal How would this look in decimal? 0101 Multiplicand (MC) x 0110 Multiplier (MP) Product (P)
Multiplication in Decimal How would this look in decimal? 0101 Multiplicand (MC) x 0110 Multiplier (MP) 0000 Product (P)
Multiplication in Decimal How would this look in decimal? 0101 Multiplicand (MC) x 0110 Multiplier (MP) 0000 0101 Product (P)
Multiplication in Decimal How would this look in decimal? 0101 Multiplicand (MC) x 0110 Multiplier (MP) 0000 0101 0101 Product (P)
Multiplication in Decimal 0101 Multiplicand (MC) x 0110 Multiplier (MP) 0000 0101 0101 0000 Product (P)
Multiplication in Decimal 0101 Multiplicand (MC) x 0110 Multiplier (MP) 0000 0101 0101 0000 0011110 Product (P)
Observations • Two 4-bit operands produces an answer up to 8 bits • What is the only number that gets added to produce the product? • What happens each iteration?
Observations • Two 4-bit operands produces an answer up to 8 bits • What is the only number that gets added to produce the product? Multiplicand • What happens each iteration?
Observations • Two 4-bit operands produces an answer up to 8 bits • What is the only number that gets added to produce the product? Multiplicand • What happens each iteration? w Shift multiplicand one to the left w Shift our attention of multiplier bit one left
MIPS Multiplication • 2 32-bit inputs create a 64-bit output • MIPS places those in regs named HI and LO • code for $s0 = $s1 * $s2 mult $s1, $s2 # implicit dests HI & LO mflo $s0 # $s0 <- LO
Multiplier Hardware
1st Multiplication Algorithm Start MP[0] != 0 MP[0] == 0 Test multiplier bit 0 MP[0] == 0? P = P + MC MC << 1 MP >> 1 No: < 32 32 nd iteration? iterations Yes: 32 Done iterations
1st Multiplication Algorithm Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0000 0101 0000 0000 1 Shift left MC Shift right MP 2 Shift left __ Shift right __ 3 Shift left __ Shift right __ 4 Shift left __ Shift right __
1st Multiplication Algorithm Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0000 0101 0000 0000 011 0 1 0 -> no op Shift left MC 0000 1010 001 1 Shift right MP 2 Shift left __ Shift right __ 3 Shift left __ Shift right __ 4 Shift left __ Shift right __
1st Multiplication Algorithm Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0000 0101 0000 0000 011 0 1 0 -> no op Shift left MC 0000 1010 001 1 Shift right MP 2 1 -> Pr = Pr + MC 0000 1010 Shift left MC 0001 0100 000 1 Shift right MP 3 Shift left __ Shift right __ 4 Shift left __ Shift right __
1st Multiplication Algorithm Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0000 0101 0000 0000 011 0 1 0 -> no op Shift left MC 0000 1010 001 1 Shift right MP 2 1 -> Pr = Pr + MC 0000 1010 Shift left MC 0001 0100 000 1 Shift right MP 3 1 -> Pr = Pr + MC 0001 1110 Shift left MC 0010 1000 000 0 Shift right MP 4 Shift left __ Shift right __
1st Multiplication Algorithm Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0000 0101 0000 0000 011 0 1 0 -> no op Shift left MC 0000 1010 001 1 Shift right MP 2 1 -> Pr = Pr + MC 0000 1010 Shift left MC 0001 0100 000 1 Shift right MP 3 1 -> Pr = Pr + MC 0001 1110 Shift left MC 0010 1000 000 0 Shift right MP 4 0 -> no op 0001 1110 Shift left MC 0101 0000 Shift right MP 0000
Observation of 1st Algorithm • Half of the MC is 0’s at any given time • We really only care about 4 bits of the MC • Can we shift something else and keep MC the same?
Observation of 1st Algorithm • Half of the MC is 0’s at any given time • We really only care about 4 bits of the MC • Can we shift something else and keep MC the same? Product
2nd Algorithm • Always add MC to the top bits of the product • Shift the Product to the right
2nd Multiplication Algorithm Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0101 0000 0000 011 0 1 0 -> no op Shift Right P 001 1 Shift right MP 2 1 -> Pr = Pr + MC Shift Right P 000 1 Shift right MP 3 1 -> Pr = Pr + MC Shift Right P 000 0 Shift right MP 4 0 -> no op Shift Right P Shift right MP 0000
2nd Multiplication Algorithm Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0101 0000 0000 011 0 1 0 -> no op Shift Right P 0000 0000 001 1 Shift right MP 2 1 -> Pr = Pr + MC Shift Right P 000 1 Shift right MP 3 1 -> Pr = Pr + MC Shift Right P 000 0 Shift right MP 4 0 -> no op Shift Right P Shift right MP 0000
2nd Multiplication Algorithm Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0101 0000 0000 011 0 1 0 -> no op Shift Right P 0000 0000 001 1 Shift right MP 2 1 -> Pr = Pr + MC 0101 0000 Shift Right P 0010 1000 000 1 Shift right MP 3 1 -> Pr = Pr + MC Shift Right P 000 0 Shift right MP 4 0 -> no op Shift Right P Shift right MP 0000
2nd Multiplication Algorithm Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0101 0000 0000 011 0 1 0 -> no op Shift Right P 0000 0000 001 1 Shift right MP 2 1 -> Pr = Pr + MC 0101 0000 Shift Right P 0010 1000 000 1 Shift right MP 3 1 -> Pr = Pr + MC 0111 1000 Shift Right P 0011 1100 000 0 Shift right MP 4 0 -> no op Shift Right P Shift right MP 0000
2nd Multiplication Algorithm Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0101 0000 0000 011 0 1 0 -> no op Shift Right P 0000 0000 001 1 Shift right MP 2 1 -> Pr = Pr + MC 0101 0000 Shift Right P 0010 1000 000 1 Shift right MP 3 1 -> Pr = Pr + MC 0111 1000 Shift Right P 0011 1100 000 0 Shift right MP 4 0 -> no op 0011 1100 Shift Right P 0001 1110 Shift right MP 0000
Savings • We have a 4-bit adder, not 8-bit adder • Save 4 bits of storage in Multiplicand
Observations about 2nd Algorithm • The # of bits we care about in the Multiplier is equal to the # of 0’s in Product
Observations about 2nd Algorithm • The # of bits we care about in the Multiplier is equal to the # of 0’s in Product • Solution Store the Multiplier in the lower portion of the Product
3rd Multiplication Algorithm Iteration Step Multiplicand Product / MP 0 Initialize values 0101 0000 011 0 1 0 -> no op Shift Right P 2 1 -> Pr = Pr + MC Shift Right P 3 1 -> Pr = Pr + MC Shift Right P 4 0 -> no op Shift Right P
3rd Multiplication Algorithm Iteration Step Multiplicand Product / MP 0 Initialize values 0101 0000 011 0 1 0 -> no op Shift Right P 0000 001 1 2 1 -> Pr = Pr + MC Shift Right P 3 1 -> Pr = Pr + MC Shift Right P 4 0 -> no op Shift Right P
3rd Multiplication Algorithm Iteration Step Multiplicand Product / MP 0 Initialize values 0101 0000 011 0 1 0 -> no op Shift Right P 0000 001 1 2 1 -> Pr = Pr + MC 0101 0011 Shift Right P 0010 100 1 3 1 -> Pr = Pr + MC Shift Right P 4 0 -> no op Shift Right P
3rd Multiplication Algorithm Iteration Step Multiplicand Product / MP 0 Initialize values 0101 0000 011 0 1 0 -> no op Shift Right P 0000 001 1 2 1 -> Pr = Pr + MC 0101 0011 Shift Right P 0010 100 1 3 1 -> Pr = Pr + MC 0111 1001 Shift Right P 0011 110 0 4 0 -> no op Shift Right P
3rd Multiplication Algorithm Iteration Step Multiplicand Product / MP 0 Initialize values 0101 0000 011 0 1 0 -> no op Shift Right P 0000 001 1 2 1 -> Pr = Pr + MC 0101 0011 Shift Right P 0010 100 1 3 1 -> Pr = Pr + MC 0111 1001 Shift Right P 0011 110 0 4 0 -> no op Shift Right P 0001 1110
Savings • Only one shift per iteration • Save 4 bits of storage from Multiplier • Limitation: 4 * -1?
Division
Division in Decimal How would this look in decimal? Quotient Divisor Dividend 0010 0111
Division in Decimal How would this look in decimal? 11 Quotient Divisor Dividend 0010 0111 -0010 0011 -0010 Remainder 1
Divisor Hardware Initially divisor in left half Initially dividend
1st Division Algorithm Start Rem = Rem - Divisor Test Rem < 0 Rem >= 0 Remainder Quotient << 1 Rem = Divisor + Rem Rightmost bit = 1 Quotient << 1 Rightmost bit = 0 Divisor >> 1 33rd No: < 33 repetitions repetition? Yes: 33 repetitions Done
1st Division Algorithm Iteration Step Quotient Divisor Remainder 0 Initialize values 0000 0010 0000 0000 0111 1 Rem = Rem - Div Div>>1 2 Rem = Rem - Div Div>>1 3 Rem = Rem - Div Div>>1 4 Rem = Rem - Div Div>>1 5 Rem = Rem - Div Div>>1
Recommend
More recommend