0% found this document useful (0 votes)
7 views32 pages

Lecture # 09

Uploaded by

Anoosha Alina
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)
7 views32 pages

Lecture # 09

Uploaded by

Anoosha Alina
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
You are on page 1/ 32

Arithmetic for Computers

Lecture # 09
Computer Organization and Assembly Language
Overflow and Underflow Conditions
• What is Overflow?
• When the result of a calculation is larger than the maximum value that
the data type can hold
• What is Underflow?
• When the result of a calculation is smaller than the minimum value that
the data type can represent
• What is the effect of overflow and underflow on result?
• The result of an operation is too large or too small for the data type
being used to store it.
Overflow and Underflow Conditions
• Let's use an 8-bit integer (which can store values from -128 to
127)
• Overflow Example
• 120 + 10 = 130
• 130 is too large for an 8-bit integer to store
• Now, it will wrap around to the minimum value it can hold, which is -128.
So, instead of 130, the result would be -126
• This is incorrect but a common way for systems to handle overflow

- - 0 1 +12 +12
128 127 6 7
Overflow and Underflow Conditions
• Let's use an 8-bit integer (which can store values from -128 to
127)
• Underflow Example
• -10 - 120 = -130
• -130 is outside the representable range for 8-bit integer
• It will wrap around to the maximum positive value, which is 127
• Thus, instead of -130, the result would be +126
• This is another example of how underflow can lead to inaccurate
results
- - 0 1 +12 +12
128 127 6 7
Overflow and Underflow Conditions
• MIPS uses standard 32-bit registers for arithmetic operations.
• These registers can hold values from -231 to 2 31 – 1 which in
decimal is from -2,147,483,648 to +2,147,483,647.
• In other words, -2,147,483,648 is the minimum negative value
that can be represented in 32-bit register and +2,147,483,647 is
the maximum positive value that can be represented in 32-bit
register
Overflow and Underflow Conditions
• Consider adding two signed 32-bit integers:
• x = 2147483647 (maximum positive 32-bit integer)
• y = 10 (arbitrary number)
• Expected result = x + y = 2147483657
• However, since the maximum positive 32-bit integer is
2147483647, adding 10 to it will result in overflow.
• In a two's complement representation (commonly used for
signed integers), the result will wrap around to the minimum
value:
• result = -2147483639 (overflow occurred)
Overflow and Underflow Conditions
• Consider subtracting two signed 32-bit integers:
• x = -2147483647 (minimum negative 32-bit integer)
• y = 10 (arbitrary number)
• Expected result = x - y = -2147483657
• However, since the minimum negative 32-bit integer is -
2147483648, subtracting 10 to it will result in underflow.
• The result will be 2147483639 as the result will wrap around to
the maximum positive value.
Overflow and Underflow Conditions
• When can overflow occur?
• Overflow occurs when adding two positive numbers and the sum is
negative.
• Overflow occurs in subtraction when we subtract a negative number
from a positive number and get a negative result
• When we subtract a positive number from a negative number and get a
positive result
Overflow and Underflow Conditions
• When does overflow cannot occur?
• When adding operands with different signs, overflow cannot occur
• The reason is the sum must be no larger than one of the operands
• For example, -10+4 =-6. Since the operands fit in 32 bits and the sum
is no larger than an operand, the sum must fit in 32 bits as well.
• Therefore, no overflow can occur when adding positive and negative operands
• In subtraction, when the signs of the operands are the same, overflow
cannot occur.
Overflow and Underflow Conditions
• How MIPS ignore and recognize overflow?
• MIPS has two kinds of arithmetic instructions
1. Add (add), add immediate (addi), and subtract (sub) cause
exceptions on overflow
2. Add unsigned (addu), add immediate unsigned (addiu), and
subtract unsigned (subu) do not cause exceptions on overflow
• In MIPS32 assembly language, add and addu are both
instructions used for addition, but they differ in how they handle
overflow.
Overflow and Underflow Conditions
• add Instruction
• The add instruction stands for "add with overflow."
• It performs signed addition on two numbers.
• If overflow occurs (i.e., the result is too large to be represented with signed numbers), it
triggers an exception.
• This instruction is useful for signed arithmetic operations where you want to ensure that
overflow conditions are caught and handled appropriately.
• Example: add $t0, $t1, $t2
• addu Instruction
• The addu instruction stands for "add without overflow."
• It performs unsigned addition on two numbers.
• Unlike add, it does not check for overflow. Instead, it simply performs the addition and
ignores any overflow that might occur.
• This instruction is useful in scenarios where overflow is not a concern, or where you want
to handle overflow manually.
• Example: addu $t0, $t1, $t2
Overflow and Underflow Conditions
• If arithmetic overflow occurs, what happens in processor?
• Exception occurs
• Wha tis exception?
• Exceptions are events that occur during program execution that disrupt the
normal flow of instructions
• How is exception recognized?
• Exception codes are used to classify different types of exceptions
• Exception codes provide information about the cause of the exception and
help in identifying the appropriate exception handling routine
• For example, in MIPS32 architecture, the value 0x00000030 in the
Cause register indicates that an exception occurred, specifically an
arithmetic overflow.
Floating Point
• Real numbers

