分段与全局描述符表(GDT)
学习 x86 分段如何与分页协同工作:了解段选择子、描述符和 GDT,以及平坦内存模型如何为保护模式奠定基础。
分段与全局描述符表(GDT) 是 CoddyKit 上的免费 Assembly Language & x86 Low-Level Systems Programming 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Assembly Language & x86 Low-Level Systems Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Assembly Language & x86 Low-Level Systems Programming 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What Is Segmentation?
Segmentation divides memory into variable-sized regions called segments, each with a base address, limit, and access rights. It predates paging and is the first translation step in x86 address generation.
Real Mode Segments
In 16-bit real mode, a physical address is computed as segment * 16 + offset. This gave access to 1 MB using 16-bit registers but offered no protection at all.
Protected Mode Selectors
In protected mode a segment register no longer holds a base directly. It holds a selector — an index into a descriptor table plus a privilege level and table indicator bit.
The Segment Descriptor
Each descriptor is 8 bytes and encodes:
- Base (32-bit start address)
- Limit (size of the segment)
- Type (code, data, system)
- DPL (descriptor privilege level 0-3)
- Flags (granularity, default size)
The Global Descriptor Table
The GDT is an array of these descriptors in memory. The CPU finds it through the GDTR register, which holds the table base and limit. Entry 0 is always the null descriptor.
Loading the GDT
You point the CPU at your GDT with the lgdt instruction, which reads a 6-byte pseudo-descriptor (limit + base).
gdt_descriptor:
dw gdt_end - gdt_start - 1 ; limit
dd gdt_start ; base
lgdt [gdt_descriptor] ; load GDTRDefining a Flat Code Segment
Modern OSes use a flat model: base 0 and a 4 GB limit, so segmentation becomes a no-op and paging does the real work. Here is a typical code descriptor.
gdt_code:
dw 0xFFFF ; limit low
dw 0x0000 ; base low
db 0x00 ; base mid
db 10011010b ; present, ring 0, code, readable
db 11001111b ; granularity 4K, 32-bit, limit high
db 0x00 ; base highActivating Protected Mode
You enter protected mode by setting bit 0 (PE) of control register CR0, then performing a far jump to flush the prefetch queue and load CS with a protected-mode selector.
mov eax, cr0
or eax, 1 ; set PE bit
mov cr0, eax
jmp 0x08:pmode ; far jump, 0x08 = code selectorSelectors in Practice
A selector like 0x08 breaks down as: index 1 (byte offset 8 / 8), table indicator 0 (GDT), requested privilege level 0. After the jump, data segment registers are loaded with the data selector (often 0x10).
Segmentation vs Paging Today
In long (64-bit) mode segmentation is largely disabled: base and limit are ignored for most segments. Protection and translation are handled by paging. Segmentation survives mainly for FS/GS base pointers used for thread-local storage.
Why It Still Matters
Even though the flat model neutralizes segmentation, every protected-mode and long-mode OS must set up a valid GDT to boot. Understanding selectors and descriptors is essential for kernel bring-up and exception handling.
Quick Check
Test your segmentation knowledge.
Recap
You learned x86 segmentation:
- A selector indexes a descriptor in the GDT
- Descriptors hold base, limit, type, and privilege level
lgdtloads the table; setting CR0.PE plus a far jump enters protected mode- The flat model neutralizes segmentation so paging does the work
常见问题解答
「分段与全局描述符表(GDT)」课时是免费的吗?
是的 — 「分段与全局描述符表(GDT)」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Assembly Language & x86 Low-Level Systems Programming 课程的其余内容,请升级到 CoddyKit PRO。 Assembly Language & x86 Low-Level Systems Programming 课程共包含 4 节课。
「分段与全局描述符表(GDT)」这节课中我会学到什么?
学习 x86 分段如何与分页协同工作:了解段选择子、描述符和 GDT,以及平坦内存模型如何为保护模式奠定基础。 你通过在浏览器中直接运行的动手代码来练习 Assembly Language & x86 Low-Level Systems Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Assembly Language & x86 Low-Level Systems Programming 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Assembly Language & x86 Low-Level Systems Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「分段与全局描述符表(GDT)」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Assembly Language & x86 Low-Level Systems Programming 课中编写并运行代码吗?
能。每节 Assembly Language & x86 Low-Level Systems Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 分页与内存管理单元(MMU)
- 保护环与权限
- 虚拟机监控程序与虚拟化基础
- 分段与全局描述符表(GDT)