Learning Outcomes
SLO | After this lecture you can… | CLO | Bloom |
|---|---|---|---|
SLO 10.1 | Compare variable, fixed and hybrid instruction encodings and the trade-off between code size and decoding. | CLO3 | C2 Understand |
SLO 10.2 | Encode and decode MIPS R, I and J format instructions by hand, and state their meaning (semantics). | CLO3 | C3 Apply |
SLO 10.3 | Compute branch and jump fields and explain the limits set by field sizes. | CLO3 | C3 Apply |
§1 Encoding Choices
§1.1 What must be encoded
Slide L10-S06
Every instruction carries an opcode (which operation) and, for each operand, an address specifier (which addressing mode and which register or constant). Three forces pull against each other: (1) we want many registers and addressing modes; (2) every extra field makes programs bigger; (3) we want lengths that are easy to fetch and decode - ideally one fixed length for pipelining. Real-world: a CNIC number is always 13 digits, so every part is in a known place; a postal address has no fixed length and must be read word by word.
§1.2 Variable, fixed and hybrid
Slide L10-S07
Style | Idea | Lengths | Examples | Code size | Decoding |
|---|---|---|---|---|---|
Variable | Any number of operands, each with its own mode specifier | 1-53 bytes | VAX | Smallest | Hardest |
Fixed | The opcode implies the modes; one length | 4 bytes | MIPS, ARM32, RISC-V, SPARC, PowerPC | Largest | Easiest |
Hybrid | A few formats of different lengths | x86: 1-15 bytes; IBM 360: 2, 4, 6 | 80x86, IBM 360 | Medium | Medium |
VAX example: addl3 r1, 737(r2), (r3) takes 6 bytes - 1 opcode byte, 1 byte for r1 (register mode), 3 bytes for 737(r2) (a mode byte plus a 2-byte displacement) and 1 byte for (r3). In MIPS every instruction takes 4 bytes, however simple.
§1.3 Code density versus decode simplicity
Slide L10-S08
Code density is how much work fits in a given number of bytes. Small code matters for embedded systems and instruction caches. Simple decoding matters for speed: with fixed 4-byte instructions the next one is always at PC + 4 and every field is in a known place. Modern ISAs add compressed 16-bit forms of common instructions (ARM Thumb, MIPS16, RISC-V 'C'); typical programs shrink by roughly 25-30%. Inside the chip, x86 decodes its variable-length instructions into fixed-size micro-operations. Real-world: SMS short forms ('u r gr8') save space but take longer to read.
§2 The MIPS Formats
§2.1 R, I and J
Slide L10-S10
Format | Fields (bits) | Used by |
|---|---|---|
R | op (6) | rs (5) | rt (5) | rd (5) | shamt (5) | funct (6) | add, sub, and, or, slt, sll, srl, jr |
I | op (6) | rs (5) | rt (5) | immediate (16) | addi, andi, ori, slti, lui, lw, sw, lb, sb, beq, bne |
J | op (6) | address (26) | j, jal |
Opcode / funct | Value | Opcode / funct | Value |
|---|---|---|---|
R format (op) | 0 | addi (op) | 8 (0x08) |
add (funct) | 0x20 | lw (op) | 35 (0x23) |
sub (funct) | 0x22 | sw (op) | 43 (0x2B) |
and / or (funct) | 0x24 / 0x25 | beq / bne (op) | 4 / 5 |
slt (funct) | 0x2A | j / jal (op) | 2 / 3 |
sll / jr (funct) | 0x00 / 0x08 | lui / ori (op) | 15 / 13 |
Registers | Numbers |
|---|---|
$zero, $at | 0, 1 |
$v0-$v1, $a0-$a3 | 2-3, 4-7 |
$t0-$t7 | 8-15 |
$s0-$s7 | 16-23 |
$t8-$t9 | 24-25 |
$gp, $sp, $fp, $ra | 28, 29, 30, 31 |
§2.2 Semantics (register-transfer notation)
Slide L10-S11
Instruction | Format | Meaning |
|---|---|---|
add rd, rs, rt | R | R[rd] ← R[rs] + R[rt] |
sll rd, rt, shamt | R | R[rd] ← R[rt] << shamt |
addi rt, rs, imm | I | R[rt] ← R[rs] + SignExt(imm) |
ori rt, rs, imm | I | R[rt] ← R[rs] OR ZeroExt(imm) |
lw rt, imm(rs) | I | R[rt] ← Mem[R[rs] + SignExt(imm)] |
sw rt, imm(rs) | I | Mem[R[rs] + SignExt(imm)] ← R[rt] |
beq rs, rt, off | I | if R[rs] = R[rt] then PC ← PC + 4 + SignExt(off) x 4 |
j addr | J | PC ← (PC + 4)[31:28] joined with addr x 4 |
jal addr | J | R[31] ← PC + 4, then as j |
SignExt copies the top bit of the 16-bit value into the upper 16 bits (so -5 stays -5); ZeroExt fills them with zeros (logical instructions treat the constant as unsigned).
§2.3 Encoding by hand
Slides L10-S12, S13
Method: (1) find the format and the opcode/funct; (2) write each register number and constant in binary with the right number of bits; (3) join the fields; (4) group in fours and convert to hex. All machine codes below were read from the MARS Code column.
Instruction | Fields | Binary (by field) | Hex |
|---|---|---|---|
add $t0, $s1, $s2 | op 0, rs 17 ($s1), rt 18 ($s2), rd 8 ($t0), shamt 0, funct 0x20 | 000000 10001 10010 01000 00000 100000 | 0x02324020 |
sll $t2, $t1, 4 | op 0, rs 0, rt 9 ($t1), rd 10 ($t2), shamt 4, funct 0 | 000000 00000 01001 01010 00100 000000 | 0x00095100 |
lw $t0, 32($s3) | op 35, rs 19 ($s3), rt 8 ($t0), imm 32 | 100011 10011 01000 0000000000100000 | 0x8E680020 |
sw $t0, 48($s3) | op 43, rs 19 ($s3), rt 8 ($t0), imm 48 | 101011 10011 01000 0000000000110000 | 0xAE680030 |
addi $t1, $t0, -5 | op 8, rs 8 ($t0), rt 9 ($t1), imm -5 = 0xFFFB | 001000 01000 01001 1111111111111011 | 0x2109FFFB |
Watch out: in assembly rd is written first (add $t0, $s1, $s2) but in the bits it is third. In I format the destination is rt - there is no rd. |
§2.4 Branch and jump fields
Slide L10-S14
0x00400014 loop: beq $t0, $t1, done |
beq offset = (target - (PC + 4)) / 4 = (0x00400020 - 0x00400018) / 4 = 2 → 0x11090002. j field = target / 4 with the top 4 bits dropped = 0x00400014 / 4 = 0x100005 → (2 << 26) joined with 0x100005 = 0x08100005. The CPU rebuilds the jump target as the top 4 bits of PC + 4 joined with 0x100005 x 4 = 0x00400014.
§2.5 Decoding by hand
Slide L10-S15
Recipe: (1) hex to binary; (2) the first 6 bits are the opcode: 0 → R format (then read funct), 2 or 3 → J format, anything else → I format; (3) cut the other bits into the fields of that format; (4) turn register numbers into names.
Machine code | Fields | Instruction (MARS Basic column) |
|---|---|---|
0x8E480008 | op 35 (lw), rs 18, rt 8, imm 8 | lw $t0, 8($s2) |
0x01095022 | op 0, rs 8, rt 9, rd 10, shamt 0, funct 0x22 (sub) | sub $t2, $t0, $t1 |
0x1500FFFE | op 5 (bne), rs 8, rt 0, imm 0xFFFE = -2 | bne $t0, $zero, -2 (at 0x0040000C → back to 0x00400008) |
§3 Limits and Regularity
§3.1 Limits set by field sizes
Slide L10-S17
Field | Bits | Limit |
|---|---|---|
Register number | 5 | 32 registers |
shamt | 5 | shift by 0-31 |
Immediate | 16 | -32768 to +32767 (signed) |
Branch offset | 16 (in words) | about ±32K instructions (±128 KB) from PC + 4 |
Jump address | 26 (in words) | targets in the same 256 MB region |
A constant that does not fit is built by the assembler in $at. Real MARS Basic column for addi $t0, $t0, 40000 (40000 > 32767):
lui $at, 0x0 |
§3.2 Why MIPS looks like this
Slide L10-S18
MIPS choice | Benefit |
|---|---|
Every instruction is 4 bytes | The next instruction is always at PC + 4 |
Opcode always in bits 31-26 | The type of instruction is known at once |
rs and rt in the same bits in R and I | Registers can be read before decoding is finished (pipelining, Unit 3) |
Only three formats | Small, fast decoder |
16-bit constants in I format | A good compromise: most constants fit (Lecture 8 statistics) |
Patterson and Hennessy's design principles: 'simplicity favours regularity' and 'good design demands good compromises'.
§4 Common Mistakes
Slide L10-S20
Wrong idea | Correct idea |
|---|---|
In add $t0, $s1, $s2 the first field is $t0. | The bit order is rs, rt, rd: $s1, $s2, then $t0. |
In lw the destination goes in rd. | I format has no rd: the destination is rt. |
The branch offset is target minus branch address, in bytes. | (target - (PC + 4)) / 4, in words. |
Any constant fits in addi. | Only -32768 to +32767; bigger ones become lui + ori + add. |
Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Encoding | How an instruction is written as bits | Writing a CNIC number in its fixed format |
Opcode | The part that says which operation | The form type on a NADRA token |
Format | The layout of fields in an instruction | The boxes printed on a form |
Field | A group of bits with one meaning | One box on the form |
Code density | How much work fits in few bytes | SMS short forms |
Sign extension | Copy the top bit to make a longer number | Writing -5 as -0005 |
Semantics | The exact meaning of an instruction | What the form actually asks you to do |
Two's complement | The usual binary form for negative numbers | -5 = 1111 1111 1111 1011 (16 bits) |
Micro-operation | A simple internal step a complex instruction is split into | A big order split into small tasks in a kitchen |
Self-Check Questions
- Give one advantage and one disadvantage of fixed-length encoding. (§1.2-§1.3)
- How many bytes does the VAX instruction addl3 r1, 737(r2), (r3) take? Why? (§1.2)
- Draw the R, I and J formats with field sizes. (§2.1)
- Write the meaning of lw and beq in RTL. (§2.2)
- Encode sub $s0, $t1, $t2 in hex. (§2.3)
- Encode addi $t0, $zero, 7 in hex. (§2.3)
- A beq at 0x00400030 must reach 0x00400010. What is the offset field? (§2.4)
- Decode 0x8D090004. (§2.5)
- What happens to addi $t0, $t0, 40000 in MARS? (§3.1)
Answers (assembled in MARS): 0x012A8022; 0x20080007; -9 = 0xFFF7; lw $t1, 4($t0).
Further Reading
- Hennessy and Patterson (2012): Appendix A, section A.7 (encoding an instruction set) and A.9 (MIPS).
- Patterson and Hennessy, Computer Organization and Design: Chapter 2, sections 2.5 (representing instructions) and 2.10 (MIPS addressing).