Skip to main content
CPU, Memory & Program Execution
Lesson 1 of 10

CPU, Memory & Program Execution

Understand how a CPU fetches and executes instructions from RAM, how a running program's memory is organised, and how to inspect hardware resources with lscpu and free.

Beginner 20 min IT foundations

Learning objectives

  • Explain the roles of the CPU, registers, cache, and RAM during program execution
  • Trace the fetch-decode-execute cycle for a machine instruction
  • Distinguish the text, data, heap, and stack regions of a running program
  • Inspect CPU architecture and memory usage on Linux using lscpu and free

#Why this matters

Every security event, from a login check to a memory-corruption exploit, happens on real hardware. Passwords, encryption keys, and active payloads all exist as bytes in memory while the processor runs instructions one by one. Without understanding how a CPU reads instructions and data from memory, concepts like buffer overflows, shellcode, and memory forensics remain abstract.

#The hardware hierarchy: CPU, cache, and RAM

Modern computers balance speed against capacity in tiers. Storage closest to the processor is fastest and smallest; storage farther away is slower and larger.

  1. 1RegistersBytes inside each CPU core, instant access
  2. 2L1 / L2 / L3 CacheKilobytes to megabytes of fast SRAM near cores
  3. 3RAM (Main Memory)Gigabytes of volatile DRAM holding active programs
  4. 4SSD / HDD StorageTerabytes of persistent files when power is off

The CPU (Central Processing Unit) contains one or more cores, each running an instruction stream independently. Inside each core sit registers - small named slots (typically 64 bits wide on modern systems) holding numbers under active computation. Two registers matter constantly in security:

  • Instruction pointer (RIP on x86-64): holds the memory address of the next instruction to run.
  • Stack pointer (RSP on x86-64): tracks the top of the current function's stack frame.

RAM (Random Access Memory) stores gigabytes of running programs and data, addressed byte by byte. Unlike an SSD, RAM is volatile: removing power clears it within seconds. Between the CPU and RAM sits CPU cache (L1, L2, L3), keeping copies of recently used bytes so the CPU avoids stalling on slower RAM.

#The fetch-decode-execute cycle

General-purpose computers use the von Neumann architecture, storing both program instructions and program data in the same RAM as raw bytes. Each CPU core repeats a three-step loop billions of times per second:

  1. FetchRead instruction bytes from RAM at the instruction pointer address
  2. DecodeInterpret the opcode and identify target registers or addresses
  3. ExecuteRun the operation and update the instruction pointer

Suppose an instruction at address 0x401000 adds 5 to register RAX. The core fetches the bytes from 0x401000, decodes the add operation, executes it in the arithmetic logic unit (ALU), and advances the instruction pointer to 0x401004. Branch instructions (jump, call, ret) redirect the instruction pointer to another address for loops and function calls. Because code and data share RAM, systems must prevent attackers from redirecting the instruction pointer into user-supplied data.

#How a program sits in memory

When a compiled program launches, its bytes map into four main regions:

Region Contents Typical permissions Security relevance
Text (code) Compiled machine instructions Read, Execute Read-only so code cannot be overwritten
Data / BSS Global and static variables Read, Write Holds persistent program state
Heap Dynamically allocated blocks Read, Write Target of use-after-free and heap overflows
Stack Local variables, return addresses Read, Write Classic buffer overflows overwrite return addresses

The stack grows and shrinks automatically as functions are called and return; calling a function pushes a return address so execution knows where to resume. The heap holds data allocated explicitly at runtime (such as malloc in C) whose size is not fixed in advance.

#Inspecting CPU and memory in Linux

Check a host's architecture, core count, and memory in a terminal using lscpu and free -h (-h prints human-readable units such as Gi).

Checking CPU architecture and memory on a lab host
$ lscpu | grep -E "Architecture|Byte Order|CPU\(s\)|Model name|L[123]"
Architecture:            x86_64
Byte Order:              Little Endian
CPU(s):                  4
Model name:              Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
L1d cache:               128 KiB
L1i cache:               128 KiB
L2 cache:                1 MiB
L3 cache:                6 MiB
$ free -h
               total        used        free      shared  buff/cache   available
Mem:            15Gi       4.2Gi       6.1Gi       312Mi       5.2Gi        10Gi
Swap:          2.0Gi          0B       2.0Gi

Notice free (6.1Gi) versus available (10Gi). Linux uses spare RAM as buff/cache to speed up disk reads and reclaims it automatically when programs need memory. Therefore, available shows how much RAM applications can actually use without touching Swap.

#Common mistakes

  • Confusing free with available: Low free memory is normal when Linux caches files in buff/cache; check available before assuming memory exhaustion.
  • Powering off a compromised machine prematurely: Cutting power clears volatile RAM, erasing running scripts and encryption keys.
  • Assuming data cannot execute: Without non-executable (NX) memory protections, redirecting the instruction pointer into writable buffers can execute attacker bytes.
  • Ignoring byte order: Reading multi-byte addresses backwards on little-endian systems breaks memory analysis.

#Summary

  • A CPU executes instructions using fast internal registers, caches, and larger volatile RAM.
  • In the fetch-decode-execute cycle, the instruction pointer tracks which bytes in RAM run next.
  • Programs split memory into text (code), data, heap, and stack regions.
  • On Linux, lscpu reports architecture, endianness, cores, and caches, while free -h shows usable memory in available.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in