Assembly Language - File Management
File management is the foundation of a program's interaction with external storage.
Through system calls, an assembly program can open, read, write, and close files, handling file IO as flexibly as high-level languages.
Overview of the file operation flow
File operations follow the basic flow open -> read/write -> close, along with three standard file descriptors:
System calls related to file operations
| System call | Call number | Purpose | Main Parameters |
|---|---|---|---|
| sys_open | 5 | Open/create file | EBX=filename, ECX=flags, EDX=permissions |
| sys_read | 3 | Reading a file | EBX=fd, ECX=buffer, EDX=byte count |
| sys_write | 4 | Writing to a File | EBX=fd, ECX=buffer, EDX=byte count |
| sys_close | 6 | Closing a File | EBX=fd |
| sys_creat | 8 | Create file (old method) | EBX=filename, ECX=permissions |
| sys_lseek | 19 | Move the file pointer | EBX=fd, ECX=offset, EDX=start position |
| sys_unlink | 10 | Delete a file | EBX=filename |
File open flags
| Constant | Value | Description |
|---|---|---|
| O_RDONLY | 0 | read-only |
| O_WRONLY | 1 | Write only |
| O_RDWR | 2 | Read/write |
| O_CREAT | 64 (0x40) | Create if the file does not exist |
| O_TRUNC | 512 (0x200) | Truncate the file content when opening |
| O_APPEND | 1024 (0x400) | Append to the end of the file |
Multiple flags are combined with OR, for example O_WRONLY | O_CREAT | O_TRUNC = 1 | 64 | 512 = 577
Create and write to a file
Example
; File path: file_write.asm
; Create a file and write content
section
.data
filename
db
'runoob_output.txt'
,
0
; File name (null-terminated)
content
db
'Hello, RUNOOB!'
,
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 the file
mov
eax
,
8
; sys_creat
mov
ebx
,
filename
; File name
mov
ecx
,
0o644
; Permissions: rw-r--r--
; 0o644 = owner read/write, group read-only, others read-only
int
0x80
mov
[
fd
]
,
eax
; Save the file descriptor
; 2. Write content to the 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 the file
mov
eax
,
6
; sys_close
mov
ebx
,
[
fd
]
; File descriptor
int
0x80
; 4. Print a 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
Running 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 runoob_output.txt
Hello, RUNOOB!
Read file contents
Example
; File path: file_read.asm
; Open and read the file content
section
.data
filename
db
'runoob_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 the file (read-only)
mov
eax
,
5
; sys_open
mov
ebx
,
filename
; File name
mov
ecx
,
0
; Read-only mode
mov
edx
,
0
; Permissions (ignored when read-only)
int
0x80
cmp
eax
,
0
; File descriptor < 0 indicates an error
jl
open_failed
mov
[
fd
]
,
eax
; Save the file descriptor
; 2. Read the file content
mov
eax
,
3
; sys_read
mov
ebx
,
[
fd
]
; File descriptor
mov
ecx
,
buffer
; Buffer
mov
edx
,
1024
; Read at most 1024 bytes
int
0x80
; Return value eax = number of bytes actually read
mov
esi
,
eax
; Save the number of bytes read
; 3. Close the file
mov
eax
,
6
; sys_close
mov
ebx
,
[
fd
]
int
0x80
; 4. Output a prompt message
mov
eax
,
4
mov
ebx
,
1
mov
ecx
,
read_success
mov
edx
,
read_success_len
int
0x80
; 5. Output the file content to the screen
mov
eax
,
4
mov
ebx
,
1
mov
ecx
,
buffer
mov
edx
,
esi
; Use the number of bytes actually 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
Running 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, RUNOOB!
Appending to a file
Example
; File path: file_append.asm
; Open the file in append mode and write
section
.data
filename
db
'runoob_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 the file (create + append mode)
mov
eax
,
5
; sys_open
mov
ebx
,
filename
; File name
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 when creating
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 the file
mov
eax
,
6
mov
ebx
,
[
fd
]
int
0x80
mov
eax
,
1
mov
ebx
,
0
int
0x80
Copy a file
A comprehensive example: copying the content of one file to another:
Example
; File path: file_copy.asm
; Copy a file: copy runoob_input.txt to runoob_copy.txt
section
.data
src_file
db
'runoob_input.txt'
,
0
dst_file
db
'runoob_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 the 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 the 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 the 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 the 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 the loop
copy_done
:
; 5. Close the files
mov
eax
,
6
; sys_close
mov
ebx
,
[
src_fd
]
int
0x80
mov
eax
,
6
mov
ebx
,
[
dst_fd
]
int
0x80
; 6. Output a 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
Always check the return value of system calls (in EAX) before and after file operations. A negative value indicates an error (e.g. -2 is ENOENT file not found, -13 is EACCES permission denied); ignoring error return values is the biggest source of file-operation bugs.