---
# System prepended metadata

title: Lab 5

---

# Lab 6
Name: Manan Chavda
Roll No.: CS22B017

---


**Test Code** 

```c=
#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


```c=
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
**Code** 
```c=
 .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"
```
```c=
  text	   data	    bss	    dec	    hex	filename
   8724	   1372	    840	  10936	   2ab8	test.elf

```
    
___
## O1 Optimisation
**Code** 
```c=
  .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"
```
```c=
text	   data	    bss	    dec	    hex	filename
   8620	   1372	    840	  10832	   2a50	test_o1.elf
```

___
## O2 Optimisation
**Code** 
```c=
    .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"

```
```c=
 text	   data	    bss	    dec	    hex	filename
   8608	   1372	    840	  10820	   2a44	test_o2.elf
```
___
## O3 Optimisation
**Code** 
```c=
    .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"

```
```c=
text	   data	    bss	    dec	    hex	filename
   8608	   1372	    840	  10820	   2a44	test_o3.elf
```
___
## Os Optimisation
**Code** 
```c=
    .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"

```
```c=
text	   data	    bss	    dec	    hex	filename
   8608	   1372	    840	  10820	   2a44	test_os.elf
```
___
## Ofast Optimisation
**Code** 
```c=
    .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"

```
```c=
text	   data	    bss	    dec	    hex	filename
   8608	   1372	    840	  10820	   2a44	test_ofast.elf
```
___


