Interrupts and system calls allow programs to interact with the operating system or hardware. In assembly, you can invoke these by using the int
instruction for software interrupts or using dedicated system call mechanisms.
An interrupt is a mechanism that allows a process or program to temporarily halt execution and pass control to a special function called an interrupt handler. Interrupts can be triggered by hardware or software.
There are two types of interrupts:
int
InstructionTo invoke a software interrupt, the int
instruction is used:
int 0x80 ; Trigger interrupt 0x80, used for system calls
System calls in Linux are invoked via interrupt 0x80
. Each system call is identified by a number, and parameters are passed through registers:
eax
: System call number.ebx
, ecx
, edx
: Arguments to the system call.eax
: Return value from the system call.Example: Writing to standard output using the write
system call (syscall number 4):
mov eax, 4 ; sys_write mov ebx, 1 ; file descriptor (stdout) mov ecx, msg ; message to print mov edx, msg_len ; message length int 0x80 ; make the system call section .data msg db 'Hello, world!', 0xA msg_len equ $ - msg
Interrupt handling is done by setting up an interrupt descriptor table (IDT) and linking interrupt numbers to handler functions. This process is often done at the kernel level and not within user-space programs.
Some common system calls include:
sys_exit
(1): Exits the program.sys_fork
(2): Creates a new process.sys_read
(3): Reads data from a file or device.sys_write
(4): Writes data to a file or device.