RISC-V
This is a GCD (Greatest Common Divisor) program with Euclidean Algorithm.
This program use Euclidean algorithm to calculate GCD. In each loop, the program will modulo two numbers and write new values to original values. Until one of values becomes 0, the program will print the result and terminate.
Pipeline has 5 stages, IF, ID, EX, MEM and WB. We use pipeline to improve the efficiency of program.
beq
, jal
, j
etc.MemRead
and MemWrite
which can distinguish between write and read operation.MemtoReg
which decide whether write data back to the register file.lw s1, num2
and beqz s1, print
lw
will get a value from memory to s1
on DM, but beqz
want to read s1
on ID. Thus, using a nop
to avoid the data hazard, and forwarding the DM to the register.bnez s1, loop
After the ALU, it knew the branch condition is met. The program needed to flush last two instructions, and refetched the instruction.
Before jump, the result of ALU will send to a MUX in order to change the PC (program counter). Meanwhile, the clear bit in IFID and IDEX will be set.
After jump
j print