ตัวควบคุมอินเทอร์รัพท์ที่ตั้งโปรแกรมได้ (PIC) และ APIC
ค้นพบว่าคำขออินเทอร์รัพท์จากฮาร์ดแวร์ส่งไปยัง CPU ผ่าน 8259 PIC และ APIC รุ่นใหม่อย่างไร รวมถึงการแมป IRQ ใหม่ การปิดกั้น และการส่งสัญญาณสิ้นสุดอินเทอร์รัพท์
ตัวควบคุมอินเทอร์รัพท์ที่ตั้งโปรแกรมได้ (PIC) และ APIC เป็นบทเรียน 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 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why a Controller Is Needed
The CPU has only a single hardware interrupt pin (INTR), yet a PC has dozens of devices that need attention. A dedicated chip multiplexes these IRQ lines onto that one pin and tells the CPU which device fired.
That chip is the Programmable Interrupt Controller (PIC).
The Legacy 8259 PIC
The classic PC uses two cascaded 8259A chips, giving 15 usable IRQ lines (IRQ0-IRQ15). The master handles IRQ0-7, the slave IRQ8-15 connected through IRQ2.
- IRQ0 = system timer
- IRQ1 = keyboard
- IRQ14/15 = ATA disks
I/O Ports of the PIC
Each PIC is programmed through two I/O ports:
- Master: command
0x20, data0x21 - Slave: command
0xA0, data0xA1
You send Initialization Command Words (ICWs) and Operation Command Words (OCWs) to these ports.
IRQ Remapping
On boot the PIC maps IRQ0-7 onto interrupt vectors 8-15 — which collide with CPU exceptions like the double fault (vector 8). Kernels remap the PIC, typically to vectors 0x20-0x2F, to avoid this clash.
Remapping in Assembly
Remapping is an ICW sequence: start init, set the new vector offset, define cascade wiring, set 8086 mode.
mov al, 0x11 ; ICW1: begin init, expect ICW4
out 0x20, al ; master command
out 0xA0, al ; slave command
mov al, 0x20 ; ICW2: master vector offset 0x20
out 0x21, al
mov al, 0x28 ; ICW2: slave vector offset 0x28
out 0xA1, alMasking Interrupts
The Interrupt Mask Register (IMR) lets you disable individual IRQ lines. Writing a 1 to a bit masks (ignores) that line; 0 enables it.
in al, 0x21 ; read current master mask
or al, 0x02 ; set bit 1 -> mask IRQ1 (keyboard)
out 0x21, al ; write it backEnd of Interrupt (EOI)
After servicing an IRQ the handler must send an End Of Interrupt command, or the PIC will not deliver further interrupts on that line.
For IRQ8-15 you must EOI both the slave and the master.
mov al, 0x20 ; non-specific EOI
out 0x20, al ; tell master we are doneLimitations of the PIC
The 8259 design is single-CPU only and supports just 15 lines. It cannot route interrupts to different cores, which makes it useless for SMP systems.
This is why modern hardware moved to the APIC.
The APIC Architecture
The Advanced Programmable Interrupt Controller has two parts:
- Local APIC — one per CPU core, handles the timer and inter-processor interrupts (IPIs)
- I/O APIC — routes external device IRQs to any core
Local APIC and IPIs
The Local APIC is memory-mapped (default base 0xFEE00000). It provides a per-core timer and lets one core signal another via an IPI through the Interrupt Command Register — essential for waking up cores during boot.
Disabling the PIC for APIC
To use the APIC you mask all PIC lines by writing 0xFF to both data ports, then enable the Local APIC via the spurious interrupt vector register. Modern OSes prefer the APIC for multicore scaling.
mov al, 0xFF
out 0x21, al ; mask all master IRQs
out 0xA1, al ; mask all slave IRQsQuick Check
Test your understanding of the PIC.
Recap
You learned how device IRQs reach the CPU:
- The 8259 PIC multiplexes 15 IRQ lines onto one CPU pin
- IRQs are remapped to vectors 0x20-0x2F to avoid exception clashes
- The IMR masks lines; an EOI ends each interrupt
- The APIC replaces the PIC for multicore routing and IPIs
เรียนรู้ Assembly ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “ตัวควบคุมอินเทอร์รัพท์ที่ตั้งโปรแกรมได้ (PIC) และ APIC” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ตัวควบคุมอินเทอร์รัพท์ที่ตั้งโปรแกรมได้ (PIC) และ APIC” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Assembly Language & x86 Low-Level Systems Programming ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Assembly Language & x86 Low-Level Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ตัวควบคุมอินเทอร์รัพท์ที่ตั้งโปรแกรมได้ (PIC) และ APIC”
ค้นพบว่าคำขออินเทอร์รัพท์จากฮาร์ดแวร์ส่งไปยัง CPU ผ่าน 8259 PIC และ APIC รุ่นใหม่อย่างไร รวมถึงการแมป IRQ ใหม่ การปิดกั้น และการส่งสัญญาณสิ้นสุดอินเทอร์รัพท์ คุณปฏิบัติ 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 บทเรียน
บทเรียน “ตัวควบคุมอินเทอร์รัพท์ที่ตั้งโปรแกรมได้ (PIC) และ APIC” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Assembly Language & x86 Low-Level Systems Programming นี้ได้ไหม
ได้ บทเรียน Assembly Language & x86 Low-Level Systems Programming ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำความเข้าใจอินเทอร์รัปต์และกับดัก
- ตารางตัวบอกอินเทอร์รัปต์ (IDT)
- ตัวจัดการข้อยกเว้นแบบกำหนดเอง
- ตัวควบคุมอินเทอร์รัพท์ที่ตั้งโปรแกรมได้ (PIC) และ APIC