Interrupts are mechanisms used to handle asynchronous events. They can be software-initiated (via the int instruction) or hardware-generated.
Software interrupts are invoked using the int instruction followed by an interrupt vector number. For example:
int 0x80 ; Linux system call interrupt
int 0x10 ; BIOS video services
This example demonstrates using int 0x80 for a Linux system call to write a string to the console.
section .data
message db 'Hello, Interrupt!', 0xA
msg_len equ $ - message
section .text
global _start
_start:
mov eax, 4 ; System call: write
mov ebx, 1 ; File descriptor: stdout
mov ecx, message ; Address of the message
mov edx, msg_len ; Length of the message
int 0x80 ; Trigger interrupt
mov eax, 1 ; System call: exit
xor ebx, ebx ; Exit code: 0
int 0x80 ; Trigger interrupt
This example changes the text color using BIOS interrupt int 0x10.
section .text
org 0x7C00 ; Code starts at boot sector
_start:
mov ah, 0x0E ; Teletype output
mov al, 'A' ; Character to display
int 0x10 ; Trigger BIOS interrupt
hlt ; Halt the CPU
Hardware interrupts are generated by devices (e.g., keyboards, timers). The CPU uses an Interrupt Descriptor Table (IDT) to handle them.
Example: A keyboard interrupt typically uses vector 0x09 (on older systems). Handling these interrupts requires writing an interrupt service routine (ISR).