---
# System prepended metadata

title: Lab 7

---

# Lab 7
**Name:** **G.NISHCHITH**
**Roll No.:** **CS22B021**

---
## Question 1

Given to convert (0.1)<sub>10</sub> into Binary.

To convert the fractions into binary we need to take the fractional part and multiply with two and take the integer part and again take the fractional part after multiplication and multiply it by 2 repeat this process until the fractional part disappears but in some cases the fractional part never disappears in that case repeat the process for few number of times.

    
  

| Fractional Part | Integer | Fractional Part |
| -------- | -------- | -------- |
| 0.1*2    | 0        | 0.2      |
|0.2*2| 0 |0.4|
|0.4*2|0|0.8|
|0.8*2|1|0.6|
|0.6*2|1|0.2|
|0.2*2|0|0.4|
|0.4*2|0|0.8|
|0.8*2|1|0.6|
|0.6*2|1|0.2|
|0.2*2|0|0.4|
|0.4*2|0|0.8|
|0.8*2|1|0.6|
|0.6*2|1|0.2|
.
.
.
.





    
Here we need to append the integer part from top to bottom.
Therefore the answer is (0.0001100110011....)<sub>2</sub>.

            

---
## Question 2

Given to convert (0.1)<sub>10</sub> into binary using IEEE standards.

From above question given number in binary is (0.0001100110011...)<sub>2</sub>

1. As 0.1 is a positive number so its sign bit is **'0'**.

2. The given number in binary is **(0.0001100110011...)<sub>2</sub>**.

3. We need to shift the binary form so that there is only one non-zero digit to its left. This gives **1.1001100110011x2<sup>-4</sup>**. The exponent is **-4**.

4. In IEEE 754 single precision binary floating point representation, an exponent bias of 127 is used so the actual value stored will be 127+(-4) = 123. The binary representation of 123 is **(01111011)<sub>2</sub>**.

5. We know as per IEEE 754 we can store only 23 bits in mantissa so the . Mantissa is part after the binary point. So mantissa will be **10011001100110011001101** but here we did an approximation as the mantissa can go indefintely.  

So, the IEEE 754 representation of 0.1 will be

Sign bit: 0
Exponent: 01111011
Mantissa: 10011001100110011001101
Representation: **0 011110011 10011001100110011001101**

---
## Question 3

Given to convert (0.2)<sub>10</sub> to binary using IEEE 754 standards and again convert it to decimal

As the given number is positive so the sign bit is **'0'**.

Now we need to convert  0.2 into binary.

| Fractional Part | Integer | Fractional Part |
| -------- | -------- | -------- |
| 0.2*2    | 0        | 0.4      |
|0.4*2| 0 |0.8|
|0.8*2|1|0.6|
|0.6*2|1|0.2|
|0.2*2|0|0.4|
|0.4*2|0|0.8|
|0.8*2|1|0.6|
|0.6*2|1|0.2|
|0.2*2|0|0.4|
|0.4*2|0|0.8|
|0.8*2|1|0.6|
|0.6*2|1|0.2|
|0.2*2|0|0.4|
.
.
.
.

So the given number in binary form is **(0.001100110011...)<sub>2</sub>**. It is of undefinite length.

1. Sign bit:**0**

2. Binary form is: **(0.001100110011..)<sub>2</sub>**.

3. We need to shift the binary form so that there is only one non-zero digit to its left. This gives **1.100110011x2<sup>-3</sup>**. The exponent is **-3**. 

4. In IEEE 754 single precision binary floating point representation, an exponent bias of 127 is used so the actual value stored will be 127+(-3) = 124. The binary representation of 124 is **(0111100)<sub>2</sub>**.

5. We know as per IEEE 754 we can store only 23 bits in mantissa so the . Mantissa is part after the binary point. So mantissa will be **10011001100110011001101**.


So, the IEEE 754 representation of 0.1 will be

Sign bit: 0
Exponent: 01111100
Mantissa: 11001100110011001100110
Representation: **0 01111100 11001100110011001100110**.


Given to convert again back to decimal

1. The sign bit is **0** so it is postive.

