Conditional Branches
Learning Outcomes
- Write conditional statements in RISC-V.
- Use
bneinstructions for if-conditions that compare on equality. - Differentiate between conditional branch instructions and unconditional jump instructions.
::::{note} 🎥 Lecture Video :class: dropdown
In the past few chapters, we have learned how to use RISC-V as a calculator. We have learned arithmetic and bitwise operations and data transfers instructions for accessing memory. To support modern programming languages, however, we need to support decision-making–meaning, the computer should be able to conditionally execute certain instructions based on the results of other instructions.
Review: Control Flow
In C and higher-level languages, we specify decision-making with control flow syntax. Recall that in C, typically we execute statements sequentially. Control flow syntax creates structures that “jump” to other lines of code:
ifstatements (andif-else,if-else-elif, etc.)forandwhileloops- Function calls1
If we consider the processor as a central component, we transfer control to subsequent statements for their execution. Control flow can specify conditional or unconditional transfer to out-of-order statements. Consider the C code in #fig-c-control-flow:
Illustration of unconditional and conditional control flow transfer.
The conditional if statement conditionally executes Line 4 only if n > 5; otherwise, it executes the next statement, on Line 6. By contrast, the call to foo in Line 11 unconditionally transfers control to the first statement in foo on Line 2; then, when foo returns on Line 6, control unconditionally transfers back to Line 12.
Branches and Jumps
RISC-V implements control flow by changing the order of execution in the code. Specifically, such instructions transfer control by updating the program counter not to the next instruction as is the default, but another instruction. There are two types2 of such instructions in RV32I: unconditional jumps and conditional branches.
Unconditional jump. Always set PC to a different instruction. We introduce the primary jump instruction here:
j Label
When run, this instruction will unconditionally Jump to the instruction labeled Label.
Conditional branch. Condition on the comparison of two register values. If the condition is met, set PC to a different instruction. Otherwise, the condition is not met, so set PC to the next instruction. The general format for branch instructions is bxx rs1 rs2 Label, where “xx” specifies the type of comparison to make.
Branch Example
How do we use branch instructions? Let’s check out beq and bne in #tab-rv-beq-bne:
Consider the two examples below, which assume the following mapping of C integer variables to registers:
x y z i j
x10 x11 x12 x13 x14
Branch Instructions Summary
#tab-rv-branch lists the conditional branch instructions from the RISC-V green card Control table:
This set4 is sufficient to describe the C comparators: ==, !=, >, <, >=, <= for signed and unsigned integers. From the RV32I Specification:
Note, BGT, BGTU, BLE, and BLEU can be synthesized by reversing the operands to BLT, BLTU, BGE, and BGEU, respectively.
In other words, bgt, bgtu, ble, bleu are pseudoinstructions! We leave their translation as an exercise to you :-)
We have also discussed one jump pseudoinstruction in #tab-rv-jump. We will explain this pseudoinstruction in more detail in a future section.
Footnotes
-
More later! ↩
-
If jumps are defined as unconditional, are there conditional jumps? What about unconditional branches? Both of these can probably be defined using the other, and you will see this overlap in terminology in lecture, in conversation, and in the literature. The RISC-V ISA is careful to refer to both “control transfer instructions” and, in almost all cases, associates the adjectives “unconditional” and “conditional” solely with “jump” and “branch,” respectively. ↩
-
More later. ↩
-
Here is Professor Bora Nikolic’s tip for remembering which branch instructions are supported: “There exists a BLT sandwich (Bacon, Lettuce, Tomato), but I have never seen a BGT sandwich.” ↩