การแบ่งเซกเมนต์และตารางตัวบอกส่วนกลาง (GDT)
เรียนรู้การทำงานของการแบ่งเซกเมนต์ใน x86 ควบคู่กับเพจจิง ทั้งตัวเลือกเซกเมนต์ ตัวบอกส่วน GDT และวิธีที่แบบจำลองหน่วยความจำแบบแบนปูทางสู่โหมดป้องกัน
การแบ่งเซกเมนต์และตารางตัวบอกส่วนกลาง (GDT) เป็นบทเรียน Assembly Language & x86 Low-Level Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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
เรียนรู้ Assembly ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การแบ่งเซกเมนต์และตารางตัวบอกส่วนกลาง (GDT)” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การแบ่งเซกเมนต์และตารางตัวบอกส่วนกลาง (GDT)” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส 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 ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Assembly Language & x86 Low-Level Systems Programming หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Assembly Language & x86 Low-Level Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การแบ่งเซกเมนต์และตารางตัวบอกส่วนกลาง (GDT)” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Assembly Language & x86 Low-Level Systems Programming นี้ได้ไหม
ได้ บทเรียน Assembly Language & x86 Low-Level Systems Programming ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การจัดหน้าและหน่วยจัดการหน่วยความจำ (MMU)
- วงแหวนการป้องกันและสิทธิ์การใช้งาน
- พื้นฐานไฮเปอร์ไวเซอร์และการทำเสมือน
- การแบ่งเซกเมนต์และตารางตัวบอกส่วนกลาง (GDT)