0% found this document useful (0 votes)
4 views20 pages

Managing Multiplication Overflow

Uploaded by

jrshafi69
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views20 pages

Managing Multiplication Overflow

Uploaded by

jrshafi69
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Arithmetic for Computers

Dr.-Ing. Nusrat Jahan Lisa


[Link]@[Link]
Arithmetic for Computers
➢ Operations on integers
• Addition and subtraction
• Multiplication and division
• Dealing with overflow

➢ Floating-point real numbers


• Representation and operations

2
Integer Addition
➢ Example: 7 + 6

■ Overflow if result out of range


■ Adding +ve and –ve operands, no overflow
■ Adding two +ve operands
■ Overflow if result sign is 1
■ Adding two –ve operands
■ Overflow if result sign is 0 3
Integer Subtraction
■ Add negation of second operand
■ Example: 7 – 6 = 7 + (–6)
+7: 0000 0000 … 0000 0111
–6: 1111 1111 … 1111 1010
+1: 0000 0000 … 0000 0001
■ Overflow if result out of range
■ Subtracting two +ve or two –ve operands, no overflow
■ Subtracting +ve from –ve operand
■ Overflow if result sign is 0
■ Subtracting –ve from +ve operand
■ Overflow if result sign is 1
4
Dealing with Overflow
■ C language compiler ignore overflow
■ Use MIPS addu, addui, subu instructions
■ Other languages (e.g., Ada, Fortran) require raising
an exception/interrupt
■ Use MIPS add, addi, sub instructions
■ On overflow, invoke exception/interrupt handler

5
Multiplication

multiplicand
1000
multiplier
× 1001
1000
0000
0000
1000
product 1001000

6
Initially 0
MIPS Multiplication
■ Two 32-bit registers for product
■ HI: most-significant 32 bits
■ LO: least-significant 32-bits
■ Instructions
■ mult $s0, $s1
■ multu $s0, $s1
■ 64-bit product in HI/LO
■ mfhi $s7
■ mflo $s7
■ Move from HI/LO to $s7
Division
MIPS Division
■ Use HI/LO registers for result
■ HI: 32-bit remainder
■ LO: 32-bit quotient

■ Instructions
■ div $s0, $s1
■ divu $s0, $s1
■ No overflow or divide-by-0 checking
■ Software must perform checks if required
■ Use mfhi, mflo to access result
Floating Point
IEEE Floating-Point Format:

Single
precision
(32-bit)

Double
precision
(64-bit)
Floating Point
IEEE Floating-Point Format:

Single
precision
(32-bit)

Single
precision
(32-bit)
Floating Point Addition
Consider a 4-bit binary example
1.0002 × 2–1 + (–1.1102 × 2–2 ) [0.510 + (–0.4375)10]
■ 1. Align binary points
■ Shift number with smaller exponent
■ 1.0002 × 2–1 + (–0.1112 × 2–1)
■ 2. Add significands
■ 1.0002 × 2–1 + (–0.1112 × 2–1) = 0.0012 × 2–1
■ 3. Normalize result & check for over/underflow
■ 1.0002 × 2–4, with no over/underflow
■ 4. Round and renormalize if necessary
■ 1.0002 × 2–4 (no change) = 0.0625
Floating Point Multiplication
Consider a 4-bit binary example
1.0002 × 2–1 × (–1.1102 × 2–2 ) [0.510 × (–0.4375)10]
■ 1. Add exponents
■ Unbiased: –1 + –2 = –3
■ Biased: (–1 + 127) + (–2 + 127) – 127
= –3 + 254 – 127 = –3 + 127
■ 2. Multiply significands
■ 1.0002 × 1.1102 = 1.1102 ⇒ 1.1102 × 2–3
■ 3. Normalize result & check for over/underflow
■ 1.1102 × 2–3 (no change) with no over/underflow
■ 4. Round and renormalize if necessary
■ 1.1102 × 2–3 (no change)
■ 5. Determine sign: +ve × –ve ⇒ –ve
■ –1.1102 × 2–3 = –0.21875
FP Instructions in MIPS

■ Separate FP registers
■ 32 single-precision: $f0, $f1, … $f31
■ Paired for double-precision: $f0/$f1, $f2/$f3, …
■ FP instructions operate only on FP registers
■ Programs generally don’t do integer ops on FP data, or vice versa
■ FP load and store instructions
■ lwc1, ldc1, swc1, sdc1
■ e.g., ldc1 $f8, 32($sp)
FP Instructions in MIPS
■ Single-precision arithmetic
■ add.s, sub.s, mul.s, div.s
■ e.g., add.s $f0, $f1, $f6
■ Double-precision arithmetic
■ add.d, sub.d, mul.d, div.d
■ e.g., mul.d $f4, $f4, $f6
■ Single- and double-precision comparison
■ [Link].s, [Link].d (xx is eq, lt, le, …)
■ Sets or clears FP condition-code bit
■ e.g. [Link].s $f3, $f4
■ Branch on FP condition code true or false
■ bc1t, bc1f
■ e.g., bc1t TargetLabel
FP Instructions in MIPS

You might also like