CMPS290 Class Notes Chap 03
CMPS290 Class Notes Chap 03
• Operations on integers
o Addition and subtraction
o Multiplication and division
o Dealing with overflow
• Floating-point real numbers
o Representation and operations x
o Figure 3.1 shows the sums and carries. The carries are shown in parentheses, with
the arrows showing how they are passed.
FIGURE 3.1 Binary addition, showing carries from right to left. The rightmost bit adds 1 to 0,
resulting in the sum of this bit being 1 and the carry out from this bit being 0. Hence, the operation for
the second digit to the right is 0 + 1 + 1. This generates a 0 for this sum bit and a carry out of 1. The
third digit is the sum of 1 + 1 + 1, resulting in a carry out of 1 and a sum bit of 1. The fourth bit is 1 + 0
+ 0, yielding a 1 sum and no carry.
FIGURE 3.3 First version of the multiplication hardware. The Multiplicand register, ALU, and
Product register are all 64 bits wide, with only the Multiplier register containing 32 bits. (Appendix B
describes ALUs.) The 32-bit multiplicand starts in the right half of the Multiplicand register and is
shifted left 1 bit on each step. The multiplier is shifted in the opposite direction at each step. The
algorithm starts with the product initialized to 0. Control decides when to shift the Multiplicand and
Multiplier registers and when to write new values into the Product register.
• Example: (A multiply Algorithm) Use 4-bit numbers to multiple 210 X 310, or 00102 X
00112
FIGURE 3.5 Multiply example using algorithm in Figure 3.4. The bit examined to determine the next
step is circled in color.
Faster Multiplication
• Faster multiplications are possible by essentially providing one 32-bit adder for each
bit of the multiplier: one input is the multiplicand ANDed with a multiplier bit, and
the other is the output of a prior adder.
• Uses multiple adders: Cost / Performance tradeoff
FIGURE 3.7 Fast multiplication hardware. Rather than use a single 32-bit adder 31 times, this
hardware “unrolls the loop” to use 31 adders and then organizes them to minimize delay.
Multiply in MIPS
• MIPS provides a separate pair of 32-bit register to contain the 64-bit product, called
Hi and Lo.
o HI: most-significant 32 bits
o LO: least-significant 32-bits
• MIPS Instructions
o mult rs, rt / multu rs, rt
▪ 64-bit product in HI / LO
o mfhi rd / mflo rd
▪ Move from HI / LO to rd
▪ Can test HI value to see if product overflows 32 bits
Summary
• Multiplication hardware simply shifts and adds.
• Compiler even use shift instructions for multiplication by powers of 2.
• With much more hardware we can do the adds in parallel, and do them much faster.
• Divide’s two operands, called the dividend and divisor, and result, called the quotient,
are accompanied by a second result, called the remainder.
• Here is another way to express the relationship between the components:
Dividend = Quotient X Divisor + Remainder
FIGURE 3.8 First version of the division hardware. The Divisor register, ALU, and Remainder
register are all 64 bits wide, with only the Quotient register being 32 bits. The 32-bit divisor starts in the
left half of the Divisor register and is shifted right 1 bit each iteration. The remainder is initialized with
the dividend. Control decides when to shift the Divisor and Quotient registers and when to write the
new value into the Remainder register.
• Example: (A Divide Algorithm) Using a 4-bit version of the algorithm to save pages,
let’s try dividing 7 by 2 or 0000 0111 by 0010
FIGURE 3.10 Division example using the algorithm in Figure 3.9. The bit examined to determine the
next step is circled in color.
Divide in MIPS
• MIPS provides a separate pair of 32-bit Hi and 32-bit Lo registers for both multiply
and divide and.
o Hi: 32-bit remainder
o Lo: 32-bit quotient
• MIPS Instructions
o div rs, rt / divu rs, rt
▪ 64-bit product in HI / LO
o mfhi rd / mflo rd
▪ Move from HI / LO to rd
Summary
• The common hardware support for multiply and divide allow MIPS to provide a
single pair of 32-bit registers (Hi and Lo) that are used both for multiply and divide.
FIGURE 3.12 MIPS core architecture. The memory and registers of the MIPS architecture are not
included for space reasons, but this section added the Hi and Lo registers to support multiply and divide.
MIPS machine language is listed in the MIPS Reference Data Card at the front of this book.
±1.xxxxxxx2 × 2yyyy
Floating-Point Representation
• Figure 3.13 shows IEEE 754 encoding of single and double precision numbers
FIGURE 3.13 IEEE 754 encoding of floating-point numbers. A separate sign bit determines the sign.
Denormalized numbers are described in the Elaboration on page 232. This information is also found in
Column 4 of the MIPS Reference Data Card at the front of this book.
• Example: (Binary Floating-Point Addition) Add the number 0.5 and -04375 in binary.
o Now consider a 4-digit binary example
▪ 1.0002 × 2–1 + –1.1102 × 2–2 (0.5 + –0.4375)
o 1. Align binary points
▪ Shift the smaller number to right until its exponent would match the larger
exponent
▪ 1.0002 × 2–1 + –0.1112 × 2–1
o 2. Add significands
▪ 1.0002 × 2–1 + –0.1112 × 2–1 = 0.0012 × 2–1
o 3. Normalize result & check for over/underflow
▪ 1.0002 × 2–4, with no overflow / underflow
o 4. Round and renormalize if necessary
▪ 1.0002 × 2–4 (no change) = 0.0625
FIGURE 3.14 Floating-point addition. The normal path is to execute steps 3 and 4 once, but if
rounding causes the sum to be unnormalized, we must repeat step 3.
• Example: (Binary Floating-Point Multiplication) Multiply the number 0.5 and -04375
in binary.
o Now consider a 4-digit binary example
▪ 1.0002 × 2–1 × –1.1102 × 2–2 (0.5 × –0.4375)
o 1. Add exponents
▪ Unbiased: –1 + –2 = –3
▪ Biased: (–1 + 127) + (–2 + 127) = –3 + 254 – 127 = –3 + 127 = 124
o 2. Multiply significands
▪ 1.0002 × 1.1102 = 1.1102 1.1102 × 2–3
o 3. Normalize result & check for overflow / underflow
▪ 1.1102 × 2–3 (no change) with no overflow /underflow
o 4. Round and renormalize if necessary
▪ 1.1102 × 2–3 (no change)
o 5. Determine sign: positive × negative negative
▪ –1.1102 × 2–3 = –0.21875
FIGURE 3.16 Floating-point multiplication. The normal path is to execute steps 3 and 4 once, but if
rounding causes the sum to be unnormalized, we must repeat step 3.
• MIPS supports the IEEE 754 single precision and double precision formats with these
instructions:
o Floating-point addition: single (add.s) and double (add.d)
▪ e.g., add.s $f0, $f4, $f6 # $f2 = $f4 + $f6
o Floating-point subtraction: single (sub.s) and double (sub.d)
▪ e.g., sub.d $f2, $f4, $f6 # $f2 = $f4 - $f6
o Floating-point multiplication: single (mul.s) and double (mul.d)
▪ e.g., mul.s $f2, $f4, $f6 # $f2 = $f4 X $f6
o Floating-point division: single (div.s) and double (div.d)
▪ e.g., div.d $f2, $f4, $f6 # $f2 = $f4 / $f6
o Floating-point comparison: single (c.x.s) and double (c.x.d)
Where x may be equal (eq), not equal (neq),
less than (lt), less than or equal (le),
greater than (gt), greater than or equal (qe)
▪ e.g., c.lt.s $f2, $f4 # if ($f2 < $f4) cond = 1; else cond = 0
o Floating-point branch: true (bclt) and false (bclf)
▪ e.g., bclt 25 # if (cond == 1) go to PC + 4 + 100
Summary
FIGURE 3.24 The MIPS instruction set. This book concentrates on the instructions in the left column.
This information is also found in columns 1 and 2 of the MIPS Reference Data Card at the front of this
book.