Recap of Lecture 11 and Homework Check
- The compiler decides the instruction count.
- Phases: front end, high-level opt., global opt., code generator.
- One loop: 16 → 7 → 5 → 4 instructions per element.
- ISA helps: regularity, primitives, constants, 16+ registers.
Homework check
- 1. Decode 0x0211402A.
- 2. n = 1,000,000; 16 / 7 / 5 / 4 per element; CPI 1, 1 GHz: times and speedup?
- 3. Your aliasing example.
Today's Plan (50 minutes)
0-5
Recap and homework check Lecture 11
5-8
Plan and learning outcomes
8-25
Module 1: Four ISAs, one program MIPS, RISC-V, ARM, x86 - real clang output
25-32
Module 2: Fallacies and pitfalls Hennessy and Patterson A.10
32-38
Module 3: Unit 2 review one-page summary, two worked problems
38-45
Class activity review problems
45-50
Mistakes, quiz and homework mid-term preparation
Learning Outcomes (SLOs)
By the end of this lecture you will be able to:
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
CLO3: Explain instruction set principles and advanced instruction-level and thread-level parallelism techniques.
Module 1: Four ISAs, One Program
Same C code, same compiler, four families
Four Families at a Glance
Like four car makers: every car has steering and brakes - the controls are just placed differently.
The Same C Function
sum.c
one compiler, four targets
- int sum(int *a, int n) {
- int s = 0;
- for (int i = 0; i < n; i++)
- s = s + a[i];
- return s;
- }
- clang -O1 -fno-unroll-loops
- -fno-vectorize
- --target=<ISA> -S sum.c
One story, four newspapers
Urdu, English, Punjabi and Arabic papers print the same news in different words and lengths - like one C function in four ISAs.
MIPS32 and RISC-V (RV32I)
MIPS32 · 36 bytes
- sum:
- blez $5, $BB0_2
- addiu $2, $zero, 0
- $BB0_1:
- lw $1, 0($4)
- addu $2, $1, $2
- addiu $5, $5, -1
- bnez $5, $BB0_1
- addiu $4, $4, 4
- $BB0_2:
- jr $ra
- nop
RISC-V RV32I · 40 bytes
- sum:
- li a2, 0
- blez a1, .LBB0_3
- slli a1, a1, 2
- add a1, a0, a1
- .LBB0_2:
- lw a3, 0(a0)
- addi a0, a0, 4
- add a2, a3, a2
- bne a0, a1, .LBB0_2
- .LBB0_3:
- mv a0, a2
- ret
Loop: MIPS 5 (incl. delay slot) · RISC-V 4 instructions per element
ARMv8 (AArch64) and x86-64
AArch64 · 48 bytes
- sum:
- cmp w1, #1
- b.lt .LBB0_4
- mov x8, x0
- mov w0, wzr
- mov w9, w1
- .LBB0_2:
- ldr w10, [x8], #4
- subs x9, x9, #1
- add w0, w10, w0
- b.ne .LBB0_2
- ret
- .LBB0_4:
- mov w0, wzr
- ret
x86-64 · 31 bytes
- sum:
- testl %esi, %esi
- jle .LBB0_1
- movl %esi, %ecx
- xorl %edx, %edx
- xorl %eax, %eax
- .LBB0_4:
- addl (%rdi,%rdx,4), %eax
- incq %rdx
- cmpq %rdx, %rcx
- jne .LBB0_4
- retq
- .LBB0_1:
- xorl %eax, %eax
- retq
Loop: 4 instructions each - ARM uses post-increment, x86 a memory operand
Loop and Code Size Compared
Compressed forms: RV32I 40 B → RV32IC 24 B (same instructions, 40% smaller).
Key Design Differences
Flags are extra hidden state between instructions - MIPS and RISC-V avoid them.
x86: Variable Length in Practice
1 – 15 B
x86 instruction length
this function: 1 to 6 bytes
11 B
the 4-instruction loop
MIPS loop: 5 x 4 = 20 B
Words without spaces
Find the end of each word first.
Module 2: Fallacies and Pitfalls
Mistakes architects have really made
Pitfalls of ISA Design
Pitfall 1: high-level instructions
Pitfall 2: size without the compiler
VAX CALLS did a whole procedure call in one instruction - often slower than simple instructions; compilers avoided it.
The compiler can change code size as much as the ISA.
132 vs 36 B
24 – 48 B
same ISA, two optimization levels
six ISAs, same -O1
x3.7
x2
Fallacies of ISA Design
A road built for 1985
A road that was wide enough in 1985 is too narrow today - the design was not wrong, the traffic changed.
Module 3: Unit 2 Review
Everything since Lecture 7 on one page
Unit 2 in One Page
Load-store, simple operations, regular encoding, help for the compiler - the RISC recipe.
Worked Review Problems
- branch offset = (target - (PC + 4)) / 4 = (0x10 - 0x1C) / 4 = -3
- jump field = target / 4 = 0x00400040 / 4 = 0x100010
Class Activity: Unit 2 Review
7 min · pairs
In pairs. Problems of mid-term style.
Encode lw $a0, 8($s1) in hex.
An instruction is 3 bytes long and adds a memory word directly into a register. Which of the four ISAs? Which ISA class?
A compiler removes 40% of the instructions, but CPI rises from 1.0 to 1.2. Speedup?
From slide 10: how much smaller is the RV32IC function than RV32I, in %?
Common Mistakes
Wrong idea
Correct idea
✗ x86 code is always the smallest.
✓ Here RV32IC and Thumb-2 (24 B) beat x86-64 (31 B).
✗ ARM and RISC-V are just copies of MIPS.
✓ Similar load-store core, but flags, modes, compressed forms differ.
✗ More powerful instructions make programs faster.
✓ Often slower and unused by compilers (VAX CALLS).
✗ The ISA matters more for size than the compiler.
✓ MIPS 132 B (-O0) vs 36 B (-O1).
Recap Quiz
Answer on paper in 3 minutes, then we check together.
Q1
Which of the four ISAs use condition flags for conditional branches?
Q2
What is a branch delay slot, and which of the four ISAs has one?
Q3
What does the RISC-V 'C' extension add, and what did it do to our function?
Q4
State one pitfall of ISA design from Hennessy and Patterson.
Q5
Encode jal to 0x00400040 in hex.
Homework and Next Lecture
- Read Hennessy and Patterson, Appendix A, sections A.9-A.10.
- Name the mode: x86 (%rdi,%rdx,4) and ARM [x8], #4. Which Lecture 8 modes are they?
- Encode sw $t0, -4($sp). Decode 0x00094100.
- Mid-term practice (6 marks): compare fixed and variable-length encoding using MIPS and x86 examples from this lecture.
NEXT LECTURE
Lecture 13: Basic Pipeline Operations
Unit 3 begins: the five-stage MIPS pipeline (IF, ID, EX, MEM, WB), pipeline registers, timing diagrams and ideal speedup.