---
# System prepended metadata

title: 'Lab 6,7'

---

# Lab 7
Name: Aditya Raghuveer
Roll No.: CS22B019

---
## Question 1

### Step 1:multiply 0.1 with 2
so which gives that 0.1*2=0.2
### Step 2:remove the integral part and continue step 1 until we get '1.0'
so what happens here is that in 0.2 we are going to remove the integral part i.e. '0' . after that we are still left with .2 which significantly is 0.2 . we gonna repeat the step 1 after this
### 
    0.2*2=0.4
    step 1 repeat
    0.4*2=0.8
    step 1 repeat
    0.8*2=1.6
so we gonna remove the integral part that is '1' here . we are left with 0.6 after that
###  
    0.6*2=1.2
    0.2*2=0.4
### Step 3:
we actually keep track of all those integral parts which we are removing in step2.
### Step 4:
those will be arranged after decimal point in the radix 2 representation of the number.

## Answer
hence our number will be 0.0001100110011...... 
in binary representation

---
## Question 2

sign bit of 0.1 is 0

now it's exponent in scientific form is -4 so it's exponent is 123(-4+127) since it's bias for precesion is 127 

now 0.1/0.0625 is equal to 1.6

now representing 0.6 in binary form:

1. Start with 0.6.
2. Multiply 0.6 by 2:
   - 0.6 * 2 = 1.2 ⟶ Bit: 1
3. Take the fractional part (0.2) and repeat:
   - 0.2 * 2 = 0.4 ⟶ Bit: 0
   - 0.4 * 2 = 0.8 ⟶ Bit: 0
   - 0.8 * 2 = 1.6 ⟶ Bit: 1
   - 0.6 * 2 = 1.2 ⟶ Bit: 1
   - 0.2 * 2 = 0.4 ⟶ Bit: 0
   - 0.4 * 2 = 0.8 ⟶ Bit: 0
   - 0.8 * 2 = 1.6 ⟶ Bit: 1
   - 0.6 * 2 = 1.2 ⟶ Bit: 1
   - ... and so on.

The pattern repeats: 1001. Note that this pattern repeats every 4 bits after the binary point. To get 23 bits after the binary point, we can extend this pattern:

0.6 in binary with 23 bits after the binary point:
0.10011001100110011001100


**Observation** (if required)
so the final answer will be 
 0 01111011 10011001100110011001100
 
---
## Question 3
sign bit of 0.2 is 0

the exponent of 0.2 in scientific notation is -3 so it's value is 124

representation of 124 in binary is 01111100

when we divide 0.2/0.125 we get 1.6 , represent 0.6 in 23 bit binary number:

now representing 0.6 in binary form:

1. Start with 0.6.
2. Multiply 0.6 by 2:
   - 0.6 * 2 = 1.2 ⟶ Bit: 1
3. Take the fractional part (0.2) and repeat:
   - 0.2 * 2 = 0.4 ⟶ Bit: 0
   - 0.4 * 2 = 0.8 ⟶ Bit: 0
   - 0.8 * 2 = 1.6 ⟶ Bit: 1
   - 0.6 * 2 = 1.2 ⟶ Bit: 1
   - 0.2 * 2 = 0.4 ⟶ Bit: 0
   - 0.4 * 2 = 0.8 ⟶ Bit: 0
   - 0.8 * 2 = 1.6 ⟶ Bit: 1
   - 0.6 * 2 = 1.2 ⟶ Bit: 1
   - ... and goes on

The pattern repeats: 1001. Note that this pattern repeats every 4 bits after the binary point. To get 23 bits after the binary point, we can extend this pattern:

0.6 in binary with 23 bits after the binary point:
0.10011001100110011001100

now entire representation:
 Comment
0 01111100 10011001100110011001100
now reverting back

sign bit 0

exponent is 124 so the exponent in scientific notation is -3

now we can see in binary form it is 

1 · 2<sup>-1</sup> + 0 · 2<sup>-2</sup> +.....

so we approximately get 0.6

so answer is 1.6*(0.125) which is 0.2

## Question 4

To represent floating-point numbers and perform addition, let's use a simple representation where each floating-point number is split into two parts: one for the integer part and another for the fractional part. We'll store these parts in separate registers. 

For this example, let's use two registers to store each floating-point number: one for the integer part and another for the fractional part. 

Here's how we can represent and perform addition for the given example of adding 5.391 with 3.012:

1. **Representation**:
   - Register 1: Store the integer part of the floating-point number.
   - Register 2: Store the fractional part of the floating-point number.

For 5.391:
- Register 1: Integer part = 5
- Register 2: Fractional part = 391

For 3.012:
- Register 3: Integer part = 3
- Register 4: Fractional part = 012

2. **Performing Addition**:
   - Add the fractional parts together. If the sum exceeds 999 (or the maximum value of the fractional part), carry over to the integer part.
   - Add the integer parts together along with the carry, if any.

Let's perform the addition:
- Fractional part: 391 + 012 = 403
  - Since 403 is within the range of the fractional part (0 to 999), no carryover is needed.
- Integer part: 5 + 3 (no carryover) = 8

So, the result is:
- Register 5: Integer part = 8
- Register 6: Fractional part = 403

Therefore, the result of adding 5.391 and 3.012 is 8.403.

## Question 5
.section .data

     # Define the two floating-point numbers in IEEE 754 format
    num1: .float 3.14159   # Example: 3.14159
    num2: .float -3.14159  # Example: -3.14159

    # Define the memory location to store the result
    result: .float 0.0     # Initialize to 0.0
    
.section .text
    .globl _start
_start:

    # Load the address of num1 into a register
    la t0, num1

    # Load the address of num2 into another register
    la t1, num2

    # Load the first floating-point number into a floating-point register (f0)
    flw f0, 0(t0)

    # Load the second floating-point number into another floating-point register (f1)
    flw f1, 0(t1)

    # Perform floating-point addition
    fadd.s f2, f0, f1

    # Store the result in the memory location
    la t2, result
    fsw f2, 0(t2)

    # Exit the program
    li a0, 0            # Exit status
    li a7, 93           # Exit system call number
    ecall               # Invoke system call

 

# Lab 6

## Question 12
cat test1.s 
test1.c is the main file for me which computes the sum of 100 elements. and test1.s is my assembly file for it. cat test1.s accesses the contents of the file present in test1.s

## Question 13
 
yes we could optimise the code. 

## Question 15
#### sizes of the files
  133 bytes- test1.c
  877 bytes -  test1.s
  877 bytes - test1_o0.s
  545 bytes - test1_o1.s
  529 bytes - test1_o2.s
  529 bytes - test1_o3.s
  529 bytes  - test1_os.s
  529 bytes  - test1_ofast.s
  
  
files are attached seperately in classroom


