0% found this document useful (0 votes)
23 views9 pages

CC2302 COAL Lab # 05

Uploaded by

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

CC2302 COAL Lab # 05

Uploaded by

Tanzeela Shakeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

University of Management and Technology

Department of Artificial Intelligence


CC2302L Computer Organization and Assembly Language Lab
Spring 2024
Participant ID: Participant Name:

Date: Total Marks: Obtained Marks:

Lab # 05: Arithmetic Overflow in MIPS


1. Learning Objectives
 Define arithmetic overflow and underflow as situations where the result of an arithmetic
operation exceeds the maximum or falls below the minimum value representable by the
data type being used.
 Differentiate between MIPS32 instructions that handle overflow differently, such as add
(add) and add unsigned (addu), sub and subu.
2. Arithmetic Overflow
Arithmetic overflow occurs when the result of an arithmetic operation exceeds the range that can
be represented by the data type being used. This typically happens when performing arithmetic
operations on fixed-size data types, such as integers or floating-point numbers, where there is a
limited range of values that can be represented.
Overflow: This happens when the result of a calculation is larger than the maximum value that
the data type can hold. Imagine adding two very large numbers and the sum exceeding the
container that was supposed to hold it.
Underflow: It occurs when the result of a calculation is smaller than the minimum value that the
data type can represent. Think of subtracting numbers very close to zero until the result becomes
too small to be stored accurately.
MIPS uses standard 32-bit registers for arithmetic operations. These registers can hold values
from -231 to 231 – 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,647is the maximum positive value that can be represented in 32-bit
register.
For example, in integer arithmetic, overflow occurs when the result of an operation exceeds the
range of values that can be represented by the data type.
Example 1: Addition Overflow (Signed Integers)
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)
Example 2: Subtraction underflow (Signed Integers)

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.
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, or when we subtract a positive number from a negative number
and get a positive result.
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.
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:
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.
1.1. 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
1.2. 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

3. MIPS Coprocessor
In MIPS32 architecture, Coprocessor 0 (CP0) and Coprocessor 1 (CP1) are special-purpose
coprocessors used for system control and floating-point operations, respectively.
1.3. Coprocessor 0 (CP0)
 CP0 is primarily responsible for system control and management.
 It handles exceptions, interrupts, and other system-level operations.
 CP0 contains various registers that hold important system state information, such as the
status of interrupts, exception causes, and process context.
 The registers in CP0 are accessed using special coprocessor instructions in MIPS
assembly language.
 Examples of registers in CP0 include the Status register, Cause register etc.
 CP0 is essential for exception handling, virtual memory management, and system-level
control in the MIPS architecture.
1.4. Coprocessor 1 (CP1)
 CP1 is dedicated to floating-point operations.
 It contains registers and instructions for performing floating-point arithmetic and
floating-point data manipulation.
 CP1 is used for operations involving floating-point numbers, such as addition,
subtraction, multiplication, division, and conversion between floating-point and integer
representations.
 Registers in CP1 include floating-point data registers (FPRs), control registers, and status
registers.
 CP1 provides support for single-precision (32-bit) and double-precision (64-bit) floating-
point arithmetic.
 CP1 instructions are used in applications that require high precision or efficiency for
floating-point calculations, such as scientific computing, graphics processing, and signal
processing.
4. Arithmetic Overflow Exception Code
In MIPS architecture, exceptions are events that occur during program execution that disrupt the
normal flow of instructions. MIPS defines several exception codes, which are used to classify
different types of exceptions that can occur. These 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.
Binary representation of 0x00000030: 0000 0000 0000 0000 0000 0000 0011 0000
Bits 6-2: 00110
The binary value 00110 in bits 6-2 represents the arithmetic exception code.
5. Task # 1: Run the following MIPS program and check if the overflow
occurs
.data
num1: .word 2147483647 # Max positive signed integer
num2: .word 10 # Just an arbitrary number

.text
.globl main

main:
# Load the numbers into registers
lw $t0, num1 # Load num1 into $t0
lw $t1, num2 # Load num2 into $t1

# Add the numbers


addu $t2, $t0, $t1 # Perform the addition

li $v0, 1 # System call for print_int


move $a0, $t2 # Move the result to $a0
syscall # Print the result

li $v0, 10 # System call for exit


syscall # Exit the program
1.5. Observations in MARS
1. What is the expected result of addition?

2. What is the result of addition after assembling and running the program?

3. Are there any exceptions raised? If yes, then why?

4. Write the values of following registers after running the program and explain what is the
value?
Name Value What is the value
$at
$v0
$a0
$t0
$t1 0x0000000a Hexadecimal value of num2 which is 10
$t2
pc
6. Task # 2: Run the following MIPS program and check if the overflow
occurs
.data
num1: .word 2147483647 # Max positive signed integer
num2: .word 10 # Just an arbitrary number

.text
.globl main

main:
# Load the numbers into registers
lw $t0, num1 # Load num1 into $t0
lw $t1, num2 # Load num2 into $t1

# Add the numbers


add $t2, $t0, $t1 # Perform the addition

li $v0, 1 # System call for print_int


move $a0, $t2 # Move the result to $a0
syscall # Print the result

li $v0, 10 # System call for exit


syscall # Exit the program
1.6. Observations in MARS
5. What is the expected result of addition?

6. What is the result of addition after assembling and running the program? If any error,
please write.

7. Are there any exceptions raised? If yes, then why?

8. Write the values of following registers after running the program and explain what is the
value?
Name Value What is the value
$at
$v0
$a0
$t0
$t1 0x0000000a Hexadecimal value of num2 which is 10
$t2
pc
9. What is the value of “cause” register?
10. What is the value of program counter pc if the program in terminated with error? Is
the value different than the address of instruction which caused exception?

7. Task # 3: Write a MIPS code which take two values and subtraction them
using subu. Answer all the questions stated in Task # 1.
num1: .word -2147483647 # Min negative signed integer
num2: .word 10 # Just an arbitrary number
1.7. Observations in MARS
1. What is the expected result of subtraction?

2. What is the result of subtraction after assembling and running the program?

3. Are there any exceptions raised? If yes, then why?

4. Write the values of following registers after running the program and explain what is the
value?
Name Value What is the value
$at
$v0
$a0
$t0
$t1 0x0000000a Hexadecimal value of num2 which is 10
$t2
pc

8. Task # 4: Write a MIPS code which take two values and subtraction them
using sub. Answer all the questions stated in Task # 2.
num1: .word -2147483647 # Min negative signed integer
num2: .word 10 # Just an arbitrary number
1.8. Observations in MARS
11. What is the expected result of subtraction?

12. What is the result of subtraction after assembling and running the program? If any error,
please write.

13. Are there any exceptions raised? If yes, then why?


14. Write the values of following registers after running the program and explain what is the
value?
Name Value What is the value
$at
$v0
$a0
$t0
$t1 0x0000000a Hexadecimal value of num2 which is 10
$t2
pc
15. What is the value of “cause” register?

16. What is the value of program counter pc if the program in terminated with error? Is
the value different than the address of instruction which caused exception?

You might also like