0Pricing
Assembly Language & x86 Low-Level Systems Programming · 课时

虚拟内存概念

从底层视角理解虚拟内存、内存分段和分页的原理。

虚拟内存概念 是 CoddyKit 上的免费 Assembly Language & x86 Low-Level Systems Programming 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Assembly Language & x86 Low-Level Systems Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Assembly Language & x86 Low-Level Systems Programming 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Virtual Memory's Role

Welcome to the world of Virtual Memory! This concept is fundamental to how modern operating systems manage memory, enabling powerful features like multitasking and memory protection.

It allows programs to use a continuous, private memory space, even if the physical RAM is fragmented or smaller than the program's needs.

Limits of Physical Memory

In early computing, programs directly accessed physical RAM. This led to several challenges:

  • Limited RAM: Programs had to fit entirely into physical memory.
  • No Isolation: One program could overwrite another's memory, causing crashes.
  • Relocation Issues: Programs needed to be loaded at specific physical addresses, complicating multitasking.

Virtual Memory was designed to solve these problems by introducing an abstraction layer.

Addresses: Virtual & Physical

The core idea is a distinction between two types of addresses:

  • Virtual Address: The address seen and used by the CPU and your program. Each program gets its own unique, isolated virtual address space.
  • Physical Address: The actual address in the computer's RAM chips. This is what the hardware truly understands and where data is physically stored.

Think of it like a house number (virtual) versus its GPS coordinates (physical).

MMU: The Address Translator

The magic behind this translation is performed by a dedicated hardware component inside the CPU called the Memory Management Unit (MMU).

When the CPU requests data from a virtual address, the MMU intercepts this request and instantly translates it into the corresponding physical address before accessing the RAM. This process is transparent to the running program.

Paging for Flexibility

One of the most common ways the MMU handles translation is through paging. In paging, both virtual and physical memory are divided into fixed-size blocks:

  • Pages: Blocks of virtual memory.
  • Frames (or Page Frames): Blocks of physical memory.

These blocks are typically 4KB (4096 bytes) in size, though larger sizes exist. Paging allows non-contiguous physical memory to appear contiguous to a program.

Page Tables: The Lookup

How does the MMU know which physical frame corresponds to which virtual page? It uses Page Tables.

Page tables are special data structures stored in physical RAM. They contain entries that map virtual page numbers to their corresponding physical frame numbers. The operating system creates and manages these tables for each running process.

Multi-Level Paging Simplified

For modern 64-bit systems with huge virtual address spaces, a single, flat page table would consume an impractical amount of memory. To save space, x86-64 uses multi-level paging.

This means there's a hierarchy of page tables. A top-level table (like PML4) points to lower-level tables, and so on, until the final page table points to a physical frame. This structure ensures that only necessary parts of the address space have page tables allocated.

CR3: The Root Pointer

The CPU needs to know where the top-level page table (the PML4 in 64-bit mode) is located in physical memory. This critical physical base address is stored in the CR3 register (Control Register 3).

Each process typically has its own set of page tables. When the operating system switches between processes, it updates the value of CR3 to point to the new process's page tables, effectively switching virtual address spaces.

An Assembly Memory Access

When your assembly program accesses a memory location, it's always using a virtual address. The MMU handles the translation transparently, so your code doesn't need to know the physical address.

Try running this simple assembly program. The my_var label refers to a virtual address, which the MMU translates to a physical address before the data is fetched.

section .data
    my_var db 42 ; A byte variable at a virtual address

section .text
    global _start

_start:
    ; Load the value from my_var into AL register
    mov al, byte [my_var] 

    ; At this point, AL contains the value 42.
    ; The address of 'my_var' was a virtual one,
    ; translated by the MMU to a physical address.

    ; Exit syscall (Linux specific)
    mov eax, 60
    xor edi, edi
    syscall

Quick Check: Memory Concepts

Which of the following components is primarily responsible for translating virtual addresses to physical addresses in an x86 system?

Recap: Virtual Memory Essentials

In this lesson, we explored the crucial concept of Virtual Memory in x86 systems. We learned:

  • Virtual memory provides programs with a private, large address space, abstracting physical RAM.
  • The MMU is the hardware unit that translates virtual addresses to physical addresses.
  • Paging divides memory into fixed-size blocks (pages/frames) and uses page tables for mapping.
  • Multi-level paging efficiently manages large address spaces in modern systems.
  • The CR3 register points to the base of the top-level page table, changing with process context.

Understanding virtual memory is key to grasping how operating systems manage processes and memory protection at a low level.

常见问题解答

「虚拟内存概念」课时是免费的吗?

是的 — 「虚拟内存概念」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Assembly Language & x86 Low-Level Systems Programming 课程的其余内容,请升级到 CoddyKit PRO。 Assembly Language & x86 Low-Level Systems Programming 课程共包含 4 节课。

「虚拟内存概念」这节课中我会学到什么?

从底层视角理解虚拟内存、内存分段和分页的原理。 你通过在浏览器中直接运行的动手代码来练习 Assembly Language & x86 Low-Level Systems Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Assembly Language & x86 Low-Level Systems Programming 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Assembly Language & x86 Low-Level Systems Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「虚拟内存概念」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Assembly Language & x86 Low-Level Systems Programming 课中编写并运行代码吗?

能。每节 Assembly Language & x86 Low-Level Systems Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 虚拟内存概念
  2. Linux 系统调用(syscalls)
  3. Windows API 交互
  4. 动态内存:汇编中的堆分配
← 返回 Assembly Language & x86 Low-Level Systems Programming