Why This Matters
Encoding은 assembler와 datapath decode를 연결합니다.
Uebung 1 스타일 문제는 format, register number, immediate slicing을 안정적으로 적용하는지 봅니다.
이 단원을 알면 branch target, load/store operand, control signal도 더 쉽게 보입니다.
Full beginner lecture
RISC-V ISA Encoding
Intuition
Assembly는 사람이 읽기 쉬운 문장이고, CPU가 fetch하는 것은 32-bit instruction word입니다. Encoding은 add t0, t1, t2 같은 문장을 opcode, rd, rs1, rs2, funct3, funct7, immediate 칸에 넣는 일입니다. Decoding은 반대로 32-bit word를 field로 잘라 instruction을 복원하는 일입니다.
Rule
먼저 오른쪽 7 bit인 inst[6:0] opcode를 읽습니다. opcode가 format을 결정합니다. R-Typ은 funct7 rs2 rs1 funct3 rd opcode, I-Typ은 imm rs1 funct3 rd opcode, S-Typ은 imm[11:5] rs2 rs1 funct3 imm[4:0] opcode, B-Typ은 imm12 imm10:5 rs2 rs1 funct3 imm4:1 imm11 opcode입니다.
Register alias는 반드시 x-number로 바꾼 뒤 5-bit binary로 적습니다. t0=x5=00101, t1=x6=00110, sp=x2=00010, a0=x10=01010입니다. Negative immediate는 해당 width의 Zweierkomplement로 만듭니다.
Visual Block
R-Typ bitfield
31..25 24..20 19..15 14..12 11..7 6..0
funct7 rs2 rs1 funct3 rd opcode
I-Typ bitfield
31........20 19..15 14..12 11..7 6..0
imm[11:0] rs1 funct3 rd opcode
S-Typ bitfield
31..25 24..20 19..15 14..12 11..7 6..0
imm[11:5] rs2 rs1 funct3 imm[4:0] opcode
B-Typ immediate reconstruction
instruction bits: inst31 inst7 inst30..25 inst11..8
immediate bits: imm12 imm11 imm10..5 imm4..1
final byte offset: imm12 imm11 imm10..5 imm4..1 0
Worked Example 1
Problem. Encode addi t0, t1, -4.
addi is I-Typ. Fields are imm[11:0] rs1 funct3 rd opcode.rd=t0=x5=00101, rs1=t1=x6=00110.opcode=0010011, funct3=000.-4 as 12-bit Zweierkomplement is 111111111100.- Word is
111111111100 00110 000 00101 0010011. - Hex is
0xFFC30293.
Worked Example 2
Problem. Encode sw t0, 12(sp).
- Store has no rd because it writes memory, not a register.
rs2=t0=x5=00101, rs1=sp=x2=00010.sw uses S-Typ, opcode=0100011, funct3=010.- Immediate 12 is
000000001100, split into imm[11:5]=0000000 and imm[4:0]=01100. - Word is
0000000 00101 00010 010 01100 0100011. - Hex is
0x00512623.
Common Mistakes
- Treating
inst[11:7] as rd for every format. In S-Typ it is imm[4:0]. - Encoding a Pseudobefehl directly. Expand
mv, li, ret, call first. - Forgetting that B-Typ immediate stores no
imm[0]; it is implicitly 0. - Reading
funct3 without opcode. The same funct3 value means different things under different opcodes.
Active Recall
- State the R-Typ field order from bit 31 to bit 0.
- In
sw t0, 12(sp), identify rs1 and rs2. - Write 12-bit
-4. - Explain why
beq has no rd.
Source Grounding
Vorlesung Teil 1: RISC-V ISA, assembler, machine language.Uebung 1 and Uebung 1 Musterloesung: instruction formats and encoding practice.RISC-V Reference.pdf: opcode, funct3, funct7, register aliases, immediate layouts.