Lecture # 09
Lecture # 09
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