Search code examples
linuxassemblyx86system-callsdos

What is the difference between int 0x21 and int 0x80?


I recently got into low level programing and learned about system interrupts. From what I understood, both interrupts are responsible for system calls, however I don't understand the difference between the two, and when to use which. Would love an explanation.


Solution

  • int 21h (spelled in MASM/TASM syntax, as this is the most widespread assembler for DOS) is the system call interrupt for DOS. You can find documentation of available services easily on the internet. A short list of the most important DOS services you can reach from DOS programs is at http://spike.scu.edu.au/~barry/interrupts.html.

    On the other hand, int 0x80 (spelled in gas syntax, as this is the "native" assembler for Linux) is the system call interrupt for Linux on 32-bit intel processors. You don't get nice tables on how to use it, as you get for DOS, because you typically don't call it directly. If you want to call it directly, look at the manpage for syscall, to find out what registers you need to set, and where you will find the result. You can find the number for the syscalls for example at https://fedora.juszkiewicz.com.pl/syscalls.html .

    The behaviour of your program, when it invokes int 21h or int 0x80, is not defined by Intel or the 80386 processor architecture. Instead, the int instruction asks the processor to look up an entry point into an entry point table (the "interrupt descriptor table"), and jumps to that entry point. This jump may include switching from user space to kernel space (if the processor runs in a mode that supports different privilege levels). DOS sets up the entry point number 21h to point to a dispatch function that offers most DOS services. On the other hand, Linux/i386 sets up entry point number 0x80 to point to a dispatch function that offers all linux kernel services. This means that a program that uses int 0x80 will only work if it is executed in a Linux (or compatible, like the windows subsystem for Linux, version 1) environment, whereas a program that uses int 21h will only work if it is executed under DOS (or a compatible environment like DOSBox or the OS/2 DOS window).