• Observation: The last number was bigger than we could represent with a 32-bit
signed integer
• What is scientific notation?
• The alternative notation for the last two numbers is called scientific notation
• It has a single digit to the left of the decimal point
• What is normalized number?
• A number in scientific notation that has no leading 0s is called a normalized number, which
is the usual way to write it.
• For example, 1.0ten x10-9 is in normalized scientific notation, but 0.1ten x 10-8 and10.0 ten
x10-10 are not.
Floating Point
• Can we show binary number in scientific notation?
• Yes. 1.0two x 2-1 and this is called binary point
• Why is the floating point called a floating point?
• Because it represents numbers in which the binary point is not fixed, just as it
is for integers
• What are the advantages of scientific notation in normalized form?
• It simplifies exchange of data that includes floating-point numbers
• It simplifies the floating-point arithmetic algorithms to know that numbers will
always be in this form
• For example, the exponents can be added or subtracted easily. This can streamline
calculations, reduce the chance of errors, and improve computational efficiency.
• It increases the accuracy of the numbers that can be stored in a word
Floating-Point Representation
• In general, floating-point numbers are of the form
(-1)S x F x 2E

where

• s is the sign of the floating-point number (1 meaning negative)


• E is exponent and it is the value of the 8-bit exponent field
(including the sign of the exponent)
• F is fraction and it is the 23-bit number
Floating-Point Representation
• What is the major advantage of these chosen sizes of exponent
and fraction? Such as E 0f 8-bits and F of 23-bits.
• An extraordinary range of number representation. For example,
• Fractions almost as small as 2.0ten x10-38 and numbers as large as 2.0ten x10+38
can be represented in a computer
• Extraordinary differs from infinite, so it is still possible for
numbers to be too large
• Thus, overflow interrupts can occur in floating-point arithmetic
as well as in integer arithmetic
Floating-Point Representation
• What is meant by overflow in floating point?
• Overflow here means that the exponent is too large to be represented
in the exponent field
• What is underflow in floating point?
• Underflow occurs when the negative exponent is too small to fit in the
exponent field
• How to overcome the overflow and underflow in floating point?
• Another format that has a larger exponent and called double precision
Floating-Point Representation
• What is size of double precision floating point?
• 64-bits or two MIPS words

• s is still the sign of the number


