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

multiplication and division
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Multiplication and Division

CMSC 301

  • Prof. Szajda
slide-2
SLIDE 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

slide-3
SLIDE 3

Review

  • What is slow in a ripple carry adder?
  • What makes a carry lookahead adder

faster than a ripple carry adder?

slide-4
SLIDE 4

Multiplication

slide-5
SLIDE 5

Multiplication in Decimal

How would this look in decimal?

0101 x 0110

Multiplicand (MC) Multiplier (MP) Product (P)

slide-6
SLIDE 6

Multiplication in Decimal

How would this look in decimal?

0101 x 0110 0000

Multiplicand (MC) Multiplier (MP) Product (P)

slide-7
SLIDE 7

Multiplication in Decimal

0101 x 0110

How would this look in decimal?

0000 0101

Multiplicand (MC) Multiplier (MP) Product (P)

slide-8
SLIDE 8

Multiplication in Decimal

0101 x 0110

How would this look in decimal?

0000 0101 0101

Multiplicand (MC) Multiplier (MP) Product (P)

slide-9
SLIDE 9

Multiplication in Decimal

0101 x 0110 0000 0101 0101 0000

Multiplicand (MC) Multiplier (MP) Product (P)

slide-10
SLIDE 10

Multiplication in Decimal

0101 x 0110 0011110 0000 0101 0101 0000

Multiplicand (MC) Multiplier (MP) Product (P)

slide-11
SLIDE 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?
slide-12
SLIDE 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?
slide-13
SLIDE 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

slide-14
SLIDE 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

slide-15
SLIDE 15

Multiplier Hardware

slide-16
SLIDE 16

1st Multiplication Algorithm

Start Done No: < 32 iterations Yes: 32 iterations

32nd iteration? Test multiplier bit 0 MP[0] == 0? MP[0] == 0 MP[0] != 0 P = P + MC MC << 1 MP >> 1

slide-17
SLIDE 17

1st Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product Initialize values 0110 0000 0101 0000 0000 1 Shift left MC Shift right MP 3 Shift left __ Shift right __ 2 Shift left __ Shift right __ 4 Shift left __ Shift right __

slide-18
SLIDE 18

1st Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product Initialize values 0110 0000 0101 0000 0000 1 0 -> no op 0110 Shift left MC 0000 1010 Shift right MP 0011 3 Shift left __ Shift right __ 2 Shift left __ Shift right __ 4 Shift left __ Shift right __

slide-19
SLIDE 19

1st Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product Initialize values 0110 0000 0101 0000 0000 1 0 -> no op 0110 Shift left MC 0000 1010 Shift right MP 0011 3 Shift left __ Shift right __ 2 1 -> Pr = Pr + MC 0000 1010 Shift left MC 0001 0100 Shift right MP 0001 4 Shift left __ Shift right __

slide-20
SLIDE 20

1st Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product Initialize values 0110 0000 0101 0000 0000 1 0 -> no op 0110 Shift left MC 0000 1010 Shift right MP 0011 3 1 -> Pr = Pr + MC 0001 1110 Shift left MC 0010 1000 Shift right MP 0000 2 1 -> Pr = Pr + MC 0000 1010 Shift left MC 0001 0100 Shift right MP 0001 4 Shift left __ Shift right __

slide-21
SLIDE 21

1st Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product Initialize values 0110 0000 0101 0000 0000 1 0 -> no op 0110 Shift left MC 0000 1010 Shift right MP 0011 3 1 -> Pr = Pr + MC 0001 1110 Shift left MC 0010 1000 Shift right MP 0000 2 1 -> Pr = Pr + MC 0000 1010 Shift left MC 0001 0100 Shift right MP 0001 4 0 -> no op 0001 1110 Shift left MC 0101 0000 Shift right MP 0000

slide-22
SLIDE 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?

slide-23
SLIDE 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

slide-24
SLIDE 24

2nd Algorithm

  • Always add MC to the top bits of the

product

  • Shift the Product to the right
slide-25
SLIDE 25

2nd Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product Initialize values 0110 0101 0000 0000 1 0 -> no op 0110 Shift right MP 0011 3 1 -> Pr = Pr + MC Shift right MP 0000 2 1 -> Pr = Pr + MC Shift right MP 0001 4 0 -> no op Shift Right P Shift right MP 0000 Shift Right P Shift Right P Shift Right P

