Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPT 334 Computer Organization Chapter 3 Arithmetic for Computers [Adapted from Computer Organization and Design 5 th Edition, Patterson & Hennessy, ©

Similar presentations


Presentation on theme: "CMPT 334 Computer Organization Chapter 3 Arithmetic for Computers [Adapted from Computer Organization and Design 5 th Edition, Patterson & Hennessy, ©"— Presentation transcript:

1 CMPT 334 Computer Organization Chapter 3 Arithmetic for Computers [Adapted from Computer Organization and Design 5 th Edition, Patterson & Hennessy, © 2014, MK]

2 2 Multiplication Example Multiplicand 1000 ten Multiplier x 1001 ten --------------- 1000 0000 1000 ---------------- Product 1001000 ten 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 Length of product is the sum of operand lengths

3 3 Multiplication Algorithm 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

4

5 5 Example Multiply 2 ten * 3 ten, or 0010 two * 0011 two

6 Faster Multiplier Uses multiple adders ▫Cost/performance tradeoff

7 7 Signed Multiplication The previous algorithm also works for signed numbers (negative numbers in 2’s complement form) We can also convert negative numbers to positive, multiply the magnitudes, and convert to negative if signs disagree

8 8 MIPS Instructions mult $s2, $s3 computes the product and stores it in two “internal” registers that can be referred to as hi and lo mfhi $s0 moves the value in hi into $s0 mflo $s1 moves the value in lo into $s1 Similarly for multu The product of two 32-bit numbers can be a 64-bit number HI: most-significant 32 bits LO: least-significant 32-bits mul rd, rs, rt Least-significant 32 bits of product –> rd

9 9 Division 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 1001 1000 1001010 -1000 10 101 1010 -1000 10 quotient dividend remainder

10 10 Hardware for Division A comparison requires a subtract; the sign of the result is examined; if the result is negative, the divisor must be added back Initially dividend Initially divisor in left half

11 Initially dividend Initially divisor in left half

12 12 Divide Example Divide 7 ten (0000 0111 two ) by 2 ten (0010 two ) IterStepQuotDivisorRemainder 0Initial values 1 2 3 4 5

13 13 Divide Example Divide 7 ten (0000 0111 two ) by 2 ten (0010 two )

14 14 Divisions involving Negatives Simplest solution: convert to positive and adjust sign later Note that multiple solutions exist for the equation: Dividend = Quotient x Divisor + Remainder +7 div +2 Quo = Rem = -7 div +2 Quo = Rem = +7 div -2 Quo = Rem = -7 div -2 Quo = Rem =

15 15 Divisions involving Negatives Simplest solution: convert to positive and adjust sign later Note that multiple solutions exist for the equation: Dividend = Quotient x Divisor + Remainder +7 div +2 Quo = +3 Rem = +1 -7 div +2 Quo = -3 Rem = -1 +7 div -2 Quo = -3 Rem = +1 -7 div -2 Quo = +3 Rem = -1 Convention: Dividend and remainder have the same sign Quotient is negative if signs disagree These rules fulfil the equation above

16 MIPS Division Use HI/LO registers for result ▫HI: 32-bit remainder ▫LO: 32-bit quotient Instructions ▫ div rs, rt / divu rs, rt ▫Use mfhi, mflo to access result


Download ppt "CMPT 334 Computer Organization Chapter 3 Arithmetic for Computers [Adapted from Computer Organization and Design 5 th Edition, Patterson & Hennessy, ©"

Similar presentations


Ads by Google