# Lab 7
Name : Bilwani K
Roll No. : CS22B013
---
## Question 1
To convert 0.1 to binary, we can follow the method of multiplying by 2 and noting the integer part of the result:
```assembly=
0.1 * 2 = 0.2 -> 0
0.2 * 2 = 0.4 -> 0
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
```
So, the binary representation of 0.1 is 0.0001100110011... (repeating).
## Question 2
The IEEE 754 standard for floating-point representation involves expressing the number in binary scientific notation (mantissa and exponent).
sign bit of 0.1 is 0
now it's exponent in scientific form is -4 so it's exponent is 123 since it's bias for precesion is 127
now 0.1/0.0625 is equal to 1.6
```
0.1 = 1.6 * 10^(-1) = 1.6 * 2^(-4) = 0.0001100110011... * 2^(-4)
```
For the normalized form, we shift the binary point to the right and adjust the exponent accordingly:
```
0.0001100110011... * 2^(-4) = 1.100110011... * 2^(-8)
```
In IEEE 754 single precision format (32 bits):
Sign bit: 0 (positive)
Exponent: Bias + (-8) = 127 - 8 = 119 (in binary, 01110111)
Mantissa: The fractional part without the leading 1, truncated to fit in 23 bits
So, the IEEE 754 representation of 0.1 would be:
```
0 01110111 10011001100110011001101
```
## Question 3
```
0.2 * 2 = 0.4 -> 0
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
```
So, the binary representation of 0.2 is 0.001100110011... (repeating).
In IEEE 754 single precision format:
```
0.2 = 1.6 * 2^(-3) = 0.001100110011... * 2^(-3)
```
Normalized form:
```
0.001100110011... * 2^(-3) = 1.100110011... * 2^(-6)
```
In IEEE 754 format:
Sign bit: 0 (positive)
Exponent: Bias + (-6) = 127 - 6 = 121 (in binary, 01111001)
Mantissa: The fractional part without the leading 1, truncated to fit in 23 bits
So, the IEEE 754 representation of 0.2 would be:
```
0 01111001 10011001100110011001101
```
To convert it back to decimal, we reverse the process:
```
(1) * 2^(-3) = 0.125
(1) * 2^(-4) = 0.0625
(0) * 2^(-5) = 0
(0) * 2^(-6) = 0
(1) * 2^(-7) = 0.0078125
(1) * 2^(-8) = 0.00390625
```
Adding these together, we get approximately 0.19921875.
So, converting 0.2 to binary using IEEE 754 and then back to decimal gives approximately 0.19921875.
## Question 4
To represent floating-point numbers and perform addition, let's use a simple representation. one for the integer part and one for the fractional part. We'll store the numbers as fixed-point numbers with a specified number of bits for the fractional part.
For example, let's say we use 16 bits for the integer part and 16 bits for the fractional part.
Custom Floating Point Representation:
Integer part: 16 bits
Fractional part: 16 bits
Addition Algorithm:
Add the integer parts of both numbers.
Add the fractional parts of both numbers.
If the sum of the fractional parts exceeds 1, carry over to the integer part.
Store the result.
Let's perform the addition of 5.391 and 3.012 using this custom representation:
5.391 => Integer part: 5, Fractional part: 0.391 * (2^16) = 25589 (approximately)
3.012 => Integer part: 3, Fractional part: 0.012 * (2^16) = 786 (approximately)
Addition:
Integer part: 5 + 3 = 8
Fractional part: 25589 + 786 = 26375
Result:
Integer part: 8
Fractional part: 26375 (approximately 0.403167724609375)
So, the result of adding 5.391 and 3.012 using this custom representation would be approximately 8.403167724609375.
## Question 5
```.section .data
# Define the two floating-point numbers in IEEE 754 format
num1: .float 3.14159 # Example: 3.14159
num2: .float -3.14159 # Example: -3.14159
# Define the memory location to store the result
result: .float 0.0 # Initialize to 0.0
.section .text
.globl _start
_start:
# Load the address of num1 into a register
la t0, num1
# Load the address of num2 into another register
la t1, num2
# Load the first floating-point number into a floating-point register (f0)
flw f0, 0(t0)
# Load the second floating-point number into another floating-point register (f1)
flw f1, 0(t1)
# Perform floating-point addition
fadd.s f2, f0, f1
# Store the result in the memory location
la t2, result
fsw f2, 0(t2)
# Exit the program
li a0, 0 # Exit status
li a7, 93 # Exit system call number
ecall # Invoke system call
```