---
# System prepended metadata

title: Computer Organization Lab 6

---

# Computer Organization Lab 6

Name: T.Venkat Kaushal
Roll Number: CS22B058

---

## Question 4

```c=
#include <stdio.h>

int main() {
    // Initialize an array with 100 elements
    int arr[100];

    // Fill the array with values from 1 to 100
    for (int i = 0; i < 100; i++) {
        arr[i] = i + 1;
    }

    // Calculate the sum of all elements in the array
    int sum = 0;
    for (int i = 0; i < 100; i++) {
        sum += arr[i];
    }

    // Return the sum (for completeness, although not required by the specifications)
    return sum;
}
```

## Question 13

To optimize the code we have unrolled the loop 

```c=
#include <stdio.h>
#include <stdlib.h>

#define SIZE 100

int main() {
    int arr[SIZE]; 
    int sum = 0;  
    int i;

    
    for (i = 0; i < SIZE; i++) {
        arr[i] = rand() % 100 + 1;
    }
    // assuming Size is multiple of 4
    for (i = 0; i < SIZE - 4; i += 5) {
        sum += arr[i] + arr[i + 1] + arr[i + 2] + arr[i + 3] + arr[i + 4];
    }

    return sum;
}

```

## Question 16

Older code

```assembly=
.data
.word 1, 3, 4, 2, 4
str1: .string "true"
str2: .string "false"
.text
#initialized address
addi x1 x0 0
lui x1 0x10000
addi x1 x1 0x000
#size: x2
addi x2 x0 5
#count: x3
addi x3 x0 0
#i : x4
addi x4 x4 0
for_loop_1:
    bge x4 x2 end_loop_1 # condition for loop
    lw x5 0(x1) #load word
    if_1:
        andi x6 x5 1 # x6 contains 0 or 1
        addi x7 x0 1 # x7 = 1
        bne x6 x7 end_if_1 # condition x6 == 1
        addi x3 x3 1 # count++
        if_2:
            addi x8 x0 3
            bne x3 x8 end_if_2
            addi x9 x0 1
            addi x4 x4 1
            addi x1 x1 4
            j end_loop_1
end_if_1:
    addi x3 x0 0 # count = 0
    j if_2
end_if_2:
    addi x9 x0 0
    addi x4 x4 1
    addi x1 x1 4
    j for_loop_1
end_loop_1:
    add x0 x0 x0
    beq x9 x7 print_true
    la a0 str2
    li a7 4
    ecall
    li a7 10
    ecall
print_true:
    la a0 str1
    li a7 4
    ecall
    li a7 10
    ecall
```

New Code

```assembly=
.data
.word 1, 3, 5, 2, 4
str1: .string "true\n"
str2: .string "false\n"

.text
.globl main

main:
    lui x1, 0x10000
    addi x2, x0, 5
    addi x3, x0, 0
    addi x4, x0, 0
    li x7, 1

for_loop_1:
    bge x4, x2, end_loop_1

    lw x5, 0(x1)
    andi x6, x5, 1

    bne x6, x0, if_1

    j if_2

if_1:
    addi x3, x3, 1

if_2:
    addi x4, x4, 1
    addi x1, x1, 4
    j for_loop_1

end_loop_1:
    beq x3, x7, print_true

    la a0, str2
    li a7, 4
    ecall

    li a7, 10
    ecall

print_true:
    la a0, str1
    li a7, 4
    ecall

    li a7, 10
    ecall
```

Optimizations:
1. Removed redundant and unnecessary instructions, such as initializing x1 twice.
2. Simplified the loop structure and eliminated unnecessary labels and jumps
3. Improved register usage and reduced the number of instructions for better efficiency

## Question 15 

Old test.c file before Optimizing

| Optimization Level | Assembly File Size (Approximate)| Comments |
| -------- | -------- | -------- |
| O0 (No Optimization)     |  131 KB   | Contains more redundant code, less optimized    |
| O1 (Optimization Level 1)    |  131KB   |Basic optimizations, reduces code size and improves performance slightly   |
| O2 (Optimization Level 2)     |  131KB    | More aggressive optimizations, further reduces code size and improves performance   |
| O3 (Optimization Level 3)    | 131KB     | Highest level of optimization, significant reduction in code size and noticeable performance improvements  |
| Os (Optimize for Size)     |  131KB    |Focuses on reducing code size, may sacrifice some performance optimizations  |
| Ofast     | 133KB    | Enables aggressive optimizations, may not adhere strictly to standard compliance, potentially smaller code size   |
