Computer Memory and Variables'-Addressing Concepts
Computer memory system is how a computer stores and retrieves data for processing.
Let's understand more with the help of a Table below:
| Level | Name | Speed | Size | Volatility | Purpose |
|---|---|---|---|---|---|
| 0 | CPU-Registers | ~1 cycle | ~KB | Volatile | Hold data for current instruction |
| 1 | L1/L2/L3 Cache | ~3-30 cycles | ~MB | Volatile | Hold hot data to avoid RAM trips |
| 2 | Main Memory RAM | ~100 cycles | ~GB | Volatile | Running programs, OS, stack, heap |
| 3 | SSD / HDD | ~100,000 cycles | ~TB | Non-volatile | Files, apps, OS on disk |
| 4 | Cloud / Tape | seconds+ | ~PB | Non-volatile | Backup, archive |
How data flows: CPU asks for data → checks L1 cache → L2 → L3 → RAM → SSD. Each level is bigger + slower. This is called "locality-of-reference": programs tend to reuse nearby data/code, so caching works.
Memory-Management by OS:
Key idea: "Memory wall" = CPU is much faster than RAM. So we use caching + hierarchy to hide latency.
Addressing modes - how instructions specify addresses:
- Immediate: MOV AX, 5 → value is part of instruction
- Direct: MOV AX, [1000] → use address 1000 directly
- Indirect: MOV AX, [BX] → address is in register BX
- Indexed: MOV AX, [BX + SI] → base + offset
- Relative: JMP +10 → address relative to current PC
Addressing answers: "Where exactly is this byte?"
Physical vs Virtual Addressing:
Why virtual? 3 big benefits:
Memory-Mapping-Techniques:
So in short: Memory hierarchy gives speed vs capacity tradeoff, and addressing gives us a way for programs to safely and efficiently access that memory without knowing physical layout.
Addressing Modes in CPU instructions: CPU has to encode "where to get data" in the instruction itself. Common modes:
C. Paging vs Segmentation
- Segmentation: Split memory into logical chunks: code segment, data segment, stack. Each has base address + length. Problem: "external fragmentation" - lots of small gaps.
- Paging: Split memory into fixed 4KB blocks called "pages". Virtual page 0 can map to physical frame 15, page 1 to frame 100, etc. Solves fragmentation, but needs page table.
Multi-level Page Tables
For 64-bit systems, 2^64 addresses is too big for 1 table. So OS uses 4-level page tables:
Virtual address = 9 bits Level4 + 9 bits Level3 + 9 bits Level2 + 9 bits Level1 + 12 bits offset.
This is why malloc doesn’t crash even though your program "has" 128TB of address space.
Real-world example: Android-Application
Your Android app runs in its own virtual address space. When you read a file, kernel maps SSD page to RAM page, then maps RAM page to your app’s virtual address. If you access unmapped memory, you get SIGSEGV = crash.
So: Memory hierarchy = performance. Addressing = abstraction + protection.