Computer Memory and Variables'-Addressing Concepts

Computer memory system is how a computer stores and retrieves data for processing. 

  1. Computer Memory System Concept: Memory is organized in a hierarchy based on speed, cost, and size:
  • Registers: Smallest, fastest, inside CPU. Hold current instruction + data being processed. 
  • Cache L1/L2/L3: SRAM memory on or near CPU. Caches frequently used data to avoid slow RAM access. 
  • Main Memory / RAM: DRAM. Volatile. Holds OS, apps, data currently in use. Accessed by CPU via memory bus.
  • Secondary Storage: HDD, SSD, NVMe. Non-volatile, large, slow. Stores OS, files, programs long-term.
  • Tertiary Storage: Tape, cloud. For backup/archival.

Let's understand more with the help of a Table below:

Think of memory like a library with different storage rooms
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: 

  • Allocation: malloc / new gets you a chunk of virtual memory.
  • Protection: Each process gets its own virtual space. Process A can’t read Process B’s RAM.
  • Swapping: If RAM is full, OS moves inactive pages to disk = "page file" or "swap".

Key idea: "Memory wall" = CPU is much faster than RAM. So we use caching + hierarchy to hide latency.

  • Addressing Concepts: Addressing is how CPU refers to a specific memory location.
  1. Physical Address: Actual location in RAM chips. The hardware memory controller uses this.
  2. Logical / Virtual Address: Address generated by CPU/program. OS + MMU translates it to physical address. This enables memory protection + virtual memory.
  3. Address Space: Range of addresses a program can use. 32-bit = 2^32 = 4GB. 64-bit = 2^64 = huge.

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:

  • Physical Address: The real wire-level address on RAM chip. Only OS + hardware sees this.
  • Virtual Address: What your program sees. int x = 10; might be at virtual address 0x7ffd1234. 
  • MMU + Page Table: Memory Management Unit converts virtual → physical. If page isn’t in RAM, CPU triggers "page fault" and OS loads it from disk.

Why virtual? 3 big benefits:

  • Isolation: '2' programs can both think they own address 0x0000 without conflict.
  • Flexibility: Program doesn’t need to be loaded in contiguous RAM.
  • Security: OS can mark pages as "read-only" or "no-execute" to stop buffer overflows.

Memory-Mapping-Techniques:

  • Segmentation: Divide memory into segments like code, data, stack. Each has base + limit.
  • Paging: Divide memory into fixed-size pages, usually 4KB. Page table maps virtual page → physical frame. Used in Windows, Linux, Android.
  • TLB: Translation Lookaside Buffer = cache for page table entries to speed up virtual → physical translation.

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:

  • Register: ADD R1, R2 → use data in CPU registers. Fastest.
  • Immediate: ADD R1, #5 → constant 5 is in instruction. 
  • Direct: LOAD R1, [2000] → go to memory address 2000.
  • Indirect: LOAD R1, [R2] → R2 holds the address. Used for pointers.
  • Base + Offset: LOAD R1, [R2 + 8] → array access. R2 is array start, 8 is index*size.

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.

Now, you all knew about cache-memories and we see how it works inside the computer-system.

Cache Lines & Cache Misses

Central Processing Unit doesn’t fetch 1 byte from RAM at a time. It fetches in chunks called cache-lines, usually 64 bytes on modern x86/ARM.

How it works:

  • Cache line: 64 consecutive bytes in RAM are loaded together into cache.
  • Spatial locality: If you access arr[0], CPU assumes you’ll soon access arr[1] to arr[15] if "int {integer varaible is 4-bytes} is 4 bytes". So it loads the whole line.
  • Cache hit: Data is already in L1/L2/L3. ∼1-10 cycles. Super fast.
  • Cache miss: Data not in cache. CPU must go to RAM. ∼100-300 cycles. This is a "stall".
  • Bug risk: Access unmapped virtual address = SIGSEGV crash.

