owned this note
owned this note
Published
Linked with GitHub
# Assignment2: RISC-V Toolchain Bubble Sort
###### tags: `Computer architure 2020`, `riscv`, `rv32emu`
The original program was implemented by 王傑世, and I took the sample lab as a reference.
C Program:
####
```c=
void _start() {
int tmp;
int c=0;
int *arr = (int*)0x15000;
arr[0] = 2;
arr[1] = 3;
arr[2] = 7;
arr[3] = 4;
arr[4] = 1;
for( int i = 0; i < 4; i++ ) {
for( int j = 0; j < 4 - i; j++ ) {
if( arr[j] > arr[j + 1] ) {
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
volatile int* tx = (volatile int*)0x40002000;
while( c < 5 ) {
*tx = *arr;
arr++;
c++;
}
}
```
####
-Os
```
./emu-rv32i test1
illegal write 32, PC: 0x000100d0, address: 0x40002000
>>> Execution time: 77174 ns
>>> Instruction count: 127 (IPS=1645631)
>>> Jumps: 23 (18.11%) - 10 forwards, 13 backwards
>>> Branching T=19 (67.86%) F=9 (32.14%)
```
-O1
```
./emu-rv32i test1
illegal write 32, PC: 0x000100d4, address: 0x40002000
>>> Execution time: 96923 ns
>>> Instruction count: 111 (IPS=1145239)
>>> Jumps: 20 (18.02%) - 6 forwards, 14 backwards
>>> Branching T=15 (53.57%) F=13 (46.43%)
```
-O2
```
./emu-rv32i test1
illegal write 32, PC: 0x000100bc, address: 0x40002000
>>> Execution time: 81763 ns
>>> Instruction count: 97 (IPS=1186355)
>>> Jumps: 15 (15.46%) - 6 forwards, 9 backwards
>>> Branching T=15 (62.50%) F=9 (37.50%)
```
-O3
```
./emu-rv32i test1
illegal write 32, PC: 0x00010128, address: 0x40002000
>>> Execution time: 30513 ns
>>> Instruction count: 47 (IPS=1540327)
>>> Jumps: 4 (8.51%) - 4 forwards, 0 backwards
>>> Branching T=4 (57.14%) F=3 (42.86%)
```
objdump:
```
test1: file format elf32-littleriscv
Disassembly of section .text:
00010054 <_start>:
10054: 000157b7 lui a5,0x15
10058: 00300613 li a2,3
1005c: 00c7a023 sw a2,0(a5) # 15000 <__global_pointer$+0x36ac>
10060: 00100613 li a2,1
10064: 00600713 li a4,6
10068: 00500693 li a3,5
1006c: 00c7a223 sw a2,4(a5)
10070: 00400613 li a2,4
10074: 00d7a823 sw a3,16(a5)
10078: 00c7a423 sw a2,8(a5)
1007c: 00e7a623 sw a4,12(a5)
10080: 00e6d663 bge a3,a4,1008c <_start+0x38>
10084: 00d7a623 sw a3,12(a5)
10088: 00e7a823 sw a4,16(a5)
1008c: 000157b7 lui a5,0x15
10090: 0007a703 lw a4,0(a5) # 15000 <__global_pointer$+0x36ac>
10094: 0047a683 lw a3,4(a5)
10098: 00e6d663 bge a3,a4,100a4 <_start+0x50>
1009c: 00d7a023 sw a3,0(a5)
100a0: 00e7a223 sw a4,4(a5)
100a4: 000157b7 lui a5,0x15
100a8: 0047a703 lw a4,4(a5) # 15004 <__global_pointer$+0x36b0>
100ac: 0087a683 lw a3,8(a5)
100b0: 00e6d663 bge a3,a4,100bc <_start+0x68>
100b4: 00d7a223 sw a3,4(a5)
100b8: 00e7a423 sw a4,8(a5)
100bc: 000157b7 lui a5,0x15
100c0: 0087a703 lw a4,8(a5) # 15008 <__global_pointer$+0x36b4>
100c4: 00c7a683 lw a3,12(a5)
100c8: 08e6c063 blt a3,a4,10148 <_start+0xf4>
100cc: 000157b7 lui a5,0x15
100d0: 0007a703 lw a4,0(a5) # 15000 <__global_pointer$+0x36ac>
100d4: 0047a683 lw a3,4(a5)
100d8: 00e6d663 bge a3,a4,100e4 <_start+0x90>
100dc: 00d7a023 sw a3,0(a5)
100e0: 00e7a223 sw a4,4(a5)
100e4: 000157b7 lui a5,0x15
100e8: 0047a703 lw a4,4(a5) # 15004 <__global_pointer$+0x36b0>
100ec: 0087a683 lw a3,8(a5)
100f0: 00e6d663 bge a3,a4,100fc <_start+0xa8>
100f4: 00d7a223 sw a3,4(a5)
100f8: 00e7a423 sw a4,8(a5)
100fc: 000157b7 lui a5,0x15
10100: 0007a703 lw a4,0(a5) # 15000 <__global_pointer$+0x36ac>
10104: 0047a683 lw a3,4(a5)
10108: 00e6d663 bge a3,a4,10114 <_start+0xc0>
1010c: 00d7a023 sw a3,0(a5)
10110: 00e7a223 sw a4,4(a5)
10114: 00015737 lui a4,0x15
10118: 00072683 lw a3,0(a4) # 15000 <__global_pointer$+0x36ac>
1011c: 00472583 lw a1,4(a4)
10120: 400027b7 lui a5,0x40002
10124: 00872603 lw a2,8(a4)
10128: 00d7a023 sw a3,0(a5) # 40002000 <__global_pointer$+0x3fff06ac>
1012c: 00c72683 lw a3,12(a4)
10130: 00b7a023 sw a1,0(a5)
10134: 01072703 lw a4,16(a4)
10138: 00c7a023 sw a2,0(a5)
1013c: 00d7a023 sw a3,0(a5)
10140: 00e7a023 sw a4,0(a5)
10144: 00008067 ret
10148: 00d7a423 sw a3,8(a5)
1014c: 00e7a623 sw a4,12(a5)
10150: f7dff06f j 100cc <_start+0x78>
```
readelf
```
riscv-none-embed-readelf -h test1
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: 0x10054
Start of program headers: 52 (bytes into file)
Start of section headers: 608 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 1
Size of section headers: 40 (bytes)
Number of section headers: 6
Section header string table index: 5
```
size:
```
text data bss dec hex filename
256 0 0 256 100 test1
```