Recap of Lecture 9 and Homework Check
- Categories: ALU, data transfer, control, system (+ FP).
- Ten simple x86 instructions = 96% of those executed.
- Branch target = (PC + 4) + offset x 4.
- jal puts PC + 4 in $ra; jr $ra returns.
Homework check
- 1. Classify lw, add, beq, jal, syscall, sll, sw, jr, add.s, lbu.
- 2. beq at 0x00400020, offset -3? bne at 0x00400100, offset 5?
- 3. jal at 0x00400010: $ra? What if the procedure also uses jal?
Today's Plan (50 minutes)
0-5
Recap and homework check Lecture 9
5-8
Plan and learning outcomes
8-16
Module 1: Encoding choices variable, fixed, hybrid; code density
16-33
Module 2: MIPS R, I and J formats semantics, encode and decode by hand
33-38
Module 3: Limits and regularity field sizes, big constants, why MIPS looks like this
38-45
Class activity encode, decode, branch offset
45-50
Mistakes, quiz and homework
Learning Outcomes (SLOs)
By the end of this lecture you will be able to:
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
CLO3: Explain instruction set principles and advanced instruction-level and thread-level parallelism techniques.
Module 1: Encoding Choices
How instructions become bits
What Must Be Encoded?
CNIC vs address
Fixed 13 digits: every part is in a known place. An address must be read word by word.
Three forces: many registers and modes × small code × easy, fixed-length decoding.
No encoding wins on all three - every ISA picks a compromise.
Variable, Fixed and Hybrid Encodings
- VAX: addl3 r1, 737(r2), (r3)
- opcode | r1 | mode+r2 | 737 (2 B) | (r3)
- 1 B | 1 B| 1 B | 2 B | 1 B = 6 bytes
MIPS needs the same 4 bytes for every instruction - even a tiny one.
Code Density versus Decode Simplicity
4 B
16-bit
≈ 25-30%
every MIPS instruction
Thumb, MIPS16, RV 'C'
smaller code
next one at PC + 4
for common instructions
typical with compressed forms
SMS short forms
x86 inside
'u r gr8' is shorter but harder to read - the same trade-off as dense code.
Variable-length x86 is decoded into fixed-size micro-operations before execution.
Module 2: The MIPS Formats
Three formats, 32 bits each
R, I and J Formats
op (6)
rs (5)
rt (5)
rd (5)
shamt (5)
funct (6)
op
rs
rt
rd
shamt
funct
R add, sub, and, or, slt, sll, jr
op (6)
rs (5)
rt (5)
immediate / offset (16)
op
rs
rt
16-bit constant
I addi, andi, ori, slti, lw, sw, lb, sb, lui, beq, bne
op (6)
address (26)
op
26-bit word address
J j, jal
Semantics: What Each Instruction Means
Encoding R Format by Hand
add $t0, $s1, $s2 → op 0, rs 17, rt 18, rd 8, shamt 0, funct 32
op (6)
rs (5)
rt (5)
rd (5)
shamt (5)
funct (6)
000000
10001
10010
01000
00000
100000
= 0x02324020 (MARS Code column)
sll $t2, $t1, 4 → op 0, rs 0, rt 9, rd 10, shamt 4, funct 0
op (6)
rs (5)
rt (5)
rd (5)
shamt (5)
funct (6)
000000
00000
01001
01010
00100
000000
= 0x00095100 (MARS Code column)
Registers: $t0-$t7 = 8-15 · $s0-$s7 = 16-23 · $sp = 29 · $ra = 31
Encoding I Format by Hand
lw $t0, 32($s3) → op 35, rs 19, rt 8, imm 32
op (6)
rs (5)
rt (5)
immediate (16)
100011
10011
01000
0000000000100000
= 0x8E680020 · sw $t0, 48($s3) = 0xAE680030
addi $t1, $t0, -5 → op 8, rs 8, rt 9, imm -5 (two's complement 0xFFFB)
op (6)
rs (5)
rt (5)
immediate (16)
001000
01000
01001
1111111111111011
= 0x2109FFFB (MARS Code column)
In I format the destination is rt - there is no rd.
Branch and Jump Fields
L10_encode.asm (part)
- 0x00400014 loop: beq $t0, $t1, done
- 0x00400018 addi $t0, $t0, 1
- 0x0040001C j loop
- 0x00400020 done: li $v0, 10
- beq offset = (target - (PC + 4)) / 4 = (0x20 - 0x18) / 4 = 2
- j field = target / 4 (drop top 4 bits) = 0x00400014 / 4 = 0x100005
Rule: branch offsets are words from PC + 4; jump fields are word addresses (target ÷ 4).
Decoding Machine Code by Hand
Decoding recipe
- 1. hex → binary
- 2. first 6 bits = opcode: 0 → R (read funct) 2, 3 → J else → I
- 3. cut the other bits into the fields of that format
- 4. register numbers → names
Module 3: Limits and Regularity
What 32 bits can and cannot hold
Limits Set by the Field Sizes
40000 > 32767
- # source
- addi $t0, $t0, 40000
- # MARS Basic column (real)
- lui $at, 0x0
- ori $at, $at, 0x9c40
- add $t0, $t0, $at
Big constants: the assembler builds them with lui + ori in $at, then uses a register-register add.
Why MIPS Looks Like This
Principles: simplicity favours regularity · good design demands good compromises.
Class Activity: Encode, Decode, Branch
7 min · pairs
In pairs. Use the register numbers and opcodes on slides 10-13.
Encode sub $s0, $t1, $t2 in hex (funct 0x22).
Encode sw $s1, -8($sp) in hex (opcode 43).
Decode 0x2128000A.
A beq at 0x00400030 must reach 0x00400010. What is its offset field (decimal and hex)?
Common Mistakes
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 - 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.
Recap Quiz
Answer on paper in 3 minutes, then we check together.
Q1
Name the six fields of an R-format instruction and their sizes in bits.
Q2
Encode addi $t0, $zero, 7 in hex.
Q3
Decode 0x8D090004.
Q4
What are the largest and smallest constants addi can hold?
Q5
Give two reasons why fixed-length instructions help a fast processor.
Homework and Next Lecture
- Read Hennessy and Patterson, Appendix A, sections A.7-A.8.
- Encode: or $t3, $t4, $t5 (funct 0x25); lw $s0, -12($fp); j to 0x00400040.
- Decode: 0x00A63020 and 0xAFBF0004.
- Code size: a program has 200 MIPS instructions. If 60% could use 16-bit compressed forms, what is its new size in bytes and the saving in %?
NEXT LECTURE
Lecture 11: The Role of Compilers
Compiler structure, optimizations such as register allocation and strength reduction, and how an ISA can help the compiler writer.