LEC 06

1. RISC-V Operations So Far (Review)

Before decision-making instructions, RISC-V supports:

  • Addition/Subtraction:

    • add rd, rs1, rs2 (addition)

    • sub rd, rs1, rs2 (subtraction)

    • addi rd, rs1, imm (add immediate)

  • Load/Store:

    • lw rd, imm(rs1) (load word)

    • sw rs2, imm(rs1) (store word)

These instructions are important because arithmetic results and memory data are often used to make decisions.


2. Computer Decision Making

  • Branching: In high-level programming languages, decision-making is done with control structures like if-else, for, while, etc.

  • In assembly, this is done through branch instructions that alter the control flow based on comparisons. If the condition is met, the program "branches" to another part of the code.


3. Types of Branches

  • Conditional Branches: The control flow changes depending on the outcome of a comparison between two registers.

    • beq (branch if equal)

    • bne (branch if not equal)

    • blt (branch if less than)

    • bltu (branch if less than, unsigned)

    • bge (branch if greater than or equal)

    • bgeu (branch if greater than or equal, unsigned)

  • Unconditional Branches: Always change the control flow, regardless of conditions.

    • j (jump)

    • jal (jump and link)

    • jr (jump register)


4. RISC-V Conditional Branch Instruction Syntax

  • Syntax:

    css

    Copy code

    b[cond] rs1, rs2, L

    • b[cond]: Branch instruction with a condition (e.g., beq, bne).

    • rs1 and rs2: Registers containing values for comparison.

    • L: Label or symbolic address indicating where to branch if the condition is met.


5. Example: if Statement

C Code:
c

Copy code

if (i == j) { f = g + h; }

Assume i is in x13, j is in x14, f is in x10, g is in x11, and h is in x12.

RISC-V Assembly:
assembly

Copy code

bne x13, x14, Exit # Branch to Exit if i != j add x10, x11, x12 # f = g + h Exit:

  • Explanation: The bne instruction branches to the label Exit if x13 (i) is not equal to x14 (j). If they are equal, the addition is performed.


6. Example: if-else Statement

C Code:
c

Copy code

if (i == j) { f = g + h; } else { f = g - h; }

RISC-V Assembly:
assembly

Copy code

bne x13, x14, Else # Branch to Else if i != j add x10, x11, x12 # f = g + h j Exit # Jump to Exit after if block Else: sub x10, x11, x12 # f = g - h Exit:

  • Explanation: The bne instruction checks if i != j. If true, it jumps to Else. Otherwise, it performs the addition. After the addition, an unconditional jump (j Exit) skips the else block.


7. Magnitude Comparisons in RISC-V

  • Branch on Less Than (blt):

    assembly

    Copy code

    blt reg1, reg2, Label

    Branches to Label if reg1 is less than reg2.

  • Unsigned Comparisons:

    • bltu and bgeu compare registers as unsigned integers.

  • No bgt (branch if greater than): Instead, use blt with swapped operands.


8. Loops in C and Assembly

Loops in C (e.g., while, for, do-while) can be implemented using conditional branches in assembly. The key is to compare the loop variable and branch back to the beginning until the condition is false.

Example: for Loop
C Code:
c

Copy code

int A[20]; int sum = 0; for (int i = 0; i < 20; i++) { sum += A[i]; }

RISC-V Assembly:
assembly

Copy code

add x9, x8, x0 # x9 = &A[0] add x10, x0, x0 # sum = 0 add x11, x0, x0 # i = 0 addi x13, x0, 20 # x13 = 20 Loop: bge x11, x13, Done # If i >= 20, exit the loop lw x12, 0(x9) # Load A[i] into x12 add x10, x10, x12 # sum += A[i] addi x9, x9, 4 # Move to next element in A addi x11, x11, 1 # i++ j Loop # Jump back to start of loop Done:

  • Explanation: The loop checks if i has reached 20. If true, it branches to Done. Otherwise, it loads A[i], adds it to sum, and increments both i and the array pointer.


9. Switch-Case Statement

A switch-case statement can be optimized using a branch/jump table, which stores the addresses of the case labels in an array.

C Code:
c

Copy code

switch (x) { case 0: y = 10; break; case 1: y = 20; break; case 2: y = 30; break; default: y = 0; }

RISC-V Assembly (using a jump table):
assembly

Copy code

li x12, 2 # Upper bound of cases bltu x12, x10, default # If x > 2, go to default la x13, jumptable # Load address of jump table slli x10, x10, 2 # Multiply x by 4 (size of word) add x13, x13, x10 # Add offset to jump table address lw x13, 0(x13) # Load jump address jr x13 # Jump to case jumptable: .word case0 .word case1 .word case2

  • Explanation: The value of x determines the offset into the jump table, and the program jumps to the correct case label.


Summary

  • This lecture covered decision-making instructions in RISC-V (beq, bne, blt, bge) and how to use them to implement:

    • if, if-else statements.

    • Loops (for, while).

    • Switch-case statements using jump tables.