# Lab 7
Name: SUDDULA VINEETH RAGHAVENDRA
Roll No.: CS22B045
---
## Question 1
**Observation :**
the binary representation of (0.1)10 is : (.0001100110011001100110011.....)2
| Present Value | Integer Value | Decimal part |
| -------- | -------- | -------- |
| 0.1 *2 = 0.2 | 0 | 0.2 |
| 0.2 *2 = 0.4 | 0 | 0.4 |
| 0.4 *2 = 0.8 | 0 | 0.8 |
| 0.8 *2 = 1.6 | 1 | 0.6 |
| 0.6 *2 = 1.2 | 1 | 0.2 |
| 0.2 *2 = 0.4 | 0 | 0.4 |
| 0.4 *2 = 0.8 | 0 | 0.8 |
| 0.8 *2 = 1.6 | 1 | 0.6 |
| 0.6 *2 = 1.2 | 1 | 0.2 |
..... it will be continuing from here in the same above pattern.
---
## Question 2
first we have to get first 24 bits of the binary form to get the IEEE 754 standard form.
So, (0.1)10 in base 2 is : (0.0001100110011001100110011)2
Normalize the binary representation by moving the binary point to the left and adjusting the exponent accordingly:
0.00011001100110011001100 → 1.10011001100110011001100 × 2^(-4)
From the given 24 bits encode the sign, exponent, and fraction according to the IEEE 754 format:
Sign bit= 0 (because 0.1 is positive)
8 bit Exponent= 127 - 4 = 123 (in binary: 01111011)
23 bit mantisaa = 10011001100110011001100
**Observation :**
IEEE format = sign bit + 8 bit exponent + 23 bit mantisaa
IEEE Format : 00111101110011001100110011001100
---
## Question 3
**Observation :**
the binary representation of (0.2)10 is : (.001100110011001100110011.....)2
| Present Value | Integer Value | Decimal part |
| -------- | -------- | -------- |
| 0.1 *2 = 0.2 | 0 | 0.2 |
| 0.2 *2 = 0.4 | 0 | 0.4 |
| 0.4 *2 = 0.8 | 0 | 0.8 |
| 0.8 *2 = 1.6 | 1 | 0.6 |
| 0.6 *2 = 1.2 | 1 | 0.2 |
| 0.2 *2 = 0.4 | 0 | 0.4 |
| 0.4 *2 = 0.8 | 0 | 0.8 |
| 0.8 *2 = 1.6 | 1 | 0.6 |
| 0.6 *2 = 1.2 | 1 | 0.2 |
..... it will be continuing from here in the same above pattern.
first we have to get first 24 bits of the binary form to get the IEEE 754 standard form.
So, (0.2)10 in base 2 is : (0.001100110011001100110011)2
Normalize the binary representation by moving the binary point to the left and adjusting the exponent accordingly:
0.0011001100110011001100 → 1.10011001100110011001100 × 2^(-3)
From the given 24 bits encode the sign, exponent, and fraction according to the IEEE 754 format:
Sign bit= 0 (because 0.2 is positive)
8 bit Exponent= 127 - 3 = 124 (in binary: 1111100)
23 bit mantisaa = 10011001100110011001100
**Observation :**
IEEE format = sign bit + 8 bit exponent + 23 bit mantisaa
IEEE Format : 00111110010011001100110011001100
***converting back to decimal***
First we have to divide it into parts as sign bit, 8 bit exponent and 23 bit mantisaa
0 01111100 10011001100110011001100
sign = (-1)^0 = 0
exponent = 124-127 = -3
mantisa = 10011001100110011001100
Scientific form = (1.10011001100110011001100) * 2^-3
Final binary form = (0.00110011001100110011001100)2
Now convert this binary into the decimal
2^-3 +2^-4 +2^-7 +2^-8 +2^-11 +2^-12 +2^-15 +2^-16 +2^-19 +2^-20 +2^-23 +2^-24 = 0.2(approximately)
---
## Question 4
We can store the integer part and the fractional part separately by using multiple registers or memory locations to represent each part. For simplicity, let's use two registers to store each floating-point number, one for the integer part and one for the fractional part.
Now initialize the registers with the integer and fractional parts of the two numbers.
Add the fractional parts together, taking care of any overflow.
If the sum of the fractional parts exceeds 1, increment the integer part and adjust the fractional part accordingly.
---
## Question 5
Source-(internet)
**Observation:**
```
.data
# Define the two floating-point numbers in IEEE 754 format
number1: .word 1096328704 # 5.391 in IEEE 754 format (0x40A4CCCC in hexadecimal format)
number2: .word 1076664269 # 3.012 in IEEE 754 format (0x40C3D70A in hexadecimal format)
# Define a memory location to store the result
result: .word 0 # Initialize to 0
.text
# Load the two floating-point numbers from memory
flw f0, number1
flw f1, number2
# Get the IEEE 754 representation of the two floating-point numbers
lui t0, %hi(number1) # Load upper immediate: t0 = address of number1
flw f2, %lo(number1)(t0) # Load the contents of number1 into f2
lui t1, %hi(number2) # Load upper immediate: t1 = address of number2
flw f3, %lo(number2)(t1) # Load the contents of number2 into f3
# Extract the sign, exponent, and fraction of the first floating-point number
andi t2, t0, 0x80000000 # Extract the sign bit
srl t3, t0, 23 # Extract the exponent (shifted to the right)
andi t4, t0, 0x007FFFFF # Extract the fraction
# Extract the sign, exponent, and fraction of the second floating-point number
andi t5, t1, 0x80000000 # Extract the sign bit
srl t6, t1, 23 # Extract the exponent (shifted to the right)
andi t7, t1, 0x007FFFFF # Extract the fraction
# Perform addition of the exponent
sub t8, t3, t6 # Subtract the exponents
bgez t8, skip_shift_left # If t8 >= 0, skip shifting the fraction of f3 left
sll t7, t7, t8 # Shift left the fraction of f3 by t8 bits
j after_shift_left
skip_shift_left:
sll t4, t4, -t8 # Shift left the fraction of f2 by -t8 bits
after_shift_left:
bne t2, t5, different_signs # If signs are different, skip sign correction
# Signs are the same, perform addition of the fractions
add t9, t4, t7 # Add the fractions
j store_result
different_signs:
bne t3, t6, finish_addition # If exponents are different, skip fraction addition
# Exponents are the same, perform subtraction of the fractions
sub t9, t4, t7 # Subtract the fractions
j store_result
finish_addition:
# Restore the original exponent and fraction of the number with the larger exponent
bgez t8, restore_exponent_f2 # If t8 >= 0, restore exponent and fraction of f2
li t9, 1 # Set the most significant bit (hidden bit) of the result fraction
j store_result
restore_exponent_f2:
addi t3, t3, 1 # Increment the exponent of f2
li t9, 0 # Clear the most significant bit (hidden bit) of the result fraction
store_result:
# Combine the sign, exponent, and fraction to form the IEEE 754 representation of the result
sll t10, t9, 31 # Shift the sign bit to its position
sll t11, t3, 23 # Shift the exponent to its position
or t12, t10, t11 # Combine sign and exponent
or t13, t12, t9 # Combine with the fraction
# Store the result in the memory location
sw t13, result
# Exit the program
li a7, 10 # Exit syscall
ecall
```
---