2. The 8 bit number i.e 01111100 we need to convert it to decimal
   0x2<sup>0</sup>+0x2<sup>1</sup>+1x2<sup>2</sup>+1x2<sup>3</sup>+1x2<sup>4</sup>+1x2<sup>5</sup>+1x2<sup>6</sup>+0x2<sup>7</sup> = 0+0+4+8+16+32+64+0 = **124**.
   
3. Actually the exponent is baised by 127 so actual exponent is 124-127= **-3**.

4. And the mantissa is 11011001100110011001110 therefore the binary number is 1.0011001100110011001101x2<sup>-3</sup> this is nearly equals to 0.0011001100110011001100110 =  (0.19921875)<sub>10</sub> which is nearly equals to **(0.2)<sub>10</sub>**.

---
## Question 4

When performing addition of two floating point numbers represented with separate integer and fractional parts, it's essential to maintain distinct sets of registers for each component. The integer part holds the digits to the left of the decimal point while the fractional part contains the digits to the right.

To add these numbers together start by adding the integer parts. This involves adding each digit from right to left, carrying over any excess to the next highest digit if neccessary. Once the integer parts are added if necessary. Once the integer parts are added, then add the fractional parts in the similar way.

However, if the addition of the fractional parts results in a carry that means sum is exceeding one in that position so  the carry must be accounted to the sum of integer parts. This adjustment makes the result maintains the same formart as the original numbers with integer and fractional parts appropriatly combined.

This is the process of adding two floating numbers.

---
## Question 5

Given to write assembly code for adding two floating points

```assembly=
.data
    num1_exp:  .word 0x77    # Exponent of the first input number
    num1_mant: .word 0x8F7C4 # Mantissa of the first input number
    num1_sign: .word 0x0     # Sign bit of the first input number (0 for positive)

    num2_exp:  .word 0x14    # Exponent of the second input number
    num2_mant: .word 0x20460A # Mantissa of the second input number
    num2_sign: .word 0x0     # Sign bit of the second input number (0 for positive)

    sum_exp:  .word 0        # Memory location to store the exponent of the result
    sum_mant: .word 0        # Memory location to store the mantissa of the result
    sum_sign: .word 0        # Memory location to store the sign bit of the result

.text
main:
    # Load addresses of input and result into registers
    la x2, num1_exp    # Load address of exponent of first input into x2
    la x3, num1_mant   # Load address of mantissa of first input into x3
    la x4, num1_sign   # Load address of sign bit of first input into x4

    la x5, num2_exp    # Load address of exponent of second input into x5
    la x6, num2_mant   # Load address of mantissa of second input into x6
    la x7, num2_sign   # Load address of sign bit of second input into x7

    la x8, sum_exp     # Load address of exponent of result into x8
    la x9, sum_mant    # Load address of mantissa of result into x9
    la x11, sum_sign   # Load address of sign bit of result into x11

    # Load input floating-point numbers into registers
    lw x20, 0(x2)     # Load exponent of first input into x20
    lw x21, 0(x3)     # Load mantissa of first input into x21
    lw x22, 0(x4)     # Load sign bit of first input into x22

    lw x23, 0(x5)     # Load exponent of second input into x23
    lw x24, 0(x6)     # Load mantissa of second input into x24
    lw x25, 0(x7)     # Load sign bit of second input into x25

    # Perform addition of exponents (for normalization)
    sub x26, x20, x23   # Calculate difference in exponents
    bgez x26, add_exp1  # Branch if exponent of first input is greater or equal
    add x26, x23, x20   # Otherwise, add exponent of second input to result

add_exp1:
    # Determine which mantissa to shift (if any)
    bgez x26, add_mant1 # Branch if exponent of first input is greater or equal
    sll x21, x21, x26   # Shift mantissa of first input left by difference in exponents
    j store_result      # Jump to store the result

add_mant1:
    sll x24, x24, x26  # Shift mantissa of second input left by difference in exponents

store_result:
    # Store the result back to memory
    sw x26, 0(x8)  # Store exponent of result
    sw x21, 0(x9)  # Store mantissa of result
    sw x22, 0(x11) # Store sign bit of result

exit:
    li a7 10
    ecall
```



