Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Assembly Language – System Calls

Assembly Language - System Calls

A system call (System Call) is the only way for a user program to interact with the operating system kernel. Through system calls, an assembly program can read and write files, allocate memory, create processes, and more.


What is a system call

User programs run in restricted User Mode and cannot directly access hardware or perform privileged operations.

When a program needs kernel-level functions such as I/O operations or memory allocation, it must request the service from the OS kernel through a system call.

The system call flow is: user program initiates a system call -> CPU switches to kernel mode -> kernel performs the corresponding service -> returns to user mode to continue execution.

You can think of a system call as:The user program makes a phone call to the operating system, asking it to do something the program has no permission to do itself.


Linux system call mechanism

In 32-bit Linux, a system call is completed through the following steps:

  1. Put the system call number into the EAX register
  2. Put the arguments into registers such as EBX, ECX, EDX, ESI, EDI
  3. Executing int 0x80 The instruction triggers a software interrupt
  4. The CPU switches to kernel mode, and the kernel executes the corresponding service based on the call number in EAX
  5. The kernel places the return value in EAX and switches back to user mode to continue execution

Overview of common system calls

System call Call number (EAX) Function Parameters
sys_exit 1 Exit program EBX = return value (exit code)
sys_fork 2 Create a subprocess No parameters
sys_read 3 Read file/input EBX=fd, ECX=buf, EDX=count
sys_write 4 Write file/output EBX=fd, ECX=buf, EDX=count
sys_open 5 Opening a File EBX=filename, ECX=flags, EDX=mode
sys_close 6 Closing a File EBX=fd
sys_creat 8 Create a file EBX=filename, ECX=mode
sys_lseek 19 Move the file pointer EBX=fd, ECX=offset, EDX=whence
sys_brk 45 Adjust the data segment size (memory allocation) EBX=new address

The full list of system calls can be found in /usr/include/asm/unistd_32.h to view. Note that system call numbers differ between 32-bit and 64-bit.


sys_write - output a string

The most commonly used system call, for printing content to the screen:

Example

; File path: write_syscall.asm

; Use sys_write to output a string

section

.data

    msg

db

'Hello, runoob! Welcome to assembly.'

,

0xA

    len

equ

$

msg

section

.text

   

global

_start

_start

:

   

; Call sys_write (4)

   

mov

eax

,

4

                 

; System call number 4 = sys_write

   

mov

ebx

,

1

                 

; File descriptor 1 = stdout (standard output)

   

mov

ecx

,

msg                

; Address of the data to output

   

mov

edx

,

len                

; Data length (number of bytes)

   

int

0x80

                   

; Trigger the system call

   

; On success, EAX returns the number of bytes actually written

   

; Exit the program

   

mov

eax

,

1

   

mov

ebx

,

0

   

int

0x80

Running result:



$ nasm -f elf32 write_syscall.asm -o write_syscall.o

$ ld -m elf_i386 write_syscall.o -o write_syscall

$ ./write_syscall

Hello, runoob! Welcome to assembly.


sys_read - read user input

Read data from standard input (keyboard):

Example

; File path: read_syscall.asm

; Use sys_read to read user input and echo it back

section

.bss

    buffer

resb

64

             

; Reserve a 64-byte input buffer

section

.data

    prompt

db

'Please enter your name: '

    prompt_len

equ

$

prompt

    output_msg

db

'Hello, '

    output_msg_len

equ

$

output_msg

section

.text

   

global

_start

_start

:

   

; Output a prompt message

   

mov

eax

,

4

   

mov

ebx

,

1

   

mov

ecx

,

prompt

   

mov

edx

,

prompt_len

   

int

0x80

   

; Read user input

   

mov

eax

,

3

                 

; System call number 3 = sys_read

   

mov

ebx

,

0

                 

; File descriptor 0 = stdin (standard input)

   

mov

ecx

,

buffer            

; Input buffer address

   

mov

edx

,

64

               

; Read at most 64 bytes

   

int

0x80

   

; EAX returns the number of bytes actually read (including the trailing newline)

   

mov

esi

,

eax

               

; Save the actual input length to esi

   

; Output "Hello, "

   

mov

eax

,

4

   

mov

ebx

,

1

   

mov

ecx

,

output_msg

   

mov

edx

,

output_msg_len

   

int

0x80

   

; Output what the user entered

   

mov

eax

,

4

   

mov

ebx

,

1

   

mov

ecx

,

buffer

   

mov

edx

,

esi

               

; Use the actual number of input bytes

   

int

0x80

   

; Exit the program

   

mov

eax

,

1

   

mov

ebx

,

0

   

int

0x80

Running result:



$ nasm -f elf32 read_syscall.asm -o read_syscall.o

$ ld -m elf_i386 read_syscall.o -o read_syscall

$ ./read_syscall

Please enter your name: runoob

Hello, runoob


sys_exit - exit the program

Every program must call sys_exit to exit normally; otherwise the CPU will continue executing the following garbage data and cause a segmentation fault.

Example

; Exit with different exit codes

   

mov

eax

,

1

                 

; System call number 1 = sys_exit

   

mov

ebx

,

0

                 

; Exit code 0 = normal exit (a nonzero value is also allowed)

   

int

0x80

The exit code can be checked in the shell through $? view:



$ ./program

$ echo $?

0


General system call template

When writing a system call, you can follow this general template:

Example

; General system call template

; For 32-bit Linux systems

; Step 1: put the system call number into EAX

mov

eax

,

System call number

; Step 2: put in the arguments in order

mov

ebx

,

The

1

arguments

mov

ecx

,

The

2

arguments

mov

edx

,

The

3

arguments

mov

esi

,

The

4

arguments

mov

edi

,

The

5

arguments

; Step 3: trigger the system call

int

0x80

; Step 4: check the return value (in EAX)

; Usually a negative value indicates an error

cmp

eax

,

0

jl

error_handler              

; If EAX < 0, jump to error handling

int 0x80 After triggering, EAX holds the return value. Most system calls return a nonnegative value on success and a negative error code on failure (e.g., -1 means EPERM).