# Assignment2: Complete Applications > Due: ==Nov 12, 2025== ## Project Overview This project challenges you to develop a simple yet highly practical system using **RISC-V assembly language**. Throughout the process, you will explore essential low-level programming concepts, such as: - Efficient use of registers for optimized code execution. - Writing functions and adhering to RISC-V calling conventions for both internal and external functions. - Memory management by allocating space on the stack and heap. ## Requirements 1. Following the instructions in [Lab2: RISC-V](https://hackmd.io/@sysprog/Sko2Ja5pel), enable system emulation in [rv32emu](https://github.com/sysprog21/rv32emu) and ensure `tests/system/playground` is fully verified. 2. Pick one complete program (with test suite) done in your [Homework 1](https://hackmd.io/@sysprog/2025-arch-homework1) and make it run in a **bare-metal** environment using rv32emu’s system emulation. * There are just **RV32I** instructions that can be used. This means that you MUST build C programs with the `-march=rv32izicsr` or `-march=rv32i_zicsr` 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. 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 [``tests/system/playground``](https://gist.github.com/jserv/5f682ac880773cab69e3564f4f20d60a) directory instead. You shall modify `Makefile` and the linker script accordingly. 3. Adapt Problem A from [Quiz 2](https://hackmd.io/@sysprog/arch2025-quiz2) and Problem C from [Quiz 3](https://hackmd.io/@sysprog/arch2025-quiz3), and make them run in a bare-metal environment using rv32emu’s system emulation. Refer to [Approximating sine function in BF16 data type](https://hackmd.io/@Max042004/bf16_sin) for guidance on analyzing precision and performance. Always measure and refine your implementation based on profiling results. 4. 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. 5. Check the [ticks.c](https://github.com/sysprog21/rv32emu/blob/master/tests/ticks.c) and [perfcounter](https://github.com/sysprog21/rv32emu/tree/master/tests/perfcounter) 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). > Quote from [RISC-V Instruction Set Manual](https://github.com/riscv/riscv-isa-manual): The RDCYCLE pseudo-instruction reads the low XLEN bits of the cycle CSR which holds a count of the number of clock cycles executed by the processor on which the hardware thread is running from an arbitrary start time in the past. RDCYCLEH is an RV32I-only instruction that reads bits 63–32 of the same cycle counter. The underlying 64-bit counter should never overflow in practice. 6. 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. 7. BONUS: Find the bugs inside [rv32emu](https://github.com/sysprog21/rv32emu) and send pull requests to improve it! 8. BONUS: Ask instructor if you would like to collaborate the [UCI](https://en.wikipedia.org/wiki/Universal_Chess_Interface) compatible Chess engine using customized RISC-V instructions to accelerate. 9. Fill the [Google form](https://forms.gle/GyNbSQzNoXdEELdFA) to submit the your repository and HackMD note.