---
title: "Lab2: RISC-V RV32I[MA] emulator with ELF support"
---
Lab2: RISC-V RV32I[MA] emulator with ELF support for Count Leading Zeros
===
###### tags: `RISC-V` `Computer Architeture`
[TOC]
**[Original Document](https://hackmd.io/@WeiCheng14159/rkUifs2Hw)**
## Rewritten Code in C
Above original program is rewritten in `CLZ.c` by myself as below.
```c=
unsigned char CLZ(unsigned int src){
unsigned char result;
unsigned int mask = 0x80000000;
for(result = 0; !(src & mask) && (result < 32); result ++){
mask >>= 1;
}
return result;
}
int _start(){
const unsigned int toCount = 0xf70;
unsigned char result = CLZ(toCount);
// Printing Part
volatile char* tx = (volatile char*) 0x40002000;
char toPrint;
*tx = '0';
*tx = 'x';
toPrint = (result&0xf0)>>4;
if(toPrint > 9) *tx = toPrint + 'A';
else *tx = toPrint + '0';
toPrint = result & 0xf;
if(toPrint > 9) *tx = toPrint + 'A';
else *tx = toPrint + '0';
return 0;
}
```
## Execution with rv32emu
1. By using `riscv-none-embed-gcc -march=rv32i -mabi=ilp32 -O3 -nostdlib ./CLZ.c -o CLZ3` to compile `CLZ.c` above with optimization in favor of speed and executing with `emu-rv32i CLZ3`, I had the result below.
```bash
0x14
>>> Execution time: 18295 ns
>>> Instruction count: 137 (IPS=7488384)
>>> Jumps: 22 (16.06%) - 2 forwards, 20 backwards
>>> Branching T=19 (50.00%) F=19 (50.00%)
```
2. By using `riscv-none-embed-gcc -march=rv32i -mabi=ilp32 -Os -nostdlib ./CLZ.c -o CLZs` to compile `CLZ.c` above with optimization in favor of code size and executing with `emu-rv32i CLZs`, I had the result below.
```bash
0x14
>>> Execution time: 21801 ns
>>> Instruction count: 177 (IPS=8118893)
>>> Jumps: 48 (27.12%) - 24 forwards, 24 backwards
>>> Branching T=23 (53.49%) F=20 (46.51%)
```
## Checking Code Size
By using `riscv-none-objdump -h CLZ3 > CLZ3h` and `riscv-none-objdump -h CLZs > CLZsh` to check the segments of code, I have below result. For `-Os` the compiler did a great job with a significant reduction in code size.
* CLZ with -O3
```bash
CLZ3: file format elf32-littleriscv
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 000000e0 00010054 00010054 00000054 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
1 .comment 00000033 00000000 00000000 00000134 2**0
CONTENTS, READONLY
```
* CLZ with -Os
```bash
CLZs: file format elf32-littleriscv
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 000000ac 00010054 00010054 00000054 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
1 .comment 00000033 00000000 00000000 00000100 2**0
CONTENTS, READONLY
```
## Checking Disassembled Program
By using `riscv-none-embed-objdump -d CLZ3 > CLZ3.as
` and `riscv-none-embed-objdump -d CLZs > CLZs.as` to check the disassembled code of the two elf files, I have below result.
* CLZ with -O3
```bash
CLZ3: file format elf32-littleriscv
Disassembly of section .text:
00010054 <CLZ>:
10054: 02054a63 bltz a0,10088 <CLZ+0x34>
10058: 80000737 lui a4,0x80000
1005c: 00000793 li a5,0
10060: 02000613 li a2,32
10064: 0080006f j 1006c <CLZ+0x18>
10068: 00c78c63 beq a5,a2,10080 <CLZ+0x2c>
1006c: 00175713 srli a4,a4,0x1
10070: 00178793 addi a5,a5,1
10074: 00e576b3 and a3,a0,a4
10078: 0ff7f793 andi a5,a5,255
1007c: fe0686e3 beqz a3,10068 <CLZ+0x14>
10080: 00078513 mv a0,a5
10084: 00008067 ret
10088: 00000793 li a5,0
1008c: ff5ff06f j 10080 <CLZ+0x2c>
00010090 <_start>:
10090: 00001637 lui a2,0x1
10094: 00100793 li a5,1
10098: 40000737 lui a4,0x40000
1009c: f7060613 addi a2,a2,-144 # f70 <CLZ-0xf0e4>
100a0: 02000593 li a1,32
100a4: 0080006f j 100ac <_start+0x1c>
100a8: 04b78a63 beq a5,a1,100fc <_start+0x6c>
100ac: 00175713 srli a4,a4,0x1
100b0: 00178793 addi a5,a5,1
100b4: 00c776b3 and a3,a4,a2
100b8: 0ff7f793 andi a5,a5,255
100bc: fe0686e3 beqz a3,100a8 <_start+0x18>
100c0: 40002737 lui a4,0x40002
100c4: 03000693 li a3,48
100c8: 00d70023 sb a3,0(a4) # 40002000 <__global_pointer$+0x3fff06cc>
100cc: 07800613 li a2,120
100d0: 0047d693 srli a3,a5,0x4
100d4: 03068693 addi a3,a3,48
100d8: 00c70023 sb a2,0(a4)
100dc: 00d70023 sb a3,0(a4)
100e0: 00f7f793 andi a5,a5,15
100e4: 00900693 li a3,9
100e8: 04f6f263 bgeu a3,a5,1012c <_start+0x9c>
100ec: 04178793 addi a5,a5,65
100f0: 00f70023 sb a5,0(a4)
100f4: 00000513 li a0,0
100f8: 00008067 ret
100fc: 400027b7 lui a5,0x40002
10100: 03000713 li a4,48
10104: 00e78023 sb a4,0(a5) # 40002000 <__global_pointer$+0x3fff06cc>
10108: 07800713 li a4,120
1010c: 00e78023 sb a4,0(a5)
10110: 03200713 li a4,50
10114: 00e78023 sb a4,0(a5)
10118: 03000793 li a5,48
1011c: 40002737 lui a4,0x40002
10120: 00f70023 sb a5,0(a4) # 40002000 <__global_pointer$+0x3fff06cc>
10124: 00000513 li a0,0
10128: 00008067 ret
1012c: 03078793 addi a5,a5,48
10130: fedff06f j 1011c <_start+0x8c>
```
* CLZ with -Os
```bash
CLZs: file format elf32-littleriscv
Disassembly of section .text:
00010054 <CLZ>:
10054: 80000737 lui a4,0x80000
10058: 00000793 li a5,0
1005c: 02000693 li a3,32
10060: 00a77633 and a2,a4,a0
10064: 00061463 bnez a2,1006c <CLZ+0x18>
10068: 00d79663 bne a5,a3,10074 <CLZ+0x20>
1006c: 00078513 mv a0,a5
10070: 00008067 ret
10074: 00178793 addi a5,a5,1
10078: 00175713 srli a4,a4,0x1
1007c: 0ff7f793 andi a5,a5,255
10080: fe1ff06f j 10060 <CLZ+0xc>
00010084 <_start>:
10084: 00001537 lui a0,0x1
10088: ff010113 addi sp,sp,-16
1008c: f7050513 addi a0,a0,-144 # f70 <CLZ-0xf0e4>
10090: 00112623 sw ra,12(sp)
10094: fc1ff0ef jal ra,10054 <CLZ>
10098: 40002737 lui a4,0x40002
1009c: 03000793 li a5,48
100a0: 00f70023 sb a5,0(a4) # 40002000 <__global_pointer$+0x3fff0700>
100a4: 07800793 li a5,120
100a8: 00f70023 sb a5,0(a4)
100ac: 00455793 srli a5,a0,0x4
100b0: 0ff7f793 andi a5,a5,255
100b4: 00900693 li a3,9
100b8: 02f6fc63 bgeu a3,a5,100f0 <_start+0x6c>
100bc: 04178793 addi a5,a5,65
100c0: 0ff7f793 andi a5,a5,255
100c4: 00f70023 sb a5,0(a4)
100c8: 00f57513 andi a0,a0,15
100cc: 00900793 li a5,9
100d0: 02a7f463 bgeu a5,a0,100f8 <_start+0x74>
100d4: 04150513 addi a0,a0,65
100d8: 400027b7 lui a5,0x40002
100dc: 00a78023 sb a0,0(a5) # 40002000 <__global_pointer$+0x3fff0700>
100e0: 00c12083 lw ra,12(sp)
100e4: 00000513 li a0,0
100e8: 01010113 addi sp,sp,16
100ec: 00008067 ret
100f0: 03078793 addi a5,a5,48
100f4: fcdff06f j 100c0 <_start+0x3c>
100f8: 03050513 addi a0,a0,48
100fc: fddff06f j 100d8 <_start+0x54>
```
## Interesting Findings and Further Tests
* Comparison with inline subroutine
Because I found that under `-O3` optimization option, the compiler would inline the function `CLZ` into `_start`, thus I want to compare the generated elfs with and without inlining function`CLZ`.
Below is the execution result with `riscv-none-embed-gcc -march=rv32i -mabi=ilp32 -O3 -nostdlib ./CLZ.c -o CLZ3_inline` command to compile the c program with inlined function and `emu-rv32i CLZ3_inline` command to have below result.
```bash
0x14
>>> Execution time: 8677 ns
>>> Instruction count: 137 (IPS=15788867)
>>> Jumps: 22 (16.06%) - 2 forwards, 20 backwards
>>> Branching T=19 (50.00%) F=19 (50.00%)
```
In comparison with the result of `emu-rv32i CLZ3` below. They in fact has the same program and performance, but with the overhead of the loader and memory, the execution time can even differ between two execution results with the same elf.
```bash
0x14
>>> Execution time: 18295 ns
>>> Instruction count: 137 (IPS=7488384)
>>> Jumps: 22 (16.06%) - 2 forwards, 20 backwards
>>> Branching T=19 (50.00%) F=19 (50.00%)
```
By using `riscv-none-embed-objdump -d CLZ3_inline > CLZ3_inline.as`, we have the disassembled file of program compiled with `-O3` flag and `inline` function below.
```bash
CLZ3_inline: file format elf32-littleriscv
Disassembly of section .text:
00010054 <_start>:
10054: 00001637 lui a2,0x1
10058: 00100793 li a5,1
1005c: 40000737 lui a4,0x40000
10060: f7060613 addi a2,a2,-144 # f70 <_start-0xf0e4>
10064: 02000593 li a1,32
10068: 0080006f j 10070 <_start+0x1c>
1006c: 04b78a63 beq a5,a1,100c0 <_start+0x6c>
10070: 00175713 srli a4,a4,0x1
10074: 00178793 addi a5,a5,1
10078: 00c776b3 and a3,a4,a2
1007c: 0ff7f793 andi a5,a5,255
10080: fe0686e3 beqz a3,1006c <_start+0x18>
10084: 40002737 lui a4,0x40002
10088: 03000693 li a3,48
1008c: 00d70023 sb a3,0(a4) # 40002000 <__global_pointer$+0x3fff0708>
10090: 07800613 li a2,120
10094: 0047d693 srli a3,a5,0x4
10098: 03068693 addi a3,a3,48
1009c: 00c70023 sb a2,0(a4)
100a0: 00d70023 sb a3,0(a4)
100a4: 00f7f793 andi a5,a5,15
100a8: 00900693 li a3,9
100ac: 04f6f263 bgeu a3,a5,100f0 <_start+0x9c>
100b0: 04178793 addi a5,a5,65
100b4: 00f70023 sb a5,0(a4)
100b8: 00000513 li a0,0
100bc: 00008067 ret
100c0: 400027b7 lui a5,0x40002
100c4: 03000713 li a4,48
100c8: 00e78023 sb a4,0(a5) # 40002000 <__global_pointer$+0x3fff0708>
100cc: 07800713 li a4,120
100d0: 00e78023 sb a4,0(a5)
100d4: 03200713 li a4,50
100d8: 00e78023 sb a4,0(a5)
100dc: 03000793 li a5,48
100e0: 40002737 lui a4,0x40002
100e4: 00f70023 sb a5,0(a4) # 40002000 <__global_pointer$+0x3fff0708>
100e8: 00000513 li a0,0
100ec: 00008067 ret
100f0: 03078793 addi a5,a5,48
100f4: fedff06f j 100e0 <_start+0x8c>
```

Magically, every instruction and registers used CLZ3_inline.as and CLZ3.as are identical in `_start` function, and the only difference is address number for jump and branch.
* Unsigned Char Causing Further Instructions
In the result of CLZ function, I think of using `unsigned char` type (same as `uint8_t`) would save the system some space. However in a 32-bit system, compiler generates code that mask lower 8 bit of register that stores `result` in while loop (`andi a5, a5, 255`). On average this action would cause 16 more instruction in execution time. Thus, using uint8_t would be a trade off between time and space in this case.