---
# System prepended metadata

title: Lab7_CS22B036

---

# Lab 7
Name: M V Sonith
Roll No.: CS22B036

---
# Question 1

**Algorithm**

Multiply given number by 2 , if it is greater  than 1, mark 1 in binary format else 0. Do until you get zero
**Steps**
| Integer part | Decimal Part |
| ------------- | ------------- |
x | 0.1|
0 | 0.2|
0 | 0.4|
0 | 0.8|
1 | 0.6|
1 | 0.2|

repetition continues after this
**Answer**
0.1~decimal~ = 0.0001<span style="text-decoration:overline">1001</span>~binary~

---
# Question 2

**Answer**

From above question we know that
0.1~decimal~ = 0.0001<span style="text-decoration:overline">1001</span>~binary~

**Scientific notation is**
0.0001<span style="text-decoration:overline">1001</span>~binary~ = 1.<span style="text-decoration:overline">1001</span> * 2^-4^


signed = 0
exponential = 01111011
mantissa = 10011001100110011001100

**IEEE 754 Format is**
00111101110011001100110011001100

---

# Question 3

**Steps**
| Integer part | Decimal Part |
| ------------- | ------------- |
x | 0.2|
0 | 0.4|
0 | 0.8|
1 | 0.6|
1 | 0.2|

So
0.2~decimal~ = 0.001<span style="text-decoration:overline">1001</span>~binary~

**Scientific notation is**
0.001<span style="text-decoration:overline">1001</span>~binary~ = 1.<span style="text-decoration:overline">1001</span> * 2^-3^

signed = 0
exponential = 01111100
mantissa = 10011001100110011001100
**IEEE 754 Format is**
00111110010011001100110011001100

**Converting Back**
1.10011001100110011001100 x 2^-3^
which is
***Conversion:***
(0.00110011001100110011001100)₂ = (0 × 2⁰) + (0 × 2⁻¹) + (0 × 2⁻²) + (1 × 2⁻³) + (1 × 2⁻⁴) + (0 × 2⁻⁵) + (0 × 2⁻⁶) + (1 × 2⁻⁷) + (1 × 2⁻⁸) + (0 × 2⁻⁹) + (0 × 2⁻¹⁰) + (1 × 2⁻¹¹) + (1 × 2⁻¹²) + (0 × 2⁻¹³) + (0 × 2⁻¹⁴) + (1 × 2⁻¹⁵) + (1 × 2⁻¹⁶) + (0 × 2⁻¹⁷) + (0 × 2⁻¹⁸) + (1 × 2⁻¹⁹) + (1 × 2⁻²⁰) + (0 × 2⁻²¹) + (0 × 2⁻²²) + (1 × 2⁻²³) + (1 × 2⁻²⁴) + (0 × 2⁻²⁵) + (0 × 2⁻²⁶) = (0.19999998807907104492)₁₀

---
# Question 4
**Algorithm**
For a floating point number we can use a representations that is using two registers where one is to store fractional other is to store integer part of number 

As the precision is upto 3 fractional places after the point we can use 10 bits rightmost 10 bits in a register for storing.

To detect overflow we add 24 to fractional part of the result obtained by adding fractional parts of two numbers, because 1000 is then 1024 which is easy to detect overflow as 11th bit in register becomes 1

***So according to Representation***
* 5.391 is stored as
  0000 0000 0000 0000 0000 0000 0000 0101 (Integer part)
  0000 0000 0000 0000 0000 0001 1001 0111 (Fractional Part)
* 3.012 is stored as
  0000 0000 0000 0000 0000 0000 0000 0011 (Integer part)
  0000 0000 0000 0000 0000 0000 0000 1100 (Fractional Part)
***adding them***   
* Integer part is 
  0000 0000 0000 0000 0000 0000 0000 1000
  which is 8
* Fractional part is 
  0000 0000 0000 0000 0000 0001 1001 0011
  which is 403
  adding 24 to fractional part for checking overflow we get
  0000 0000 0000 0000 0000 0001 1010 1011
  in which 11th bit is zero so there is no overflow
  
 **So, the number is 8.403 accoridin to representation**


---
# Question 5
**Code**
```assembly=

.data
sig: .word 0x80000000
pow: .word 0x7F800000
man: .word 0x007FFFFF
num1: .word 0xBF400000 #-0.75
num2: .word 0x3FC00000 #1.5
n1: .word 0x00800000

.text
lw x4 sig
lw x5 pow
lw x6 man
lw x7 num1
lw x8 num2
lw x15 n1

and x9 x7 x4 #s
and x10 x7 x5 #p
and x11 x7 x6 #m

and x12 x8 x4 #s
and x13 x8 x5 #p
and x14 x8 x6 #m

srli x10 x10 23 #getting pow
srli x13 x13 23

add x16 x11 x15 #make number from mantisa
add x17 x14 x15

beq x10 x13 equal #checking powers for equalizing
blt x10 x13 lesser
blt x13 x10 greater
lesser:
    srli x16 x16 1
    addi x10 x10 1
    bne x10 x13 lesser
    j equal
greater:
    srli x17 x17 1
    addi x13 x13 1
    bne x10 x13 greater
    j equal
    
j exit
equal:
    
    xor x18 x12 x9  #checking whether numbers are of same sign
    beq x18 x0 same_sign
    diff_sign:       
        beq x9 x0 num2neg  #determining which number is negative
        num1neg:
           sub x20 x17 x16  #subtracting
           beq x20 x0 zeroc1 # if both are equal answer is zero
           and x21 x20 x4   #checking resultant is negative or positive
           beq  x21 x0 next1 #if negative store only absolute value
           sub x20 x16 x17
           next1:
           lw x22 sig
           srli x22 x22 8
           underflow1:
               and x23 x22 x20  #checking underflow
               beq x23 x0 zerox1 #updating mantisa if underflow
               j nozerox1
               zerox1:
                   addi x10 x10 -1 #updating mantisa and power
                   slli x20 x20 1
                   j underflow1
               nozerox1:
                   slli x10 x10 23  # shifting back power to its place
                   or x31 x21 x10
                   sub x20 x20 x15
                   or x31 x31 x20 #storing result in x31
                   j exit
           zeroc1:
           addi x31 x0 0
           j exit
        num2neg:
           sub x20 x16 x17
           beq x20 x0 zeroc
           and x21 x20 x4
           beq  x21 x0 next
           sub x20 x17 x16
           next:
           lw x22 sig
           srli x22 x22 8
           underflow:
               and x23 x22 x20
               beq x23 x0 zerox
               j nozerox
               zerox:
                   addi x10 x10 -1
                   slli x20 x20 1
                   j underflow
               nozerox:
                   slli x10 x10 23
                   or x31 x21 x10
                   sub x20 x20 x15
                   or x31 x31 x20
                   j exit
           zeroc:
           addi x31 x0 0
           j exit
            
    same_sign:
        add x20 x16 x17
        addi x1 x1 1
        srli x21 x20 24
        beq x1 x21 overflow
        nooverFlow:
        sub x20 x20 x15
        slli x10 x10 23
        or x31 x20 x10
        or x31 x31 x9
        j exit
        overflow:
            addi x10 x10 1
            srli x20 x20 1
            sub x20 x20 x15
            slli x10 x10 23
            or x31 x20 x10
            or x31 x31 x9
            j exit


exit:
     add x0 x0 x0   
```

**Output**
we observe hexadecimal value 0x3f400000 in x31 register which is 0.75 in decimal 

---


