# Lab 7
Name: M Akash
Roll No.: CS22B037
---
## Question 1
_0.1 to binary_
0.1 * 2 = 0.2 -> 0 (integer part)
0.2 * 2 = 0.4 -> 0
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
0.6 * 2 = 1.2 -> 1
0.2 * 2 = 0.4 -> 0
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
.
.
.
.
So, the binary approximation of 0.1(decimal) is 0.000110011001....
---
## Question 2
_0.1 to binary using IEEE754 Single Precision_
Total Sign bits = 1
Total Exponent bits = 8
Total Mantissa bits = 23
For 0.1, 0.1(decimal) will be represented as 1.100110011001 * 2^(-4)
S = 0 [since positive]
E = 01111011 [since -4+127 = 123]
M = 10011001100110011001100 [after decimal point]
---
## Question 3
_0.2 to binary_
0.2 * 2 = 0.4 -> 0 (integer part)
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
0.6 * 2 = 1.2 -> 1
0.2 * 2 = 0.4 -> 0
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
So, the binary approximation of 0.2(decimal) is 0.00110011001....
_0.2 to binary using IEEE754 Single Precision_
For 0.2, 0.2(decimal) will be represented as 1.100110011001 * 2^(-3)
S = 0 [since positive]
E = 01111100 [since -3+127 = 124]
M = 10011001100110011001100 [after decimal point]
_IEEE754 binary to decimal_
(-1)^S * 1.M * 2^(E-127)
(-1)^0 * 1.10011001100110011001100 * 2^(-3)
So, the binary number is 0.00110011001100110011001100
So, the decimal number is 0.19999998807907104492
---
## Question 4
For floating point representation, we use 3 registers (31 bits each): 1 for sign[S], 1 for number before decimal point[E], 1 for number after decimal point[M].
For adding two floating point numbers, we use 10 registers: S1, E1, M1, S2, E2, M2, Sr, Er, Mr Same as before, but extra register is to store carry bit C (0 or 1)
Convert given decimal to binary-
If S1 = S2 = 0:
++Step 1++: Then add M1 and M2 and store result in Mr, we use half adders for this
++Step 2++: If carry = 1, then C = 1 else C = 0
++Step 3++: Now, add E1, E2, C and store result in Er, we use full adders for this
++Step 4++: Finally, Sr = 0 (since both are +ve)
++Step 5++: So final decimal result is (-1)^Sr * (2)^Er has exponent and (2)^Mr will have Mantissa part of result.
If S1 = 0 and S2 = 1 or vice versa:
++Step 1++: We need comparator for this, if E1 > E2 then Sr = 0 (+ve) and convert E2, M2 to 1's complement form using NOT Gate; else Sr = 1 (-ve) and convert E1, M1 to 1's complement form using NOT Gate; if E1 = E2 then check M1 and M2 and do the same.
++Step 2++: Perform steps 1, 2, 3 from before
++Step 3++: So final decimal result is (-1)^Sr * (2)^Er has exponent and (2)^Mr will have Mantissa part of result.
If S1 = S2 = 1:
++Step 1++: Perform Steps 1,2,3 from before
++Step 2++: Finally Sr = 1 (since both -ve)
++Step 3++: So final decimal result is (-1)^Sr * (2)^Er has exponent and (2)^Mr will have Mantissa part of result.
## Question 5
**Code**
```assembly=
.data
i1_exp: .word 5
i1_mant: .word 391
i1_sign: .word 0
i2_exp: .word 3
i2_mant: .word 102
i2_sign: .word 0
r_exp: .word 10000000
r_mant: .word 10000004
r_sign: .word 10000008
.text
main:
la t0, i1_exp # Load addresses
la t1, i1_mant
la t2, i1_sign
la t3, i2_exp
la t4, i2_mant
la t5, i2_sign
li t6, r_exp
li t7, r_mant
li t8, r_sign
lw a0, 0(t0) # Load values, exp1
lw a1, 0(t1) # mant1
lw a2, 0(t2) # sign1
lw a3, 0(t3)
lw a4, 0(t4)
lw a5, 0(t5)
add a6, a0, a3 # Add exponents
bgez a0, exp1_positive # Check if exponent of first input is positive
slli a1, a1, 1 # Shift mantissa of first input left by 1
addi a0, a0, 1 # Increment exponent of first input
j exp2_positive # Jump to check if exponent of second input is positive
exp1_positive:
beqz a3, exp2_positive # Check if exponent of second input is positive
slli a4, a4, 1 # Shift mantissa of second input left by 1
addi a3, a3, 1 # Increment exponent of second input
exp2_positive:
beqz a0, store_r # If exponent of first input is zero, skip normalization
beqz a3, store_r # If exponent of second input is zero, skip normalization
blt a0, a3, n_mant1 # If exponent of first input is less than exponent of second input, normalize mantissa of first input
blt a3, a0, n_mant2 # If exponent of second input is less than exponent of first input, normalize mantissa of second input
j store_r
n_mant1:
sub a6, a0, a3 # Calculate difference in exponents
sll a4, a4, a6 # Shift mantissa of second input left by difference in exponents
addi a3, a3, a6 # Increment exponent of second input
j store_r
n_mant2:
sub a6, a3, a0 # Calculate difference in exponents
sll a1, a1, a6 # Shift mantissa of first input left by difference in exponents
addi a0, a0, a6 # Increment exponent of first input
j store_r
store_r:
sw a6, 0(t6)
sw a1, 0(t7)
sw a2, 0(t8)
li a7, 10
ecall
```
**Output**
t6 register has Exponent of result, that is, 10000010 [130]
t7 register has Mantissa of result, that is, 00001101101101101101101
t8 register has Sign of result, that is, 0
---