• exponent is the value of the 11-bit exponent field
• fraction is the 52-bit number in the fraction field
• Seems like there is mistake in figure while partitioning fraction and exponents bits. E = 11-bits and F = 20-bits. Just drawing error.
Floating-Point Representation
• What is the max and min number represented by double
precision floating point?
• Numbers almost as small as 2.0ten x10-308
• Number almost as large as 2.0ten x 10+308
• What is the primary advantage of double precision other than
max and min number representation?
• Its primary advantage is its greater precision because of the much
larger fraction
• These formats go beyond MIPS. They are part of the IEEE 754
floating-point standard, found in virtually every computer
invented since 1980.
Floating-Point Representation
• Why the fraction part is 23-bits in single precision? Why its not
an even number?
• Because every binary number starts from One and it is generally
understood. That’s why when converting from binary fraction to decimal
fraction, a 1 is added to 23-bit fraction part
• What is advantage of omitting MSB 1 in binary fraction?
• One more bits can be packed into fraction part as MSB is understood
to be 1.
• Hence, the number is actually 24 bits long in single precision
(implied 1 and a 23-bit fraction), and 53 bits long in double
precision (1 + 52)
Floating-Point Representation
• To be precise, we use the term significand to represent the 24-
or 53-bit number that is 1 plus the fraction, and fraction when
we mean the 23- or 52-bit number
• Significand = 1 + Fraction
• Then how ZERO is dealt in hardware?
• Since 0 has no leading 1, it is given the reserved exponent value 0 so
that the hardware won’t attach a leading 1 to it.
Floating-Point Representation
Floating-Point Representation
• Thus 00 … 00two represents 0; the representation of the rest of
the numbers uses the form from before with the hidden 1
added:
(-1)S x (1+F) x 2E
• For example, instead of interrupting on a divide by 0, software
can set the result to a bit pattern representing +∞ or -∞; the
largest exponent is reserved for these special symbols.
• When the programmer prints the results, the program will print
an infinity symbol.
Floating-Point Representation
• IEEE 754 even has a symbol for the result of invalid operations,
such as 0/0 or subtracting infinity from infinity. This symbol is
NaN, for Not a Number.
• The purpose of NaNs is to allow programmers to postpone
some tests and decisions to a later time in the program when
they are convenient.
Floating-Point Representation
• Why have designers chosen the first bits to be sign?
• For sorting such as a quick test of less than, greater than, or equal to 0
using integer comparison instructions
• Why have they chosen exponent to be at second?
• It simplifies the sorting of floating-point numbers using integer
comparison instructions
• Since numbers with bigger exponents look larger than numbers with
smaller exponents, as long as both exponents have the same sign
• Elaboration: Next slide
Floating-Point Representation
• Why have they not chosen 2’s complement to represent
exponent?
• Negative exponents pose a challenge to simplified sorting
• If we use two’s complement or any other notation in which negative
exponents have a 1 in MSB of the exponent field, a negative exponent
will look like a big number
• For example, 1.0twox 2-1 would be represented as

• Remember that the leading 1 is implicit in the significand


Floating-Point Representation
• The value 1.0twox 2+1 would look like the smaller binary number

• Then how to represent most negative and most positive


exponent?
• First, it need to understand that the desirable notation must therefore
represent
• the most negative exponent as 00 … 00two
• the most positive exponent as 11 … 11two
• This convention is called biased notation, with the bias being the number
subtracted from the normal, unsigned representation to determine the real value
• 2(Exponent - 127)
Floating-Point Representation
• What is bias for single precision and double precision?
• For single precision 127
• For double prevision1023
• What is biased exponent of the value 1.0twox 2-1?
• -1+127 = 126 which is 0111 1110 two
• What is biased exponent of the value 1.0twox 2+1?
• 1+127 = 128 which is 1000 0000 two
Floating-Point Representation
• Biased exponent means that the value represented by a
floating-point number is really
• (-1)S x (1 + Fraction) x 2(Exponent - Bias)
• The range of single precision numbers is then from as small as
• ±1.00000000000000000000000two x 2-126
• The range of single precision numbers is then from as large as
• ± 1.11111111111111111111111two x 2+127

You might also like