Module 4 [02-12-2024]
Module 4 [02-12-2024]
Topic – 4:
Arithmetic for Computers
Course Teacher:
Dr. Md. Tarek Habib
Assistant Professor
Department of Computer Science and Engineering
Reference Book
2
Topic Contents
• Introduction [Sec. – 3.1]
3
The Five Classic
Components of A Computer
4
• Section 3.1:
Introduction
5
Arithmetic
■ Where we've been:
■ evolution
■ performance
■ abstractions
■ instruction set architecture
■ assembly language and machine language
■ What's up ahead:
■ implementing the architecture
operation
32 ALU
result
32
b
32
6
Numbers
■ Bits are just bits (no inherent meaning)
■ conventions define the relationship between bits and numbers
■ Binary integers (base 2)
■ 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001...
n
n bits
■ decimal: 0, …, 2 -1
■ How do we represent negative integers?
■ which bit patterns will represent which integers?
7
• Section 3.X:
8
Signed Numbers
■ Representations
■ Sign-magnitude
■ Two’s complement
9
Signed Numbers…
10
• Section 3.2:
11
Binary Addition and Subtraction
12
Binary Addition and Subtraction…
13
Overflow
■ Overflow occurs when the result from an operation
cannot be represented with the available hardware,
in this case (MIPS32) a 32-bit word.
■ When adding operands with different signs,
overflow cannot occur.
16
Overflow…
■ What is about the overflow with unsigned integers?
■ Unsigned integers are commonly used for memory
addresses where overflows are ignored.
■ The computer designer must therefore provide a way
to ignore overflow in some cases and to recognize it in
others.
■ The MIPS solution is to have two kinds of arithmetic
instructions to recognize the two choices:
17
Overflow…
■ The MIPS solution is to have two kinds of arithmetic
instructions to recognize the two choices:
■ Add (add), add immediate (addi), and subtract (sub)
cause exceptions on overflow.
■
18
Overflow…
■ Because C ignores overflows, the MIPS C compilers
will always generate the unsigned versions of the
arithmetic instructions addu, addiu, and subu, no
matter what the type of the variables.
21
ALU…
22
23
• Section 3.3:
Multiplication
24
Operands in Multiplication
25
Operands in Multiplication…
■ The first observation is that the number of digits in the product
is considerably larger than the number in either the
multiplicand or the multiplier.
■ In fact, if we ignore the sign bits, the length of the
multiplication of an n-bit multiplicand and an m-bit multiplier is
a product that is n + m bits long.
■ That is, n + m bits are required to represent all possible
products.
■ Hence, like add, multiply must cope with overflow because we
frequently want a 32-bit product as the result of multiplying
two 32-bit numbers.
26
The First
Multiplication
Algorithm
■ The requirement
of this algorithm
is that it takes n
steps to get the
proper product.
27
The Hardware for the First
Multiplication Algorithm
28
29
Signed Multiplication
■ So far, we have dealt with positive numbers.
■ The easiest way to understand how to deal with
signed numbers is to first convert the multiplier and
multiplicand to positive numbers and then remember
the original signs.
■ The algorithms should then be run for 31 iterations,
leaving the signs out of the calculation.
■ We need negate the product only if the original signs
disagree. 30
Multiplication in MIPS
■ MIPS provides a separate pair of 32-bit registers to
contain the 64-bit product, called Hi and Lo.
■ To produce a properly signed or unsigned product,
MIPS has two instructions:
▪ multiply (mult)
and
▪ multiply unsigned (multu)
31
Multiplication in MIPS…
■ To fetch the integer 32-bit product, the programmer
uses move from lo (mflo).
■ The MIPS assembler generates a pseudoinstruction
for multiply that specifies three general-purpose
registers, generating mflo and mfhi instructions to
place the product into registers.
32
Multiplication in MIPS…
■ The following code fragment which multiplies the
value in $t2 by the value in $t1, and stores the
result in $t0.
mult $t2, $t1
mflo $t0
33
Multiplication in MIPS…
■ Example:
Let the variables i, j, m, and n be assigned to the registers
$s1, $s2, $s3, and $s4, respectively. What is the
compiled MIPS code for the following code fragment?
int i, j, m, n;
m = 2;
n = 1;
j = i % 2;
if (j == 0)
m = m * i;
else
n = n * i;
34
Multiplication in MIPS…
■ Slon:
addi $s3, $zero, 2
addi $s4, $zero, 1
div $s1, $s3
mfhi $s2
bne $s2, $zero, L1
mult $s3, $s1
mflo $s3
j L2
L1:
mult $s4, $s1
mflo $s4
L2:
… 35
• Section 3.4:
Division
36
Operands in Division
■ The reciprocal operation of multiply is divide, an
operation that is even less frequent and even more
quirky.
■ It even offers the opportunity to perform a
mathematically invalid operation: dividing by 0.
37
Operands in Division…
38
A Division Algorithm
■ Let’s assume that both the dividend and the divisor
are positive and hence the quotient and the
remainder are nonnegative.
■ The division operands and both results are 32-bit
values, and we will ignore the sign for now.
39
A Division
Algorithm…
■ The surprising
requirement of
this algorithm is
that it takes n
+ 1 steps to
get the proper
quotient and
remainder!
40
The Hardware for the Division
Algorithm
41
42
Signed Division
■ So far, we have ignored signed numbers in the
division.
■ The simplest solution is to remember the signs of the
divisor and dividend and then negate the quotient if
the signs disagree.
■ Thus the correctly signed division algorithm negates
the quotient if the signs of the operands are opposite
and makes the sign of the nonzero remainder match
the dividend.
43
44
45
Division in MIPS
■ MIPS uses the 32-bit Hi and 32-bit Lo registers for
both multiply and divide.
■ Hi contains the remainder, and Lo contains the
quotient after the divide instruction completes.
47
Division in MIPS…
■ Example:
Let the variables i and j be assigned to the registers $s0 and
$s1, respectively. What is the compiled MIPS code for the
following code fragment?
int i, j;
j = i % 2;
if (j == 0)
i = i + 2;
else
i = i + 1;
48
Division in MIPS…
■ Slon:
addi $t0, $zero, 2
div $s0, $t0
mfhi $s1
bne $s1, $zero, L1
addi $s0, $s0, 2
j L2
L1:
addi $s0, $s0, 1
L2:
…
49
50
• Section 3.5:
Floating Point
51
Floating-Point Representation
■ We need a way to represent
■ numbers with fractions, e.g., 3.1416
■ very small numbers (in absolute value), e.g., .00000000023
■ very large numbers (in absolute value) , e.g., –3.15576 × 1046
■ Representation:
■ Scientific : sign, exponent, significand form: binary
point
■ (–1)Sign × Significand × 2Exponent . E.g., –101.001101 × 2111001
■ more bits for significand gives more accuracy
■ more bits for exponent increases range
■ if 1 ≤ significand < 10two(= 2ten) then number is normalized , except
for number 0 which is normalized to significand 0
■ E.g., –101.001101 × 2111001 = –1.01001101 × 2111011 (normalized)
52
Floating-Point Representation…
■ General form of floating-point numbers in MIPS:
■ single precision: one word
53
Floating-Point Representation…
■ Example: Represent 1.0two × 2-1 in single precision.
54
Floating-Point Representation…
■ These formats go beyond MIPS…
■ They are part of the IEEE 754 floating-point standard
■ found in virtually every computer invented since 1980
■ has greatly improved both the ease of porting floating-point
programs and the quality of computer arithmetic
55
IEEE 754 Floating-Point Standard
■ IEEE 754 floating point standard:
■ single precision: one word
31 bits 30 to bits 22 to
23 0
sign 8-bit 23-bit
exponent significand
■ double precision: two words
31 bits 30 to bits 19 to
20 0
sign 11-bit exponent upper 20 bits of 52-bit significand
bits 31 to
0
lower 32 bits of 52-bit significand
56
IEEE 754 Floating-Point Standard…
■ Sign bit is 0 for positive numbers, 1 for negative numbers
57
IEEE 754 Floating-Point Standard…
■ Special treatment of 0:
■ if exponent is all 0 and significand is all 0, then the value is
0 (sign bit may be 0 or 1)
■ if exponent is all 0 and significand is not all 0, then the value is
(–1)sign * (1 + significand) * 2-127
■ therefore, all 0s is taken to be 0 and not 2-127 (as would be for a non-zero
normalized number); similarly, 1 followed by all 0’s is taken to be 0 and not
- 2-127
59
Floating Point
Addition
■ Hardware:
60
Floating Point
Multpication
■ Algorithm:
61
Floating Point Complexities
■ In addition to overflow we can have underflow (number too
small)
■ Accuracy is the problem with both overflow and underflow
because we have only a finite number of bits to represent
numbers that may actually require arbitrarily many bits
■ limited precision ⇒ rounding ⇒ rounding error
■ IEEE 754 keeps two extra bits, guard and round
■ four rounding modes
■ positive divided by zero yields infinity
■ zero divide by zero yields not a number
■ other complexities
■ Implementing the standard can be tricky
■ Not implementing the standard can be even worse
■ see text for discussion of Pentium bug!
62
MIPS Floating Point
■ MIPS has a floating point coprocessor (numbered 1, SPIM) with
thirty-two 32-bit registers $f0 - $f31. Two of these are required
to hold doubles. Floating point instructions must use only
even-numbered registers (including those operating on single
floats). SPIM simulates MIPS floating point.
■ Other instructions…
63