---
# System prepended metadata

title: Lab7_CS22B047

---

# Lab 7
Name:    Tilak Reddy
Roll No.: CS22B047    

---
## Question 1

### Step 1 - convert inegral part to binary
0 in decimal = 0 in binary
    
### Step 2 - convert fractional part to binary

0.1 x 2 = 0.2, integral part = 0
0.2 x 2 = 0.4, integral part = 0
0.4 x 2 = 0.8, integral part = 0
0.8 x 2 = 1.6, integral part = 1
0.6 x 2 = 1.2, integral part = 1
.
.
.

combine all intergral parts to get binary: 
.1 in decimal = .00011.... in binary

### Step 3 - combine both parts
0.1 in decimal = 0.00011.... in binary

---
## Question 2

### Step 1 - Find sign bit

sign bit = 0

### Step 2 - write 0.1 in base 2 sintific notation
0.1 / 2<sup>-1</sup> = 0.2
0.1 / 2<sup>-2</sup> = 0.4
0.1 / 2<sup>-3</sup>= 0.8
__0.1 / 2<sup>-4</sup> = 1.6__

0.1 in base 2 scientific = 1.6 x 2<sup>-4</sup>

sign = 0
mantesa = 6
exponent = -4

### Step 3 - convert 0.6 to binary

mantesa has 23 bits in IEEE notation. Therefore, 0.6 in binary can be approximated up to 23 bits.

Python script to convert 0.6 to binary 

```python=
fraction = 0.6
integer = 0
for i in range(23):
    fraction *= 2;
    integer =int(fraction);
    print(integer,end="")
    fraction -= integer
```
0.6 in binary approximated to 23 bits = 0.10011001100110011001100
matesa = 10011001100110011001100

### Step 4 - convert -4 to binary

Exponent has bais of 127 in IEEE format. therefore, -4 is represented as -4 + 127 = 123

123 in binary = 01111011
exponent  = 01111011

### Step 5 - combine sign, mantesa and exponent

    0.1 in binary = 0 01111011 10011001100110011001100

---

## Question 3

sign = 0
0.2 = 1.6 x 2<sup>-3</sup>
_mantesa will be same as in previous question_
exponent = -3+127 = 124
exponent in binary = 01111100

    0.2 in binary = 0 01111100 10011001100110011001100
    
python script to convert IEEE to decimal
```python=
mantesa = "10011001100110011001100"
i=-1;
decimal = 1;
exponent = 3;
for bit in mantesa:
    decimal += int(bit)*pow(2,i)
    i-=1
for i in range(exponent):
    decimal *= 0.5;
print(decimal)
```
    output - 0.19999998807907104
---    
## Question 4

#### Floating Point Representation:
- Register 1: Integer part (3 digits)
- Register 2: Fractional part (3 digits)

#### Addition Algorithm:
1. Align the decimal points by padding with zeros if necessary.
2. Add the fractional parts first. If the sum exceeds 999 (our maximum value for the fractional part), carry over to the integer part.
3. Add the integer parts, including any carry from the fractional part addition.

#### Representation:
- 5.391 is represented as:
  - Register 1: 005
  - Register 2: 391
- 3.012 is represented as:
  - Register 1: 003
  - Register 2: 012

#### Addition:
1. Align the decimal points:
   - 005.391
   - +003.012
2. Add the fractional parts: 391 + 012 = 403
   - Carry: 1 (as 403 > 999)
3. Add the integer parts with the carry: 5 + 3 + carry = 9

#### Result:
- Register 1: 009
- Register 2: 403

---
## Question 5
```assembley=
.data
    number1: .float 3.14    # Example floating-point number 1
    number2: .float 2.71    # Example floating-point number 2
    result:  .float 0       # Initialize the result variable

.text
.global _start

_start:
    # Load the addresses of the numbers into registers
    la  t0, number1
    la  t1, number2
    la  t2, result

    # Load the floating-point numbers into floating-point registers
    flw fa0, 0(t0)  # Load number1 into fa0
    flw fa1, 0(t1)  # Load number2 into fa1

    # Perform addition
    fadd.s fa2, fa0, fa1   # Add number1 and number2, result in fa2

    # Store the result back into memory
    fsw fa2, 0(t2)  # Store the result at the address pointed to by t2

    # Exit the program
    li  a7, 10      # Exit syscall number
    ecall           # Invoke syscall
```

    