CS 101 › Lesson 5 of 8

How Programs Work

Lesson 5 · OKSTEM College · Associate of Science in Computer Science

How Programs Work

A program is a sequence of instructions stored in memory. The CPU fetches, decodes, and executes these instructions in a continuous cycle. Understanding this cycle explains performance, bugs, and security vulnerabilities.

The Fetch-Decode-Execute Cycle

  1. Fetch: Read the next instruction from memory at the address held in the Program Counter (PC).
  2. Decode: Interpret the instruction opcode to determine the operation (ADD, MOV, JMP…).
  3. Execute: Perform the operation using the ALU or memory subsystem.
  4. Writeback: Store the result into a register or memory.
  5. Increment PC and repeat.

Memory Layout of a Running Program

RegionContentsNotes
StackLocal variables, return addressesGrows downward; fast; fixed size
HeapDynamically allocated objectsManaged by malloc/GC; flexible
DataGlobal/static variablesInitialized at program load
Text (Code)Compiled machine instructionsRead-only in most OSes

Stack overflow occurs when recursive calls or large local variables exhaust stack space. Heap fragmentation can slow dynamic allocation.

Compilation vs Interpretation

Compiled languages (C, C++, Rust) translate source code to machine code ahead of time. Faster execution, but you must recompile for each architecture.

Interpreted languages (Python, JavaScript) execute source line-by-line at runtime via an interpreter. Slower but portable and interactive.

JIT-compiled (Java, C#, modern JavaScript engines) compile to bytecode, then to native code at runtime — combining portability with near-native speed.

Lab — CPU Cycle Simulator

Knowledge Check

In the fetch-decode-execute cycle, what does the Program Counter (PC) hold?

Which memory region stores local variables and function return addresses?

A compiled language differs from an interpreted one in that it

JIT compilation combines the benefits of

Stack overflow most commonly occurs due to

← PreviousNext →