---
# System prepended metadata

title: 'Assignment 2-2: Decompress Run-Length Encoded List'
tags: [Computer Architecture 2022]

---

---
###### tags: `Computer Architecture 2022`
---

<!-- * 編譯
    > riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -O3 -o HW2 HW2.c
* 反組譯並存成txt
    > riscv-none-elf-objdump -d ./HW2 > asb.txt
* 看elf標頭
    > riscv-none-elf-readelf -h ./HW2
* 看size
    > riscv-none-elf-size ./HW2
* 執行code
    > build/rv32emu HW2 -->

# Assignment 2-2: Decompress Run-Length Encoded List

## Problem description:
- This problem is based on [LeetCode 1](https://leetcode.com/problems/two-sum/) from [周士翔](https://hackmd.io/@zDmciYQATNm-8XeyJ5Th0Q/r1htullMj)
- I have
- Besides, I compared the original handwritten assembly code with unrolling handwritten assembly code. [Link](https://hackmd.io/Y4PD3GQjTO2FlA8KRGGb6g)


## C code 

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

void twoSum(int* nums,int numSize, int target){
	int i,j;
	for(i=0;i<numSize;i++){
		for(j=i+1;j<numSize;j++){
			if(nums[i]+nums[j]==target)
				printf("%d %d\n",i,j);
		}
	}
}

int main(){
	int nums1[4] = {2,7,11,15}, target1 = 9, size1=4;
	int nums2[3] = {3,2,4}, target2 = 6, size2=3;
	int nums3[2] = {3,3}, target3 = 6, size3=2;
	twoSum(nums1,size1,target1);
	twoSum(nums2,size2,target2);
	twoSum(nums3,size3,target3);
	
	return 0;
}
```

## Assembly code
I modifed the assembly code, so it can be compile with `riscv-none-elf-gcc` and run in the `rv32emu`
```asm=
	.file	"twosum_.c"
	.option nopic
	.attribute arch, "rv32i2p1"
	.attribute unaligned_access, 0
	.attribute stack_align, 16
	.text
	.section	.rodata
	.align	2
.LC1:
	.string	"%d %d\n"
	.text  				#command
	.align	2
	.globl	twoSum
	.type	twoSum, @function
twoSum:
	addi sp, sp, -4
	sw ra, 0(sp)
	mv t0, zero         #int i=0
	addi t1, t0, 1      #int j=i+1
	mv a6, s0           #store nums1 address for i
	mv a1, s0           #store nums1 address for j
loop2:
	lw a2, 0(a6)        #load nums[i]
	lw a3, 4(a1)        #load nums[j]
	add a3, a2, a3      #nums1[i]+nums2[j]
	beq a3, s1, print   #if (nums1[i]+nums2[j]==target) go to print
	addi a1, a1, 4      #point to next nums1[j] address
	addi t1, t1, 1      #j++
	blt t1, s2, loop2
loop1:
	addi t0, t0, 1      #i++
	addi a6, a6, 4      #point to next nums[i] address
	mv a1, a6
	addi t1, t0, 1      #j=i+1
	bltu t0, s2, loop2  #if (i<numsize) go to loop2
print: 
	mv a1, t0           #move i
	mv a2, t1
	lui	a5,%hi(.LC1)
	addi	a0,a5,%lo(.LC1)
	call	printf
	lw ra, 0(sp)
	addi sp, sp, 4
	jr ra
	.size	twoSum, .-twoSum
	.section	.rodata
	.align	2
.data
	nums1: .word 2, 7, 11, 15
	target1: .word 9
	size1: .word 4
	nums2: .word 3, 2, 4
	target2: .word 6
	size2: .word 3
	nums3: .word 3, 3
	target3: .word 6
	size3: .word 2
	.text	
	.align	2
	.globl	main
	.type	main, @function
main:
	addi	sp,sp,-80
	sw	ra,76(sp)
	sw	s0,72(sp)
	addi	s0,sp,80
	la s0, nums1        #load nums1 address to s0
	lw s1, target1      #load target1 to s1
	lw s2, size1        #load size1 to s2
	call twoSum
	la s0, nums2
	lw s1, target2
	lw s2, size2
	call twoSum
	la s0, nums3
	lw s1, target3
	lw s2, size3
	call twoSum
	lw	ra,76(sp)
	lw	s0,72(sp)
	addi	sp,sp,80
	jr	ra
	#li a7, 10 #end
	#ecall	
	.size	main, .-main
	.ident	"GCC: (xPack GNU RISC-V Embedded GCC x86_64) 12.2.0"
```




## Instruction


## Result

### -O0
- compile
`riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -O0 -o hw2/twosum_o0 -s hw2/twosum_.c`
- size
  | text  | data | bss | dec   | hex   | filename      |
  | ----- | ---- | --- | ----- | ----- | ------------- |
  | 74932 | 2816 | 812 | 78560 | 132e0 | hw2/twosum_o0 |
  
<!--   ![](https://i.imgur.com/bVquxKR.png) -->
- ELF
![](https://i.imgur.com/AYt0yQO.png)
- code
```asm=
00010184 <twoSum>:
   10184:	fd010113          	addi	sp,sp,-48
   10188:	02112623          	sw	ra,44(sp)
   1018c:	02812423          	sw	s0,40(sp)
   10190:	03010413          	addi	s0,sp,48
   10194:	fca42e23          	sw	a0,-36(s0)
   10198:	fcb42c23          	sw	a1,-40(s0)
   1019c:	fcc42a23          	sw	a2,-44(s0)
   101a0:	fe042623          	sw	zero,-20(s0)
   101a4:	0800006f          	j	10224 <twoSum+0xa0>
   101a8:	fec42783          	lw	a5,-20(s0)
   101ac:	00178793          	addi	a5,a5,1
   101b0:	fef42423          	sw	a5,-24(s0)
   101b4:	0580006f          	j	1020c <twoSum+0x88>
   101b8:	fec42783          	lw	a5,-20(s0)
   101bc:	00279793          	slli	a5,a5,0x2
   101c0:	fdc42703          	lw	a4,-36(s0)
   101c4:	00f707b3          	add	a5,a4,a5
   101c8:	0007a703          	lw	a4,0(a5)
   101cc:	fe842783          	lw	a5,-24(s0)
   101d0:	00279793          	slli	a5,a5,0x2
   101d4:	fdc42683          	lw	a3,-36(s0)
   101d8:	00f687b3          	add	a5,a3,a5
   101dc:	0007a783          	lw	a5,0(a5)
   101e0:	00f707b3          	add	a5,a4,a5
   101e4:	fd442703          	lw	a4,-44(s0)
   101e8:	00f71c63          	bne	a4,a5,10200 <twoSum+0x7c>
   101ec:	fe842603          	lw	a2,-24(s0)
   101f0:	fec42583          	lw	a1,-20(s0)
   101f4:	000217b7          	lui	a5,0x21
   101f8:	79078513          	addi	a0,a5,1936 # 21790 <__clzsi2+0x88>
   101fc:	350000ef          	jal	ra,1054c <printf>
   10200:	fe842783          	lw	a5,-24(s0)
   10204:	00178793          	addi	a5,a5,1
   10208:	fef42423          	sw	a5,-24(s0)
   1020c:	fe842703          	lw	a4,-24(s0)
   10210:	fd842783          	lw	a5,-40(s0)
   10214:	faf742e3          	blt	a4,a5,101b8 <twoSum+0x34>
   10218:	fec42783          	lw	a5,-20(s0)
   1021c:	00178793          	addi	a5,a5,1
   10220:	fef42623          	sw	a5,-20(s0)
   10224:	fec42703          	lw	a4,-20(s0)
   10228:	fd842783          	lw	a5,-40(s0)
   1022c:	f6f74ee3          	blt	a4,a5,101a8 <twoSum+0x24>
   10230:	00000013          	nop
   10234:	00000013          	nop
   10238:	02c12083          	lw	ra,44(sp)
   1023c:	02812403          	lw	s0,40(sp)
   10240:	03010113          	addi	sp,sp,48
   10244:	00008067          	ret
00010248 <main>:
   10248:	fb010113          	addi	sp,sp,-80
   1024c:	04112623          	sw	ra,76(sp)
   10250:	04812423          	sw	s0,72(sp)
   10254:	05010413          	addi	s0,sp,80
   10258:	000217b7          	lui	a5,0x21
   1025c:	79878793          	addi	a5,a5,1944 # 21798 <__clzsi2+0x90>
   10260:	0007a603          	lw	a2,0(a5)
   10264:	0047a683          	lw	a3,4(a5)
   10268:	0087a703          	lw	a4,8(a5)
   1026c:	00c7a783          	lw	a5,12(a5)
   10270:	fcc42423          	sw	a2,-56(s0)
   10274:	fcd42623          	sw	a3,-52(s0)
   10278:	fce42823          	sw	a4,-48(s0)
   1027c:	fcf42a23          	sw	a5,-44(s0)
   10280:	00900793          	li	a5,9
   10284:	fef42623          	sw	a5,-20(s0)
   10288:	00400793          	li	a5,4
   1028c:	fef42423          	sw	a5,-24(s0)
   10290:	00300793          	li	a5,3
   10294:	faf42e23          	sw	a5,-68(s0)
   10298:	00200793          	li	a5,2
   1029c:	fcf42023          	sw	a5,-64(s0)
   102a0:	00400793          	li	a5,4
   102a4:	fcf42223          	sw	a5,-60(s0)
   102a8:	00600793          	li	a5,6
   102ac:	fef42223          	sw	a5,-28(s0)
   102b0:	00300793          	li	a5,3
   102b4:	fef42023          	sw	a5,-32(s0)
   102b8:	00300793          	li	a5,3
   102bc:	faf42a23          	sw	a5,-76(s0)
   102c0:	00300793          	li	a5,3
   102c4:	faf42c23          	sw	a5,-72(s0)
   102c8:	00600793          	li	a5,6
   102cc:	fcf42e23          	sw	a5,-36(s0)
   102d0:	00200793          	li	a5,2
   102d4:	fcf42c23          	sw	a5,-40(s0)
   102d8:	fc840793          	addi	a5,s0,-56
   102dc:	fec42603          	lw	a2,-20(s0)
   102e0:	fe842583          	lw	a1,-24(s0)
   102e4:	00078513          	mv	a0,a5
   102e8:	e9dff0ef          	jal	ra,10184 <twoSum>
   102ec:	fbc40793          	addi	a5,s0,-68
   102f0:	fe442603          	lw	a2,-28(s0)
   102f4:	fe042583          	lw	a1,-32(s0)
   102f8:	00078513          	mv	a0,a5
   102fc:	e89ff0ef          	jal	ra,10184 <twoSum>
   10300:	fb440793          	addi	a5,s0,-76
   10304:	fdc42603          	lw	a2,-36(s0)
   10308:	fd842583          	lw	a1,-40(s0)
   1030c:	00078513          	mv	a0,a5
   10310:	e75ff0ef          	jal	ra,10184 <twoSum>
   10314:	00000793          	li	a5,0
   10318:	00078513          	mv	a0,a5
   1031c:	04c12083          	lw	ra,76(sp)
   10320:	04812403          	lw	s0,72(sp)
   10324:	05010113          	addi	sp,sp,80
   10328:	00008067          	ret
```
- Observation
    - default compiler
    - Register Usage
        - s-type：s0
        - a-type：a0, a1, a2, a3, a4, a5
        - t-type：0
        - other：sp, ra, zero
        - Total：10
    - branch instruction：11
    - lines：108

### -O1
- compile
`riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -O1 -o hw2/twosum_o1 -s hw2/twosum_.c`
- size
  | text  | data | bss | dec   | hex   | filename      |
  | ----- | ---- | --- | ----- | ----- | ------------- |
  | 74852 | 2816 | 812 | 78480 | 13290 | hw2/twosum_o1 |
<!--   ![](https://i.imgur.com/4spMQci.png)
 -->
- ELF
![](https://i.imgur.com/vF0Szg8.png)
- code
```asm=
00010184 <twoSum>:
   10184:	0cb05263          	blez	a1,10248 <twoSum+0xc4>
   10188:	fd010113          	addi	sp,sp,-48
   1018c:	02112623          	sw	ra,44(sp)
   10190:	02812423          	sw	s0,40(sp)
   10194:	02912223          	sw	s1,36(sp)
   10198:	03212023          	sw	s2,32(sp)
   1019c:	01312e23          	sw	s3,28(sp)
   101a0:	01412c23          	sw	s4,24(sp)
   101a4:	01512a23          	sw	s5,20(sp)
   101a8:	01612823          	sw	s6,16(sp)
   101ac:	01712623          	sw	s7,12(sp)
   101b0:	00058993          	mv	s3,a1
   101b4:	00060a13          	mv	s4,a2
   101b8:	00050913          	mv	s2,a0
   101bc:	00000a93          	li	s5,0
   101c0:	00021bb7          	lui	s7,0x21
   101c4:	0400006f          	j	10204 <twoSum+0x80>
   101c8:	00048613          	mv	a2,s1
   101cc:	000b0593          	mv	a1,s6
   101d0:	740b8513          	addi	a0,s7,1856 # 21740 <__clzsi2+0x88>
   101d4:	328000ef          	jal	ra,104fc <printf>
   101d8:	00148793          	addi	a5,s1,1
   101dc:	00440413          	addi	s0,s0,4
   101e0:	00f98e63          	beq	s3,a5,101fc <twoSum+0x78>
   101e4:	00078493          	mv	s1,a5
   101e8:	00092783          	lw	a5,0(s2)
   101ec:	00442703          	lw	a4,4(s0)
   101f0:	00e787b3          	add	a5,a5,a4
   101f4:	ff4792e3          	bne	a5,s4,101d8 <twoSum+0x54>
   101f8:	fd1ff06f          	j	101c8 <twoSum+0x44>
   101fc:	00490913          	addi	s2,s2,4
   10200:	009b0e63          	beq	s6,s1,1021c <twoSum+0x98>
   10204:	000a8b13          	mv	s6,s5
   10208:	001a8a93          	addi	s5,s5,1
   1020c:	01598863          	beq	s3,s5,1021c <twoSum+0x98>
   10210:	00090413          	mv	s0,s2
   10214:	000a8493          	mv	s1,s5
   10218:	fd1ff06f          	j	101e8 <twoSum+0x64>
   1021c:	02c12083          	lw	ra,44(sp)
   10220:	02812403          	lw	s0,40(sp)
   10224:	02412483          	lw	s1,36(sp)
   10228:	02012903          	lw	s2,32(sp)
   1022c:	01c12983          	lw	s3,28(sp)
   10230:	01812a03          	lw	s4,24(sp)
   10234:	01412a83          	lw	s5,20(sp)
   10238:	01012b03          	lw	s6,16(sp)
   1023c:	00c12b83          	lw	s7,12(sp)
   10240:	03010113          	addi	sp,sp,48
   10244:	00008067          	ret
   10248:	00008067          	ret
0001024c <main>:
   1024c:	fc010113          	addi	sp,sp,-64
   10250:	02112e23          	sw	ra,60(sp)
   10254:	000217b7          	lui	a5,0x21
   10258:	74878793          	addi	a5,a5,1864 # 21748 <__clzsi2+0x90>
   1025c:	0007a603          	lw	a2,0(a5)
   10260:	0047a683          	lw	a3,4(a5)
   10264:	0087a703          	lw	a4,8(a5)
   10268:	00c7a783          	lw	a5,12(a5)
   1026c:	02c12023          	sw	a2,32(sp)
   10270:	02d12223          	sw	a3,36(sp)
   10274:	02e12423          	sw	a4,40(sp)
   10278:	02f12623          	sw	a5,44(sp)
   1027c:	00300793          	li	a5,3
   10280:	00f12a23          	sw	a5,20(sp)
   10284:	00200713          	li	a4,2
   10288:	00e12c23          	sw	a4,24(sp)
   1028c:	00400713          	li	a4,4
   10290:	00e12e23          	sw	a4,28(sp)
   10294:	00f12623          	sw	a5,12(sp)
   10298:	00f12823          	sw	a5,16(sp)
   1029c:	00900613          	li	a2,9
   102a0:	00400593          	li	a1,4
   102a4:	02010513          	addi	a0,sp,32
   102a8:	eddff0ef          	jal	ra,10184 <twoSum>
   102ac:	00600613          	li	a2,6
   102b0:	00300593          	li	a1,3
   102b4:	01410513          	addi	a0,sp,20
   102b8:	ecdff0ef          	jal	ra,10184 <twoSum>
   102bc:	00600613          	li	a2,6
   102c0:	00200593          	li	a1,2
   102c4:	00c10513          	addi	a0,sp,12
   102c8:	ebdff0ef          	jal	ra,10184 <twoSum>
   102cc:	00000513          	li	a0,0
   102d0:	03c12083          	lw	ra,60(sp)
   102d4:	04010113          	addi	sp,sp,64
   102d8:	00008067          	ret
```
- Observation
    - optimize compiler
    -  Register Usage
            - s-type：s0 ~ s7
            - a-type：a0, a1, a2, a3, a4, a5
            - t-type：0
            - other：sp, ra, zero
            - Total：17
    - branch instruction：15
    - lines：88
### -O2
- compile
`riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -O2 -o hw2/twosum_o2 -s hw2/twosum_.c`

- size  
  | text  | data | bss | dec   | hex   | filename      |
  | ----- | ---- | --- | ----- | ----- | ------------- |
  | 74848 | 2816 | 812 | 78476 | 1328c | hw2/twosum_o2 |
<!--   ![](https://i.imgur.com/UsZTue8.png) -->

- ELF
![](https://i.imgur.com/G6ew8js.png)

- code
```asm=
000100c4 <main>:
   100c4:	000217b7          	lui	a5,0x21
   100c8:	74878793          	addi	a5,a5,1864 # 21748 <__clzsi2+0x94>
   100cc:	00c7a703          	lw	a4,12(a5)
   100d0:	fc010113          	addi	sp,sp,-64
   100d4:	0007a883          	lw	a7,0(a5)
   100d8:	0047a803          	lw	a6,4(a5)
   100dc:	0087a683          	lw	a3,8(a5)
   100e0:	02e12623          	sw	a4,44(sp)
   100e4:	00200713          	li	a4,2
   100e8:	00300793          	li	a5,3
   100ec:	02010513          	addi	a0,sp,32
   100f0:	00e12c23          	sw	a4,24(sp)
   100f4:	00900613          	li	a2,9
   100f8:	00400713          	li	a4,4
   100fc:	00400593          	li	a1,4
   10100:	02112e23          	sw	ra,60(sp)
   10104:	03112023          	sw	a7,32(sp)
   10108:	03012223          	sw	a6,36(sp)
   1010c:	02d12423          	sw	a3,40(sp)
   10110:	00f12a23          	sw	a5,20(sp)
   10114:	00e12e23          	sw	a4,28(sp)
   10118:	00f12623          	sw	a5,12(sp)
   1011c:	00f12823          	sw	a5,16(sp)
   10120:	0f4000ef          	jal	ra,10214 <twoSum>
   10124:	01410513          	addi	a0,sp,20
   10128:	00600613          	li	a2,6
   1012c:	00300593          	li	a1,3
   10130:	0e4000ef          	jal	ra,10214 <twoSum>
   10134:	00c10513          	addi	a0,sp,12
   10138:	00600613          	li	a2,6
   1013c:	00200593          	li	a1,2
   10140:	0d4000ef          	jal	ra,10214 <twoSum>
   10144:	03c12083          	lw	ra,60(sp)
   10148:	00000513          	li	a0,0
   1014c:	04010113          	addi	sp,sp,64
   10150:	00008067          	ret
   00010214 <twoSum>:
   10214:	0cb05063          	blez	a1,102d4 <twoSum+0xc0>
   10218:	fd010113          	addi	sp,sp,-48
   1021c:	02812423          	sw	s0,40(sp)
   10220:	03212023          	sw	s2,32(sp)
   10224:	01312e23          	sw	s3,28(sp)
   10228:	01412c23          	sw	s4,24(sp)
   1022c:	01712623          	sw	s7,12(sp)
   10230:	02112623          	sw	ra,44(sp)
   10234:	02912223          	sw	s1,36(sp)
   10238:	01512a23          	sw	s5,20(sp)
   1023c:	01612823          	sw	s6,16(sp)
   10240:	00100413          	li	s0,1
   10244:	00058993          	mv	s3,a1
   10248:	00060a13          	mv	s4,a2
   1024c:	00450913          	addi	s2,a0,4
   10250:	00021bb7          	lui	s7,0x21
   10254:	04858a63          	beq	a1,s0,102a8 <twoSum+0x94>
   10258:	00040b13          	mv	s6,s0
   1025c:	fff40a93          	addi	s5,s0,-1
   10260:	00090493          	mv	s1,s2
   10264:	00c0006f          	j	10270 <twoSum+0x5c>
   10268:	00140413          	addi	s0,s0,1
   1026c:	03345863          	bge	s0,s3,1029c <twoSum+0x88>
   10270:	0004a703          	lw	a4,0(s1)
   10274:	ffc92783          	lw	a5,-4(s2)
   10278:	00448493          	addi	s1,s1,4
   1027c:	00e787b3          	add	a5,a5,a4
   10280:	ff4794e3          	bne	a5,s4,10268 <twoSum+0x54>
   10284:	00040613          	mv	a2,s0
   10288:	000a8593          	mv	a1,s5
   1028c:	740b8513          	addi	a0,s7,1856 # 21740 <__clzsi2+0x8c>
   10290:	00140413          	addi	s0,s0,1
   10294:	264000ef          	jal	ra,104f8 <printf>
   10298:	fd344ce3          	blt	s0,s3,10270 <twoSum+0x5c>
   1029c:	001b0413          	addi	s0,s6,1
   102a0:	00490913          	addi	s2,s2,4
   102a4:	fa899ae3          	bne	s3,s0,10258 <twoSum+0x44>
   102a8:	02c12083          	lw	ra,44(sp)
   102ac:	02812403          	lw	s0,40(sp)
   102b0:	02412483          	lw	s1,36(sp)
   102b4:	02012903          	lw	s2,32(sp)
   102b8:	01c12983          	lw	s3,28(sp)
   102bc:	01812a03          	lw	s4,24(sp)
   102c0:	01412a83          	lw	s5,20(sp)
   102c4:	01012b03          	lw	s6,16(sp)
   102c8:	00c12b83          	lw	s7,12(sp)
   102cc:	03010113          	addi	sp,sp,48
   102d0:	00008067          	ret
   102d4:	00008067          	ret
```
- Observation
    - optimize compiler
    -  Register Usage
            - s-type：s0 ~ s7
            - a-type：a0 ~ a7
            - t-type：0
            - other：sp, ra
            - Total：18
    - branch instruction：14
    - lines：87
### -O3
- compile
`riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -O3 -o hw2/twosum_o3 -s hw2/twosum_.c`
- size
    | text  | data | bss | dec   | hex   | filename   |
    | ----- | ---- | --- | ----- | ----- | ---------- |
    | 74848 | 2816 | 812 | 78476 | 1328c | hw2/twosum_o3 |

<!-- ![](https://i.imgur.com/pO1epic.png) -->
- ELF
![](https://i.imgur.com/tQ3gHSb.png)
- code
```asm=
000100c4 <main>:
   100c4:	000217b7          	lui	a5,0x21
   100c8:	74878793          	addi	a5,a5,1864 # 21748 <__clzsi2+0x94>
   100cc:	00c7a703          	lw	a4,12(a5)
   100d0:	fc010113          	addi	sp,sp,-64
   100d4:	0007a883          	lw	a7,0(a5)
   100d8:	0047a803          	lw	a6,4(a5)
   100dc:	0087a683          	lw	a3,8(a5)
   100e0:	02e12623          	sw	a4,44(sp)
   100e4:	00200713          	li	a4,2
   100e8:	00300793          	li	a5,3
   100ec:	02010513          	addi	a0,sp,32
   100f0:	00e12c23          	sw	a4,24(sp)
   100f4:	00900613          	li	a2,9
   100f8:	00400713          	li	a4,4
   100fc:	00400593          	li	a1,4
   10100:	02112e23          	sw	ra,60(sp)
   10104:	03112023          	sw	a7,32(sp)
   10108:	03012223          	sw	a6,36(sp)
   1010c:	02d12423          	sw	a3,40(sp)
   10110:	00f12a23          	sw	a5,20(sp)
   10114:	00e12e23          	sw	a4,28(sp)
   10118:	00f12623          	sw	a5,12(sp)
   1011c:	00f12823          	sw	a5,16(sp)
   10120:	0f4000ef          	jal	ra,10214 <twoSum>
   10124:	01410513          	addi	a0,sp,20
   10128:	00600613          	li	a2,6
   1012c:	00300593          	li	a1,3
   10130:	0e4000ef          	jal	ra,10214 <twoSum>
   10134:	00c10513          	addi	a0,sp,12
   10138:	00600613          	li	a2,6
   1013c:	00200593          	li	a1,2
   10140:	0d4000ef          	jal	ra,10214 <twoSum>
   10144:	03c12083          	lw	ra,60(sp)
   10148:	00000513          	li	a0,0
   1014c:	04010113          	addi	sp,sp,64
   10150:	00008067          	ret
00010214 <twoSum>:
   10214:	0cb05063          	blez	a1,102d4 <twoSum+0xc0>
   10218:	fd010113          	addi	sp,sp,-48
   1021c:	02812423          	sw	s0,40(sp)
   10220:	03212023          	sw	s2,32(sp)
   10224:	01312e23          	sw	s3,28(sp)
   10228:	01412c23          	sw	s4,24(sp)
   1022c:	01712623          	sw	s7,12(sp)
   10230:	02112623          	sw	ra,44(sp)
   10234:	02912223          	sw	s1,36(sp)
   10238:	01512a23          	sw	s5,20(sp)
   1023c:	01612823          	sw	s6,16(sp)
   10240:	00100413          	li	s0,1
   10244:	00058993          	mv	s3,a1
   10248:	00060a13          	mv	s4,a2
   1024c:	00450913          	addi	s2,a0,4
   10250:	00021bb7          	lui	s7,0x21
   10254:	04858a63          	beq	a1,s0,102a8 <twoSum+0x94>
   10258:	00040b13          	mv	s6,s0
   1025c:	fff40a93          	addi	s5,s0,-1
   10260:	00090493          	mv	s1,s2
   10264:	00c0006f          	j	10270 <twoSum+0x5c>
   10268:	00140413          	addi	s0,s0,1
   1026c:	03345863          	bge	s0,s3,1029c <twoSum+0x88>
   10270:	0004a703          	lw	a4,0(s1)
   10274:	ffc92783          	lw	a5,-4(s2)
   10278:	00448493          	addi	s1,s1,4
   1027c:	00e787b3          	add	a5,a5,a4
   10280:	ff4794e3          	bne	a5,s4,10268 <twoSum+0x54>
   10284:	00040613          	mv	a2,s0
   10288:	000a8593          	mv	a1,s5
   1028c:	740b8513          	addi	a0,s7,1856 # 21740 <__clzsi2+0x8c>
   10290:	00140413          	addi	s0,s0,1
   10294:	264000ef          	jal	ra,104f8 <printf>
   10298:	fd344ce3          	blt	s0,s3,10270 <twoSum+0x5c>
   1029c:	001b0413          	addi	s0,s6,1
   102a0:	00490913          	addi	s2,s2,4
   102a4:	fa899ae3          	bne	s3,s0,10258 <twoSum+0x44>
   102a8:	02c12083          	lw	ra,44(sp)
   102ac:	02812403          	lw	s0,40(sp)
   102b0:	02412483          	lw	s1,36(sp)
   102b4:	02012903          	lw	s2,32(sp)
   102b8:	01c12983          	lw	s3,28(sp)
   102bc:	01812a03          	lw	s4,24(sp)
   102c0:	01412a83          	lw	s5,20(sp)
   102c4:	01012b03          	lw	s6,16(sp)
   102c8:	00c12b83          	lw	s7,12(sp)
   102cc:	03010113          	addi	sp,sp,48
   102d0:	00008067          	ret
   102d4:	00008067          	ret
```
- Observation
    - optimize compiler
    -  Register Usage
            - s-type：s0 ~ s7
            - a-type：a0 ~ a7
            - t-type：0
            - other：sp, ra
            - Total：18
    - branch instruction：14
    - lines：87

### -Ofast

- compile
`riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -Ofast -o hw2/twosum_fast -s hw2/twosum_.c`
- size
    | text  | data | bss | dec   | hex   | filename        |
    | ----- | ---- | --- | ----- | ----- | --------------- |
    | 74848 | 2816 | 812 | 78476 | 1328c | hw2/twosum_fast |
<!-- ![](https://i.imgur.com/kWTbX0X.png) -->
- ELF
![](https://i.imgur.com/vk0pGij.png)
- code
```asm=
000100c4 <main>:
   100c4:	000217b7          	lui	a5,0x21
   100c8:	74878793          	addi	a5,a5,1864 # 21748 <__clzsi2+0x94>
   100cc:	00c7a703          	lw	a4,12(a5)
   100d0:	fc010113          	addi	sp,sp,-64
   100d4:	0007a883          	lw	a7,0(a5)
   100d8:	0047a803          	lw	a6,4(a5)
   100dc:	0087a683          	lw	a3,8(a5)
   100e0:	02e12623          	sw	a4,44(sp)
   100e4:	00200713          	li	a4,2
   100e8:	00300793          	li	a5,3
   100ec:	02010513          	addi	a0,sp,32
   100f0:	00e12c23          	sw	a4,24(sp)
   100f4:	00900613          	li	a2,9
   100f8:	00400713          	li	a4,4
   100fc:	00400593          	li	a1,4
   10100:	02112e23          	sw	ra,60(sp)
   10104:	03112023          	sw	a7,32(sp)
   10108:	03012223          	sw	a6,36(sp)
   1010c:	02d12423          	sw	a3,40(sp)
   10110:	00f12a23          	sw	a5,20(sp)
   10114:	00e12e23          	sw	a4,28(sp)
   10118:	00f12623          	sw	a5,12(sp)
   1011c:	00f12823          	sw	a5,16(sp)
   10120:	0f4000ef          	jal	ra,10214 <twoSum>
   10124:	01410513          	addi	a0,sp,20
   10128:	00600613          	li	a2,6
   1012c:	00300593          	li	a1,3
   10130:	0e4000ef          	jal	ra,10214 <twoSum>
   10134:	00c10513          	addi	a0,sp,12
   10138:	00600613          	li	a2,6
   1013c:	00200593          	li	a1,2
   10140:	0d4000ef          	jal	ra,10214 <twoSum>
   10144:	03c12083          	lw	ra,60(sp)
   10148:	00000513          	li	a0,0
   1014c:	04010113          	addi	sp,sp,64
   10150:	00008067          	ret
   00010214 <twoSum>:
   10214:	0cb05063          	blez	a1,102d4 <twoSum+0xc0>
   10218:	fd010113          	addi	sp,sp,-48
   1021c:	02812423          	sw	s0,40(sp)
   10220:	03212023          	sw	s2,32(sp)
   10224:	01312e23          	sw	s3,28(sp)
   10228:	01412c23          	sw	s4,24(sp)
   1022c:	01712623          	sw	s7,12(sp)
   10230:	02112623          	sw	ra,44(sp)
   10234:	02912223          	sw	s1,36(sp)
   10238:	01512a23          	sw	s5,20(sp)
   1023c:	01612823          	sw	s6,16(sp)
   10240:	00100413          	li	s0,1
   10244:	00058993          	mv	s3,a1
   10248:	00060a13          	mv	s4,a2
   1024c:	00450913          	addi	s2,a0,4
   10250:	00021bb7          	lui	s7,0x21
   10254:	04858a63          	beq	a1,s0,102a8 <twoSum+0x94>
   10258:	00040b13          	mv	s6,s0
   1025c:	fff40a93          	addi	s5,s0,-1
   10260:	00090493          	mv	s1,s2
   10264:	00c0006f          	j	10270 <twoSum+0x5c>
   10268:	00140413          	addi	s0,s0,1
   1026c:	03345863          	bge	s0,s3,1029c <twoSum+0x88>
   10270:	0004a703          	lw	a4,0(s1)
   10274:	ffc92783          	lw	a5,-4(s2)
   10278:	00448493          	addi	s1,s1,4
   1027c:	00e787b3          	add	a5,a5,a4
   10280:	ff4794e3          	bne	a5,s4,10268 <twoSum+0x54>
   10284:	00040613          	mv	a2,s0
   10288:	000a8593          	mv	a1,s5
   1028c:	740b8513          	addi	a0,s7,1856 # 21740 <__clzsi2+0x8c>
   10290:	00140413          	addi	s0,s0,1
   10294:	264000ef          	jal	ra,104f8 <printf>
   10298:	fd344ce3          	blt	s0,s3,10270 <twoSum+0x5c>
   1029c:	001b0413          	addi	s0,s6,1
   102a0:	00490913          	addi	s2,s2,4
   102a4:	fa899ae3          	bne	s3,s0,10258 <twoSum+0x44>
   102a8:	02c12083          	lw	ra,44(sp)
   102ac:	02812403          	lw	s0,40(sp)
   102b0:	02412483          	lw	s1,36(sp)
   102b4:	02012903          	lw	s2,32(sp)
   102b8:	01c12983          	lw	s3,28(sp)
   102bc:	01812a03          	lw	s4,24(sp)
   102c0:	01412a83          	lw	s5,20(sp)
   102c4:	01012b03          	lw	s6,16(sp)
   102c8:	00c12b83          	lw	s7,12(sp)
   102cc:	03010113          	addi	sp,sp,48
   102d0:	00008067          	ret
   102d4:	00008067          	ret
```
- Observation
    - optimize compiler
    -  Register Usage
            - s-type：s0 ~ s7
            - a-type：a0 ~ a7
            - t-type：0
            - other：sp, ra
            - Total：18
    - branch instruction：14
    - lines：87

### handwritten assembly code
<!-- In this part, I try to  run the assembly code by [周士翔](https://hackmd.io/@zDmciYQATNm-8XeyJ5Th0Q/r1htullMj) on the `rv32emu` and compare it with `-Os` and `-Ofast` -->
- compile
`riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -O0 -o hw2/handwritten  hw22/handwritten.s`
- size
    | text  | data | bss | dec   | hex   | filename        |
    | ----- | ---- | --- | ----- | ----- | --------------- |
    | 74680 | 2880 | 812 | 78372 | 13224 | ./hw22/handwritten |
    
<!-- ![](https://i.imgur.com/T7ZyM6R.png) -->
- ELF
![](https://i.imgur.com/AtH9F9u.png)
- code
[In-the-above-part](##Assembly-code)
- Observation
    -  Register Usage
            - s-type：s0 ~ s2
            - a-type：a0, a1, a2, a3, a5, a6
            - t-type：0
            - other：sp, ra
            - Total：11
    - branch instruction：14
    - lines：87

## CSR cycle
Using the `rv32emu --stats` to help me record the CSR cycle, and compare with the different compiler and handwritten assembly code.
### -O0
```
01
12
01
inferior exit code 0
CSR cycle count: 4251
```
### -O1
```
01
12
01
inferior exit code 0
CSR cycle count: 4087
```
### -O2
```
01
12
01
inferior exit code 0
CSR cycle count: 4077
```
### -O3
```
01
12
01
inferior exit code 0
CSR cycle count: 4077
```
### -Ofast
```
01
12
01
inferior exit code 0
CSR cycle count: 4077
```

### handwritten 

```
01
12
01
inferior exit code 4
CSR cycle count: 3957
```
### Result
The handwritten assembly code had fewer CSR cycle when run in the `rv32emu`, besides, I found `printf` account for a great proportion of cycle. 
So if the `printf` is removed, the program will reduce thousands of cycles.


