# Lab1: R32I Simulator ###### tags: `RISC-V` [Ripes](https://github.com/mortbopet/Ripes) is a graphical 5-stage RISC-V pipeline simulator & assembly editor. Definition of lucas number : https://en.wikipedia.org/wiki/Lucas_number ![](https://i.imgur.com/ytPW7U8.png) Breifly, I set n = 7 in this sample to practice iterative loop in risc-v implementation. ![](https://i.imgur.com/RMfR7NQ.png) we got 2 ALU, left is for program count, left is for branch intruction, and the mux int the leftest is to choose hump or not. And we got an imm part, it is for we call a data such as addi, subi, which is not in memory. and we got two mux, the up one is for choosing write back or reg1, down on is for reg2 for instead. and the unequal part is for comparing, for branch. ![](https://i.imgur.com/ZHD7kgw.png) EX we got 4 muxs,left 2 muxes are about forwarding, we can choose data from reg or forwarding. right two muxes are for choosing data from re1 or reg2. MEM mux is for forwarding mechanism. WB mux is for choosing data from memory, alu or reg. ``` #include<stdio.h> int main() { int n = 7, a1 = 2, a2 = 1, tmp; for(int i = 0; i<n-2; i++){ tmp = a1 + a2; a1 = a2; a2 = tmp; } return a2; } ``` ```clike .data argument: .word 7 # Number to find the lucas number value of str1: .string "lucus number of " str2: .string " is " .text main: lw a0, argument # Load argument from static data mv a1, a0 li a2, 0 li a3, 0 jal ra, iter ecall back: # Print the result to console mv a1, a0 #mv a2, zero #mv a3, zero lw a0, argument jal ra, printResult ecall # Exit program li a0, 10 ecall iter: li a2, 2 li a3, 1 bgt a2, a1, exit addi a1, a1, -1 jal for #ecall exit: bnez a1, zeta mv a4, a3 ret zeta: mv a4, a2 ret for: add a4, a2, a3 mv a2, a3 mv a3, a4 addi a1, a1, -1 bne a1, zero, for jal back # expects: printResult: mv t0, a0 mv t1, a4 la a1, str1 li a0, 4 ecall mv a1, t0 li a0, 1 ecall la a1, str2 li a0, 4 ecall mv a1, t1 li a0, 1 #ecall ret ```