Directly interface with hardware from Assembly Language

Interfacing with the operating system (OS) in assembly language involves making system calls to access OS services and functionalities. System calls are software interrupts that allow an assembly language program to request services from the OS, such as file I/O, process management, and memory allocation.

System call mechanism

When an assembly language program makes a system call, it follows specific steps:

Save the program's state

The program's current state, including register values and stack pointers, is saved on the stack.

Prepare system call arguments

The program prepares the arguments for the system call, typically placing them in registers or memory locations.

Trigger the system call

A special instruction, such as the int instruction in x86 architecture, triggers the system call interrupt.

Switch to kernel mode

The CPU switches from user mode, where the program executes, to kernel mode, where the OS kernel operates.

Execute system call handler

The OS kernel identifies the requested system call and executes the corresponding handler routine.

Return to user mode

The OS kernel completes the system call request and returns control to the user mode program.

Restore program's state

The program's saved state is restored, allowing it to continue execution.

System call examples

Here are some examples of system calls commonly used in assembly language programs:

  1. File I/O: System calls like open(), read(), write(), and close() allow programs to create, read, write, and close files.
  2. Process management: System calls like fork(), exec(), and wait() enable programs to create new processes, execute other programs, and wait for child processes to finish.
  3. Memory allocation: System calls like malloc(), free(), and sbrk() allow programs to allocate and deallocate memory dynamically.
  4. Input/output: System calls like read() and write() enable programs to interact with input devices like keyboards and output devices like monitors.
  5. Time and date: System calls like time() and date() allow programs to access the current time and date.

Explanation with examples:

Load System Call Number

Determine the system call number corresponding to the desired OS service.

mov eax, 4 ; System call number for write

Load Arguments

Place the necessary arguments in the registers according to the system call convention.

mov ebx, 1 ; File descriptor: STDOUT mov ecx, msg ; Buffer address mov edx, len ; Number of bytes to write

Trigger System Call

Execute the system call instruction to transfer control to the operating system.

int 0x80 ; Interrupt to invoke the kernel (Linux x86)

Handle Return Values

Retrieve any return values or error codes from the appropriate registers.

cmp eax, 0 ; Check if the system call was successful jl handle_error

Error Handling

Implement code to handle errors, such as printing an error message or terminating the program.

handle_error: ; Handle error (e.g., print error message, exit program)

Invoke Additional System Calls

Repeat the process for other system calls or services as needed.

mov eax, 1 ; System call number for exit xor ebx, ebx ; Exit code 0 int 0x80 ; Invoke kernel to exit program

System call conventions

Each OS has specific conventions for making system calls, including the format of system call arguments, the use of registers, and the handling of return values. Assembly language programmers must adhere to these conventions to ensure compatibility with the OS.

Interfacing with libraries

Assembly language programs can also interact with libraries, which provide pre-written code for frequently used tasks. Libraries can be written in assembly language or in higher-level languages and typically provide an API (application programming interface) for accessing their functions.


Interfacing with the OS in assembly language requires understanding system call mechanisms, OS conventions, and library APIs. By effectively utilizing system calls, assembly language programs can access the full range of OS services and functionalities.