# Lab1: RV32I Simulator
###### tags: `108-1_RISC-V`
## Find largest element
:::info
It uses a for-loop that repeatedly steps through the array,then compares each value of the array with a 'flag' parameter used to store the largest value.
After for-loop, we output the value stored in 'flag' which is the largest value of the array.
:::
Array of 10: `9, 28, 63, 88, 52, 9, 75, 6, 26, 7`
## Code
**C++ code**
####
```cpp=
#include <iostream>
using namespace std;
int main()
{
int i, flag=0, size = 10;
int arr[size] = {9,28,63,88,52,9,75,6,26,7};
// Loop to store largest number to arr[0]
for(i = 0;i < size; i++)
{
// Change < to > if you want to find the smallest element
if(flag < arr[i])
flag = arr[i] ;
}
cout << "The largest number in array of " << size << " is "<< flag;
return 0;
}
```
####
**Assembly code**
```cpp=
.data
array: .word 9, 28, 63, 88, 52, 9, 75, 6, 26,7
range: .word 10 # range of the array
str1: .string "The largest number in array of "
str2: .string " is "
.text
#s1 = Base address of current array
#s2 = i
#s3 = Range to compare (10)
#t0 = Flag
#t1 = Current Address
main:
la s1, array # s1 = base address of array
add s2, x0, x0 # i = 0
add t0, x0, x0 # define Flag
lw s3, range # size = 10
jal ra, forloop # Jump-and-link to the 'forloop' label
# Exit program
li a7, 10
ecall
forloop:
addi s2, s2, 1 #i = i+1
lw t1,0(s1) #t1 = next value of the array
addi s1, s1, 4 #update new position of the next address
#if-else
bge t0, t1, then #if Flag >= A[i] jump to 'then'
add t0, x0, t1 #else Flag = A[i]
beq s2, s3, printResult #if i = 10 => stop
j forloop #back to for loop
then:
beq s2, s3, printResult #if i = 10 => stop
j forloop #else continue run for loop
printResult:
la a0, str1
li a7, 4 #print string
ecall
mv a0, s3
li a7, 1 #print integer
ecall
la a0, str2
li a7, 4
ecall
mv a0, t0
li a7, 1
ecall
ret
```
## RISC-V assembly program
this program includes of 3 parts
1. main
```cpp=
.data
array: .word 9, 28, 63, 88, 52, 9, 75, 6, 26,7
range: .word 10 # range of the array
str1: .string "The largest number in array of "
str2: .string " is "
.text
#s1 = Base address of current array
#s2 = i
#s3 = Range to compare (10)
#t0 = Flag
#t1 = Current Address
main:
la s1, array # s1 = base address of array
add s2, x0, x0 # i = 0
add t0, x0, x0 # define Flag
lw s3, range # size = 10
jal ra, forloop # Jump-and-link to the 'forloop' label
# Exit program
li a7, 10
ecall
```
* Initial parameters
* Jump to for-loop or exit the program
2. for-loop

* plus 1 to i per loop
* load value from array
* update new position of the next address for next loop
* conditional branches (if/else)
3. print result

* use ecall to print out the result which combined of string and integers
## Description
* IF(Instruction Fetch)
- Firstly, Program Counter will give instruction an address, which comes from last instruction address plus 4 (because each instruction is 32 bits (4 bytes) in RV32) or the Arithmetic Logic Unit if there is a branch instruction(in EX stage such like beq, jal,...).
- After that, the instruction and its PC address will pass down through pipeline.

* ID (Instruction decode)
- Decoder will decode the instruction and update value.

* EX (Execute)
- In this stage, we just execute the instruction according to instruction opcode. (such as arithmetic computation, branch target computation and load/store memory address)
* MEM (Memory access)
- Storing the result which executed into memory and getting data from memory or go to next stage, according to instruction opcode.
* WB (Writeback)
- write the result into register file.
## Observation
* Hazards in pipeline and solution in RIPES
- There are 3 type of hazards in pipeline
1. Structural hazards:
When hardware resource is not enough
2. Data hazard:
* Read after write (RAW)
* Write after read (WAR)
* Write after write (WAW)
It occur when an instruction scheduled blindly, use data before the data is available in register file like the picture below:

After that, the loaded value is forwarded to x6 after the MEM stage.(as the picture below)

3. Control hazard(branch hazard):
Caused by conditional and unconditional branching
My code is also have the control hazard when it jump to forloop.
(Set clear to 1 in EX stage)

After that, it adds two NOPs and jumps to the begining instruction of the label
