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

可编程中断控制器(PIC)与 APIC

了解硬件中断请求如何通过 8259 PIC 和现代 APIC 到达 CPU,包括 IRQ 重映射、屏蔽和中断结束信号。

可编程中断控制器(PIC)与 APIC 是 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 节课。

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

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, data 0x21
  • Slave: command 0xA0, data 0xA1

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, al

Masking 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 back

End 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 done

Limitations 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 IRQs

Quick 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

常见问题解答

「可编程中断控制器(PIC)与 APIC」课时是免费的吗?

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

「可编程中断控制器(PIC)与 APIC」这节课中我会学到什么?

了解硬件中断请求如何通过 8259 PIC 和现代 APIC 到达 CPU,包括 IRQ 重映射、屏蔽和中断结束信号。 你通过在浏览器中直接运行的动手代码来练习 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 节。

「可编程中断控制器(PIC)与 APIC」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. 理解中断与陷阱
  2. 中断描述符表(IDT)
  3. 自定义异常处理程序
  4. 可编程中断控制器(PIC)与 APIC
← 返回 Assembly Language & x86 Low-Level Systems Programming