CMPT 334 Computer Organization Chapter 3 Arithmetic for Computers [Adapted from Computer Organization and Design 5 th Edition, Patterson & Hennessy, © 2014, MK]
2 Multiplication Example Multiplicand 1000 ten Multiplier x 1001 ten Product 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 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
5 Example Multiply 2 ten * 3 ten, or 0010 two * 0011 two
Faster Multiplier Uses multiple adders ▫Cost/performance tradeoff
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 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 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 quotient dividend remainder
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
Initially dividend Initially divisor in left half
12 Divide Example Divide 7 ten ( two ) by 2 ten (0010 two ) IterStepQuotDivisorRemainder 0Initial values
13 Divide Example Divide 7 ten ( two ) by 2 ten (0010 two )
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 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 = div +2 Quo = -3 Rem = div -2 Quo = -3 Rem = 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
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