# Assignment 2-2 Toolchain
###### tags: `Computer Architecture`
contributed by <`joe-U16`>
## Power function
program contributed by [<王傑世>](https://hackmd.io/@vvang/ryyDGoarw)
## (1) Rewrite into C implementations
Array: `2, 3, 7, 4, 1`
We sort the array from large to small with [Bubble sort](https://en.wikipedia.org/wiki/Bubble_sort).
Then, we can get the array `7, 4, 3, 2, 1`.
```
void BBSort(int *arr, int size)
{
for (int i = 0; i < size; ++i)
for (int j = 0; j < size - i; ++j)
if (arr[j] < arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
void _start()
{
volatile char *tx = (volatile char *) 0x40002000;
int arr[] = {2, 3, 7, 4, 1};
int size = 5;
BBSort(arr, size - 1);
for (int i = 0; i < size; ++i) {
*tx = (char) (arr[i] + '0');
}
}
```
**After rewrite, we can see the result with both optimization flags.**
Optimization flags = `-O1`(optimized for size)
```
➜ rv32emu git:(master) ✗ ./emu-rv32i BubbleSort1
74321
>>> Execution time: 34275 ns
>>> Instruction count: 146 (IPS=4259664)
>>> Jumps: 27 (18.49%) - 7 forwards, 20 backwards
>>> Branching T=18 (52.94%) F=16 (47.06%)
```
optimization flags = `-O3`(optimized for speed)
```
➜ rv32emu git:(master) ✗ ./emu-rv32i BubbleSort
74321
>>> Execution time: 26537 ns
>>> Instruction count: 74 (IPS=2788559)
>>> Jumps: 4 (5.41%) - 3 forwards, 1 backwards
>>> Branching T=3 (30.00%) F=7 (70.00%)
```
**The execution time has the gap between flags `-O1` and `-O3`**
`-O3` has about half Intruction count and less Jumps.
## (2) Disassemble the ELF files generated by C compiler
- [ ] [objdump](http://man7.org/linux/man-pages/man1/objdump.1.html)
> `-d`:Display the assembler mnemonics for the machine instructions
Optimization flags = `-O1`(optimized for size)
```
➜ rv32emu git:(master) ✗ riscv-none-embed-objdump -d BubbleSort1
BubbleSort1: file format elf32-littleriscv
Disassembly of section .text:
00010054 <BBSort>:
10054: 04b05863 blez a1,100a4 <BBSort+0x50>
10058: 00058813 mv a6,a1
1005c: 00050893 mv a7,a0
10060: 00259613 slli a2,a1,0x2
10064: 00c50633 add a2,a0,a2
10068: 0300006f j 10098 <BBSort+0x44>
1006c: 00478793 addi a5,a5,4
10070: 00c78e63 beq a5,a2,1008c <BBSort+0x38>
10074: 0007a703 lw a4,0(a5)
10078: 0047a683 lw a3,4(a5)
1007c: fed758e3 bge a4,a3,1006c <BBSort+0x18>
10080: 00d7a023 sw a3,0(a5)
10084: 00e7a223 sw a4,4(a5)
10088: fe5ff06f j 1006c <BBSort+0x18>
1008c: fff80813 addi a6,a6,-1
10090: ffc60613 addi a2,a2,-4
10094: 00080863 beqz a6,100a4 <BBSort+0x50>
10098: 00088793 mv a5,a7
1009c: fd004ce3 bgtz a6,10074 <BBSort+0x20>
100a0: fedff06f j 1008c <BBSort+0x38>
100a4: 00008067 ret
000100a8 <_start>:
100a8: fd010113 addi sp,sp,-48
100ac: 02112623 sw ra,44(sp)
100b0: 000107b7 lui a5,0x10
100b4: 11c78793 addi a5,a5,284 # 1011c <_start+0x74>
100b8: 0007a583 lw a1,0(a5)
100bc: 0047a603 lw a2,4(a5)
100c0: 0087a683 lw a3,8(a5)
100c4: 00c7a703 lw a4,12(a5)
100c8: 0107a783 lw a5,16(a5)
100cc: 00b12623 sw a1,12(sp)
100d0: 00c12823 sw a2,16(sp)
100d4: 00d12a23 sw a3,20(sp)
100d8: 00e12c23 sw a4,24(sp)
100dc: 00f12e23 sw a5,28(sp)
100e0: 00400593 li a1,4
100e4: 00c10513 addi a0,sp,12
100e8: f6dff0ef jal ra,10054 <BBSort>
100ec: 00c10713 addi a4,sp,12
100f0: 02010613 addi a2,sp,32
100f4: 400026b7 lui a3,0x40002
100f8: 00072783 lw a5,0(a4)
100fc: 03078793 addi a5,a5,48
10100: 0ff7f793 andi a5,a5,255
10104: 00f68023 sb a5,0(a3) # 40002000 <__global_pointer$+0x3fff06d0>
10108: 00470713 addi a4,a4,4
1010c: fec716e3 bne a4,a2,100f8 <_start+0x50>
10110: 02c12083 lw ra,44(sp)
10114: 03010113 addi sp,sp,48
10118: 00008067 ret
```
Optimization flags = `-O3`(optimized for speed)
```
➜ rv32emu git:(master) ✗ riscv-none-embed-objdump -d BubbleSort3
BubbleSort3: file format elf32-littleriscv
Disassembly of section .text:
00010054 <BBSort>:
10054: 02b05a63 blez a1,10088 <BBSort+0x34>
10058: 00259613 slli a2,a1,0x2
1005c: 00c50633 add a2,a0,a2
10060: 00050793 mv a5,a0
10064: 0007a703 lw a4,0(a5)
10068: 0047a683 lw a3,4(a5)
1006c: 00d75663 bge a4,a3,10078 <BBSort+0x24>
10070: 00d7a023 sw a3,0(a5)
10074: 00e7a223 sw a4,4(a5)
10078: 00478793 addi a5,a5,4
1007c: fec794e3 bne a5,a2,10064 <BBSort+0x10>
10080: ffc60613 addi a2,a2,-4
10084: fcc51ee3 bne a0,a2,10060 <BBSort+0xc>
10088: 00008067 ret
0001008c <_start>:
1008c: 000107b7 lui a5,0x10
10090: 1f878793 addi a5,a5,504 # 101f8 <_start+0x16c>
10094: 0007a703 lw a4,0(a5)
10098: 0047a683 lw a3,4(a5)
1009c: 0087a583 lw a1,8(a5)
100a0: 00c7a603 lw a2,12(a5)
100a4: 0107a783 lw a5,16(a5)
100a8: fe010113 addi sp,sp,-32
100ac: 00e12623 sw a4,12(sp)
100b0: 00d12823 sw a3,16(sp)
100b4: 00b12a23 sw a1,20(sp)
100b8: 00c12c23 sw a2,24(sp)
100bc: 00f12e23 sw a5,28(sp)
100c0: 00d75663 bge a4,a3,100cc <_start+0x40>
100c4: 00d12623 sw a3,12(sp)
100c8: 00e12823 sw a4,16(sp)
100cc: 01012783 lw a5,16(sp)
100d0: 01412703 lw a4,20(sp)
100d4: 00e7dc63 bge a5,a4,100ec <_start+0x60>
100d8: 00078693 mv a3,a5
100dc: 00e12823 sw a4,16(sp)
100e0: 00f12a23 sw a5,20(sp)
100e4: 00070793 mv a5,a4
100e8: 00068713 mv a4,a3
100ec: 01812603 lw a2,24(sp)
100f0: 00c75c63 bge a4,a2,10108 <_start+0x7c>
100f4: 00070693 mv a3,a4
100f8: 00c12a23 sw a2,20(sp)
100fc: 00e12c23 sw a4,24(sp)
10100: 00060713 mv a4,a2
10104: 00068613 mv a2,a3
10108: 01c12683 lw a3,28(sp)
1010c: 0cd64e63 blt a2,a3,101e8 <_start+0x15c>
10110: 00c12683 lw a3,12(sp)
10114: 00f6dc63 bge a3,a5,1012c <_start+0xa0>
10118: 00078593 mv a1,a5
1011c: 00f12623 sw a5,12(sp)
10120: 00d12823 sw a3,16(sp)
10124: 00068793 mv a5,a3
10128: 00058693 mv a3,a1
1012c: 00e7dc63 bge a5,a4,10144 <_start+0xb8>
10130: 00078593 mv a1,a5
10134: 00e12823 sw a4,16(sp)
10138: 00f12a23 sw a5,20(sp)
1013c: 00070793 mv a5,a4
10140: 00058713 mv a4,a1
10144: 08c74a63 blt a4,a2,101d8 <_start+0x14c>
10148: 00f6dc63 bge a3,a5,10160 <_start+0xd4>
1014c: 00078613 mv a2,a5
10150: 00f12623 sw a5,12(sp)
10154: 00d12823 sw a3,16(sp)
10158: 00068793 mv a5,a3
1015c: 00060693 mv a3,a2
10160: 00e7d863 bge a5,a4,10170 <_start+0xe4>
10164: 00f12a23 sw a5,20(sp)
10168: 00e12823 sw a4,16(sp)
1016c: 00070793 mv a5,a4
10170: 00f6d663 bge a3,a5,1017c <_start+0xf0>
10174: 00f12623 sw a5,12(sp)
10178: 00d12823 sw a3,16(sp)
1017c: 00c12703 lw a4,12(sp)
10180: 400027b7 lui a5,0x40002
10184: 03070713 addi a4,a4,48
10188: 0ff77713 andi a4,a4,255
1018c: 00e78023 sb a4,0(a5) # 40002000 <__global_pointer$+0x3fff05f4>
10190: 01012703 lw a4,16(sp)
10194: 03070713 addi a4,a4,48
10198: 0ff77713 andi a4,a4,255
1019c: 00e78023 sb a4,0(a5)
101a0: 01412703 lw a4,20(sp)
101a4: 03070713 addi a4,a4,48
101a8: 0ff77713 andi a4,a4,255
101ac: 00e78023 sb a4,0(a5)
101b0: 01812703 lw a4,24(sp)
101b4: 03070713 addi a4,a4,48
101b8: 0ff77713 andi a4,a4,255
101bc: 00e78023 sb a4,0(a5)
101c0: 01c12703 lw a4,28(sp)
101c4: 03070713 addi a4,a4,48
101c8: 0ff77713 andi a4,a4,255
101cc: 00e78023 sb a4,0(a5)
101d0: 02010113 addi sp,sp,32
101d4: 00008067 ret
101d8: 00e12c23 sw a4,24(sp)
101dc: 00c12a23 sw a2,20(sp)
101e0: 00060713 mv a4,a2
101e4: f65ff06f j 10148 <_start+0xbc>
101e8: 00c12e23 sw a2,28(sp)
101ec: 00d12c23 sw a3,24(sp)
101f0: 00068613 mv a2,a3
101f4: f1dff06f j 10110 <_start+0x84>
```
- [ ] [readelf](http://man7.org/linux/man-pages/man1/readelf.1.html)
> `-h`:Display the ELF file header
Optimization flags = `-O1`(optimized for size)
```
➜ rv32emu git:(master) ✗ riscv-none-embed-readelf -h BubbleSort1
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: 0x100a8
Start of program headers: 52 (bytes into file)
Start of section headers: 736 (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: 7
Section header string table index: 6
```
Optimization flags = `-O3`(optimized for speed)
```
➜ rv32emu git:(master) ✗ riscv-none-embed-readelf -h BubbleSort3
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: 0x1008c
Start of program headers: 52 (bytes into file)
Start of section headers: 956 (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: 7
Section header string table index: 6
```
- [ ] [size](http://man7.org/linux/man-pages/man1/size.1.html)
>list the section sizes—and the total size—for each of the object or archive files objfile in its argument list.
`➜ riscv-none-embed-size`
flags = `-O1`
```
➜ rv32emu git:(master) ✗ riscv-none-embed-size BubbleSort1
text data bss dec hex filename
220 0 0 220 dc BubbleSort1
```
flags = `-O3`
```
➜ rv32emu git:(master) ✗ riscv-none-embed-size BubbleSort3
text data bss dec hex filename
440 0 0 440 1b8 BubbleSort3
```