Types of cache misses:

  • Compulsory miss: First time accessing that address. Inevitable.
  • Capacity miss: Cache is too small. Your working set > cache size.
  • Conflict miss: Two addresses map to same cache slot. Happens in "set-associative" caches.

Code impact example:

"for(i=0; i<1000; i++)
   for(j=0; j<1000; j++)
      sum += arr[i][j]; // row-major = cache friendly
This is fast because arr[i][j] and arr[i][j+1] are in same cache line.
for(j=0; j<1000; j++)
   for(i=0; i<1000; i++)
      sum += arr[i][j]; // column-major = cache unfriendly
This jumps 4000 bytes each step, causes tons of cache misses."

Virtual Memory: Mobile vs Desktop

Both use paging, but constraints are different:

Desktop / Server:

  • RAM: 16GB-256GB+ common.
  • Swap: Large SSD pagefile. OS can be aggressive swapping.
  • Page-size: 4KB standard, but supports "huge pages" 2MB/1GB for DBs, games.
  • TLB: Big TLB, 1000+ entries. Low page fault rate.
  • Goal: Maximize throughput, don’t kill apps.

Mobile / Android:

  • RAM: 4GB-16GB, but OS + background apps eat a lot.
  • Swap / ZRAM: Often no disk swap. Instead Android uses "zRAM" = compressed RAM, or kills background apps. SSD is slow + has limited write cycles.
  • Low Memory Killer: Android kills apps when RAM is low, instead of swapping. That’s why you lose app state.
  • Page-size: 4KB on ARM, but Android 12+ uses 16KB pages on some devices to reduce TLB pressure.
  • Goal: Battery + responsiveness. Page faults are expensive because flash is slow + uses power.

Real-impact:

  • Desktop: If you leak memory, OS swaps it out. You get slow, but app keeps running.
  • Mobile: If you leak memory, OS kills your app. OutOfMemoryError is common.
  • Android apps use onTrimMemory() to release caches when OS warns low memory.

Putting it together: Why this matters for developers?

  • Cache-aware code: Loop over arrays in row-major order, avoid random pointer chasing. This is 10x-100x faster.
  • Memory alignment: struct padding helps avoid cache line splits.
  • Mobile optimization: Avoid big object allocations. Use SparseArray instead of HashMap. Release bitmaps in onPause().

So memory hierarchy gives speed, addressing gives safety, and understanding both helps you write code that runs fast on both Personal-Computer and Mobile-Device.

Virtual → Physical Address Lookup - Simple version:
Your program asks for 0x7ffd1234 = Virtual Address
CPU splits it: Page Number + Offset 
Page Table = map that page number to a physical RAM frame
Physical Address = Frame Number + Offset → actual RAM chip
If page not in RAM = Page Fault → OS loads it from SSD

A virtual address is a binary, logical address generated by a CPU or program that acts as a reference to a memory location, independent of the computer's actual physical RAM. It provides an abstraction layer, allowing programs to operate within their own isolated "virtual address space".

Key Characteristics and Concepts:

  • Translation: A hardware component, the Memory Management Unit (MMU), translates virtual addresses into physical addresses (the actual location in RAM or on disk) using page tables.
  • Isolation & Security: Every process has its own virtual address space, preventing one program from accessing the memory of another.
  • Virtual Memory: A virtual address can map to either physical RAM or to secondary storage (like an SSD) if the physical memory is full.
  • Abstraction: Programs appear to have a large, contiguous block of memory, even if their data is scattered across physical storage.

This system enables efficient memory management and allows modern operating systems to run applications that require more memory than is physically installed.

A virtual address in hexadecimal is a base-16 memory identifier (e.g., 0x7ffd1234) used by applications, which the Operating System's Memory Management Unit (MMU) translates into physical RAM locations. Hexadecimal is used to compactly represent binary addresses, where 4 bits map directly to one hex digit (0-F).

