Assembly Language - File Management

File management is the foundation for programs to interact with external storage.

Through system calls, assembly programs can open, read, write, and close files, handling file I/O as flexibly as high-level languages.


Overview of File Operation Process

File operations follow the basic flow of open → read/write → close, along with three standard file descriptors:

文件操作流程图

System Calls Related to File Operations

System CallCall NumberPurposeMain Parameters
sys_open5Open/Create FileEBX=filename, ECX=flags, EDX=permissions
sys_read3Read FileEBX=fd, ECX=buffer, EDX=byte count
sys_write4Write FileEBX=fd, ECX=buffer, EDX=byte count
sys_close6Close FileEBX=fd
sys_creat8Create File (legacy)EBX=filename, ECX=permissions
sys_lseek19Move File PointerEBX=fd, ECX=offset, EDX=origin
sys_unlink10Delete FileEBX=filename

File Open Flags

ConstantValueDescription
O_RDONLY0Read-only
O_WRONLY1Write-only
O_RDWR2Read-write
O_CREAT64(0x40)Create if file does not exist
O_TRUNC512(0x200)Truncate file on open
O_APPEND1024(0x400)Append to end of file

Multiple flags are combined with OR, for example:O_WRONLY | O_CREAT | O_TRUNC = 1 | 64 | 512 = 577


Create and Write to File

Example

; File path: file_write.asm
; Create a file and write content

section .data
    filename db 'example_output.txt', 0    ; Filename (null-terminated)
    content db 'Hello, EXAMPLE!', 0xA       ; Content to write
    content_len equ $ - content
    create_msg db 'File created successfully!', 0xA
    create_msg_len equ $ - create_msg

section .bss
    fd resd 1                               ; File descriptor

section .text
    global _start

_start:
    ; 1. Create file
    mov eax, 8                  ; sys_creat
    mov ebx, filename           ; Filename
    mov ecx, 0o644              ; Permissions: rw-r--r--
    ; 0o644 = owner read/write, group read-only, others read-only
    int 0x80

    mov [fd], eax               ; Save file descriptor

    ; 2. Write content to file
    mov eax, 4                  ; sys_write
    mov ebx, [fd]               ; File descriptor
    mov ecx, content            ; Data address
    mov edx, content_len        ; Data length
    int 0x80

    ; 3. Close file
    mov eax, 6                  ; sys_close
    mov ebx, [fd]               ; File descriptor
    int 0x80

    ; 4. Print success message
    mov eax, 4
    mov ebx, 1
    mov ecx, create_msg
    mov edx, create_msg_len
    int 0x80

    mov eax, 1
    mov ebx, 0
    int 0x80

Run result:

$ nasm -f elf32 file_write.asm -o file_write.o
$ ld -m elf_i386 file_write.o -o file_write
$ ./file_write
File created successfully!
$ cat example_output.txt
Hello, EXAMPLE!

Read File Contents

Example

; File path: file_read.asm
; Open and read file contents

section .data
    filename db 'example_output.txt', 0
    open_error db 'Error: cannot open file', 0xA
    open_error_len equ $ - open_error
    read_success db 'File content:', 0xA
    read_success_len equ $ - read_success

section .bss
    fd resd 1
    buffer resb 1024                 ; Read buffer

section .text
    global _start

_start:
    ; 1. Open file (read-only)
    mov eax, 5                  ; sys_open
    mov ebx, filename           ; Filename
    mov ecx, 0                  ; Read-only mode
    mov edx, 0                  ; Permissions (ignored for read-only)
    int 0x80

    cmp eax, 0                  ; File descriptor < 0 indicates error
    jl  open_failed

    mov [fd], eax               ; Save file descriptor

    ; 2. Read file contents
    mov eax, 3                  ; sys_read
    mov ebx, [fd]               ; File descriptor
    mov ecx, buffer             ; Buffer
    mov edx, 1024               ; Read up to 1024 bytes
    int 0x80
    ; Return value eax = number of bytes actually read
    mov esi, eax                ; Save byte count read

    ; 3. Close file
    mov eax, 6                  ; sys_close
    mov ebx, [fd]
    int 0x80

    ; 4. Output prompt message
    mov eax, 4
    mov ebx, 1
    mov ecx, read_success
    mov edx, read_success_len
    int 0x80

    ; 5. Output file contents to screen
    mov eax, 4
    mov ebx, 1
    mov ecx, buffer
    mov edx, esi                ; Use actual bytes read
    int 0x80
    jmp exit

