Linux System Calls
Introduction
The Linux operating system protects the operating system and the hardware from user processes. This prevents buggy applications from crashing the operating system. However, this also means that user applications cannot have direct access to the hardware --- even for routine activities such as printing to the screen. To accomplish tasks like reading from the keyboard, printing to the screen, opening a file and writing to the file, a user process must ask the operating system for help. In high-level languages, this mechanism is transparent to the programmer (e.g., the printf function in C behaves the same way regardless of the target platform's operating system). In Linux assembly language progams, these tasks are accomplished through "system calls" to the kernel.
Linux System Calls
These system routines are called using software interrupt 80H (= 12810). The mnemonic of this instruction is:Almost 200 different system calls are available through this single int 80h instruction. The mechanism used to distinguish these calls is the value stored in the EAX register. For example, if EAX contains 4, the system call requested is the "write" function. If EAX contains 1, the system call requested is "exit". A complete list of the names of the system calls and the corresponding system call number is stored in the C header file "/usr/include/asm/unistd.h". Documentation for the functions are available in chapter 2 of the UNIX man pages. Arguments to be passed to the system calls are stored in registers EBX, ECX, EDX, ESI, EDI and EBP in that order. The value returned by the system call, if any, is stored in EAX. The values of all other registers should be preserved according to the documentation. (In case of weird bugs, double check that the system call has not mangled the contents of the registers.)
An Example
Suppose that we want to print to the screen. We look through the system calls in the unistd.h header file and notice the following lines: