Assembly File
## Assembly Language - File Management
File management is the foundation of program interaction with external storage.
Through system calls, assembly programs can open, read, write, and close files, handling file IO as flexibly as high-level languages.
* * *
## File Operation Flow Overview
File operations follow the basic flow of open β read/write β close, along with three standard file descriptors:

* * *
## System Calls for File Operations
| System Call | Call Number | Purpose | Main Parameters |
| --- | --- | --- | --- |
| sys_open | 5 | Open/Create file | EBX=filename, ECX=flags, EDX=permissions |
| sys_read | 3 | Read file | EBX=fd, ECX=buffer, EDX=byte count |
| sys_write | 4 | Write file | EBX=fd, ECX=buffer, EDX=byte count |
| sys_close | 6 | Close file | EBX=fd |
| sys_creat | 8 | Create file (old method) | EBX=filename, ECX=permissions |
| sys_lseek | 19 | Move file pointer | EBX=fd, ECX=offset, EDX=origin |
| sys_unlink | 10 | Delete 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 file doesn't exist |
| O_TRUNC | 512 (0x200) | Truncate file content when opening |
| O_APPEND | 1024 (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 File
## Example
; File path: file_write.asm
; Create file and write content
section.data
filename db'tutorial_output.txt',0; File name (null terminated)
content db'Hello, TUTORIAL!',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 ; File name
mov ecx, 0o644 ; Permissions: rw-r--r--
; 0o644 = owner read/write, group read-only, others read-only
int 0x80
mov,eax; Save file descriptor
; 2. Write content to file
mov eax,4; sys_write
mov ebx,; 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,; File descriptor
int 0x80
; 4. Prompt success
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 tutorial_output.txt Hello, TUTORIAL!
* * *
## Read File Content
## Example
; File path: file_read.asm
; Open and read file content
section.data
filename db'tutorial_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 ; File name
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,eax; Save file descriptor
; 2. Read file content
mov eax,3; sys_read
mov ebx,; File descriptor
mov ecx, buffer ; Buffer
mov edx,1024; Read up to 1024 bytes
int 0x80
; Return value eax = actual bytes read
mov esi,eax; Save bytes read
; 3. Close file
mov eax,6; sys_close
mov ebx,
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 content 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
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, TUTORIAL!
* * *
## Append to File
## Example
; File path: file_append.asm
; Open file in append mode and write
section.data
filename db'tutorial_log.txt',0
log_entry db' 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 ; File name
mov ecx,0x441; O_WRONLY | O_CREAT | O_APPEND
; O_WRONLY=1, O_CREAT=0x40, O_APPEND=0x400
; Combined: 1 | 0x40 | 0x400 = 0x441
mov edx, 0o644 ; Permissions when creating
int 0x80
mov,eax
; Append write
mov eax,4; sys_write
mov ebx,; File descriptor
mov ecx, log_entry ; Log content
mov edx, log_len
int 0x80
; Close file
mov eax,6
mov ebx,
int 0x80
mov eax,1
mov ebx,0
int 0x80
* * *
## Copy File
A comprehensive example: copy the content of one file to another:
## Example
; File path: file_copy.asm
; Copy file: copy tutorial_input.txt to tutorial_copy.txt
section.data
src_file db'tutorial_input.txt',0
dst_file db'tutorial_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
YouTip