---
tags: computer-arch
---
# Assignment2: RISC-V Toolchain
> Due: ==Oct 31, 2022==
## Requirements
1. Following the instructions of [Lab2: RISC-V RV32I[MACF] emulator with ELF support](https://hackmd.io/@sysprog/SJAR5XMmi), pick up one assembly program listed at [Assignment1: RISC-V Assembly and Instruction Pipeline](https://hackmd.io/@sysprog/2022-arch-homework1) and adapt it into both RISC-V assembly and C implementations which can be flawlessly executed with [rv32emu](https://github.com/sysprog21/rv32emu).
* You must NOT pick up the program(s) that you have previously submitted on your own.
* You should briefly describe your motivations for picking up.
* You may choose to study the same subject as other students, but you must make your own discoveries.
* There are just **RV32I** instructions that can be used. This means that you MUST build C programs with the `-march=rv32i -mabi=ilp32` flags.
* RV32M (multiply and divide) and RV32F (single-precision floating point) are not permitted.
* :warning: [rv32emu](https://github.com/sysprog21/rv32emu) and [Ripes](https://github.com/mortbopet/Ripes) may not work together, therefore please be aware of the potential incompatibility. Please check [docs/syscall.md](https://github.com/sysprog21/rv32emu/blob/master/docs/syscall.md) and [src/syscall.c](https://github.com/sysprog21/rv32emu/blob/master/src/syscall.c) in advance.
* Do not duplicate workspaces or the entire repository from [rv32emu](https://github.com/sysprog21/rv32emu). As a starting point, copy the [`asm-hello`](https://github.com/sysprog21/rv32emu/tree/master/tests/asm-hello) directory instead. You shall modify `Makefile` and the linker script accordingly.
* [kdnvt](https://github.com/kdnvt) produced [some excellent work](https://hackmd.io/@kdnvt/2022-arch-hw1) that can be used as a benchmark for program analysis and future optimizations. Please read his report carefully and pay attention to certain suggestions and observations.
* (**Optional**) You have the choice to choose the programs, [pi.c](https://github.com/sysprog21/rv32emu/blob/master/tests/pi.c) and [nqueens.c](https://github.com/sysprog21/rv32emu/blob/master/tests/nqueens.c), and create RISC-V assembly that is superior to that produced by GNU Toolchain. That is, your handwritten RISC-V assembly program should run more quickly and occupy less space in the ELF image.
2. Disassemble the ELF files produced by the C compiler and contrast the handwritten and compiler-optimized assembly listings.
* You can append the compilation options to experiment. e.g., Change `-Ofast` (optimized for speed) to `-Os` (optimized for size).
* Describe your obserations and explain.
3. Check the results of `rv32emu --stats` for the statistics of your program's execution. Then, try to optimize the handwritten/generated assembly. You shall read [RISC-V Assembly Programmer's Manual](https://github.com/riscv/riscv-asm-manual/blob/master/riscv-asm.md) carefully.
* :warning: We care about CSR cycles at the moment.
* Can you improve the assembly code generated by gcc with optimizations? Or, can you write even faster/smaller programs in RISC-V assembly?
* You may drop some function calls and apply techniques such as [loop unrolling](https://en.wikipedia.org/wiki/Loop_unrolling) and [peephole optimization](http://homepage.cs.uiowa.edu/~dwjones/compiler/notes/38.shtml).
4. Write down your thoughts and progress in [HackMD notes](https://hackmd.io/s/features).
* [Example page](https://hackmd.io/@wIVnCcUaTouAktrkMVLEMA/SJEP_amvK)
> :warning: Do not modify this note.
* Insert your HackMD notes and programs in the following table.
* Of course, you MUST write in English.
5. BONUS: Find 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 | Sort Colors | [Homework2](https://hackmd.io/@wIVnCcUaTouAktrkMVLEMA/SJEP_amvK) |
| Sample2 | Length of Last Word | [Homework2](https://hackmd.io/@Wc9buHGHRaudUz3D5Dp_FA/By0vP2C8t) |
| Sample3 | Single number | [Homework2](https://hackmd.io/@WeiCheng14159/S1H6X_VOw) |
| 黃昱澄 | Decompress Run-Length Encoded List | [Homework2](https://hackmd.io/@7UuLPITUR02HgYqwo0PaIQ/B1i0rQ_Xj) |
| 王昱承 | Decode XORed Array | [Homework2](https://hackmd.io/@zKOCm3SSTKyUyiPV-nfEjw/Hyj0urMQo) |
| 黃冠予 | Find Numbers with Even Number of Digits | [Homework2](https://hackmd.io/@ZLQisilvQvOh2DclLmk1bg/SywD-HK7s) |
| 賴致文 | Build Array from Permutation | [Homework2](https://hackmd.io/@b3nzYEHmSeCBZhvZgxI-DA/HkNScCoXi) |
| 王漢祺 | Move zeroes | [Homework2](https://hackmd.io/@wanghanchi/S1q0aBHQj) |
| 江坤諦 | Valid Anagram | [Homework2](https://hackmd.io/@chiangkd/assignment2) |
| 俞杉麒 | Running Sum of 1d Array | [Homework2](https://hackmd.io/@XiaXia/ComputerArchitecture_Hw2) |
| 張瑞甫 | Best Time to Buy and Sell Stock II | [Homework2](https://hackmd.io/ADNQPiEFSPC_daP2GJ_sSQ) |
| 黃榆哲 | Jewels and Stones | [Homework2](https://hackmd.io/kJviW9L9S9W0jAa6yHsZoA) |
| 鄒崴丞 | Length of Last Word | [Homework2](https://hackmd.io/@StevenChou43/2022_ComputerArchitecture_HW2) |
| 何坤霖 | Find Target Indices After Sorting Array | [Homework2](https://hackmd.io/@dgKSmDN0T_quhIoU9NVThA/SkiTk0jXo/edit) |
| 吳宇晨 | Majority Element | [Homework2](https://hackmd.io/vwNJ8LarSgCgKmRDOzKfLQ?edit) |
| 潘鴻福 | Contains Duplicate| [Homework2](https://hackmd.io/XThOd5JjQtGF9dzGY4yh_Q?both) |
| 謝長成 | Find the Highest Altitude | [Homework2](https://hackmd.io/@Osk-p534T9KkyTBAk79ijQ/HkAHDzi7j) |
| 杜孟聰 | Monotonic Array | [Homework2](https://hackmd.io/-0LlzZPcSmGmdhqp8oyA2A) |
| 洪嘉志 | Reverse Linked List | [Homework2](https://hackmd.io/KqhB3hrVRHGjFbx91qQIcg) |
| 鄭至崴 | Convert Binary Number in a Linked List to Integer | [Homework2](https://hackmd.io/@Fo7UsdePRsKPVV4CPYGbpA/Sy7ZmxN7i) |
| 陳彥甫 | Long Pressed Name | [Homework2](https://hackmd.io/@qwe661234/BJ52GwGVi) |
| 莊集 | Roman to Integer | [Homework2](https://hackmd.io/@y8jRQNyoRe6WG-qekloIlA/H1u3j7ONs) |
| 鄭明奇 | Concatenation of Array | [Homework2](https://hackmd.io/@teimeikichengmingchi/RISC-V-Assignment2) |
| 陳韋勳 | Pascal’s Triangle II | [Homework2](https://hackmd.io/5A53d6tpRj2Wq1zpfsWVNg) |
| 吳紀寬 | Lemonade Change | [Homework2](https://hackmd.io/Wb_6HTrxSHaOz2g0QKCibw)|
| 吳峻廷 | Binary Search|[Homework2](https://hackmd.io/mQDusGQqTWyYh5xtqqdbqA)|
| 張中龍 | Single Number|[Homework2](https://hackmd.io/FzyzhQlXT7SXXQg7o4OHjg)|
| 蘇勇達 | Three Consecutive Odds|[Homework2](https://hackmd.io/@2gMSvG25RxKj2-N_rAY6zg/BJ8Oq0wEj)|
| 方宣翔 | Move Zeroes|[Homework2](https://hackmd.io/ulmD9RnsRTuva93-MRX-wg)|
| 馮柏為 | Two sum|[Homework2](https://hackmd.io/Ukda2ym6TrCKaWX_eOGimg)|
| 楊淳皓 | Search Insert Position | [Homework2](https://hackmd.io/@Vgwl_uixQFasIvsDbsFlvA/risc-v-hw2) |
| 黃柏瑜 | Best Time to Buy and Sell Stock | [Homework2](https://hackmd.io/@maromaSamsa/rJFkoUiNj) |
| 陳嘉晟 | Invert Binary Tree | [Homework2](https://hackmd.io/@Haser/r1p6fwqVs/TCzPfzDhT1yOrt0TmyUUEA) |
| 陳品崴 | Find the Highest Altitude|[Homework2](https://hackmd.io/Lo8GqdnzQAyGHn7HLUv3gg)|
| 周士翔 | Pascal’s triangle|[Homework2](https://hackmd.io/@zDmciYQATNm-8XeyJ5Th0Q/B1GGWL5No/edit)|
|陳靖雯| Contains Duplicate|[Homework2](https://hackmd.io/9nLG2gW2TuyB4IJXahxp1Q?both)|
|曾晧峖| Decode XORed Array|[Homework2](https://hackmd.io/@tseng0201/SyjsEXKEi)|
|吳昱| Monotonic Array|[Homework2](https://hackmd.io/@kdnvt/arch-2022-hw2)|
|李協儒|Run-Length Encoded List|[Homework2](https://hackmd.io/nltUszHCShyf6wbsod9fmw)
|陳柏瑋|Remove Linked List Elements|[Homework2](https://hackmd.io/@JxPCbLVmQh2_5-6E4fvX9w/Sy6TYoFLi)