Study Hub

ISA / High

RISC-V ISA Encoding

Assembly instruction을 32-bit machine code field로 옮기고 다시 읽는 규칙입니다.

Why This Matters

Encoding은 assembler와 datapath decode를 연결합니다.

Uebung 1 스타일 문제는 format, register number, immediate slicing을 안정적으로 적용하는지 봅니다.

이 단원을 알면 branch target, load/store operand, control signal도 더 쉽게 보입니다.

Beginner Story

Instruction word는 빈 양식입니다.

opcode는 어떤 양식인지 알려 주는 칸입니다.

register alias는 사람용 이름이고 machine code에는 5-bit register number가 들어갑니다.

immediate는 format에 따라 한 덩어리로 들어가거나 여러 조각으로 흩어집니다.

Glossary / Key Terms

  • opcodeinstruction 종류와 format을 알려 주는 bit 6..0 field입니다. Exam tip: decode에서는 가장 먼저 읽습니다.
  • rddestination register field입니다. Exam tip: store와 branch에는 rd가 없습니다.
  • rs1첫 source register field입니다. Exam tip: load/store에서는 base register로 자주 쓰입니다.
  • rs2두 번째 source register field입니다. Exam tip: store에서는 memory에 저장할 값의 register입니다.
  • funct3opcode 안에서 세부 operation을 고르는 3-bit field입니다. Exam tip: opcode와 함께 해석합니다.
  • funct7R-Typ에서 add와 sub 같은 차이를 만드는 7-bit field입니다. Exam tip: add와 sub 구분에 필요합니다.
  • immediateinstruction 안에 직접 들어가는 constant입니다. Exam tip: width와 signedness를 확인합니다.
  • SignExtsigned immediate를 32-bit로 넓히는 과정입니다. Exam tip: negative immediate에서 중요합니다.
  • S-Typstore instruction format입니다. Exam tip: rd 대신 immediate 하위 조각이 있습니다.
  • B-Typconditional branch format입니다. Exam tip: immediate 조각 순서와 implicit 0을 확인합니다.

Step-by-Step Method

  1. 1. real instruction 확인Pseudobefehl이면 먼저 Basisinstruktion으로 확장합니다.
  2. 2. opcode와 format 선택opcode를 reference에서 찾고 R/I/S/B/U/J를 정합니다.
  3. 3. register 변환ABI name을 x-number와 5-bit binary로 바꿉니다.
  4. 4. immediate 배치Zweierkomplement와 format별 slicing을 적용합니다.

Visual Model

31..2019..1514..1211..76..0 imm[11:0]rs1funct3rdopcode

I-type template: decide opcode/format first, then fill rs1, funct3, rd, and the sign-extended immediate.

R/I/S/B format gallery

각 format의 field order를 한 줄로 비교합니다.

R funct7 rs2 rs1 funct3 rd opcode, I imm rs1 funct3 rd opcode, S imm11:5 rs2 rs1 funct3 imm4:0 opcode, B imm12 imm10:5 rs2 rs1 funct3 imm4:1 imm11 opcode

Encoding flow

mnemonic에서 final hex까지의 절차입니다.

real instruction, format, register numbers, immediate bits, 32-bit word, hex

Interactive Visual

RISC-V Encoding Playground

instruction format을 고르면 active bit fields와 immediate layout이 바뀝니다.

ready

JavaScript가 켜져 있으면 이 영역이 조작 가능한 visual lab으로 바뀝니다.

Worked Examples

addi encode

Problem: addi t0, t1, -4

  1. I-Typ을 고릅니다.
  2. t0=x5, t1=x6입니다.
  3. imm은 111111111100입니다.
  4. field를 붙이면 0xFFC30293입니다.

rs2가 없어야 합니다.

sw encode

Problem: sw t0, 12(sp)

  1. S-Typ을 고릅니다.
  2. rs2=t0, rs1=sp입니다.
  3. imm 12를 0000000과 01100으로 나눕니다.
  4. hex는 0x00512623입니다.

rd field를 만들지 않습니다.

Common Mistakes

  • S-Typ에 rd를 넣습니다.store는 rs2를 memory에 씁니다.
  • B-Typ immediate를 그대로 붙입니다.bit label대로 재배치합니다.
  • funct3만 보고 instruction을 정합니다.opcode와 함께 읽습니다.

Active Recall

  • opcode 위치는 어디인가Hint: 가장 오른쪽 7 bit입니다.
    정답 확인

    inst[6:0]입니다.

  • t0의 x-number와 5-bit field는Hint: temporary 첫 register입니다.
    정답 확인

    x5, 00101입니다.

  • B-Typ imm[0]은 저장되는가Hint: branch target alignment를 생각합니다.
    정답 확인

    저장되지 않고 0으로 붙습니다.

Exam Connection

Uebung 1은 field table을 채우는 능력을 봅니다.

부분점수를 위해 final hex 전에 field 값을 보여 주는 것이 좋습니다.

Source Grounding

Grounding entries are course-file and source-window hints for study. When a problem needs an exact page number, branch penalty, address, or formula convention, verify the cited PDF window before finalizing the answer.

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.

  1. addi is I-Typ. Fields are imm[11:0] rs1 funct3 rd opcode.
  2. rd=t0=x5=00101, rs1=t1=x6=00110.
  3. opcode=0010011, funct3=000.
  4. -4 as 12-bit Zweierkomplement is 111111111100.
  5. Word is 111111111100 00110 000 00101 0010011.
  6. Hex is 0xFFC30293.

Worked Example 2

Problem. Encode sw t0, 12(sp).

  1. Store has no rd because it writes memory, not a register.
  2. rs2=t0=x5=00101, rs1=sp=x2=00010.
  3. sw uses S-Typ, opcode=0100011, funct3=010.
  4. Immediate 12 is 000000001100, split into imm[11:5]=0000000 and imm[4:0]=01100.
  5. Word is 0000000 00101 00010 010 01100 0100011.
  6. Hex is 0x00512623.

Common Mistakes

Active Recall

  1. State the R-Typ field order from bit 31 to bit 0.
  2. In sw t0, 12(sp), identify rs1 and rs2.
  3. Write 12-bit -4.
  4. Explain why beq has no rd.

Source Grounding