# Lab2: RISC-V RV32I[MA] emulator with ELF support
**Tower of Hanoi** done by 黃瀚群
This is my C code
```c
void TowerOfHanoi(int n, int A, int B, int C ){
volatile char* tx = (volatile char*) 0x40002000;
//volatile char* move= (volatile char*) 0x40002000;
char* Hanoi= "Move Disk ";
char* To = " To ";
if( n = 1)
{ *tx = A + '0';
*tx = *To;
*tx = B + '0';
char *tmp = Hanoi;
while(*tmp){
*tx = *tmp;
tmp++;
}
return;
}
TowerOfHanoi(n-1, A, C ,B);
*tx = A + '0';
*tx = *To;
*tx = B + '0';
char *tmp = Hanoi;
while(*tmp){
*tx = *tmp;
tmp++;
}
TowerOfHanoi(n-1, C, B, A);
}
int _start()
{
volatile int n = 3;
volatile int A = 1;
volatile int B = 2;
volatile int C = 3;
TowerOfHanoi(n, A, B, C);
return 0;
}
```
After execution of the above C code. I get this assembly code.
```shell
surajubuntu@surajubuntu:~/rv32emu$ riscv-none-embed-objdump -d test1
test1: file format elf32-littleriscv
Disassembly of section .text:
00010054 <TowerOfHanoi>:
10054: 03058593 addi a1,a1,48
10058: 400027b7 lui a5,0x40002
1005c: 0ff5f593 andi a1,a1,255
10060: 00b78023 sb a1,0(a5) # 40002000 <__global_pointer$+0x3fff06dc>
10064: 02000713 li a4,32
10068: 03060613 addi a2,a2,48
1006c: 00e78023 sb a4,0(a5)
10070: 0ff67613 andi a2,a2,255
10074: 00c78023 sb a2,0(a5)
10078: 000107b7 lui a5,0x10
1007c: 11878793 addi a5,a5,280 # 10118 <_start+0x7c>
10080: 04d00713 li a4,77
10084: 400026b7 lui a3,0x40002
10088: 00e68023 sb a4,0(a3) # 40002000 <__global_pointer$+0x3fff06dc>
1008c: 00178793 addi a5,a5,1
10090: 0007c703 lbu a4,0(a5)
10094: fe071ae3 bnez a4,10088 <TowerOfHanoi+0x34>
10098: 00008067 ret
0001009c <_start>:
1009c: ff010113 addi sp,sp,-16
100a0: 00300793 li a5,3
100a4: 00f12023 sw a5,0(sp)
100a8: 00100713 li a4,1
100ac: 00e12223 sw a4,4(sp)
100b0: 00200713 li a4,2
100b4: 00e12423 sw a4,8(sp)
100b8: 00f12623 sw a5,12(sp)
100bc: 00012783 lw a5,0(sp)
100c0: 00412703 lw a4,4(sp)
100c4: 00812783 lw a5,8(sp)
100c8: 400026b7 lui a3,0x40002
100cc: 03070713 addi a4,a4,48
100d0: 0ff77713 andi a4,a4,255
100d4: 00c12603 lw a2,12(sp)
100d8: 03078793 addi a5,a5,48
100dc: 00e68023 sb a4,0(a3) # 40002000 <__global_pointer$+0x3fff06dc>
100e0: 02000713 li a4,32
100e4: 00e68023 sb a4,0(a3)
100e8: 0ff7f793 andi a5,a5,255
100ec: 00f68023 sb a5,0(a3)
100f0: 000107b7 lui a5,0x10
100f4: 11878793 addi a5,a5,280 # 10118 <_start+0x7c>
100f8: 04d00713 li a4,77
100fc: 00e68023 sb a4,0(a3)
10100: 00178793 addi a5,a5,1
10104: 0007c703 lbu a4,0(a5)
10108: fe071ae3 bnez a4,100fc <_start+0x60>
1010c: 00000513 li a0,0
10110: 01010113 addi sp,sp,16
10114: 00008067 ret
```
Readelf
```shell
surajubuntu@surajubuntu:~/rv32emu$ 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: 0x1009c
Start of program headers: 52 (bytes into file)
Start of section headers: 728 (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:
```shell
surajubuntu@surajubuntu:~/rv32emu$ riscv-none-embed-size test1
text data bss dec hex filename
208 0 0 208 d0 test1
```
- Difference between -O3 and -Os argument
```shell
surajubuntu@surajubuntu:~/rv32emu$ riscv-none-embed-gcc -O3 -nostdlib test1.c -o t1
surajubuntu@surajubuntu:~/rv32emu$ ./emu-rv32i t1
>>> Execution time: 212 ns
>>> Instruction count: 1 (IPS=4716981)
>>> Jumps: 0 (0.00%) - 0 forwards, 0 backwards
>>> Branching T=0 (-nan%) F=0 (-nan%)
surajubuntu@surajubuntu:~/rv32emu$ riscv-none-embed-gcc -Os -nostdlib test1.c -o t2
surajubuntu@surajubuntu:~/rv32emu$ ./emu-rv32i t2
>>> Execution time: 298 ns
>>> Instruction count: 1 (IPS=3355704)
>>> Jumps: 0 (0.00%) - 0 forwards, 0 backwards
>>> Branching T=0 (-nan%) F=0 (-nan%)
```