---
# System prepended metadata

title: Lab 06

---

# Lab 06
Name: CHATSE SIDDHANT MADHUKAR
Roll No.: CS22B016

---
## Question 1
**Test File**
```cpp=
#include <stdio.h>
int main(){
        int sum=0;
        int a[100];
        for(int i=0;i<100;i++){
                a[i]=i;
                sum+=a[i];
        }
        return 0;
}
```
**Commands Executed**
```
riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -o test.elf test.c
riscv-none-elf-readelf -a test.elf
riscv-none-elf-size test.elf
riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -S test.c -o test.s
riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -o test.elf test.s
riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -O0 -S test.c -o test_o0.s
risv-none-elf-gcc -march=rv32i -mabi=ilp32 -O1 -S test.c -o test_o1.s
risv-none-elf-gcc -march=rv32i -mabi=ilp32 -O2 -S test.c -o test_o2.s
risv-none-elf-gcc -march=rv32i -mabi=ilp32 -O3 -S test.c -o test_o3.s
riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -Os -S test.c -o test_os.s
riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -Ofast -S test.c -o test_ofast.s
```
**Without Optimisation**
```assembly=
 .file   "test.c"
        .option nopic
        .attribute arch, "rv32i2p1"
        .attribute unaligned_access, 0
        .attribute stack_align, 16
        .text
        .align  2
        .globl  main
        .type   main, @function
main:
        addi    sp,sp,-432
        sw      s0,428(sp)
        addi    s0,sp,432
        sw      zero,-20(s0)
        sw      zero,-24(s0)
        j       .L2
.L3:
        lw      a5,-24(s0)
        slli    a5,a5,2
        addi    a5,a5,-16
        add     a5,a5,s0
        lw      a4,-24(s0)
        sw      a4,-408(a5)
        lw      a5,-24(s0)
        slli    a5,a5,2
        addi    a5,a5,-16
        add     a5,a5,s0
        lw      a5,-408(a5)
        lw      a4,-20(s0)
        add     a5,a4,a5
        sw      a5,-20(s0)
        lw      a5,-24(s0)
        addi    a5,a5,1
        sw      a5,-24(s0)
.L2:
        lw      a4,-24(s0)
        li      a5,99
        ble     a4,a5,.L3
        li      a5,0
        mv      a0,a5
        lw      s0,428(sp)
        addi    sp,sp,432
        jr      ra
        .size   main, .-main
        .ident  "GCC: (xPack GNU RISC-V Embedded GCC x86_64) 13.2.0"
```
```
text    data     bss     dec     hex filename
8608    1372     840   10820    2a44 test.elf
 ```

## O1 Optimisation
```assembly=
 .file   "test.c"
        .option nopic
        .attribute arch, "rv32i2p1"
        .attribute unaligned_access, 0
        .attribute stack_align, 16
        .text
        .align  2
        .globl  main
        .type   main, @function
main:
        li      a5,100
.L2:
        addi    a5,a5,-1
        bne     a5,zero,.L2
        li      a0,0
        ret
        .size   main, .-main
        .ident  "GCC: (xPack GNU RISC-V Embedded GCC x86_64) 13.2.0"
```
```
text    data     bss     dec     hex filename
8620    1372     840   10832    2a50 test.elf
```
---
## Question 2 optimization 

```assembly=
 .file   "test.c"
        .option nopic
        .attribute arch, "rv32i2p1"
        .attribute unaligned_access, 0
        .attribute stack_align, 16
        .text
        .section        .text.startup,"ax",@progbits
        .align  2
        .globl  main
        .type   main, @function
main:
        li      a0,0
        ret
        .size   main, .-main
        .ident  "GCC: (xPack GNU RISC-V Embedded GCC x86_64) 13.2.0"
```
```
text    data     bss     dec     hex filename
8608    1372     840   10820    2a44 test.elf
```

---

## Question 3
```assembly=
.file   "test.c"
        .option nopic
        .attribute arch, "rv32i2p1"
        .attribute unaligned_access, 0
        .attribute stack_align, 16
        .text
        .section        .text.startup,"ax",@progbits
        .align  2
        .globl  main
        .type   main, @function
main:
        li      a0,0
        ret
        .size   main, .-main
        .ident  "GCC: (xPack GNU RISC-V Embedded GCC x86_64) 13.2.0"
```
```
text    data     bss     dec     hex filename
8608    1372     840   10820    2a44 test.elf
```   

**OS optimization**
```
.file   "test.c"
        .option nopic
        .attribute arch, "rv32i2p1"
        .attribute unaligned_access, 0
        .attribute stack_align, 16
        .text
        .section        .text.startup,"ax",@progbits
        .align  2
        .globl  main
        .type   main, @function
main:
        li      a0,0
        ret
        .size   main, .-main
        .ident  "GCC: (xPack GNU RISC-V Embedded GCC x86_64) 13.2.0"
```
```
text    data     bss     dec     hex filename
8608    1372     840   10820    2a44 test.elf
```

**ofast optimization**
```assembly=
 .file   "test.c"
        .option nopic
        .attribute arch, "rv32i2p1"
        .attribute unaligned_access, 0
        .attribute stack_align, 16
        .text
        .section        .text.startup,"ax",@progbits
        .align  2
        .globl  main
        .type   main, @function
main:
        li      a0,0
        ret
        .size   main, .-main
        .ident  "GCC: (xPack GNU RISC-V Embedded GCC x86_64) 13.2.0"
```
```
text    data     bss     dec     hex filename
8608    1372     840   10820    2a44 test.el
```
---

