Learning Outcomes
SLO | After this lecture you can… | CLO | Bloom |
|---|---|---|---|
SLO 12.1 | Compare MIPS, RISC-V, ARM and x86 by registers, encoding, addressing and code size, using real compiled code. | CLO3 | C2 Understand |
SLO 12.2 | Explain the main fallacies and pitfalls of instruction set design. | CLO3 | C2 Understand |
SLO 12.3 | Solve Unit 2 review problems on encoding, branches and the effect of compilers. | CLO3 | C3 Apply |
§1 Four ISAs, One Program
§1.1 The four families
Slide L12-S06
MIPS32 | RISC-V | ARM (AArch64) | x86-64 | |
|---|---|---|---|---|
Origin | Stanford MIPS project; chips from 1985 | UC Berkeley, 2010 | Acorn (UK) 1985; 64-bit A64 in ARMv8 (2011) | Intel 8086 (1978); AMD64 (2003) |
Licence | licensed | open, free standard | licensed by Arm to Apple, Qualcomm, Samsung… | Intel and AMD only |
Class | load-store | load-store | load-store | register-memory |
Integer registers | 32 ($zero always 0) | 32 (x0 always 0) | 31 + zero register / SP | 16 |
Instruction length | 4 bytes (+ MIPS16) | 4 bytes (+ 2-byte 'C' extension) | 4 bytes (32-bit Thumb-2: 2 or 4) | 1 to 15 bytes |
Where used | teaching, routers, older consoles | microcontrollers, storage and AI chips | phones, Apple Macs, servers | PCs, laptops, servers |
RISC-V is modular: a small base (RV32I or RV64I) plus optional standard extensions - M (multiply/divide), A (atomics), F and D (floating point), C (compressed 16-bit instructions) and V (vectors). The MIPS company itself moved to RISC-V designs in 2021. Real-world: four car makers - every car has steering, brakes and an engine, but the controls are placed differently.
§1.2 The experiment
Slide L12-S07
One C function (the array sum of Lecture 11) was compiled with one compiler (Ubuntu clang version 18.1.3) and the same options (-O1, no loop unrolling, no vectorizing); only the target changed. Sizes are exact, read from the object files with llvm-nm.
sum.c |
§1.3 The four listings (real compiler output)
Slides L12-S08, S09
MIPS32 - 36 bytes |
Loop $BB0_1: lw, addu, addiu (counter - 1), bnez, and addiu $4, $4, 4 in the branch delay slot - it runs after the branch. The addiu after blez is also in a delay slot. 5 instructions per element.
RISC-V RV32I - 40 bytes |
Loop .LBB0_2: lw, addi (pointer + 4), add, bne a0, a1 - the branch compares two registers directly (the pointer and the end pointer). No delay slot. li, blez, mv and ret are pseudo-instructions. 4 per element.
ARMv8 AArch64 - 48 bytes |
ldr w10, [x8], #4 loads and then adds 4 to x8 in one instruction: post-indexed (autoincrement) addressing. subs sets the condition flags; b.ne reads the Z flag. wzr is the zero register. 4 per element.
x86-64 - 31 bytes |
addl (%rdi,%rdx,4), %eax adds a memory word straight into a register (register-memory) with the scaled-index mode rdi + rdx x 4. Two operands, the destination is written LAST in this (AT&T) syntax. cmpq sets flags, jne reads them. 4 per element.
§1.4 Size and loop compared
Slide L12-S10
ISA | Instructions | Loop instructions per element | Function bytes (-O1) | Function bytes (-O0) |
|---|---|---|---|---|
MIPS32 | 9 | 5 (incl. delay slot) | 36 | 132 |
RISC-V RV32I | 10 | 4 | 40 | 112 |
RISC-V RV32IC | 10 | 4 | 24 | 68 |
ARMv8 AArch64 | 12 | 4 | 48 | 100 |
ARM Thumb-2 | 11 | 4 | 24 | 54 |
x86-64 | 12 (+ 1 padding nop) | 4 | 31 (6 B padding) | 81 |
Lessons. (1) Fixed 4-byte ISAs give the largest code. (2) Compressed 16-bit forms win: the RISC-V C extension cut 40 to 24 bytes - 40% smaller with the same instructions - and Thumb-2 matched it. (3) Variable-length x86 is dense but not automatically the smallest. (4) The work per element is the same (4-5 instructions); only the packaging differs. A small function is one example, not a benchmark.
§1.5 Key design differences
Slide L12-S11
Feature | MIPS32 | RISC-V | AArch64 | x86-64 |
|---|---|---|---|---|
Conditional branch | beq/bne compare registers; < via slt | beq, bne, blt, bge compare two registers | condition flags (cmp, subs) + b.cond | flags (cmp, test) + jcc |
Zero register | $zero | x0 | xzr / wzr | none (xor reg, reg) |
Addressing modes | displacement (+ PC-relative) | displacement (+ PC-relative) | + scaled register, pre/post-index | base + index x scale + displacement |
Operands | 3 | 3 | 3 | 2, destructive |
Delay slot | yes | no | no | no |
Formats | 3 (R, I, J) | 6 (R, I, S, B, U, J) | many, all 32 bits | variable |
Condition flags are extra hidden state passed between instructions; MIPS and RISC-V avoid them, which simplifies pipelining (Unit 3).
§1.6 x86 variable length in practice
Slide L12-S12
x86-64 instruction (llvm-objdump) | Bytes |
|---|---|
testl %esi, %esi | 2 |
jle 0x1c <sum+0x1c> | 2 |
movl %esi, %ecx | 2 |
xorl %edx, %edx | 2 |
xorl %eax, %eax | 2 |
nopw (%rax,%rax) | 6 |
addl (%rdi,%rdx,4), %eax | 3 |
incq %rdx | 3 |
cmpq %rdx, %rcx | 3 |
jne 0x10 <sum+0x10> | 2 |
retq | 1 |
xorl %eax, %eax | 2 |
retq | 1 |
The 0x48 byte at the start of incq and cmpq is a REX prefix ('use 64-bit registers'). The 6-byte nopw only aligns the loop to a 16-byte boundary. The hardware must find where each instruction ends before it can decode the next; modern x86 chips translate instructions into simple fixed-size micro-operations. Real-world: reading words written without spaces - you must recognise each word to find the next one.
§2 Fallacies and Pitfalls
Slides L12-S14, S15
A pitfall is an easy mistake; a fallacy is a belief that sounds right but is false (Hennessy and Patterson, A.10).
Statement | Why | Example / evidence |
|---|---|---|
Pitfall: designing a 'high-level' instruction for one language feature | It does more than most uses need (semantic gap), is often slower than simple instructions, and compilers avoid it | VAX CALLS |
Pitfall: changing the ISA to cut code size without considering the compiler | Compilers for the same ISA differ in size as much as ISAs do | MIPS -O0 132 B vs -O1 36 B; six ISAs at -O1: 24-48 B |
Fallacy: there is such a thing as a typical program | Instruction mixes, data sizes and branches differ widely | Benchmark suites (Lecture 5) |
Fallacy: an architecture with flaws cannot be successful | Binary compatibility and clever hardware can outweigh flaws | x86 dominates PCs and servers |
Fallacy: you can design a flawless architecture | Technology changes the trade-offs | MIPS delay slot; 32-bit addresses |
Real-world: a road that was wide enough in 1985 is too narrow today - the design was not wrong, the traffic changed.
§3 Unit 2 Review
§3.1 Unit 2 in one page
Slide L12-S17
Lecture | Key idea | Typical exam question |
|---|---|---|
7 Classification of ISAs | load-store won after 1980 | Code for C = A + B in stack, accumulator, register-memory and load-store |
8 Memory addressing | displacement and immediate dominate; alignment; endianness | Name the mode; draw a word in little and big endian |
9 Operations and control flow | simple instructions dominate; PC-relative branches | Branch target from an offset; jal and $ra |
10 Formats and encoding | fixed vs variable; MIPS R, I, J | Encode and decode by hand; immediate limits |
11 Compilers | the compiler decides IC; register allocation | Name and apply an optimization; count instructions |
12 Case studies | same principles, different trade-offs | Compare two ISAs; fallacies and pitfalls |
§3.2 Worked problems
Slide L12-S18
Instruction | Fields | Machine code (MARS) |
|---|---|---|
addi $t1, $t1, -4 | op 8, rs 9, rt 9, imm -4 = 0xFFFC | 0x2129FFFC |
srl $t2, $t3, 5 | op 0, rs 0, rt 11, rd 10, shamt 5, funct 2 | 0x000B5142 |
bne $t0, $zero at 0x00400018 → 0x00400010 | offset = (0x00400010 - 0x0040001C) / 4 = -3 = 0xFFFD | 0x1500FFFD |
jal at 0x0040001C → 0x00400040 | address field = 0x00400040 / 4 = 0x100010 | 0x0C100010 |
Compiler and the CPU equation: if a compiler removes 40% of the instructions but CPI rises from 1.0 to 1.2, time ratio = (1 x 1.0) / (0.6 x 1.2) = 1 / 0.72 = 1.39 - faster, but less than the 1 / 0.6 = 1.67 the instruction count alone suggests.
§4 Common Mistakes
Slide L12-S20
Wrong idea | Correct idea |
|---|---|
x86 code is always the smallest. | In our example RV32IC and Thumb-2 (24 B) beat x86-64 (31 B). |
ARM and RISC-V are copies of MIPS. | Similar load-store core, but flags, addressing modes and compressed forms differ. |
More powerful instructions make programs faster. | They are often slower and unused by compilers (VAX CALLS). |
The ISA matters more for code size than the compiler. | MIPS 132 B at -O0 vs 36 B at -O1. |
Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Open standard | A specification anyone may use without paying | A public road anyone may drive on |
Licence | Paid permission to use a design | A franchise fee for a restaurant brand |
Extension | An optional add-on to a base ISA | Optional extra seats in a car |
Condition flags | Bits set by one instruction and read by a later branch | A sticky note left for the next person |
Post-indexing | Use the address, then change the register (autoincrement) | Read a page, then turn to the next |
Prefix (x86) | An extra byte that changes the meaning of the instruction after it | 'Dr.' before a name |
Delay slot | The instruction after a branch, which always executes (MIPS) | The bus moves one more stop before turning |
Fallacy | A belief that sounds right but is false | 'Heavy cars are always safer' |
Pitfall | An easy mistake to make | A pothole on a familiar road |
Binary compatibility | New chips run old programs unchanged | A new phone that still runs your old apps |
Self-Check Questions
- Give two differences between RISC-V and MIPS. (§1.3, §1.5)
- What does ldr w10, [x8], #4 do? Which addressing mode is it? (§1.3)
- Which of the four ISAs is register-memory? Show the instruction that proves it. (§1.3)
- Why was the RV32IC function smaller than the RV32I one? (§1.4)
- Why do condition flags make pipelining harder? (§1.5)
- Why must x86 hardware find the end of an instruction before decoding the next? (§1.6)
- Explain the pitfall about code size and the compiler with our numbers. (§2)
- Why is 'a flawed architecture cannot succeed' a fallacy? (§2)
- Encode lw $a0, 8($s1). (§3.2)
- Compiler removes 40% of instructions, CPI 1.0 → 1.2: speedup? (§3.2)
Answers: lw $a0, 8($s1) = 0x8E240008; speedup 1.39.
Further Reading
- Hennessy and Patterson (2012): Appendix A, sections A.9 (MIPS) and A.10 (fallacies and pitfalls); Appendix K (survey of ISAs).
- Ledin, J. (2020), Modern Computer Architecture and Organization: chapters on x86, ARM and RISC-V architectures.