labtask2
labtask2
1 Introduction
There are two goals for this laboratory work. These are compilation and simulation of the
following C language’s statements into RISC-V assembly
1. Branching
2. Conditional Statements
2 Branching
2.1 Conditional Branching
The RISC-V instruction set has six conditional branch instructions, each of which take two
source registers and a label indicating where to go. beq (branch if equal) branches when the
values in the two source registers are equal. bne (branch if not equal) branches when they are
unequal. blt (branch if less than) branches when the value in the first source register is less
than the value in the second, and bge (branch if greater than or equal to) branches when the
1
first is greater than or equal to the second. blt and bge treat the operands as signed numbers,
while bltu and bgeu treat the operands as unsigned.
2.2 Jump
A program can jump—that is, unconditionally branch using one of three instructions: jump (j),
jump and link (jal), or jump register (jr). j jumps directly to the instruction at the specified
label.
2
Figure 3: Unconditional Branching using jump
3 Conditional Statement
If, if/else, and switch/case statements are conditional statements commonly used in highlevel
languages. They each conditionally execute a block of code consisting of one or more
statements.
3.1 If Statement
An if statement executes a block of code, the if block, only when a condition is met.
Figure 4: If Statement
3
Figure 5: If-Else Statement
4
4 Laboratory Tasks
Note: Submit your lab tasks in hard copy that inlcudes (Pg-5 to Pg-10) 1. Implement
codes in examples 6.15 and 6.16 with beq instruction in place of bne.
s0 (x8) 0x00000007
s1 (x9) 0x0000000a
s2 (x18) 0x0000000b
s3 (x19) 0x00000008
s4 (x20) 0x00000003
5
Simulate the results after RUN:
Registers Value
s0 (x8) 0x0000000a
s1 (x9) 0x00000012
s2 (x18) 0x00000002
s3 (x19) 0x00000008
s4 (x20) 0x00000003
2. Implement the High-Level Code in example 6.17 using If-Else statement and write its
corresponding RISC-V Assembly Code using following instructions
6
###################### 6.17
(Sample Code)
######################
.data
button: .word 1 amt: .word
.text
lw s0, button # Load value at memory location ’button’ into s0 lw s1, amt # Load value at
memory location ’amt’ into s1
case1:
addi t0, zero, 1 bne s0, t0,
case2 addi s1, zero, 20 j
done
case2:
addi t0, zero, 2 bne s0, t0,
case3 addi s1, zero, 50 j
done
case3:
addi t0, zero, 3 bne s0, t0,
default addi s1, zero, 100 j
done
done:
(a) High-Level Code
intbutton=1;
intamt;
7
Simulate the results after RUN:
Registers Value
t0 (x5) 0x00000001
t1 (x6) 0x00000001
t2 (x7) 0x00000000
t3 (x28) 0x00000000
t4 (x29) 0x00000014
8
t5 (x30) 0x00000000
9
t0 (x5) 0x00000002
t1 (x6) 0x00000001
t2 (x7) 0x00000002
t3 (x28) 0x00000000
t4 (x29) 0x00000032
t5 (x30) 0x00000000
10
Figure 7: RISC-V Base ISA
11
12
Figure 8: Complete Single-Cycle RISC-V Microarchitecture