# Assignment2: RISC-V Toolchain (Print Square)
###### tags: `RISC-V`
> Rewrite assignment from [謝宜紘](https://hackmd.io/@hsieh22/CA_lab1)
```c=
# include <stdio.h>
void _start()
{
int n, i, j;
volatile char *tx = (volatile char *)0x40002000;
const char *res = "Input the length of square:\n";
for (i = 0; i < 5; i++){
for (j = 0; j < 5; j++){
*tx = '*' & 0x000000ff;
*tx = ' ' & 0x000000ff;
}
*tx = '\n';
}
}
```
## -O3
`riscv-none-embed-gcc -march=rv32i -mabi=ilp32 -O3 -nostdlib Squre.c -o Square`
```
./emu-rv32i Square
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
>>> Execution time: 62700 ns
>>> Instruction count: 61 (IPS=972886)
>>> Jumps: 1 (1.64%) - 0 forwards, 1 backwards
>>> Branching T=0 (-nan%) F=0 (-nan%)
```
#####
:::spoiler `riscv-none-embed-objdump -d Square`
```
00010054 <_start>:
10054: 400027b7 lui a5,0x40002
10058: 02a00693 li a3,42
1005c: 00d78023 sb a3,0(a5) # 40002000 <__global_pointer$+0x3fff06bc>
10060: 02000713 li a4,32
10064: 00e78023 sb a4,0(a5)
10068: 00d78023 sb a3,0(a5)
1006c: 00e78023 sb a4,0(a5)
10070: 00d78023 sb a3,0(a5)
10074: 00e78023 sb a4,0(a5)
10078: 00d78023 sb a3,0(a5)
1007c: 00e78023 sb a4,0(a5)
10080: 00d78023 sb a3,0(a5)
10084: 00e78023 sb a4,0(a5)
10088: 00a00613 li a2,10
1008c: 00c78023 sb a2,0(a5)
10090: 00d78023 sb a3,0(a5)
10094: 00e78023 sb a4,0(a5)
10098: 00d78023 sb a3,0(a5)
1009c: 00e78023 sb a4,0(a5)
100a0: 00d78023 sb a3,0(a5)
100a4: 00e78023 sb a4,0(a5)
100a8: 00d78023 sb a3,0(a5)
100ac: 00e78023 sb a4,0(a5)
100b0: 00d78023 sb a3,0(a5)
100b4: 00e78023 sb a4,0(a5)
100b8: 00c78023 sb a2,0(a5)
100bc: 00d78023 sb a3,0(a5)
100c0: 00e78023 sb a4,0(a5)
100c4: 00d78023 sb a3,0(a5)
100c8: 00e78023 sb a4,0(a5)
100cc: 00d78023 sb a3,0(a5)
100d0: 00e78023 sb a4,0(a5)
100d4: 00d78023 sb a3,0(a5)
100d8: 00e78023 sb a4,0(a5)
100dc: 00d78023 sb a3,0(a5)
100e0: 00e78023 sb a4,0(a5)
100e4: 00c78023 sb a2,0(a5)
100e8: 00d78023 sb a3,0(a5)
100ec: 00e78023 sb a4,0(a5)
100f0: 00d78023 sb a3,0(a5)
100f4: 00e78023 sb a4,0(a5)
100f8: 00d78023 sb a3,0(a5)
100fc: 00e78023 sb a4,0(a5)
10100: 00d78023 sb a3,0(a5)
10104: 00e78023 sb a4,0(a5)
10108: 00d78023 sb a3,0(a5)
1010c: 00e78023 sb a4,0(a5)
10110: 00c78023 sb a2,0(a5)
10114: 00d78023 sb a3,0(a5)
10118: 00e78023 sb a4,0(a5)
1011c: 00d78023 sb a3,0(a5)
10120: 00e78023 sb a4,0(a5)
10124: 00d78023 sb a3,0(a5)
10128: 00e78023 sb a4,0(a5)
1012c: 00d78023 sb a3,0(a5)
10130: 00e78023 sb a4,0(a5)
10134: 00d78023 sb a3,0(a5)
10138: 00e78023 sb a4,0(a5)
1013c: 00c78023 sb a2,0(a5)
10140: 00008067 ret
```
:::
#####
:::spoiler `riscv-none-embed-readelf -h Square`
```
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: 708 (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
```
:::
#####
`riscv-none-embed-size Square`
```
text data bss dec hex filename
240 0 0 240 f0 Square
```
* Compared with [謝宜紘](https://hackmd.io/@hsieh22/CA_lab1), -O3 unrolled the loop to reduce the count of branch.
* Due to less branch prediction, execution time will be reduced.
## -Os
`riscv-none-embed-gcc -march=rv32i -mabi=ilp32 -Os -nostdlib Squre.c -o Square`
```
./emu-rv32i Square
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
>>> Execution time: 60300 ns
>>> Instruction count: 72 (IPS=1194029)
>>> Jumps: 5 (6.94%) - 0 forwards, 5 backwards
>>> Branching T=4 (80.00%) F=1 (20.00%)
```
#####
:::spoiler riscv-none-embed-objdump -d Square
```
00010054 <_start>:
10054: 00500613 li a2,5
10058: 400027b7 lui a5,0x40002
1005c: 02a00693 li a3,42
10060: 02000713 li a4,32
10064: 00a00593 li a1,10
10068: 00d78023 sb a3,0(a5) # 40002000 <__global_pointer$+0x3fff0760>
1006c: 00e78023 sb a4,0(a5)
10070: 00d78023 sb a3,0(a5)
10074: 00e78023 sb a4,0(a5)
10078: 00d78023 sb a3,0(a5)
1007c: 00e78023 sb a4,0(a5)
10080: 00d78023 sb a3,0(a5)
10084: 00e78023 sb a4,0(a5)
10088: 00d78023 sb a3,0(a5)
1008c: 00e78023 sb a4,0(a5)
10090: 00b78023 sb a1,0(a5)
10094: fff60613 addi a2,a2,-1
10098: fc0618e3 bnez a2,10068 <_start+0x14>
1009c: 00008067 ret
```
:::
#####
:::spoiler riscv-none-embed-readelf -h Square
```
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: 544 (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
```
:::
#####
`riscv-none-embed-size Square`
```
text data bss dec hex filename
76 0 0 76 4c Square
```
| | O0 | O1 | O2 | O3 | Os |
| -------- | -------- | -------- | -------- | -------- | -------- |
| **Ex. time** | 130400ns | 63800ns | 61900ns | 66800ns | 64500ns |
| **IC** | 388 | 72 | 72 | 61 | 72 |
| **Jumps** | 37 (9.54%) 6for, 31back | 5 (6.94%) 0for, 5back | 1 (1.94%) 0for, 5back | 1 (1.64%) 0for, 1back | 5 (6.94%) 0for, 5back |
| **Branching** | T=30(83.33%) F=6(16.67%) | T=4(80.00%) F=1(20.00%) | T=4(80.00%) F=1(20.00%) | T=0(-nan%) F=0(-nan%) | T=4(80.00%) F=1(20.00%) |
> The fluctuating range of Ex. time is violent, it won't be used for reference.
* Since -Os is used to optimize for size, the size of text(76) is shorter than -O3(240).