---
# System prepended metadata

title: Lab7_CS22B057

---

# Lab 7
Name: SHIVADHARSHAN S
Roll No.: CS22B057

---
## Question 1

(0.1)~10~  --> (0.00011(0011)~recurring~)~2~

Conversion algorithm
| Multiplication | value |
| -------- | -------- | 
|0.1 * 2 = 0.2  | 0|
|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|
|0.6 * 2 = 1.2  | 1|
goes on like this...

___

## Question 2

(0.1)~10~ in IEEE 745 format

First convert to binary format

0.1 -> (0.0001100110011001100110011)~2~

Now convert to Scientific Notation,

(1.100110011001100110011) * 2^-4^

exponent = -4+127 = (123)~10~ = (1111011)~2~
sign bit = 0
8 bit exponent = 01111011
23 bit mantisaa = 10011001100110011001100

**IEEE Format** : 00111101110011001100110011001100

___

## Question 3

(0.2)~10~ = (0.(0011)~recur~)~2~


| Multiplication | value |
| -------- | -------- | 
|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|
|0.6 * 2 = 1.2  | 1|


goes on like this...


First convert to binary format

0.2 -> (0.001100110011001100110011)~2~

Now convert to Scientific Notation,

(1.100110011001100110011) * 2^-3^

e = -3+127 = (124)~10~ = (1111100)~2~
sign bit = 0
8 bit exponent = 01111100
23 bit mantisa = 10011001100110011001100

**IEEE Format** : 00111110010011001100110011001100

Converting back to decimal

0 01111100 10011001100110011001100
sign = (-1)^0 = 0
exponent = 124-127 = -3
mantisa = 10011001100110011001100
Scientific form = (1.10011001100110011001100) * 2^-3^

Removing exponent = (0.00110011001100110011001100)~2~

Converting to binary

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.19999998807907104

___

## Question 4

My simple representation for floating point number is to represent the fractional and integral parts seperately.

Integer part + Fractional part

When the numbers are added in this representaiton, the respective integer and fractional parts are added. If the fractional part addition is done with mod and the overflow is added to the integer part.

Implementation can be done in many ways. One way would be to keep subsequent memory address for int and fractional part.

Another good approach to improve flexibilty would be using linked list with the head node having the integer part and the next node having the fractional part.

As the frctional part addition is not the same as int addition, 
the number in factional part is saved as follows

1 {fractional part}
Meaning a 1 will be stored signifying the start of the fractional part
this one will always stay the same and is not added when two numbers are added. If the one becomes 2 due to carry, that carry is then just added to the integer part.

**Example:**



| Number   | Integer   | fractional | 
| -------- | --------  |-------- | 
| 5.391    | 5         | 1391   |
| 3.012    | 3         |1012   |
| 8.403    | 8         |1403   |

___


## Question 5

**Code:**

I am assuming that the number stored in label aa1 is bigger in absolute value.
If not we just need to swap the places to run the code for the case of one number + and one -.
```assembly=
.data
aa2: .word 0b11000001001010000000000000000000 # -10.5 # smaller number in abs
aa1: .word 0b01000001001100000000000000000000 # 11.0 # bigger number in abs
aa3: .word 0
exmask: .word 0x7f800000
mmask: .word 0b00000000011111111111111111111111
extra1: .word 0b00000000100000000000000000000000
mantis: .word 0b00000000010000000000000000000000
.text
lw x1 aa1
lw x2 aa2
srli a1 x1 31 # sign
srli a2 x2 31 
lw t1 exmask 
lw t2 mmask
lw t0 extra1

beq a1 a2 same
j notsame

same:
    and a1 x1 t1
    srli s1 a1 23 # exponent
    mv s9 s1
    and a1 x1 t2 # mantisaa
    or a1 a1 t0
    
    and a2 x2 t1
    srli s2 a2 23
    mv s10 s2 
    and a2 x2 t2
    or a2 a2 t0
    
expsame:
    beq s10 s9 exit
    blt s10 s9 inc
    addi s9 s9 1
    srli a1 a1 1
    j expsame 
    
inc:
    addi s10 s10 1
    srli a2 a2 1 
    j expsame
exit:
    add t6 a1 a2 # mantisaa sum
    srli t5 t6 23 # extra mantisa
    and t6 t6 t2
    addi x1 x0 1
shiftex:
    beq x1 t5 exit1
    addi s9 s9 1
    srli t5 t5 1
    srli t6 t6 1
    j shiftex
exit1:
    #concatenate the answer
    lw x1 aa1
    srli a1 x1 31
    slli a1 a1 31
    slli s9 s9 23
    add a1 a1 s9
    and t6 t6 t2
    add a1 a1 t6
    la x1 aa3
    sw a1 0(x1)
    j e

notsame:
    # assuming abs(a) > abs(b)
    and a1 x1 t1
    srli s1 a1 23 # exponent
    mv s9 s1
    and a1 x1 t2 # mantisaa
    or a1 a1 t0
    
    and a2 x2 t1
    srli s2 a2 23
    mv s10 s2 
    and a2 x2 t2
    or a2 a2 t0
    
expnotsame:
    beq s10 s9 exit2
    blt s10 s9 inc2
    addi s9 s9 1
    srli a1 a1 1
    j expnotsame 
    
inc2:
    addi s10 s10 1
    srli a2 a2 1 
    j expnotsame
exit2:
    sub t6 a1 a2 # mantisaa dif
    lw s7 extra1
    mv s4 t6
    and s6 s7 t6
shiftex2:
    beq s6 s7 exit1
    slli t6 t6 1
    addi s9 s9 -1
    and s6 s7 t6
    j shiftex2
e:
    addi a7 x0 10
    ecall
```

**Output:**
1) **Input : -10.5 ,11.0**
    
    - Memory
    [![Memory](https://hackmd.io/_uploads/ryHPO3RAT.png)
    ](https://)

    - Number in a converter
    ![IEEE converted image](https://hackmd.io/_uploads/BJxPd3AA6.png)

2) **Input : 10.5 ,11.0**
    
    - Memory
    ![Memory](https://hackmd.io/_uploads/SyE8Y200a.png)

    - Number in a converter
    ![Screenshot from 2024-03-25 14-12-06](https://hackmd.io/_uploads/H1WhKhC06.png)


___