A virtual address is a binary address assigned by the operating system to a process, representing a memory location in "virtual memory" rather than physical RAM. It provides an abstract, consistent memory view for programs, which is translated to actual physical locations by the memory management unit (MMU).

Key Aspects of Virtual Addresses:

  • Abstraction: Each process has its own dedicated, contiguous virtual address space, making it appear as if it has all memory to itself.
  • Address Translation: Virtual addresses are translated into physical addresses (RAM or disk) using mapping tables (page tables) managed by the OS.
  • Efficiency & Security: This system allows the OS to run more memory than physically available by swapping data to disk and provides security through process isolation.
  • Mapping to Physical Storage: A virtual address might map to high-speed dynamic random-access memory (DRAM) or secondary storage (SSD/HDD).

Virtual vs. Physical Addresses

  • Virtual: Used by the running program/CPU.
  • Physical: The actual location in the computer's memory hardware.

Key Aspects of Virtual Addresses (Hexadecimal)

  • Representation: Represented as 0x followed by alphanumeric characters, making long binary strings (e.g., 32-bit or 64-bit) readable for developers and systems.
  • Structure: A 32-bit address like 0x7F3E5234 is often split into a virtual page number and a page offset.
  • Common Examples:
    • 32-bit System: Ranges from 0x00000000 to 0xFFFFFFFF.
    • 64-bit Process (Windows): Can span up to 0x7FFF'FFFFFFFF.
  • Translation: A virtual address like 0x0012F980 is broken down by the Memory Managemengt Unit (e.g., lower 12 bits 0x980 as offset) and mapped to a physical address (e.g., 0x0012F980).

Why Hexadecimal is Used

  • Human-Friendly: 0x4000 is easier to read and debug than 0100000000000000.
  • Compact: It condenses 16 binary digits (bits) into only 4 hexadecimal characters.
  • Direct Binary Mapping: Since (16 = 2^4), each hexadecimal character represents exactly four bits, allowing fast mental conversion, unlike decimal.

The offset in a virtual address represents the specific location of data within a memory page, typically identified by the lower bits of the virtual address. In a standard {4 KB (2^12) bytes} page system, the offset is 12 bits, corresponding to the last 3 hexadecimal digits (e.g., 0x000 to 0xFFF). It defines the distance from the start of the page, remaining constant during translation.

Key Aspects of Virtual Address Offsets

  • Definition: The offset is the distance, in bytes, from the beginning of a page or segment to the requested data.
  • Hexadecimal Representation: In a 32-bit system with 4 KB pages, the virtual address can be divided into a page number and an offset. The 12-bit offset means that in a hex-address like 0x7F3E5234, the lower 3 digits (234) represent the offset.
  • Common Offset Sizes:
    • 4 KB Pages (2^12 bytes): Uses 12 bits (3 hex digits).
    • 2 MB Pages (2^21 bytes): Uses 21 bits (5 hex digits + 1 bit).
  • Calculation: If you have a virtual address, you can find the offset by masking the lower bits. For example, if the page size is (4096 bytes) i.e. (2^12 bytes), the offset is "virtual_address & 0xFFF".

Example of Hexadecimal Offset

