Miscellaneous - Instruction Cycle, Pipeline, Interrupts, and ISA
π§ Instruction Cycle, Pipeline, Interrupts, and ISA
A structured overview of how CPUs execute instructions, handle interrupts, improve performance, and differ by instruction set architecture (ISA).
1οΈβ£ Instruction Cycle
The CPU repeatedly executes instructions using a cycle:
1
Fetch β Execute β Fetch β Execute
Sometimes an extra Indirect Cycle occurs when indirect addressing is used.
Main Phases
- Fetch β Load instruction from memory
- Decode β Interpret instruction
- Execute β Perform operation
- Writeback β Store result
2οΈβ£ Interrupts β Handling Urgent Events
Interrupts allow the CPU to pause current work to handle urgent tasks.
2.1 Synchronous Interrupts (Exceptions)
Triggered by CPU-detected conditions:
- Faults (e.g., page fault)
- Traps (software interrupts)
- Aborts (critical failures)
π Caused by current instruction execution
2.2 Asynchronous Interrupts (Hardware Interrupts)
Triggered by external devices:
- Keyboard
- Disk
- Network card
Types
- Maskable Interrupts β Can be disabled
- Non-Maskable Interrupts (NMI) β Cannot be ignored
Interrupt Handling Flow
- Interrupt request signal occurs
- CPU checks Interrupt Flag (in Flag Register)
- CPU saves execution state to stack
- Jump to Interrupt Service Routine (ISR)
- ISR executes required work
- CPU restores state and resumes execution
Saved State Includes
- Program Counter (PC)
- Instruction Register (IR)
- Memory Address Register (MAR)
- Memory Buffer Register (MBR)
Interrupt Vector
A table that maps interrupt signals β ISR addresses
Allows CPU to quickly find correct handler.
3οΈβ£ CPU Performance Scaling
Ways to Make CPUs Faster
β Higher clock speed (Hz)
β More CPU cores
β More hardware threads
β Better instruction-level parallelism
Important Reality
Performance does NOT scale linearly with core count.
Hardware Threads vs Software Threads
| Type | Meaning |
|---|---|
| Hardware Threads | Parallel execution units inside a core |
| Software Threads | Logical execution paths in programs |
π Multithreading works because each thread has its own register state.
4οΈβ£ Instruction Pipeline
CPUs overlap instruction execution like an assembly line.
Pipeline Stages
1
Fetch β Decode β Execute β Writeback
Allows multiple instructions to run simultaneously.
5οΈβ£ Pipeline Hazards
πΉ Data Hazard
Occurs when instructions depend on earlier results.
πΉ Control Hazard
Occurs when branches change Program Counter unexpectedly. β Mitigated by Branch Prediction
πΉ Structural Hazard
Occurs when instructions compete for same CPU resource.
6οΈβ£ Superscalar Architecture
A superscalar CPU executes multiple instructions per cycle using multiple pipelines.
Tradeoff
More pipelines = more hazard complexity
β‘ Performance does not scale perfectly
7οΈβ£ Out-of-Order Execution
CPU reorders instructions internally as long as program results remain correct.
β Improves efficiency
β Reduces idle cycles
8οΈβ£ Instruction Set Architecture (ISA)
ISA defines what instructions a CPU understands.
It affects:
- Instruction formats
- Register count
- Pipeline design
- Performance characteristics
8.1 CISC β Complex Instruction Set Computer
π§ͺ Example Instructions (CISC β x86)
CISC instructions can perform multiple operations in one instruction.
REP MOVSB ; Copy memory block (loop + load + store)
ADD [MEM], REG ; Memory read + add + write back
π One instruction may involve memory access, arithmetic, and looping.
Characteristics
- Variable-length instructions
- Many instruction types
- Fewer instructions per program
Pros
β Compact programs
Cons
β Hard to pipeline
β Variable execution time
8.2 RISC β Reduced Instruction Set Computer
π§ͺ Example Instructions (RISC β ARM / RISC-V)
RISC instructions perform one simple operation per instruction.
LDR X0, [ADDR] ; Load
ADD X1, X0, X2 ; Compute
STR X1, [ADDR] ; Store
π Memory access is separated from computation.
Characteristics
- Fixed-length instructions
- Load/Store architecture
- Many general-purpose registers
- Most instructions execute in ~1 clock cycle
Pros
β Pipeline-friendly
β Predictable timing
Cons
β More instructions required
πΉ CISC Example (x86)
CISC instructions often perform multiple operations in one instruction.
Example
REP MOVSB
Meaning: Copy a block of memory (loop + load + store)
Equivalent conceptual steps:
1
2
3
4
5
loop:
load byte from source
store byte to destination
increment pointers
repeat until done
Another Example
ADD [MEM], REG
π Reads memory + adds + writes back in one instruction
πΉ RISC Example (ARM / RISC-V)
RISC instructions are simple and fixed-length, usually one operation per instruction.
Example
LDR X0, [ADDR] ; Load
ADD X1, X0, X2 ; Compute
STR X1, [ADDR] ; Store
π Memory access is separated from arithmetic
πΉ Same Task β CISC vs RISC Comparison
Task: A = A + B
CISC
ADD [A], [B]
RISC
LDR R0, [A]
LDR R1, [B]
ADD R0, R0, R1
STR R0, [A]
π§ Key Insight
| Architecture | Style |
|---|---|
| CISC | Few powerful instructions |
| RISC | Many small, predictable instructions |
CISC = complex instructions, fewer lines
RISC = simpler instructions, more lines, faster pipelines
9οΈβ£ Developer Takeaways
β Interrupts protect responsiveness
β Pipeline improves throughput, but introduces hazards
β Superscalar β linear performance scaling
β RISC simplifies pipelines
β ISA choice shapes CPU behavior
π§© One-Line Mental Model
Modern CPUs execute many instructions in parallel while preserving correct program behavior.