C82 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C8 · Lesson 2 · RISC-V instructions and assembly

The words a processor understands

A processor understands only a fixed set of instructions, its instruction set. RISC-V is an open instruction set used in microcontrollers, phones' helper cores and research chips, and its base set, RV32I, has only about 40 instructions. Each one does something small: add two registers, add a constant, shift, compare, load, store, branch. Assembly language is those instructions written as text, one per line, which an assembler turns into 32-bit words. Programs that look clever in C are long runs of these small steps.

Play

  • Step through the program and watch each register change (it lights up when it does).
  • RV32I has no multiply instruction. How does the program compute 10 × 7?
  • The line li a2, 0x12345678 is special. Watch how far the PC moves when it runs.

Predict

Every RISC-V instruction is 32 bits long. How many instructions does li a2, 0x12345678 (load a 32-bit constant) turn into?

Name it

Registers, instructions and their formats

Registers. 32 registers of 32 bits, x0 to x31. x0 always reads as 0, and writes to it are thrown away. Each has a conventional name for how programs use it: a0 to a7 for function arguments and results, t0 to t6 for temporaries, s0 to s11 for values kept across calls, sp the stack pointer and ra the return address.

Assembly. The destination comes first: add t2, t0, t1 means t2 = t0 + t1.

KindExamplesDoes
register-registeradd sub and or xor sll srl sra slt slturd = rs1 op rs2
register-immediateaddi andi ori xori slli srli srai sltird = rs1 op constant
load and storelw lh lb lbu sw sh sbmove data between registers and memory
branch and jumpbeq bne blt bge jal jalrchange the PC
upper immediatelui auipcload the upper 20 bits

sll shifts left (×2 per place), srl shifts right filling with 0s, and sra shifts right copying the sign bit, which divides a signed number by 2 per place. slt sets rd to 1 if rs1 < rs2.

Encoding. The fields sit in fixed places, so decoding is quick. An I-type instruction such as addi holds a 12-bit signed immediate, from −2048 to 2047:

bits31 to 2524 to 2019 to 1514 to 1211 to 76 to 0
R-type (add)funct7rs2rs1funct3rdopcode
I-type (addi)imm[11:5]imm[4:0]rs1funct3rdopcode

For example, addi x1, x0, 5 has imm = 5, rs1 = 0, funct3 = 000, rd = 1 and opcode 0010011 (0x13), which packs into the word 0x00500093.

Pseudo-instructions are shorthands: li (load a constant), mv a0, a1 (addi a0, a1, 0), not (xori with −1, which is all 1s), neg (sub from zero) and nop (addi x0, x0, 0).

Big constants. lui loads 20 bits into bits 31 to 12, and addi adds a 12-bit signed value. Because addi sign-extends, a lower part with bit 11 set counts as negative, so lui takes one more: 0x12345FFF becomes lui 0x12346 plus addi −1.

Worked example

t0 = 12 and t1 = 5. What do sub t2, t0, t1, slli t3, t1, 3 and slt t4, t1, t0 leave?

Show the solution
  1. The subtraction. sub t2, t0, t1 gives t2=12−5=7
  2. The shift. slli t3, t1, 3 shifts 5 left 3 places, which multiplies it by 8: t3=5×8=40
  3. The comparison. slt t4, t1, t0 gives t4 = 1, because 5 < 12.

Answer t2 = 7, t3 = 40 and t4 = 1.

Worked example

Why does li t0, 5000 become two instructions?

Show the solution
  1. Check the immediate's range. An addi immediate is 12 bits, at most 2047, so 5000 doesn't fit.
  2. Split the constant. 5000=4096+904
  3. Load the upper part. lui t0, 1 puts 4096 in t0.
  4. Add the rest. addi t0, t0, 904 adds the other 904.

Answer 5000 is too big for a 12-bit immediate, so the assembler uses lui t0, 1 (4096) and then addi t0, t0, 904.

Worked example

t0 holds −16 (0xFFFFFFF0). What do srli t1, t0, 2 and srai t2, t0, 2 leave?

Show the solution
  1. The logical shift. srli shifts in zeros: 0x3FFFFFFC, which is 1,073,741,820, a huge positive number.
  2. The arithmetic shift. srai copies the sign bit into the vacated places: 0xFFFFFFFC, which is −4, exactly −16 ÷ 4.
  3. Which to use. Use the arithmetic shift to divide signed numbers by powers of two, and the logical shift for unsigned numbers and bit fields.
  4. Rounding. For negative odd values the arithmetic shift rounds toward −∞, unlike C's division, which rounds toward zero.

Answer srli leaves 0x3FFFFFFC (1,073,741,820) in t1, and srai leaves 0xFFFFFFFC (−4) in t2.

Check

RV32I has no multiply instruction. With n in a0, put 5 × n in a1 using shifts and adds.

RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.

Check

With n in a0, put n with its lowest 4 bits cleared into a1. Use one instruction before the ebreak.

RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.

Check

Encode addi x12, x0, 217 as a 32-bit word in hex.

Check

The assembler splits li a0, 0x066228D8 into lui and addi. What 20-bit value does lui load?

Check

mv a0, a1 is a pseudo-instruction. Which real instruction does the assembler write for it?

Check

What's the largest value a signed 8-bit immediate can hold?

Check

a0 holds a negative number and a1 a shift amount. Put a0 shifted right arithmetically by a1 into a2.

RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.

Check

t0 holds −99 and is shifted right by 1.

(a)

What does srai give?

(b)

What does srli give, as an unsigned number?

Explain why

Why does RISC-V need two instructions to put an arbitrary 32-bit constant in a register?

Practice

Keep practicing

11 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.