Lab1: RV32I Simulator
contributed by < MatthewChen
>
Fruits into Baskets
This problem is based on LeetCode 904.
Given a farm that has a single row of fruit trees arranged from left to right.
- You only have two baskets, and each basket can only hold a single type of fruit.
- Starting from any tree of your choice, you pick the fruit into your basket.
- There is no limit on the amount of fruit each basket can hold.
- However, once you reach a tree with fruit that cannot fit in your baskets, you must stop.
- Return the maximum number of fruits you can pick.
C++
Assembly code
.data
trees: .word 3,3,3,1,4 #Line of trees
len: .word 5 #Length
str: .string "The answer is "
.text
main:
lw s1, len
addi t1, zero, 3
blt s1, t1, lessThanThree
addi s2, zero, 1 #max
addi s3, zero, 1 #tempMax
addi s4, zero, 1 #cnum
la s5, trees
lw s6, 0(s5) #temp1
addi s5, s5, 4
lw s7, 0(s5) #temp2
la s5, trees
addi s9, zero, 1 #i? counter
loop:
lw t1, 0(s5) #t1 = fruits[i-1]
addi s5, s5, 4
lw t2, 0(s5) #t2 = fruits[i]
beq t2, s6, oldFruit
beq t2, s7, oldFruit
addi s6, t1, 0 #temp1 = fruits[i-1]
addi s7, t2, 0 #temp2 = fruits[i]
addi s3, s4, 1 #tempmax = cnum + 1
loop1:
blt s2, s3, maxUpdate
loop2:
addi s9, s9, 1
beq s9, s1, finishP
bne t1, t2, newCounter
addi s4, s4, 1
j loop
oldFruit:
addi s3, s3, 1 #tempmax += 1
j loop1
newCounter:
addi s4, zero, 1 #cnum reset
j loop
maxUpdate:
addi s2, s3, 0 #max = tempmax
j loop2
lessThanThree:
add a0, s1, zero
li a7, 1 #print size
ecall
li a7, 10 #end program
ecall
finishP:
la a0, str #string
li a7, 4 #print string
ecall
add a0, s2, zero #load answer
li a7, 1 #print max
ecall
li a7, 10 #end program
ecall
Compiled Memory
Processing Pipeline

Executions in processor can be divided in to five stages. They are :
- IF: Instruction fetch
- ID: Instruction Decode
- EX: Exceute
- MEM: Memory read/write
- WB: Write Back
IF (Instruction fetch)
The instruction is fetched from memory and placed in the instruction register according to the program counter(PC).
This currently loading instruction is at the memory location 0x000010 (marked as red). As shown in picture below, the next instruction is also prepared.

ID (Instruction Decode)
The current instruction are decoded into control signals.
It also moves operands from registers or immediate fields to working registers.
For branch instructions, the branch condition is tested and the corresponding branch address computed.

EX (Exceute)
Where Arithmetic Logic Unit(ALU) locate. Compute according to opcode and get the answer. Include (+ - * / << >> & | …).

MEM (Memory read/write)
Write data to memory or Read data from memory.
Load the length of list into register s1.

WB (Write Back)
Write the result back to register.
The multiplexer add zero and 1 from ALU as final output. So the output value is 0x00000001.

Reference
The RISC-V Instruction Set Manual