slide-26
SLIDE 26

2nd Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product Initialize values 0110 0101 0000 0000 1 0 -> no op 0110 Shift right MP 0011 3 1 -> Pr = Pr + MC Shift right MP 0000 2 1 -> Pr = Pr + MC Shift right MP 0001 4 0 -> no op Shift Right P Shift right MP 0000 Shift Right P 0000 0000 Shift Right P Shift Right P

slide-27
SLIDE 27

2nd Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product Initialize values 0110 0101 0000 0000 1 0 -> no op 0110 Shift right MP 0011 3 1 -> Pr = Pr + MC Shift right MP 0000 2 1 -> Pr = Pr + MC 0101 0000 Shift right MP 0001 4 0 -> no op Shift Right P Shift right MP 0000 Shift Right P 0000 0000 Shift Right P 0010 1000 Shift Right P

slide-28
SLIDE 28

2nd Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product Initialize values 0110 0101 0000 0000 1 0 -> no op 0110 Shift right MP 0011 3 1 -> Pr = Pr + MC 0111 1000 Shift right MP 0000 2 1 -> Pr = Pr + MC 0101 0000 Shift right MP 0001 4 0 -> no op Shift Right P Shift right MP 0000 Shift Right P 0000 0000 Shift Right P 0010 1000 Shift Right P 0011 1100

slide-29
SLIDE 29

2nd Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product Initialize values 0110 0101 0000 0000 1 0 -> no op 0110 Shift right MP 0011 3 1 -> Pr = Pr + MC 0111 1000 Shift right MP 0000 2 1 -> Pr = Pr + MC 0101 0000 Shift right MP 0001 4 0 -> no op 0011 1100 Shift Right P 0001 1110 Shift right MP 0000 Shift Right P 0000 0000 Shift Right P 0010 1000 Shift Right P 0011 1100

slide-30
SLIDE 30

Savings

  • We have a 4-bit adder, not 8-bit adder
  • Save 4 bits of storage in Multiplicand
slide-31
SLIDE 31

Observations about 2nd Algorithm

  • The # of bits we care about in the

Multiplier is equal to the # of 0’s in Product

slide-32
SLIDE 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

slide-33
SLIDE 33

3rd Multiplication Algorithm

Iteration Step Multiplicand Product / MP Initialize values 0101 0000 0110 1 0 -> no op 3 1 -> Pr = Pr + MC 2 1 -> Pr = Pr + MC 4 0 -> no op Shift Right P Shift Right P Shift Right P Shift Right P

slide-34
SLIDE 34

3rd Multiplication Algorithm

Iteration Step Multiplicand Product / MP Initialize values 0101 0000 0110 1 0 -> no op 3 1 -> Pr = Pr + MC 2 1 -> Pr = Pr + MC 4 0 -> no op Shift Right P Shift Right P 0000 0011 Shift Right P Shift Right P

slide-35
SLIDE 35

3rd Multiplication Algorithm

Iteration Step Multiplicand Product / MP Initialize values 0101 0000 0110 1 0 -> no op 3 1 -> Pr = Pr + MC 2 1 -> Pr = Pr + MC 0101 0011 4 0 -> no op Shift Right P Shift Right P 0000 0011 Shift Right P 0010 1001 Shift Right P

slide-36
SLIDE 36

3rd Multiplication Algorithm

Iteration Step Multiplicand Product / MP Initialize values 0101 0000 0110 1 0 -> no op 3 1 -> Pr = Pr + MC 0111 1001 2 1 -> Pr = Pr + MC 0101 0011 4 0 -> no op Shift Right P Shift Right P 0000 0011 Shift Right P 0010 1001 Shift Right P 0011 1100

slide-37
SLIDE 37

3rd Multiplication Algorithm

Iteration Step Multiplicand Product / MP Initialize values 0101 0000 0110 1 0 -> no op 3 1 -> Pr = Pr + MC 0111 1001 2 1 -> Pr = Pr + MC 0101 0011 4 0 -> no op Shift Right P 0001 1110 Shift Right P 0000 0011 Shift Right P 0010 1001 Shift Right P 0011 1100

slide-38
SLIDE 38

Savings

  • Only one shift per iteration
  • Save 4 bits of storage from Multiplier
  • Limitation: 4 * -1?
