Post

Miscellaneous - Instruction Cycle, Pipeline, Interrupts, and ISA

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

  1. Interrupt request signal occurs
  2. CPU checks Interrupt Flag (in Flag Register)
  3. CPU saves execution state to stack
  4. Jump to Interrupt Service Routine (ISR)
  5. ISR executes required work
  6. 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

TypeMeaning
Hardware ThreadsParallel execution units inside a core
Software ThreadsLogical 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

ArchitectureStyle
CISCFew powerful instructions
RISCMany 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.

This post is licensed under CC BY 4.0 by the author.