multiplication and division
play

Multiplication and Division CMSC 301 Prof. Szajda Administrative - PowerPoint PPT Presentation

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


  1. Multiplication and Division CMSC 301 Prof. Szajda

  2. Administrative • Read Ch. 3.1-3.4, C.11 • Program #1 due 10/24 • HW #5 assigned w Due Monday, 10/21, at 5pm

  3. Review • What is slow in a ripple carry adder? • What makes a carry lookahead adder faster than a ripple carry adder?

  4. Multiplication

  5. Multiplication in Decimal How would this look in decimal? 0101 Multiplicand (MC) x 0110 Multiplier (MP) Product (P)

  6. Multiplication in Decimal How would this look in decimal? 0101 Multiplicand (MC) x 0110 Multiplier (MP) 0000 Product (P)

  7. Multiplication in Decimal How would this look in decimal? 0101 Multiplicand (MC) x 0110 Multiplier (MP) 0000 0101 Product (P)

  8. Multiplication in Decimal How would this look in decimal? 0101 Multiplicand (MC) x 0110 Multiplier (MP) 0000 0101 0101 Product (P)

  9. Multiplication in Decimal 0101 Multiplicand (MC) x 0110 Multiplier (MP) 0000 0101 0101 0000 Product (P)

  10. Multiplication in Decimal 0101 Multiplicand (MC) x 0110 Multiplier (MP) 0000 0101 0101 0000 0011110 Product (P)

  11. 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?

  12. 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?

  13. 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

  14. 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

  15. Multiplier Hardware

  16. 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

  17. 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 __

  18. 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 __

  19. 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 __

  20. 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 __

  21. 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

  22. 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?

  23. 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

  24. 2nd Algorithm • Always add MC to the top bits of the product • Shift the Product to the right

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. Savings • We have a 4-bit adder, not 8-bit adder • Save 4 bits of storage in Multiplicand

  31. Observations about 2nd Algorithm • The # of bits we care about in the Multiplier is equal to the # of 0’s in Product

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. Savings • Only one shift per iteration • Save 4 bits of storage from Multiplier • Limitation: 4 * -1?

  39. Division

  40. Division in Decimal How would this look in decimal? Quotient Divisor Dividend 0010 0111

  41. Division in Decimal How would this look in decimal? 11 Quotient Divisor Dividend 0010 0111 -0010 0011 -0010 Remainder 1

  42. Divisor Hardware Initially divisor in left half Initially dividend

  43. 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

  44. 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