Given a 32-bit virtual address 0x7F3E5234:

  1. Binary: 0111 1111 0011 1110 0101 0010 0011 0100
  2. Split (12-bit offset): 0111 1111 0011 1110 0101 (page #) | 0010 0011 0100 (offset)
  3. Hexadecimal Offset: 0x234 

In this scenario, 0x234 is the position within that specific (4 KB) page.

In computer science, "0x" is a prefix used to indicate that the following number is written in hexadecimal (base-16) notation. It has no numerical value on its own and is purely a visual label for compilers, interpreters, and programmers.

Why "0x" Is Used?

  • Clarifies Base: Distinguishes hexadecimal values from decimal (base-10) numbers. For example, 10 means ten, but 0x10 means sixteen.
  • Origin: Derived from the C programming language and later adopted by almost all modern languages (Python, Java, C++, JavaScript) and computer architecture documentation.
  • The "x" Meaning: The 0 signals the compiler that a numeric literal is starting, and the x stands for "hexadecimal."

Comparative Examples

Representation Value as Decimal Meaning
15 15 Standard decimal fifteen
0x15 21 Hexadecimal (1 *16 + 5)
0x234 564 The offset from the previous example (2 * 256 + 3 * 16 + 4)
0xFFF 4095 The maximum offset in a 4 KB page

In computer memory management, specifically paging, a logical (virtual) address generated by the CPU is divided into a page-number (p) and a page-offset (d). The page number serves as an index into a page table, identifying which specific page of a process is being accessed, while the offset provides the exact displacement or location within that page

Core Analogy: Book Chapter and Sentence

To understand address translation without complex math, think of virtual memory like a book:

  • Virtual Address: A complete directions pointer, like "Chapter 5, Sentence 12."
  • Page Number: The Chapter (e.g., Chapter 5). It tells you which broad block of information you need.
  • Page Offset: The Sentence Number (e.g., Sentence 12). It tells you exactly how far to count from the start of that chapter.

When the computer maps this to physical hardware, the "chapters" (pages) are shuffled and stored 'out-of-order' in physical memory "slots" (frames). However, the text inside the chapter remains exactly the same.

Therefore, the system only needs to translate the chapter number. Once it finds where Chapter 5 is physically stored, it counts down 12 sentences to get your data.

Key Components

  • Page Number (p): Identifies the page in the logical address space. It is used as an index to find the corresponding physical frame in the page table.
  • Page Offset (d): Determines the specific memory location (byte/word) within a page. It is identical to the offset in the physical frame.

Structure of a Logical Address

If the logical address space is 2m and the page size is 2n, the address is divided as follows:

  • High-order (m-n) bits: Page Number (p)
  • Low-order (n) bits: Page Offset (d)

TEXT


|-------------------|-------------------|
|   Page Number (p) |   Page Offset (d) |
|-------------------|-------------------|

Example Calculation

If a system has a 16-bit logical address and a page size of 4 KB [Kilo-{4096 = 212 bytes}-Bytes]:

  1. Offset bits: n = 12 bits (since 4096 = 212).
  2. Page Number bits: 16 - 12 = 4 bits.
  • Logical Address: 0000 0000 0000 0000 (binary) <---> We're dealing with Hexa-Decimal Numbers;
  • Page Number (p): 0000 (First 4 bits)
  • Offset (d): 0000 0000 0000 (Last 12 bits)

Address Translation Process

  1. CPU (Central Processing Unit) generates a virtual address.
  2. MMU (Memory Management Unit) splits the address into 'p' and 'd'.
  3. 'p' is used to look up the base address of the frame in the Page Table.
  4. The frame number (physical address base) is combined with the original offset 'd' to access the physical memory.

Step-by-Step Translation Mechanism

When a program requests data at a specific logical address, the Hardware Memory Management Unit (MMU) performs the following physical translation:

  Logical Address Generated by CPU
+-------------------+-------------------+

|  Page Number (p)  |  Page Offset (d)  |
+---------+---------+---------+---------+

          |                   |
          v                   |
   [ PAGE TABLE ]             |

   |  p -> Frame# |           |
   +------+-------+           |

          |                   |
          v                   v
+-------------------+-------------------+

|  Frame Number (f) |  Page Offset (d)  |
+-------------------+-------------------+
  Physical Address Location in RAM

  1. Extraction: The MMU splits the binary address into the high-order bits (Page Number) and low-order bits (Page Offset).
  2. Lookup: The MMU treats the Page Number as an index array pointer. It searches the Page Table to find which physical RAM Frame 'f' holds that page.
  3. Assembly: The MMU grabs the physical Frame Number and slaps the original, unaltered Page Offset directly onto the end of it.
  4. Access: This newly formed hardware binary code (Physical Address) directly retrieves the byte from the physical RAM chips.

Why the Offset Never Changes

A common point of confusion is why the offset remains identical in both virtual and physical addresses.

  • Fixed Dimensions: Pages (virtual memory) and Frames (physical memory) are manufactured to be exactly the same size (commonly 4 Kilobytes).
  • Relative Position: Because the sizes match perfectly, the slot position of a byte inside a virtual page is identical to its slot position inside a physical RAM frame. If a variable sits at byte number 400 from the top of Page 2, it will still sit at byte number 400 from the top of Frame 85 once loaded.

Bit-Level Breakdown Example

To see how the hardware splits these numbers instantly, look at a 16-bit system using small 256-byte pages:

  • Page Size: 256 bytes = 28 bytes. This requires 8 bits for the Offset 'd'.
  • Page Number Bits: 16 totalBits - 8 offseBits = 8 bits for the Page Number 'p'.

If a CPU requests binary address 00000100 00001111 (Decimal 1039):

  • Page Number (p): 00000100 (The first 8 bits = Page 4)
  • Page Offset (d): 00001111 (The last 8 bits = Offset 15)

The operating system checks the Page Table and finds that Page 4 is currently residing in physical RAM Frame 30 (00011110 in binary).

The MMU replaces the page bits with the frame bits, keeping the offset untouched:

  • Physical Address: 00011110 00001111 (Frame 30, Offset 15).

In computer memory management, physical frames (or page frames) are fixed-size, contiguous blocks of physical hardware memory (RAM).

While virtual memory is divided into pages, the physical RAM chip is divided into frames. For the system to function correctly, a physical frame must be the exact same size as a virtual page.

The Dynamic Mapping Relationship

Physical frames act as physical slots or containers that hold virtual pages.

  • The Slot Analogy: Think of physical frames as parking spots in a concrete lot, and virtual pages as cars. The cars (pages) can move around, be parked in different spots, or be moved out to a storage depot (hard drive), but the parking spots (frames) remain permanently fixed in the concrete lot (RAM).
  • Non-Contiguous Allocation: A program might expect its virtual pages (Page 0, Page 1, Page 2) to be in sequential order. However, the operating system can scatter these pages across completely random physical frames (e.g., Frame 94, Frame 12, Frame 203) depending on what slots are empty in the RAM [1]. The user/program never notices this chaos because the translation happens instantly in the hardware.

Key Attributes of Physical Frames

  • Frame Number (f): Also known as the Physical Frame Number (PFN), this is the unique hardware address index of the frame in Random Access Memory.
  • Fixed Size: Frame sizes are determined by the processor architecture and operating system, typically 4 KB for standard systems, though modern systems also support "Huge-Pages" of 2 MB or 1 GB.
  • Identity Mapping: The internal byte layout inside a frame is identical to a page. Byte 10 of a virtual page will land exactly on Byte 10 of whatever physical frame it is loaded into.

Frame Allocation and the Page Table

The Page Table is the master map kept by the operating system to keep track of which virtual page is currently sitting inside which physical frame.

Virtual Page Number Valid/Invalid Bit Physical Frame Number (PFN)
Page 0 1 (In RAM) Frame 5
Page 1 0 (On Disk / Swap) — (None)
Page 2 1 (In RAM) Frame 12
  • Valid Bit (1): The page is currently residing in a physical frame in RAM. The memory management unit (MMU) reads the frame number and fetches the data instantly.
  • Invalid Bit (0): The page is not in any physical frame. It is currently saved on the hard drive (swap space). This triggers a Page Fault, forcing the OS to find an empty physical frame, load the data from the disk into that frame, and update the table.

Frame Management and Thrashing

Because physical RAM is limited, a computer usually has far fewer physical frames than the total number of virtual pages requested by running programs.

  1. Page Replacement: When all physical frames are completely full and a program requests a new page, the OS must use a page replacement algorithm (like Least Recently Used) to select a physical frame, kick its current contents out to the hard drive, and clear the frame for the new data.
  2. Thrashing: If a system runs critically low on physical frames, it spends more time swapping data back and forth between frames and the hard drive than actually executing code, causing the computer to freeze or lag heavily.

In computer memory management, a page table is the master data structure maintained by the operating system to map a process's virtual page numbers to physical frame numbers in RAM.

Every running process has its own dedicated page table. Without it, the processor's Memory Management Unit (MMU) cannot translate virtual memory addresses into physical hardware addresses to retrieve data.

The Translation Lookup Process

When a program requests a specific memory address, the hardware intercepts the request and reads the page table: [1]

Virtual Address: [ Page Number (p) | Offset (d) ]

                         |
                         v
                +------------------+
  Page Table -> | Index p: Frame f | 
                +------------------+

                         |
                         v
Physical Address: [ Frame Number (f) | Offset (d) ]

  1. Indexing: The system uses the Page Number (p) directly as an array index to look up that exact row in the page table.
  2. Retrieval: The table returns the corresponding Physical Frame Number (f) where that page is stored.
  3. Assembly: The system replaces 'p' with 'f' and appends the original Offset (d) to target the exact byte in hardware RAM.

Inside a Page Table Entry (PTE)

A page table does not just store frame numbers. Each row, known as a Page Table Entry (PTE), contains several critical control bits used by the hardware and operating system:

  • Frame Number: The actual binary pointer to the physical memory slot in RAM.
  • Valid / Invalid Bit: Specifies if the page is currently loaded in RAM '1' or sitting out on the storage drive '0'.
  • Dirty (Modify) Bit: Set to '1' if the CPU has written data to this page. This tells the OS it must save changes to the disk before clearing this frame.
  • Referenced (Accessed) Bit: Tracks whether the page was recently read or written to, helping page replacement algorithms decide what to swap out.
  • Protection Bits: Dictates the access permissions for that block of memory (e.g., Read-Only, Read-Write, or Execute-Only).

Modern Page Table Architectures

Storing a massive, flat array for every single process consumes too much physical RAM. Modern operating systems use optimized structural layouts to keep page tables compact:

Hierarchical (Hierarchical / Multi-level) Page Tables: This method breaks the virtual address into multiple page numbers, splitting the master table into a tree structure of smaller tables. If a large block of memory is unused, its sub-tables do not need to exist in RAM, saving massive amounts of space.

Inverted Page Tables: Instead of creating a table for every process's virtual space, the system maintains one single table for the entire physical RAM. It contains exactly one entry for each physical frame, tracking which process and page currently occupy that hardware slot.

Translation Lookaside Buffer (TLB) Cache: Because checking a page-table in RAM adds an extra hardware memory access delay, CPUs use a specialized, ultra-fast hardware cache called the TLB. The TLB stores the most recently used page-to-frame translations, allowing the MMU to skip the page table lookup entirely for the vast majority of memory requests.

To see exactly how Page Numbers, Page Offsets, Physical Frames, and Page Tables work together in real life, let us walk through a concrete example step-by-step.

The Scenario

Imagine a 16-bit computer where a program wants to execute a command located at Logical Address 92.

  • System Page Size: 32 bytes (2bytes). This means Offset (d) requires 5 bits.
  • Total Address Space: 16 bits. This leaves 11 bits for the Page Number (p).

Step 1: The CPU Splits the Address

The program asks for Address 92. In binary, 92 is written as a 16-bit number: 00000000 01011100.
The hardware immediately splits this binary string into two parts based on our 5-bit offset rule:

Page-Number(p) Page-Offset(d)
00000000111(First '11' Bits) 11100(Last '5' Bits)
Decimal-2 Decimal-28

The computer now knows it needs Page 2, and specifically Byte number 28 inside that page.

Step 2: The Memory Management Unit Checks the Page Table

The CPU cannot go directly to the physical RAM chip yet because RAM does not know what "Page 2" means. The hardware looks at the running process's Page Table to see where Page 2 is currently sitting in the actual physical RAM slots (Frames):

Index (Page Number) Valid Bit Physical Frame Number
Page 0 1 Frame 5
Page 1 1 Frame 9
Page 2 1 Frame 7
Page 3 0 (On Hard Drive)

The hardware looks up index 2, sees a Valid Bit of 1 (it is loaded in RAM), and pulls out the target: Physical Frame 7.

Step 3: Constructing the Physical Address

To pinpoint the exact location on the hardware RAM chip, the Memory Management Unit combines the Frame Number (7) with the unaltered Offset (28).

  • Frame 7 in binary is 00000000111.
  • Offset 28 in binary remains 11100.

Physical Address = [Frame 7 | Frame 28] --> 0000000011111100

Converting 0000000011111100 back to a regular number gives Physical RAM Address 252.

Final Summary of the Journey

   [ Program asks for Address 92 ]
      |
      v
     [ Page 2, Offset 28 ]
      |
       (Look up in Page Table)
      |
      v
      [ Frame 7, Offset 28 ]
      |
      v
      [ Hardware fetches RAM Address 252 ]

Because Page 7 starts at byte 224 (7*32), counting 28 bytes into Frame 7 lands exactly on physical address 252. The translation is complete, and the data is fed back to the CPU.

What Happens When Things Go Wrong: A Page Fault

Let’s use the exact same scenario from before, but this time, the CPU asks for Logical Address 100.

When the computer splits the address, it discovers this data is located on Page 3, Offset 4. It goes to check the Page Table:

Index (Page Number) Valid Bit Physical Frame Number
... ... ...
Page 3 0 — (Not in RAM)

Because the Valid Bit is 0, the data is not in the physical RAM frames; it is sitting on the slow storage drive (SSD/HDD). This triggers a hardware interruption called a Page Fault.

Step-by-Step Page Fault Resolution

[ CPU Requests Page 3 ] -> [ Valid Bit is 0 ] -> [ PAGE FAULT! ]

                                                       |
  +----------------------------------------------------+
  v
1. OS Traps the error and pauses the program.
2. OS finds an empty physical frame in RAM (e.g., Frame 12).
3. OS reads Page 3 from the storage drive and copies it into Frame 12.
4. OS updates Page Table: Page 3 -> Frame 12 (Valid Bit changes to 1).
5. OS restarts the program instruction.
  |
  v
[ Data successfully fetched from Frame 12! ]

  1. The Trap: The processor realizes it cannot fetch the data, pauses your running program, and hands control over to the Operating System.
  2. The Search: The OS looks at the physical RAM to find an empty, available Physical Frame. Let's say it finds an empty slot at Frame 12.
  3. The Disk Read: The OS schedules a hardware read to fetch the missing Page 3 from the storage drive and copies it directly into Frame 12.
  4. The Update: The OS updates the Page Table row for Page 3. It changes the entry to point to Frame 12 and flips the Valid Bit from 0 to 1.
  5. The Restart: The OS yields control back to your program and retries the exact same instruction. This time, the lookup succeeds instantly.

Grand Summary: Bringing It All Together

To wrap up everything we have discussed, here is the complete reference cheat sheet of how memory management operates:

  • Virtual Memory allows your computer to pretend it has much more RAM than it actually does by utilizing storage space.
  • Pages are the equally sized virtual blocks your programs look at, while Frames are the actual physical slots on your hardware RAM chip.
  • The Page Number specifies which block of data you need, and the Page Offset specifies the exact byte position within that block.
  • The Page Table acts as the master map, translating virtual Page Numbers into physical Frame Numbers.
  • The Page Offset never changes during translation because pages and frames are identical in size.
  • A Page Fault occurs when a requested page is missing from the physical frames, forcing the OS to safely retrieve it from storage.