5.1.2 · D3Instruction Set Architecture (ISA)

Worked examples — Instruction formats and encoding

3,469 words16 min readBack to topic

Before anything, let us re-anchor the three words this whole page rests on, in plain language:

Two prerequisites we lean on and where to revise them: Registers and the Register File (why a register number is just a small integer) and Two's Complement and Sign Extension (how negative numbers live in bits).


The scenario matrix

Every encoding question you will ever see is one of these cells. The worked examples below are tagged with the cell they cover, and together they hit all of them.

# Cell (case class) What makes it tricky Example
A Simple positive R-type just pack registers Ex 1
B Positive immediate (I-type) immediate sits in one block Ex 2
C Negative immediate top bit = sign, must sign-extend Ex 3
D Zero / degenerate input register 0, immediate 0 Ex 4
E Maximum / limiting value biggest immediate a field holds Ex 5
F Split immediate (S-type) bits scattered across two holes Ex 6
G Reordered + scaled (B-type) immediate in units of 2 bytes Ex 7
H Decode direction (bits → meaning) reverse the whole process Ex 8
I Field-width word problem choose bit counts from requirements Ex 9
J Exam twist (opcode-budget trap) more opcode ≠ more room Ex 10

The R-type field order we will reuse (from the parent note) is the "phone number" 7-5-5-3-5-7 — read the figure below for the picture; the math line just names the same fences:

Figure — Instruction formats and encoding

The worked examples

Cell A — simple positive R-type


Cell B — positive immediate (I-type)


Cell C — negative immediate (the sign-extension case)

Figure — Instruction formats and encoding

Cell D — zero / degenerate input


Cell E — maximum / limiting value


Cell F — split immediate (S-type store)

Look at the figure below. The top row shows the plain 12-bit value 12; the coloured arrows carry its two slices down into the S-type template. Notice the green slice () and the orange slice () land in non-adjacent holes, while rs1 and rs2 (blue) stay exactly where R-type put them — that unchanged register wiring is the reason for the whole split.

Figure — Instruction formats and encoding

Cell G — reordered + scaled (B-type branch)


Cell H — decode direction (bits → meaning)


Cell I — field-width word problem


Cell J — exam twist (the opcode-budget trap)


Recall Quick self-test on the whole matrix

Which cell involves multiplying the stored value by 2 before use? ::: Cell G (B-type branch, scaled by 2 bytes). Why does S-type split its immediate? ::: So rs1 and rs2 stay in their normal positions and the register-read wiring never changes. Largest value a 12-bit signed immediate holds? ::: ; smallest is . Which bit of a B-type immediate is the sign bit? ::: , the top bit, placed at the very top of the word so one sign-extend wire serves all formats. Encoding add x0,x0,x0 — is the word all zeros? ::: No — the opcode bits 0110011 are still set (0x00000033). First field the decoder reads? ::: The opcode, in the lowest 7 bits.


See also: Addressing Modes (how the immediate becomes a memory address) and Pipelining (why fixed-width fields make decode fast enough to overlap).