4.1.7 · D1Computer Architecture (Deep)

Foundations — Instruction formats — R-type, I-type, J-type (MIPS - RISC-V)

2,569 words12 min readBack to topic

This page assumes nothing. We build every symbol the parent topic leans on, in an order where each idea rests on the one before it.


0. A bit — the single switch

Picture a single lightbulb. It is dark or lit — no in-between. That "dark-or-lit" is one bit of information.

Why the topic needs it: the parent note keeps saying "the CPU only sees a 32-bit number." A "32-bit number" is just 32 of these switches sitting in a row. If you don't feel what one switch is, the whole row is a mystery.


1. A row of bits — reading binary

If one switch holds 2 possibilities (0 or 1), then a row of switches holds many. To read a row as an ordinary number, we give each switch a place value — exactly like the ones/tens/hundreds columns you already use, except each column is worth twice the one to its right instead of ten times.

Figure — Instruction formats — R-type, I-type, J-type (MIPS - RISC-V)

Why the topic needs it: every field in an instruction (opcode, register number, immediate) is a small binary number packed into a few switches. Encoding add $t0,... is turning register names into these little binary numbers.


2. Powers of two — why field widths are what they are

The single most-used fact in this whole chapter is:

Figure — Instruction formats — R-type, I-type, J-type (MIPS - RISC-V)

Why the topic needs it — and why and not some other tool: the parent asks "why is a register field 5 bits?" The honest answer is a counting question — "how many switches do I need to give 32 registers each their own pattern?" The tool that inverts " switches → how many patterns" is exactly the base-2 logarithm: . We use (not ) because each switch doubles, and doubling is base 2. This one formula justifies 5-bit register fields, 5-bit shamt, and 6-bit funct ( operations).


3. A register and the register file

MIPS has 32 such lockers, numbered 031. The whole set is the register file.

Programmers also give lockers nicknames ($t0, $s1, …) so code reads nicely. The number and the nickname are the same locker — see MIPS Register File and Conventions.

Why the topic needs it: R-type and I-type instructions spend most of their switches naming lockers. add $t0,$s1,$s2 is "take locker 17, locker 18, add them, store in locker 8."


4. A field — a labelled slice of the 32 bits

Figure — Instruction formats — R-type, I-type, J-type (MIPS - RISC-V)

Crucially, not every field appears in every instruction. Each format (R, I, J) is one particular way to snap the same 32-bit bar, and the fields always sum to exactly 32. The table below shows, per format, which pieces exist:

Format Fields (left → right, MSB first) Widths Sum
R-type opcode · rs · rt · rd · shamt · funct
I-type opcode · rs · rt · immediate
J-type opcode · address

And the plain meaning of each individual piece:

Field Width Plain meaning Used in
opcode 6 the "which rulebook" code word R, I, J
rs 5 a source locker number R, I
rt 5 source (R) / destination (I) locker R, I
rd 5 destination locker number R only
shamt 5 shift amount (how far to slide bits) R only
funct 6 which R-type math, when opcode = 0 R only
immediate 16 a constant / offset baked in I only
address 26 a jump target (partial) J only

Why the topic needs it: a "format" (R/I/J) is literally a choice of how to cut the bar into fields. That is the entire subject.


5. The opcode — the master key


6. Signed numbers & sign extension — the immediate's secret

The 16-bit immediate must be able to be negative (e.g. jump backwards, subtract a constant). Computers store negatives using two's complement: the top bit carries a negative place value.

Why the topic needs it: beq offsets and addi constants can be negative, so before the ALU adds them they get stretched to 32 bits keeping their sign. Without sign extension, "go back 3 instructions" would look like a huge positive jump forward.


7. Shifting left by 2 — the "×4" magic

Instructions are stored on word-aligned addresses: every instruction sits at a byte address that is a multiple of 4 (0, 4, 8, 12, …). In binary, "multiple of 4" means the bottom 2 bits are always 00.

Figure — Instruction formats — R-type, I-type, J-type (MIPS - RISC-V)

Why the topic needs it: this is the trick behind target = (PC+4) + imm×4 and the J-type reconstruction. See PC-relative vs Absolute Addressing for how branches count from PC+4.


8. The PC — where "PC+4" comes from

Why the topic needs it: branch and jump targets are measured relative to PC+4. If you don't know PC advances by 4 each step, formulas like are meaningless.


How these feed the topic

Bit = one switch

Binary number

Powers of two 2 to the n

log base 2 sets field widths

Register file 32 lockers

Fields rs rt rd 5 bits

Field = labelled slice

Opcode picks the format

Sign extension for immediate

Shift left by 2 is times four

PC advances by 4

Three formats specified in parent note

R-type = opcode rs rt rd shamt funct

I-type = opcode rs rt immediate

J-type = opcode address

Everything on the left is equipment. The three format boxes (R, I, J) are defined and encoded in the parent topic; this page only supplies the alphabet they are written in.


Equipment checklist

Self-test: read the left, answer before revealing.

What does "MIPS" refer to here?
A specific CPU design (rulebook) whose 32-bit instructions we are learning; RISC-V is a close cousin.
What is a bit, physically?
One switch that is either 0 (off) or 1 (on) — the smallest unit of information.
Read the binary row 10110 as a number.
.
How many distinct things can bits name?
.
How many bits to name 32 registers, and why?
bits, because each switch doubles the count.
What is a register?
A fast 32-bit storage slot (a numbered locker) inside the CPU; MIPS has 32 of them.
What is a field, and do all instructions share the same fields?
A fixed group of bits given one meaning; NO — each format (R/I/J) has its own set of fields that always sum to 32.
Which fields does an I-type instruction carry?
opcode, rs, rt, immediate (no rd, shamt, or funct).
Range of a 16-bit signed immediate?
to .
What does sign extension do to a 16-bit immediate?
Copies its top bit across the new high bits so its value (and sign) survive at 32 bits.
Why can we drop the bottom 2 bits of a branch/jump target?
Instructions are word-aligned, so those bits are always 00; hardware re-adds them via a left shift by 2 (×4).
Convert a branch immediate of words to a byte offset.
bytes.
Where does "PC+4" come from?
The PC advances by 4 bytes to the next instruction; branch/jump offsets are measured from PC+4.