---
tags: computer-arch
---
# Assignment2: RISC-V Toolchain
> Due: ==Nov 15, 2021==
## Requirements
1. Following the instructions of [Lab2: RISC-V RV32I[MA] emulator with ELF support](https://hackmd.io/@sysprog/rJAufgHYS), pick up one assembly program from [Assignment1: RISC-V Assembly and Instruction Pipeline](https://hackmd.io/@sysprog/2021-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 should explain why you picked up with brief motivations.
* You may pick up the same subject 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.
* 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 |
|:-----------------:| --------------------------------------- |:---------------------------------------------------------------------------:|
| Sample1 | Linear Search | [Homework2](https://hackmd.io/@kaeteyaruyo/risc-v-hw2) |
| Sample2 | find largest element | [Homework2](https://hackmd.io/@cccccs100203/riscv-find-largest-element) |
| Sample3 | Single number | [Homework2](https://hackmd.io/@WeiCheng14159/S1H6X_VOw) |
| 徐卓朗 | Sort Colors | [Homework2](https://hackmd.io/@wIVnCcUaTouAktrkMVLEMA/SJEP_amvK) |
| 杰凱力艾 | Missing Number | [Homework2](https://hackmd.io/@jakaria27/HkFkYeFwt) |
| 方竫泓 | Reverse Bit | [Homework2](https://hackmd.io/@7y4UQB2gRoSQRw5iAm-5lw/ryPnKAOwY) |
| 陳日昇 | Length of Last Word | [Homework2](https://hackmd.io/@Wc9buHGHRaudUz3D5Dp_FA/By0vP2C8t) |
| 陳韋綸 | Number of 1 Bits | [Homework2](https://hackmd.io/@_UHs74UQS7uNne9_7SwQFQ/SJVb685vK) |
| 劉恩澔 | Palindrome Number | [Homework2](https://hackmd.io/n-b-Xw5fRsGfyW5fNt3hrw) |
| 陳昕佑 | Power of Two | [Homework2](https://hackmd.io/@A_KUyU-fSfqgFIepPOCwcQ/BktYM91wt) |
| 鍾震 | Power of Four | [Homework2](https://hackmd.io/@Japoka/r1EDQcqDt) |
| 伍志忠 | Find the difference | [Homework2](https://hackmd.io/DicJNnalTmKJmj7v9biWEg?view) |
| 黃瑋盛 | Search Insert Position | [Homework2](https://hackmd.io/@N9qHU_eLRvKyfDfJk8cDXA/Hy0O4vFwF) |
| 李仲恩 | Find the Highest Altitude | [Homework2](https://hackmd.io/@IVUP6rGJQAmUMyQytt73_g/S1OFzGKvK) |
| 許永貞 | Move Zeroes | [Homework2](https://hackmd.io/iNnXUTHBQg-DikO-w0BpMQ?view) |
| 鄭學陽 | Power of Four | [Homework2](https://hackmd.io/T60MbDD8S-WT6rz4YXlbzQ?both) |
| 羅紹豪 | Two sum | [Homework2](https://hackmd.io/v3KAi9kNRgukamip_TYHFQ) |
| 陳力維 | Number complement | [Homework2](https://hackmd.io/W14B9aJgS9qkBB6Qe8eK6w) |
| 張又仁 | Plus one | [Homework2](https://hackmd.io/maroy5_HR82CRF7OfB1D5g?view) |
| 朱俊霖 | Add Digits | [Homework2](https://hackmd.io/@chu0809/ca_hw2) |
| 賴虓翰+ | Reverse string | [Homework2](https://hackmd.io/29AFIJvKQdC2HmfPDoEUAA?both) |
| 劉品宏 | Monotonic Array | [Homework2](https://hackmd.io/DnEIyVx3SNmhcVpfNndb8w?view) |
| 黃品程 | Number of 1 Bits | [Homework2](https://hackmd.io/ozZ2r-JXSeGCUhv6XQN96g) |
| 李安豐 | Find Numbers with Even Number of Digits | [Homework2](https://hackmd.io/r9w0vLwCQ3aL-pTH6RWRLA?view) |
| 陳銘畯 | Climbing Stairs | [Homework2](https://hackmd.io/TGhsomJQQPmFPhBW9RHVyA) |
| 林信宇 | Counting Bits | [Homework2](https://hackmd.io/@axwl03/S18oobGDK) |
| 譚亦翔 | Power of three | [Homework2](https://hackmd.io/@pt2JoPc6SNCdX7cvPan6_Q/rJkMDU0PK) |
| 李其祐 | Sort colors | [Homework2](https://hackmd.io/@jackli/arch_hw2) |
| 黃上睿 | Longest Palindrome | [Homework2](https://hackmd.io/icZhfvEKQnebO4oMvkejeQ?view) |
| 張亦妤 | Length of Last Word | [Homework2](https://hackmd.io/@Evelyn-Jhang/SkrOYF0wt) |
| 林嘉歆 | Missing Number | [Homework2](https://hackmd.io/5ChbwRfLTAuC5EANi-Ziwg?view) |
| 李政憲 | Maximum Subarray | [Homework2](https://hackmd.io/@30vhEV7FQECcWeCF1eAN5A/rJOCF27wK) |
| 鄭力維+ | Hamming Distance | [Homework2](https://hackmd.io/jKLTzunRSCyvVaI-hciJiQ) |
| 戴仕誠 | Climbing Stairs | [Homework2](https://hackmd.io/@WHKhZ7rvSTCx6LNmPBploQ/Sk8WHjCwt) |
| 張峻瑋+ | Valid Perfect Square | [Homework2](https://hackmd.io/@F7GGdY_NSxe2XlypzW57AA/ByF42mCPY) |
| 蕭珮珊 | Counting Bits | [Homework2](https://hackmd.io/m6pZScRhQHeky79OjLTDRw?both) |
| 林甄羚 | Number Complement | [Homework2](https://hackmd.io/7EX5vTyJQkiZ-oNtpelIvw?view) |
| 歐禮寬 | Defuse the Bomb | [Homework2](https://hackmd.io/G10sOm3KSsy4WZyEWlrpRg?view) |
| 孫祥鈞 | N-th Tribonacci Number | [Homework2](https://hackmd.io/_OC9vyNqRbOsDZYseTXNmA) |
| 歐子杰 | Sort an Array | [Homework2](https://hackmd.io/@oucs638/computer-arch-2021-fall-assignment2) |
| 鄒柏宇 | Add Digits | [Homework2](https://hackmd.io/bv39w_BTR02TqEHhyY8fQg) |
| 莊崴 | Majority Element | [Homework2](https://hackmd.io/_yvQu8jVSYCR-UVqRHQdFQ?both) |
| 朱育萱 | Add Digits |[Homework2](https://hackmd.io/@OliveLake/Bkd81P-Ot) | |
| 陳明吉 | Binary Number with Alternating Bits | [Homework2](https://hackmd.io/IigHYjK2RcCtFXbqJfK1tw?both) |
| 向景亘 | Sqrt(x) | [Homework2](https://hackmd.io/@oscarshiang/arch_hw2) |
| 林劭謙 | Power of Four | [Homework2](https://hackmd.io/H13zpP6QSQuyye-xqCSEvQ)
| 朱祐均 | Best Time to Buy and Sell Stock II | [Homework2](https://hackmd.io/@vuJ_c2nYTM2v6lZwDtjajg/HJ-zqTydK) |
| 陳柏瑋 | Total Hamming Distance | [Homework2](https://hackmd.io/@JxPCbLVmQh2_5-6E4fvX9w/arch_hw2) |
| 張力尹 | Move Zeroes | [Homework2](https://hackmd.io/XjmD-4DWThGW55Kngc_PiA) |
| 郭又宗 | Counting Bits | [Homework2](https://hackmd.io/@kuouu/arch_hw2) |
| 黃牧恩 | Number of 1 Bits | [Homework2](https://hackmd.io/@huang-me/riscv-toolchain) |
| 柯凱瑋 | Power of Two | [Homework2](https://hackmd.io/@k-w-k/H11fRUIuK) |