open_failed:
    mov eax, 4
    mov ebx, 1
    mov ecx, open_error
    mov edx, open_error_len
    int 0x80

exit:
    mov eax, 1
    mov ebx, 0
    int 0x80

Run result:

$ nasm -f elf32 file_read.asm -o file_read.o
$ ld -m elf_i386 file_read.o -o file_read
$ ./file_read
File content:
Hello, EXAMPLE!

Append Write to File

Example

; File path: file_append.asm
; Open file in append mode and write

section .data
    filename db 'example_log.txt', 0
    log_entry db '[INFO] Program executed successfully.', 0xA
    log_len equ $ - log_entry

section .bss
    fd resd 1

section .text
    global _start

_start:
    ; Open file (create + append mode)
    mov eax, 5                  ; sys_open
    mov ebx, filename           ; Filename
    mov ecx, 0x441              ; O_WRONLY | O_CREAT | O_APPEND
    ; O_WRONLY=1, O_CREAT=0x40, O_APPEND=0x400
    ; Combination: 1 | 0x40 | 0x400 = 0x441
    mov edx, 0o644              ; Permissions at creation time
    int 0x80

    mov [fd], eax

    ; Append write
    mov eax, 4                  ; sys_write
    mov ebx, [fd]               ; File descriptor
    mov ecx, log_entry          ; Log content
    mov edx, log_len
    int 0x80

    ; Close file
    mov eax, 6
    mov ebx, [fd]
    int 0x80

    mov eax, 1
    mov ebx, 0
    int 0x80

Copy File

A comprehensive example: copying the contents of one file to another:

Example

; File path: file_copy.asm
; Copy file: copy example_input.txt to example_copy.txt

section .data
    src_file db 'example_input.txt', 0
    dst_file db 'example_copy.txt', 0
    success_msg db 'File copied successfully!', 0xA
    success_len equ $ - success_msg
    error_msg db 'Error during file copy', 0xA
    error_len equ $ - error_msg

section .bss
    src_fd resd 1
    dst_fd resd 1
    buffer resb 4096              ; 4KB copy buffer

section .text
    global _start

_start:
    ; 1. Open source file (read-only)
    mov eax, 5
    mov ebx, src_file
    mov ecx, 0                   ; O_RDONLY
    mov edx, 0
    int 0x80
    cmp eax, 0
    jl  error_exit
    mov [src_fd], eax

    ; 2. Create destination file
    mov eax, 8                   ; sys_creat
    mov ebx, dst_file
    mov ecx, 0o644
    int 0x80
    cmp eax, 0
    jl  error_exit
    mov [dst_fd], eax

copy_loop:
    ; 3. Read from source file
    mov eax, 3                   ; sys_read
    mov ebx, [src_fd]
    mov ecx, buffer
    mov edx, 4096
    int 0x80
    ; eax = number of bytes actually read
    cmp eax, 0                   ; Read 0 bytes?
    jle copy_done                ; Yes, end of file

    ; 4. Write to destination file
    mov esi, eax                 ; Save the number of bytes read
    mov eax, 4                   ; sys_write
    mov ebx, [dst_fd]
    mov ecx, buffer
    mov edx, esi                 ; Write the number of bytes actually read
    int 0x80
    jmp copy_loop                ; Continue loop

copy_done:
    ; 5. Close file
    mov eax, 6                   ; sys_close
    mov ebx, [src_fd]
    int 0x80

    mov eax, 6
    mov ebx, [dst_fd]
    int 0x80

    ; 6. Output success message
    mov eax, 4
    mov ebx, 1
    mov ecx, success_msg
    mov edx, success_len
    int 0x80
    jmp exit

error_exit:
    mov eax, 4
    mov ebx, 1
    mov ecx, error_msg
    mov edx, error_len
    int 0x80

exit:
    mov eax, 1
    mov ebx, 0
    int 0x80

Be sure to check the return value of system calls (in EAX) before and after file operations. A negative value indicates an error (for example, -2 is ENOENT file not found, -13 is EACCES insufficient permissions). Ignoring error return values is the biggest source of file operation bugs.

Other extensions