---
# System prepended metadata

title: Lab 6

---

# Lab 6
**Name:** **G.NISHCHITH**
**Roll No.:** **CS22B021**

## Question 11 & 12

Given to get assembly code for the above c code by using 
**riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -S test.c -o test.s**

Here is the screenshot of assembly code.

![Screenshot from 2024-03-26 14-53-41](https://hackmd.io/_uploads/r1gb8LlkC.png)

---
## Question 13

We can optimize the given codes by using optimization levels while compiling the codes.

---
## Question 14

The elf file is uploaded in the drive link
https://drive.google.com/drive/folders/1ONFNCZ2BPP73eqvCERSLwjcSXnoOJakl?usp=sharing

---
## Question 15

Given to optimize the codes and check their sizes
The commands are 
**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**

test_o0.s
```assembly=
	.file	"a.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)
	addi	a4,a5,1
	lw	a5,-24(s0)
	slli	a5,a5,2
	addi	a5,a5,-16
	add	a5,a5,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"
```

test_o1.s
```assembly=
	.file	"a.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"
```

test_o2.s
```assembly=
	.file	"a.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"
```

test_o3.s
```assembly=
	.file	"a.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"
```

test_os.s
```assembly=
	.file	"a.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"
```

test_ofast.s
```assembly=
	.file	"a.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"
```
![Screenshot from 2024-03-26 14-56-32](https://hackmd.io/_uploads/SkaFw8lyA.png)







