---
tags: Computer Architecture (2022 Fall)
---
# Assignment2: RISC-V Toolchain
<ref>[馮柏為](https://hackmd.io/@r1YLxwFRRPe1xninh0Ma6w/BygEuPtfo) [leetcode1732 Find the Highest Altitude](https://leetcode.com/problems/find-the-highest-altitude/)
I choose this program because it is simple and it can make the good comparison between auto generated and handwritten assembly code.
## Auto generated by compiler with several optimzed options
### c code
I refer [馮柏為](https://hackmd.io/@r1YLxwFRRPe1xninh0Ma6w/BygEuPtfo) hw1 c code
```c=
#include<stdio.h>
int largestAltitude(int* gain, int gainSize){
int i, max, arti;
arti = 0;
max=arti;
for(i=0;i<gainSize;i++){
arti=arti+gain[i];
if(arti>max){max = arti;}
}
return max;
}
int main(){
int gain[]={-1,5,4};
int gainSize = 3;
int max = largestAltitude(gain,gainSize);
printf("max1=%d\n",max);
int gain2[]={-5,1,5,0,-7};
gainSize = 5;
max = largestAltitude(gain2,gainSize);
printf("max2=%d\n",max);
int gain3[]={-5,1,3};
gainSize = 3;
max = largestAltitude(gain3,gainSize);
printf("max3=%d\n",max);
return 0 ;
}
```
### O0
```
00010184 <largestAltitude>:
10184: fd010113 addi sp,sp,-48
10188: 02812623 sw s0,44(sp)
1018c: 03010413 addi s0,sp,48
10190: fca42e23 sw a0,-36(s0)
10194: fcb42c23 sw a1,-40(s0)
10198: fe042223 sw zero,-28(s0)
1019c: fe442783 lw a5,-28(s0)
101a0: fef42423 sw a5,-24(s0)
101a4: fe042623 sw zero,-20(s0)
101a8: 0440006f j 101ec <largestAltitude+0x68>
101ac: fec42783 lw a5,-20(s0)
101b0: 00279793 slli a5,a5,0x2
101b4: fdc42703 lw a4,-36(s0)
101b8: 00f707b3 add a5,a4,a5
101bc: 0007a783 lw a5,0(a5)
101c0: fe442703 lw a4,-28(s0)
101c4: 00f707b3 add a5,a4,a5
101c8: fef42223 sw a5,-28(s0)
101cc: fe442703 lw a4,-28(s0)
101d0: fe842783 lw a5,-24(s0)
101d4: 00e7d663 bge a5,a4,101e0 <largestAltitude+0x5c>
101d8: fe442783 lw a5,-28(s0)
101dc: fef42423 sw a5,-24(s0)
101e0: fec42783 lw a5,-20(s0)
101e4: 00178793 addi a5,a5,1
101e8: fef42623 sw a5,-20(s0)
101ec: fec42703 lw a4,-20(s0)
101f0: fd842783 lw a5,-40(s0)
101f4: faf74ce3 blt a4,a5,101ac <largestAltitude+0x28>
101f8: fe842783 lw a5,-24(s0)
101fc: 00078513 mv a0,a5
10200: 02c12403 lw s0,44(sp)
10204: 03010113 addi sp,sp,48
10208: 00008067 ret
0001020c <main>:
1020c: fb010113 addi sp,sp,-80
10210: 04112623 sw ra,76(sp)
10214: 04812423 sw s0,72(sp)
10218: 05010413 addi s0,sp,80
1021c: fff00793 li a5,-1
10220: fcf42e23 sw a5,-36(s0)
10224: 00500793 li a5,5
10228: fef42023 sw a5,-32(s0)
1022c: 00400793 li a5,4
10230: fef42223 sw a5,-28(s0)
10234: 00300793 li a5,3
10238: fef42623 sw a5,-20(s0)
1023c: fdc40793 addi a5,s0,-36
10240: fec42583 lw a1,-20(s0)
10244: 00078513 mv a0,a5
10248: f3dff0ef jal ra,10184 <largestAltitude>
1024c: fea42423 sw a0,-24(s0)
10250: fe842583 lw a1,-24(s0)
10254: 000217b7 lui a5,0x21
10258: 78078513 addi a0,a5,1920 # 21780 <__clzsi2+0x8c>
1025c: 2dc000ef jal ra,10538 <printf>
10260: 000217b7 lui a5,0x21
10264: 7a478793 addi a5,a5,1956 # 217a4 <__clzsi2+0xb0>
10268: 0007a583 lw a1,0(a5)
1026c: 0047a603 lw a2,4(a5)
10270: 0087a683 lw a3,8(a5)
10274: 00c7a703 lw a4,12(a5)
10278: 0107a783 lw a5,16(a5)
1027c: fcb42423 sw a1,-56(s0)
10280: fcc42623 sw a2,-52(s0)
10284: fcd42823 sw a3,-48(s0)
10288: fce42a23 sw a4,-44(s0)
1028c: fcf42c23 sw a5,-40(s0)
10290: 00500793 li a5,5
10294: fef42623 sw a5,-20(s0)
10298: fc840793 addi a5,s0,-56
1029c: fec42583 lw a1,-20(s0)
102a0: 00078513 mv a0,a5
102a4: ee1ff0ef jal ra,10184 <largestAltitude>
102a8: fea42423 sw a0,-24(s0)
102ac: fe842583 lw a1,-24(s0)
102b0: 000217b7 lui a5,0x21
102b4: 78c78513 addi a0,a5,1932 # 2178c <__clzsi2+0x98>
102b8: 280000ef jal ra,10538 <printf>
102bc: ffb00793 li a5,-5
102c0: faf42e23 sw a5,-68(s0)
102c4: 00100793 li a5,1
102c8: fcf42023 sw a5,-64(s0)
102cc: 00300793 li a5,3
102d0: fcf42223 sw a5,-60(s0)
102d4: 00300793 li a5,3
102d8: fef42623 sw a5,-20(s0)
102dc: fbc40793 addi a5,s0,-68
102e0: fec42583 lw a1,-20(s0)
102e4: 00078513 mv a0,a5
102e8: e9dff0ef jal ra,10184 <largestAltitude>
102ec: fea42423 sw a0,-24(s0)
102f0: fe842583 lw a1,-24(s0)
102f4: 000217b7 lui a5,0x21
102f8: 79878513 addi a0,a5,1944 # 21798 <__clzsi2+0xa4>
102fc: 23c000ef jal ra,10538 <printf>
10300: 00000793 li a5,0
10304: 00078513 mv a0,a5
10308: 04c12083 lw ra,76(sp)
1030c: 04812403 lw s0,72(sp)
10310: 05010113 addi sp,sp,80
10314: 00008067 ret
```
```
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: RISC-V
Version: 0x1
Entry point address: 0x100dc
Start of program headers: 52 (bytes into file)
Start of section headers: 94896 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 3
Size of section headers: 40 (bytes)
Number of section headers: 15
Section header string table index: 14
```
```
text data bss dec hex filename
74944 2816 812 78572 132ec ./o0/FHA_o0
```
### O1
```
00010184 <largestAltitude>:
10184: 02b05e63 blez a1,101c0 <largestAltitude+0x3c>
10188: 00050793 mv a5,a0
1018c: 00259593 slli a1,a1,0x2
10190: 00b50633 add a2,a0,a1
10194: 00000713 li a4,0
10198: 00000513 li a0,0
1019c: 00c0006f j 101a8 <largestAltitude+0x24>
101a0: 00478793 addi a5,a5,4
101a4: 00c78c63 beq a5,a2,101bc <largestAltitude+0x38>
101a8: 0007a683 lw a3,0(a5)
101ac: 00d70733 add a4,a4,a3
101b0: fee558e3 bge a0,a4,101a0 <largestAltitude+0x1c>
101b4: 00070513 mv a0,a4
101b8: fe9ff06f j 101a0 <largestAltitude+0x1c>
101bc: 00008067 ret
101c0: 00000513 li a0,0
101c4: 00008067 ret
000101c8 <main>:
101c8: fc010113 addi sp,sp,-64
101cc: 02112e23 sw ra,60(sp)
101d0: fff00793 li a5,-1
101d4: 02f12223 sw a5,36(sp)
101d8: 00500793 li a5,5
101dc: 02f12423 sw a5,40(sp)
101e0: 00400793 li a5,4
101e4: 02f12623 sw a5,44(sp)
101e8: 00300593 li a1,3
101ec: 02410513 addi a0,sp,36
101f0: f95ff0ef jal ra,10184 <largestAltitude>
101f4: 00050593 mv a1,a0
101f8: 00021537 lui a0,0x21
101fc: 6f850513 addi a0,a0,1784 # 216f8 <__clzsi2+0x88>
10200: 2b4000ef jal ra,104b4 <printf>
10204: 000217b7 lui a5,0x21
10208: 71c78793 addi a5,a5,1820 # 2171c <__clzsi2+0xac>
1020c: 0007a583 lw a1,0(a5)
10210: 0047a603 lw a2,4(a5)
10214: 0087a683 lw a3,8(a5)
10218: 00c7a703 lw a4,12(a5)
1021c: 0107a783 lw a5,16(a5)
10220: 00b12823 sw a1,16(sp)
10224: 00c12a23 sw a2,20(sp)
10228: 00d12c23 sw a3,24(sp)
1022c: 00e12e23 sw a4,28(sp)
10230: 02f12023 sw a5,32(sp)
10234: 00500593 li a1,5
10238: 01010513 addi a0,sp,16
1023c: f49ff0ef jal ra,10184 <largestAltitude>
10240: 00050593 mv a1,a0
10244: 00021537 lui a0,0x21
10248: 70450513 addi a0,a0,1796 # 21704 <__clzsi2+0x94>
1024c: 268000ef jal ra,104b4 <printf>
10250: ffb00793 li a5,-5
10254: 00f12223 sw a5,4(sp)
10258: 00100793 li a5,1
1025c: 00f12423 sw a5,8(sp)
10260: 00300793 li a5,3
10264: 00f12623 sw a5,12(sp)
10268: 00300593 li a1,3
1026c: 00410513 addi a0,sp,4
10270: f15ff0ef jal ra,10184 <largestAltitude>
10274: 00050593 mv a1,a0
10278: 00021537 lui a0,0x21
1027c: 71050513 addi a0,a0,1808 # 21710 <__clzsi2+0xa0>
10280: 234000ef jal ra,104b4 <printf>
10284: 00000513 li a0,0
10288: 03c12083 lw ra,60(sp)
1028c: 04010113 addi sp,sp,64
10290: 00008067 ret
```
```
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: RISC-V
Version: 0x1
Entry point address: 0x100dc
Start of program headers: 52 (bytes into file)
Start of section headers: 94896 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 3
Size of section headers: 40 (bytes)
Number of section headers: 15
Section header string table index: 14
```
```
text data bss dec hex filename
74812 2816 812 78440 13268 ./o1/FHA_o1
```
### O2
```
000100c4 <main>:
100c4: 00021537 lui a0,0x21
100c8: fd010113 addi sp,sp,-48
100cc: 00800593 li a1,8
100d0: 6c050513 addi a0,a0,1728 # 216c0 <__clzsi2+0x88>
100d4: 02112623 sw ra,44(sp)
100d8: 3a4000ef jal ra,1047c <printf>
100dc: 000217b7 lui a5,0x21
100e0: 6e478793 addi a5,a5,1764 # 216e4 <__clzsi2+0xac>
100e4: 0007a583 lw a1,0(a5)
100e8: 0047a603 lw a2,4(a5)
100ec: 00c7a703 lw a4,12(a5)
100f0: 0087a683 lw a3,8(a5)
100f4: 0107a783 lw a5,16(a5)
100f8: 00b12623 sw a1,12(sp)
100fc: 00c12823 sw a2,16(sp)
10100: 00e12c23 sw a4,24(sp)
10104: 00f12e23 sw a5,28(sp)
10108: 00d12a23 sw a3,20(sp)
1010c: 00c10793 addi a5,sp,12
10110: 02010613 addi a2,sp,32
10114: 00000593 li a1,0
10118: 00000713 li a4,0
1011c: 0007a683 lw a3,0(a5)
10120: 00478793 addi a5,a5,4
10124: 00d70733 add a4,a4,a3
10128: 00e5d463 bge a1,a4,10130 <main+0x6c>
1012c: 00070593 mv a1,a4
10130: fef616e3 bne a2,a5,1011c <main+0x58>
10134: 00021537 lui a0,0x21
10138: 6cc50513 addi a0,a0,1740 # 216cc <__clzsi2+0x94>
1013c: 340000ef jal ra,1047c <printf>
10140: 00021537 lui a0,0x21
10144: 00000593 li a1,0
10148: 6d850513 addi a0,a0,1752 # 216d8 <__clzsi2+0xa0>
1014c: 330000ef jal ra,1047c <printf>
10150: 02c12083 lw ra,44(sp)
10154: 00000513 li a0,0
10158: 03010113 addi sp,sp,48
1015c: 00008067 ret
```
```
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: RISC-V
Version: 0x1
Entry point address: 0x10178
Start of program headers: 52 (bytes into file)
Start of section headers: 94912 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 3
Size of section headers: 40 (bytes)
Number of section headers: 15
Section header string table index: 14
```
```
text data bss dec hex filename
74756 2816 812 78384 13230 ./o2/FHA_o2
```
### O3
```
000100c4 <main>:
100c4: 00021537 lui a0,0x21
100c8: ff010113 addi sp,sp,-16
100cc: 00800593 li a1,8
100d0: 67050513 addi a0,a0,1648 # 21670 <__clzsi2+0x8c>
100d4: 00112623 sw ra,12(sp)
100d8: 350000ef jal ra,10428 <printf>
100dc: 00021537 lui a0,0x21
100e0: 00100593 li a1,1
100e4: 67c50513 addi a0,a0,1660 # 2167c <__clzsi2+0x98>
100e8: 340000ef jal ra,10428 <printf>
100ec: 00021537 lui a0,0x21
100f0: 00000593 li a1,0
100f4: 68850513 addi a0,a0,1672 # 21688 <__clzsi2+0xa4>
100f8: 330000ef jal ra,10428 <printf>
100fc: 00c12083 lw ra,12(sp)
10100: 00000513 li a0,0
10104: 01010113 addi sp,sp,16
10108: 00008067 ret
```
```
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: RISC-V
Version: 0x1
Entry point address: 0x10124
Start of program headers: 52 (bytes into file)
Start of section headers: 94912 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 3
Size of section headers: 40 (bytes)
Number of section headers: 15
Section header string table index: 14
```
```
text data bss dec hex filename
74656 2816 812 78284 131cc ./o3/FHA_o3
```
### Ofast
```
000100c4 <main>:
100c4: 00021537 lui a0,0x21
100c8: ff010113 addi sp,sp,-16
100cc: 00800593 li a1,8
100d0: 67050513 addi a0,a0,1648 # 21670 <__clzsi2+0x8c>
100d4: 00112623 sw ra,12(sp)
100d8: 350000ef jal ra,10428 <printf>
100dc: 00021537 lui a0,0x21
100e0: 00100593 li a1,1
100e4: 67c50513 addi a0,a0,1660 # 2167c <__clzsi2+0x98>
100e8: 340000ef jal ra,10428 <printf>
100ec: 00021537 lui a0,0x21
100f0: 00000593 li a1,0
100f4: 68850513 addi a0,a0,1672 # 21688 <__clzsi2+0xa4>
100f8: 330000ef jal ra,10428 <printf>
100fc: 00c12083 lw ra,12(sp)
10100: 00000513 li a0,0
10104: 01010113 addi sp,sp,16
10108: 00008067 ret
```
```
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: RISC-V
Version: 0x1
Entry point address: 0x10124
Start of program headers: 52 (bytes into file)
Start of section headers: 94912 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 3
Size of section headers: 40 (bytes)
Number of section headers: 15
Section header string table index: 14
```
```
text data bss dec hex filename
74656 2816 812 78284 131cc ./ofast/FHA_ofast
```
### Os
```
000100c4 <main>:
100c4: fc010113 addi sp,sp,-64
100c8: 000215b7 lui a1,0x21
100cc: 02812c23 sw s0,56(sp)
100d0: 00c00613 li a2,12
100d4: 6f458413 addi s0,a1,1780 # 216f4 <__clzsi2+0xb0>
100d8: 00410513 addi a0,sp,4
100dc: 6f458593 addi a1,a1,1780
100e0: 02112e23 sw ra,60(sp)
100e4: 288000ef jal ra,1036c <memcpy>
100e8: 00300593 li a1,3
100ec: 00410513 addi a0,sp,4
100f0: 140000ef jal ra,10230 <largestAltitude>
100f4: 00050593 mv a1,a0
100f8: 00021537 lui a0,0x21
100fc: 6d050513 addi a0,a0,1744 # 216d0 <__clzsi2+0x8c>
10100: 52c000ef jal ra,1062c <printf>
10104: 01400613 li a2,20
10108: 00c40593 addi a1,s0,12
1010c: 01c10513 addi a0,sp,28
10110: 25c000ef jal ra,1036c <memcpy>
10114: 00500593 li a1,5
10118: 01c10513 addi a0,sp,28
1011c: 114000ef jal ra,10230 <largestAltitude>
10120: 00050593 mv a1,a0
10124: 00021537 lui a0,0x21
10128: 6dc50513 addi a0,a0,1756 # 216dc <__clzsi2+0x98>
1012c: 500000ef jal ra,1062c <printf>
10130: 00c00613 li a2,12
10134: 02040593 addi a1,s0,32
10138: 01010513 addi a0,sp,16
1013c: 230000ef jal ra,1036c <memcpy>
10140: 00300593 li a1,3
10144: 01010513 addi a0,sp,16
10148: 0e8000ef jal ra,10230 <largestAltitude>
1014c: 00050593 mv a1,a0
10150: 00021537 lui a0,0x21
10154: 6e850513 addi a0,a0,1768 # 216e8 <__clzsi2+0xa4>
10158: 4d4000ef jal ra,1062c <printf>
1015c: 03c12083 lw ra,60(sp)
10160: 03812403 lw s0,56(sp)
10164: 00000513 li a0,0
10168: 04010113 addi sp,sp,64
1016c: 00008067 ret
00010230 <largestAltitude>:
10230: 00050693 mv a3,a0
10234: 00000713 li a4,0
10238: 00000513 li a0,0
1023c: 00000793 li a5,0
10240: 00b7c463 blt a5,a1,10248 <largestAltitude+0x18>
10244: 00008067 ret
10248: 00279613 slli a2,a5,0x2
1024c: 00c68633 add a2,a3,a2
10250: 00062603 lw a2,0(a2)
10254: 00c70733 add a4,a4,a2
10258: 00e55463 bge a0,a4,10260 <largestAltitude+0x30>
1025c: 00070513 mv a0,a4
10260: 00178793 addi a5,a5,1
10264: fddff06f j 10240 <largestAltitude+0x10>
```
```
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: RISC-V
Version: 0x1
Entry point address: 0x10188
Start of program headers: 52 (bytes into file)
Start of section headers: 94912 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 3
Size of section headers: 40 (bytes)
Number of section headers: 15
Section header string table index: 14
```
```
text data bss dec hex filename
74792 2816 812 78420 13254 ./os/FHA_os
```
### additional discovery
-O2, -O3, -Ofast in this codes will compute the return value of largestAltitude at compliation time. Then directly use the return value without calling largestAltitude function. As a result, they execute so fast.
### script for compiling c code with rv32emu & auto generated assembly codes
step:
1. compile .c to .s
2. assemble, link and load .s to executable file
3. dump the elf header,disassemble and CSR cycle for obersevation
4. repeat the step with different optimized options
```bash=
#!/bin/bash
#o1
riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -o ./o1/FHA_o1.s -S -O1 FHA.c
riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -o ./o1/FHA_o1 ./o1/FHA_o1.s
riscv-none-elf-readelf -h ./o1/FHA_o1 > ./o1/readelf.txt
riscv-none-elf-size ./o1/FHA_o1 > ./o1/size.txt
riscv-none-elf-objdump -d ./o1/FHA_o1 > ./o1/objdump.txt
../build/rv32emu ./o1/FHA_o1 --stats
#o2
riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -o ./o2/FHA_o2.s -S -O2 FHA.c
riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -o ./o2/FHA_o2 ./o2/FHA_o2.s
riscv-none-elf-readelf -h ./o2/FHA_o2 > ./o2/readelf.txt
riscv-none-elf-size ./o2/FHA_o2 > ./o2/size.txt
riscv-none-elf-objdump -d ./o2/FHA_o2 > ./o2/objdump.txt
../build/rv32emu ./o2/FHA_o2 --stats
#o3
riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -o ./o3/FHA_o3.s -S -O3 FHA.c
riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -o ./o3/FHA_o3 ./o3/FHA_o3.s
riscv-none-elf-readelf -h ./o3/FHA_o3 > ./o3/readelf.txt
riscv-none-elf-size ./o3/FHA_o3 > ./o3/size.txt
riscv-none-elf-objdump -d ./o3/FHA_o3 > ./o3/objdump.txt
../build/rv32emu ./o3/FHA_o3 --stats
#ofast
riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -o ./ofast/FHA_ofast.s -S -Ofast FHA.c
riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -o ./ofast/FHA_ofast ./ofast/FHA_ofast.s
riscv-none-elf-readelf -h ./ofast/FHA_ofast > ./ofast/readelf.txt
riscv-none-elf-size ./ofast/FHA_ofast > ./o3/size.txt
riscv-none-elf-objdump -d ./ofast/FHA_ofast > ./ofast/objdump.txt
../build/rv32emu ./ofast/FHA_ofast --stats
```
## handwritten assembly
I refer [馮柏為](https://hackmd.io/@r1YLxwFRRPe1xninh0Ma6w/BygEuPtfo)'s hw1 assembly code
His codes are originally compatable to Ripes but not rv32emu.
So, I have to do the following modification
1. header
.file "FHA.c"
.option nopic
.attribute arch, "rv32i2p1"
.attribute unaligned_access, 0
.attribute stack_align, 16
1. label recognize & code segement size
to specify the func block begins
.text
.align 2
.globl func
.type func, @function
to specify the func block ends
.size func, .-func
1. keep the caller of main function
observe the objdump file, I found that there are not only main and functions main will call. Actually, cpu fetchs and executes some routine codes then enter main function scope. After main return, it has to keep execute next instruction, so we have to keep the ra of main in stack.

1. main will retrun a0 as inferior exit code 0 in rv32emu
return 0 so I set a0 as 0 in assembly

```
ps. In Ripes, we wrtie
li a7 10
ecall
for retun main function
```
5. printf
```
In Ripes, we write the value into a0 for printf
mv a0 a3
li a7 1
ecall
```
```
In rv32emu, we write value into a1, and using the following codes (example)
lui a5,%hi(.LC1)
addi a0,a5,%lo(.LC1)
call printf
.LC1:
.string "max = %d\n"
.align 2
```
### whole handwrite assembly code for rv32emu
```
.file "FHA.c"
.option nopic
.attribute arch, "rv32i2p1"
.attribute unaligned_access, 0
.attribute stack_align, 16
.data
gain: .word -1,5,4
gainsize: .word 3
gain2: .word -5,1,5,0,-7
gainsize2: .word 5
gain3: .word -5,1,3
gainsize3: .word 3
.LC1:
.string "max1=%d\n"
.align 2
.text
.align 2
.globl func
.type func, @function
func:
mv t1, x0 #int i = 0
mv a2, x0 #arti = 0
mv a3, a2 #max=arti
loop:
lw a1, 0(s0) #load gain[i]
addi s0, s0, 4
add a2, a2 ,a1 #arti=arti+gain[i]
blt a2, a3 ,conti #if arti[i]<max
mv a3, a2
conti:
addi t1, t1 ,1 # i++
blt t1, s1, loop # i<gainsize jump to loop conti
mv a1 ,a3
#li a7 ,1
#ecall
jr ra
.size func, .-func
.text
.align 2
.globl main
.type main, @function
main:
addi sp,sp,-32
sw ra,28(sp)
la s0, gain #load gain address
lw s1, gainsize
call func
lui a5,%hi(.LC1)
addi a0,a5,%lo(.LC1)
call printf
la s0, gain2 #load gain2 address
lw s1, gainsize2
call func
lui a5,%hi(.LC1)
addi a0,a5,%lo(.LC1)
call printf
la s0, gain3 #load gain3 address
lw s1, gainsize3
jal func
lui a5,%hi(.LC1)
addi a0,a5,%lo(.LC1)
call printf
#li a7 10
#ecall
addi a0,x0,0
lw ra,28(sp)
addi sp,sp,32
jr ra
.size main, .-main
.ident "GCC: (xPack GNU RISC-V Embedded GCC x86_64) 12.2.0"
```
script
1. assemble, link and load .s to executable file
1. dump the elf header,disassemble and CSR cycle for obersevation
```bash=
riscv-none-elf-gcc -march=rv32i -mabi=ilp32 -o FHA -Ofast FHA_handwrite.s
../../build/rv32emu FHA --stats
```
## comparison
### size
| text | data | bss | dec | hex | filename |
| - | - |- |- |- |- |
| 74944 | 2816 | 812 | 78571 | 132ec | ./o0/FHA_o0 |
| 74812 | 2816 | 812 | 78440 | 13268 |./o1/FHA_o1 |
| 74756| 2816| 812| 78384| 13230|./o2/FHA_o2|
|74656 | 2816 | 812| 78284| 131cc|./o3/FHA_o3|
| 74656| 2816 | 812| 78284| 131cc| ./ofast/FHA_ofast|
|74792| 2816 | 812 |78420| 13254 |./os/FHA_os|
|74620|2888|812|78320|131f0|./handwrite/FHA|
### CSR cycle
|CSR cycle|option|
|-|-|
|3800|os|
|3800|o0|
|3619|o1|
|3569|o2|
|3538|o3|
|3538|ofast|
|3633|handwritten|
## virtual box install
於 Oracle VirtualBox 中安裝 Ubuntu
https://learningsky.io/install-ubuntu-on-oracle-virtualbox/