slide-39
SLIDE 39

Division

slide-40
SLIDE 40

Division in Decimal

0111

How would this look in decimal?

Dividend

0010

Quotient Divisor

slide-41
SLIDE 41

Division in Decimal

0111

How would this look in decimal?

Dividend

0010

Quotient

11

  • 0010

0011

  • 0010

1

Remainder Divisor

slide-42
SLIDE 42

Divisor Hardware

Initially dividend Initially divisor in left half

slide-43
SLIDE 43

1st Division Algorithm

Start Done Test Remainder 33rd repetition? Divisor >> 1 Quotient << 1 Rightmost bit = 1 Rem = Divisor + Rem Quotient << 1 Rightmost bit = 0 Rem = Rem - Divisor Rem >= 0 Rem < 0 Yes: 33 repetitions No: < 33 repetitions

slide-44
SLIDE 44

1st Division Algorithm

Iteration Step Quotient Divisor Remainder Initialize values 0000 0010 0000 0000 0111 1 Rem = Rem - Div Div>>1 3 Rem = Rem - Div Div>>1 2 Rem = Rem - Div Div>>1 4 Rem = Rem - Div Div>>1 5 Rem = Rem - Div Div>>1

slide-45
SLIDE 45

1st Division Algorithm

Iteration Step Quotient Divisor Remainder Initialize values 0000 0010 0000 0000 0111 1 Rem = Rem - Div Rem<0=>+Div, Q<<1, Q0=0 Div>>1 3 Rem = Rem - Div Div>>1 2 Rem = Rem - Div Div>>1 4 Rem = Rem - Div Div>>1 1110 0111 0000 0111 0001 0000 0000 5 Rem = Rem - Div Div>>1

slide-46
SLIDE 46

1st Division Algorithm

Iteration Step Quotient Divisor Remainder Initialize values 0000 0010 0000 0000 0111 1 Rem = Rem - Div Rem<0=>+Div, Q<<1, Q0=0 Div>>1 3 Rem = Rem - Div Div>>1 2 Rem = Rem - Div 1111 0111 Rem<0=>+Div, Q<<1, Q0=0 Div>>1 4 Rem = Rem - Div Div>>1 1110 0111 0000 0111 0001 0000 0000 0000 0000 1000 0000 0111 5 Rem = Rem - Div Div>>1

slide-47
SLIDE 47

1st Division Algorithm

Iteration Step Quotient Divisor Remainder Initialize values 0000 0010 0000 0000 0111 1 Rem = Rem - Div Rem<0=>+Div, Q<<1, Q0=0 Div>>1 3 Rem = Rem - Div 1111 1111 Rem<0=>+Div, Q<<1, Q0=0 Div>>1 2 Rem = Rem - Div 1111 0111 Rem<0=>+Div, Q<<1, Q0=0 Div>>1 4 Rem = Rem - Div Div>>1 1110 0111 0000 0111 0001 0000 0000 0000 0000 0000 1000 0000 0100 0000 0111 0000 0111 5 Rem = Rem - Div Div>>1

slide-48
SLIDE 48

1st Division Algorithm

Iteration Step Quotient Divisor Remainder Initialize values 0000 0010 0000 0000 0111 1 Rem = Rem - Div Rem<0=>+Div, Q<<1, Q0=0 Div>>1 3 Rem = Rem - Div 1111 1111 Rem<0=>+Div, Q<<1, Q0=0 Div>>1 2 Rem = Rem - Div 1111 0111 Rem<0=>+Div, Q<<1, Q0=0 Div>>1 4 Rem = Rem - Div 0000 0011 Rem>=0=> Q<<1, Q0=1 Div>>1 1110 0111 0000 0111 0001 0000 0000 0000 0000 0001 0000 1000 0000 0100 0000 0010 0000 0111 0000 0111 5 Rem = Rem - Div Div>>1

slide-49
SLIDE 49

1st Division Algorithm

