跳转至

Lecture 9 RISC-V Decision Making and Logical Operations

Branches

Text Only
1
2
# if reg1 == reg2, jump to label
beq reg1, reg2, label
Text Only
1
2
# if reg1 != reg2, jump to label
bne reg1, reg2, label
Text Only
1
2
# if reg1 < reg2, jump to label
blt reg1, reg2, label
Text Only
1
2
# if reg1 >= reg2, jump to label
bge reg1, reg2, label
Text Only
1
2
# if reg1 < reg2 (unsigned), jump to label
bltu reg1, reg2, label
Text Only
1
2
# if reg1 >= reg2 (unsigned), jump to label
bgeu reg1, reg2, label

Loop

C
1
2
3
for (...) {
    ...
}
C
1
2
3
while(...) {
    ...
}
C
1
2
3
4
do {
    ...
}
while(...);

alt text

Logical Instructions

and

and / andi

Text Only
1
2
3
4
5
6
7
# Register

and x5, x6, x7 # x5 = x6 & x7

# Immediate

andi x5, x6, 3 # x5 = x6 & 3

alt text

Not (X)

There is no logical NOT in RISC-V

Use xor with \(11111111_{two}\)

alt text

Logical Shifting

Left Shifting (0 complement)

sll / slli

Shift Left Logical (sll) and immediate (slli):

Text Only
1
slli x11, x12, 2 # x11 = x12 << 2

Store in x11 the value from x12 shifted by 2 bits to the left (they fall off end), inserting 0’s on right; << in C.

alt text

Right Shifting

Complete with 0

srl / srli

Shift Right Logical (srl) and immediate (srli):

Complete with signal bit

sra / srai

Shift Right Logical (sra) and immediate (srai):

alt text

Program Execution

alt text

Symbolic register names

  • a0 - a7 for argument registers (x10 - x17) for function calls
  • zero for x0

Pseudo - instructions

Shorthand syntax for common assembly idioms

Text Only
1
2
3
mv rd, rs  =  addi rd, rs, 0
li rd, 13  =  addi rd, zero, 13
nop  =  addi zero, zero, 0