0Pricing
Assembly Language & x86 Low-Level Systems Programming · บทเรียน

ทำความเข้าใจอินเทอร์รัปต์และกับดัก

แยกความแตกต่างระหว่างอินเทอร์รัปต์จากฮาร์ดแวร์ อินเทอร์รัปต์จากซอฟต์แวร์ และข้อยกเว้น พร้อมทำความเข้าใจบทบาทของสิ่งเหล่านี้ในการทำงานของระบบ

ทำความเข้าใจอินเทอร์รัปต์และกับดัก เป็นบทเรียน Assembly Language & x86 Low-Level Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Assembly Language & x86 Low-Level Systems Programming และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Assembly Language & x86 Low-Level Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Events That Stop the CPU

What are interrupts and traps? Think of them as urgent signals that tell your computer's CPU to pause what it's doing and pay attention to something more important.

These signals are crucial for how operating systems manage tasks, respond to hardware, and handle errors. Without them, your computer couldn't multitask or even react when you press a key!

CPU's Normal Flow & Interrupts

Normally, the CPU executes instructions one after another in a steady flow. But what if the keyboard is pressed, or an error occurs?

Interrupts and traps are mechanisms that allow the CPU to temporarily halt its current task, handle the urgent event, and then resume its original work.

Hardware Interrupts: External Signals

Hardware interrupts are external signals, generated by hardware devices, that demand the CPU's attention.

  • They are asynchronous, meaning they can happen at any time, unrelated to the CPU's current instruction.
  • Think of them as a doorbell ringing – the CPU stops what it's doing to answer.

Common Hardware Interrupts

These are everywhere! Here are a few common examples:

  • Keyboard Press: When you type, the keyboard controller sends an interrupt.
  • Mouse Movement: Moving your mouse triggers interrupts.
  • Timer Interrupt: A special chip generates interrupts at regular intervals, allowing the OS to schedule tasks.
  • Disk I/O: When data is ready from your hard drive, it signals the CPU.

Software Interrupts: Programmed Calls

Software interrupts are intentionally triggered by a running program using a special instruction (like INT in x86 assembly).

  • They are synchronous, happening exactly when the instruction is executed.
  • Programs use them to request services from the operating system, like reading a file or printing to the screen.

Triggering a Software Interrupt

In x86 assembly, the INT instruction generates a software interrupt. Here, int 0x80 is used on Linux to call a system service (syscall) to exit the program.

Try running this simple assembly program:

section .data
  msg db "Hello from Assembly!", 0xA
  len equ $ - msg

section .text
global _start

_start:
  ; sys_write (syscall 4)
  mov eax, 4       ; syscall number for sys_write
  mov ebx, 1       ; file descriptor (stdout)
  mov ecx, msg     ; address of string to write
  mov edx, len     ; length of string
  int 0x80         ; invoke kernel

  ; sys_exit (syscall 1)
  mov eax, 1       ; syscall number for sys_exit
  mov ebx, 0       ; exit code 0
  int 0x80         ; invoke kernel

Exceptions: CPU's Internal Alarms

Exceptions are another type of synchronous event, but they are triggered internally by the CPU itself when it detects an error or an unusual condition during instruction execution.

  • They indicate that something went wrong with the current instruction or its data.
  • Unlike hardware interrupts, they are directly tied to the execution flow.

Faults, Traps, and Aborts

Exceptions are categorized by how they affect program execution:

  • Faults: An error that can often be corrected, allowing the program to restart the offending instruction (e.g., a page fault when trying to access memory not currently in RAM).
  • Traps: An exception that is reported immediately after the instruction causing it has executed, allowing the program to continue (e.g., a debugger breakpoint).
  • Aborts: Severe, unrecoverable errors that usually terminate the program (e.g., a critical hardware failure).

Real-World Exception Triggers

You've probably encountered these, even if you didn't know the name:

  • Divide-by-Zero: Trying to divide a number by zero.
  • Invalid Opcode: The CPU encounters an instruction it doesn't recognize.
  • Page Fault: A program tries to access a memory address that isn't mapped or available.
  • General Protection Fault (GPF): A program tries to access memory it doesn't have permission for, or performs a privileged operation incorrectly.

Key Differences Summarized

Let's clarify the main distinctions:

  • Source: Hardware interrupts are external, software interrupts are program-initiated, exceptions are CPU-internal.
  • Timing: Hardware interrupts are asynchronous; software interrupts and exceptions are synchronous.
  • Purpose: Interrupts handle events (I/O, timers, OS calls); exceptions handle errors or special conditions.
  • Correctability: Some exceptions (faults) are correctable, others (aborts) are not.

Distinguishing Event Types

Which of the following scenarios describes a hardware interrupt?

Recap: CPU Event Handlers

In this lesson, we explored the critical mechanisms that allow a CPU to react to events and errors:

  • Hardware Interrupts: External signals from devices (keyboard, timer).
  • Software Interrupts: Program-initiated requests for OS services (e.g., INT instruction).
  • Exceptions: Internal CPU errors or unusual conditions (e.g., divide-by-zero, page fault).

Understanding these helps you grasp how operating systems manage tasks and maintain stability at a low level.

คำถามที่พบบ่อย

บทเรียน “ทำความเข้าใจอินเทอร์รัปต์และกับดัก” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ทำความเข้าใจอินเทอร์รัปต์และกับดัก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส 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 ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Assembly Language & x86 Low-Level Systems Programming หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Assembly Language & x86 Low-Level Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “ทำความเข้าใจอินเทอร์รัปต์และกับดัก” ใช้เวลานานแค่ไหน

บทเรียน 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