Iteration Step Quotient Divisor Remainder Initialize values 0000 0010 0000 0000 0111 1 Rem = Rem - Div Rem<0=>+Div, Q<<1, Q0=0 Div>>1 3 Rem = Rem - Div 1111 1111 Rem<0=>+Div, Q<<1, Q0=0 Div>>1 2 Rem = Rem - Div 1111 0111 Rem<0=>+Div, Q<<1, Q0=0 Div>>1 4 Rem = Rem - Div 0000 0011 Rem>=0=> Q<<1, Q0=1 Div>>1 1110 0111 0000 0111 0001 0000 0000 0000 0000 0001 0000 1000 0000 0100 0000 0010 0000 0111 0000 0111 5 Rem = Rem - Div 0000 0001 Rem>=0=> Q<<1, Q0=1 Div>>1 0011 0000 0001

slide-50
SLIDE 50

Observation of 1st Algorithm

  • Half of the Divisor is 0’s at any given

time

  • We really only care about 4 bits of the

Divisor

  • Can we shift something else and keep

Divisor the same?

slide-51
SLIDE 51

Observation of 1st Algorithm

  • Half of the Divisor is 0’s at any given

time

  • We really only care about 4 bits of the

Divisor

  • Can we shift something else and keep

Divisor the same?

  • Solution:

w Shift the Remainder to the left

slide-52
SLIDE 52

Observation of 1st Algorithm

  • First step cannot produce 1 in

Quotient bit

w If it did, too large for quotient register

  • Shift first, subtract second

w Removes one iteration w Remainder in left half of Remainder register

slide-53
SLIDE 53

More Observations
 (of 2nd Algorithm)

  • The # of bits we care about in the

Quotient is equal to the # of 0’s in Remainder

  • Solution


Store the Quotient in the lower portion of the Remainder

slide-54
SLIDE 54

3rd Division Algorithm

Start

  • Done. (LH)Rem >> 1

Test Remainder 32nd repetition? Left half Rem = (LH)Rem - Divisor Rem << 1 Rightmost bit Rem = 1 (LH)Rem = Divisor + (LH)Rem Rem << 1 Rightmost bit Rem = 0 Rem << 1 Rem >= 0 Rem < 0 Yes: 32 repetitions No: < 32 repetitions

slide-55
SLIDE 55

3rd Division Algorithm

Iteration Step Divisor Remainder Initialize values 0010 0000 0111 1 Rem << 1 Rem = Rem - Div 3 Rem = Rem - Div 2 Rem = Rem - Div 4 Rem = Rem - Div Left half of Rem >> 1

slide-56
SLIDE 56

3rd Division Algorithm

Iteration Step Divisor Remainder Initialize values 0010 0000 0111 1 Rem << 1 Rem = Rem - Div Rem<0=>+Div, R<<1, R0=0 3 Rem = Rem - Div 2 Rem = Rem - Div 4 Rem = Rem - Div Left half of Rem >> 1 0000 1110 1110 1110 0001 1100

slide-57
SLIDE 57

3rd Division Algorithm

Iteration Step Divisor Remainder Initialize values 0010 0000 0111 1 Rem << 1 Rem = Rem - Div Rem<0=>+Div, R<<1, R0=0 3 Rem = Rem - Div 2 Rem = Rem - Div Rem<0=>+Div, R<<1, R0=0 4 Rem = Rem - Div Left half of Rem >> 1 0000 1110 1110 1110 0001 1100 1111 1100 0011 1000

slide-58
SLIDE 58

3rd Division Algorithm

Iteration Step Divisor Remainder Initialize values 0010 0000 0111 1 Rem << 1 Rem = Rem - Div Rem<0=>+Div, R<<1, R0=0 3 Rem = Rem - Div Rem>=0=> R<<1, R0=1 2 Rem = Rem - Div Rem<0=>+Div, R<<1, R0=0 4 Rem = Rem - Div Left half of Rem >> 1 0000 1110 1110 1110 0001 1100 1111 1100 0011 1000 0001 1000 0011 0001

slide-59
SLIDE 59

3rd Division Algorithm

Iteration Step Divisor Remainder Initialize values 0010 0000 0111 1 Rem << 1 Rem = Rem - Div Rem<0=>+Div, R<<1, R0=0 3 Rem = Rem - Div Rem>=0=> R<<1, R0=1 2 Rem = Rem - Div Rem<0=>+Div, R<<1, R0=0 4 Rem = Rem - Div Rem>=0=> R<<1, R0=1 Left half of Rem >> 1 0000 1110 1110 1110 0001 1100 1111 1100 0011 1000 0001 1000 0011 0001 0001 0001 0010 0011 0001 0011