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

C8 · Lesson 7 · Instruction encoding lab

Encoding and decoding RISC-V instructions in Python

An assembler turns beq t0, t1, loop into 32 bits, and a processor's decoder, an emulator or a debugger turns those bits back into fields. All of them are a few shifts and masks. In this lab you'll write the pieces in Python: sign extension, I-type and B-type encoders, a field decoder and a small disassembler, each checked against the words this course's own assembler produces.

  • The memory window shows the program as it sits in memory: one 32-bit word per instruction, starting at address 0.
  • Step through. Each word is an instruction from lesson 4's formats: an I-type addi, an R-type sub, an S-type sw, an I-type lw, and two B-type branches: one forward 8 bytes to leave the loop, one back 8 bytes to repeat it until t2 reaches 10.
  • Pick a word and split it by hand: the low 7 bits are the opcode, and the rest falls into the fields of its format.

Predict

A B-type instruction can branch ±4 KiB in steps of 2 bytes. How many immediate bits does the instruction actually store?

Name it

Fields with shifts and masks

To read a field of w bits starting at bit p: (word >> p) & ((1 << w) - 1). To place one: (value & mask) << p, combined with |.

Format31:2524:2019:1514:1211:76:0
Rfunct7rs2rs1funct3rdopcode
Iimm[11:5]imm[4:0]rs1funct3rdopcode
Simm[11:5]rs2rs1funct3imm[4:0]opcode
Bimm[12], imm[10:5]rs2rs1funct3imm[4:1], imm[11]opcode

Sign extension

A field of b bits holding a two's-complement number is negative when its top bit is set, and then its value is the unsigned value minus 2b:

x=u−2b⋅[u≥2b−1]

Python's integers never overflow, so masking a negative number with & 0xFFF gives exactly its 12-bit two's-complement pattern.

Worked example

Encode sw a0, 8(sp) by hand.

Show the solution
  1. Split the immediate. S-type splits 8 into imm[11:5] = 0 and imm[4:0] = 8.
  2. The other fields. rs2 = a0 = 10, rs1 = sp = 2, funct3 = 010 for a word, opcode 0x23.
  3. Assemble the word. 10⋅220+2⋅215+2⋅212+8⋅27+0x23=0x00A12423
  4. Check. That's the word the assembler gave in the program above.

Answer sw a0, 8(sp) is 0x00A12423.

Worked example

Decode 0xFE628EE3.

Show the solution
  1. Opcode and funct3. Opcode 0x63 is a branch, and funct3 = 000 makes it beq.
  2. Registers. rs1 = bits 19:15 = 5 (t0), and rs2 = bits 24:20 = 6 (t1).
  3. Gather the immediate's bits. Bit 31 = 1 gives imm[12], bits 30:25 = 111111 give imm[10:5], bits 11:8 = 1110 give imm[4:1], and bit 7 = 1 gives imm[11].
  4. Put them in order. Together, 1 1111111 1110 0 = 0x1FFC.
  5. Sign-extend from 13 bits. 0x1FFC−0x2000=−4

Answer beq t0, t1, -4, a branch to the previous instruction.

Check

What's 2783 (a 12-bit field) sign-extended?

Check

Write sign_extend(value, bits): the low bits bits of value read as a two's-complement number.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write encode_i(imm, rs1, funct3, rd, opcode): the 32-bit I-type word. imm may be negative (−2048 to 2047).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write decode(word): a dict with the fields opcode, rd, funct3, rs1, rs2, funct7, and imm_i (bits 31:20 sign-extended).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write encode_b(offset, rs2, rs1, funct3): a branch word (opcode 0x63) for an even byte offset from −4096 to 4094.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write disassemble(word) for add, sub, addi, lw, sw and beq, returning text like add x10, x11, x12, addi x5, x0, 10, lw x10, 8(x2), sw x10, 8(x2) or beq x5, x6, -4. Anything else gives unknown. sign_extend is provided.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why does RISC-V scatter the bits of a branch offset around the instruction word instead of storing them in order?

Practice

Keep practicing

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