# Assignment2: RISC-V Toolchain
> Due: ==Oct 26, 2020==
## Requirements
1. Following the instructions of [Lab2: RISC-V RV32I[MA] emulator with ELF support](https://hackmd.io/@sysprog/rJAufgHYS), pick up two assembly programs from [Assignment1: RISC-V Assembly and Instruction Pipeline](https://hackmd.io/@sysprog/2020-arch-homework1) and rewrite into C implementations which can execute with [rv32emu](https://github.com/sysprog21/rv32emu).
* You shall not pick up the program(s) submitted by youself already.
* You may pick up the same topic as other students did, but you shall figure out something by your own.
2. Disassemble the ELF files generated by C compiler and compare the assembly listing between handwritten and compiler optimized one.
* You can modify the arguments specified by `RV32I_CFLAGS` to experiment. e.g. Change `-O3` (optimized for speed) to `-Os` (optimized for size).
* Describe your obserations and explain.
* Function calls and `ecall` shall be taken into considerations.
3. Check the results of `emu-rv32i` for the statistics of execution flow and explain the internal counters such as `true_counter`, `true_counter` (crucial for [branch prediction](https://en.wikipedia.org/wiki/Branch_predictor)), `jump_counter`, etc. Then, try to optimize the generated assembly. You shall read [RISC-V Assembly Programmer's Manual](https://github.com/riscv/riscv-asm-manual/blob/master/riscv-asm.md) carefully and modify Makefile in order to append new assembly targets.
* Would you encounter data hazards? Can you do [instruction scheduling](https://en.wikipedia.org/wiki/Instruction_scheduling) manually?
4. Can you improve the assembly code generated by gcc with optimizations? Or, can you write even faster/smaller programs in RISC-V assembly?
5. Write down your thoughts and progress in [HackMD notes](https://hackmd.io/s/features).
* [Example page](https://hackmd.io/@oR8-QX4TQzGKDJ72DmqDUg/SJrNq1QuB)
> :warning: Do not modify this note.
* Insert your HackMD notes and programs in the following table.
* ==2 entries== as expected.
* Of course, you MUST write in English.
6. BONUS: figure out the bugs inside [rv32emu](https://github.com/sysprog21/rv32emu) and send pull requests to improve it!
## Fill in the table for your homework
| Formal given name | Descriptions | HackMD note | Program (GitHub link) |
|:-----------------:| ----------------------------- |:-------------------------------------------------------------------------:| --------------------- |
| Sample1 | prime number | [Homework2-1](https://hackmd.io/@lW44vTK7QuugZyKsKT1eBg/B1T3SFFtB) | - |
| Sample1 | `pow` function | [Homework2-2](https://hackmd.io/@lW44vTK7QuugZyKsKT1eBg/Hk8JP5U5H) | - |
| Sample2 | bit reversal | [Homework2](https://hackmd.io/@Z892YW7ORxSwX99S5tZKxQ/BJsjV5Gcr) | - |
| Sample3 | bubble sort, binary search | [Homework2](https://hackmd.io/@oR8-QX4TQzGKDJ72DmqDUg/r1i31xN5B) | - |
| Sample4 | Tower of Hanoi | [Homework2](https://hackmd.io/@jRxLU_UtQTW7kEnDA0hp-Q/ryMlE4K5H) | - |
| 呂紹樺 | GCD (Greatest Common Divisor) | [Homework2](/vBIUpmCCTmq04QG71a7umg) | - |
| 呂紹樺 | Insertion Sort | [Homework2](/W5B19d0gTXq9hiXsHsoiRA) | - |
| 許龍君 | Bubble Sort | [Homework2-1](https://hackmd.io/aRHowlvJQIKrr-TfginEGw) | - |
| 許龍君 | Count Leading Zero | [Homework2-2](https://hackmd.io/gmIkqBeiREeenYSjyPIqeA) | - |
| 段雅培 | Linear Search | [Homework2-1](https://hackmd.io/@kaeteyaruyo/risc-v-hw2) | - |
| 汪宗諺 | Fibonacci number | [Homework2-1](https://hackmd.io/x7W1DGCVRIqSQ4IQ4UVuig?both) | - |
| 汪宗諺 | Factorial number | [Homework2-2](https://hackmd.io/v9-MKFgLRJSKGeR-OxWFQg) | - |
| 洪邵澤 | Power Function | [Homework2-1](https://hackmd.io/@cccccs100203/riscv-power) | - |
| 洪邵澤 | Find Largest Element | [Homework2-2](https://hackmd.io/@cccccs100203/riscv-find-largest-element) | - |
| 曾紹銘 | Count Leading Zero | [Homework2-1](https://hackmd.io/@shauming1020/ass2-2) | - |
| 曾紹銘 | Bubble Sort | [Homework2-2](https://hackmd.io/@shauming1020/ass2-1) | - |
| 林霆寬 | Factorial | [Homework2-1](https://hackmd.io/Bciyy2j2RRq85mKUu0pmzg?view) | - |
| 林霆寬 | GCD (Greatest Common Divisor) | [Homework2-2](https://hackmd.io/7wE0wNKzQryC2mAuTAHcPQ?view) | - |
| 魏晉成 | Convolution Sum | [Homework2-1](https://hackmd.io/1RASw08qTWS06wCxKV7Uhw) | - |
| 魏晉成 | Count Leading Zeros | [Homework2-2](https://hackmd.io/A68UgPTAT6OZ8QFQ0Kx9TQ) | - |
| 鄭惟 | Binary Search | [Homework2-1](https://hackmd.io/@WeiCheng14159/Sk9L2gmOv) | - |
| 鄭惟 | Single number | [Homework2-2](https://hackmd.io/@WeiCheng14159/S1H6X_VOw) | - |
| 鄭育丞 | Fibonacci | [Homework2-1](https://hackmd.io/@eecheng/HJfpYnzvP) | - |
| 楊承翰 | Fibonacci number | [Homework2-1](https://hackmd.io/2rsJTRQuSG-kLcxB3E2m_g) | - |
| 楊承翰 | Bubble Sort | [Homework2-2](https://hackmd.io/ZM8TBxD6T3GgIhPwJdtGGA) | - |
| 陳冠宇 | Format printer | [Homework2-1](https://hackmd.io/@guaneec/ach2020-a2#Format-printer) | - |
| 陳冠宇 | MergeSort | [Homework2-2](https://hackmd.io/@guaneec/ach2020-a2#MergeSort) | - |
| 王傑世 | GCD | [Homework2-1](https://hackmd.io/IB4facxdTMC8Xi3OSfme6w?view) | - |
| 王傑世 | Print Square | [Homework2-2](https://hackmd.io/lZT9PdR6SHCrxH_bJYlP7A?view) | - |
| 王廣達 | Insertion Sort | [Homework2-1](https://hackmd.io/NFzhxeJaR-6dsTFGr0rNUg?both) | - |
| 王廣達 | Isoceles triangle | [Homework2-2](https://hackmd.io/CGVZHK9MRba8yKPDezwfjw?both) | - |
| 施丞宥 | Bubble sort | [Homework2-1](https://hackmd.io/v7228QGwTG6pNjb03E077g?both) | - |
| 施丞宥 | | [Homework2-2]() | - |
| 陳柏廷 | Find Largest Element | [Homework2-1](https://hackmd.io/@Max-Chen/SyQ0FGN_w) | - |
| 陳柏廷 | Insertion Sort | [Homework2-2](https://hackmd.io/@Max-Chen/SyDKkrNdP) | - |
| 謝宜紘 | Count Leading Zeros | [Homework2-1](https://hackmd.io/@hsieh22/2020_CA_lab2-1) | - |
| 謝宜紘 | Bubble Sort | [Homework2-2](https://hackmd.io/@hsieh22/2020_CA_lab2-2) | - |
| 江承緯 | Bubble Sort | [Homework2-1](https://hackmd.io/NbvqSFAeQoqj_L42owvyNw?both) | - |
| 江承緯 | power function | [Homework2-2](https://hackmd.io/EtMsH7VLTJG13V1DdKVL6A) | - |
| 林楷倫 | Insertion Sort | [Homework2-1](https://hackmd.io/@kevinlin30292/2020ca-hw2-1) | - |
| 林楷倫 | GCD | [Homework2-2](https://hackmd.io/@kevinlin30292/2020ca-hw2-2) | - |
| 吳昱宗 | Fibonacci number | [Homework2-1](https://hackmd.io/@Zong55555/Sy429rE_P) | - |
| 吳昱宗 | Print Square | [Homework2-2](https://hackmd.io/@Zong55555/SJUGk2EOv) | - |
| 林柏維 | Dot Product | [Homework2-1](https://hackmd.io/mIpYVEOVRNmeom8l5X3fyg)|[Github](https://github.com/waynelinbo/Computer_Architecture_Lab2)|
| 林柏維 | Format printer | [Homework2-2](https://hackmd.io/P7As58BGTRSOPK7tJ9DxAA)|[Github](https://github.com/waynelinbo/Computer_Architecture_Lab2)|
| 曾鈜寬 | Factorial Number | [Homework2-1](https://hackmd.io/@Tim096/rJOiVg_8v)|[Github]|
| 徐郁淞 | Binary Search | [Homework2-1](https://hackmd.io/@joe-U16/SkW_DKs5P)|
| 徐郁淞 | Power function | [Homework2-2](https://hackmd.io/@joe-U16/HkyJ9PyiD)|
| 江松穎 | Bubble Sort | [Homework 2-1](https://hackmd.io/@Uduru0522/S11Mg_Xow#Bubble-Sort) | - |
| 江松穎 | Two Sum | [Homework 2-2](https://hackmd.io/@Uduru0522/S11Mg_Xow#Two-Sum) | - |