Assembly - File Management
Assembly - File Management
File Descriptor
A file descriptor is a 16-bit integer assigned to a file as a file id. When a new file is
created or an existing file is opened, the file descriptor is used for accessing the file.
File descriptor of the standard file streams - stdin, stdout and stderr are 0, 1 and
2, respectively.
File Pointer
A file pointer specifies the location for a subsequent read/write operation in the file
in terms of bytes. Each file is considered as a sequence of bytes. Each open file is
associated with a file pointer that specifies an offset in bytes, relative to the
beginning of the file. When a file is opened, the file pointer is set to zero.
https://www.tutorialspoint.com/assembly_programming/assembly_file_management.htm 1/6
6/15/24, 3:59 PM Assembly - File Management
The steps required for using the system calls are same, as we discussed earlier −
The system call returns the file descriptor of the created file in the EAX register, in
case of error, the error code is in the EAX register.
The system call returns the file descriptor of the created file in the EAX register, in
case of error, the error code is in the EAX register.
Among the file access modes, most commonly used are: read-only (0), write-only
(1), and read-write (2).
https://www.tutorialspoint.com/assembly_programming/assembly_file_management.htm 2/6
6/15/24, 3:59 PM Assembly - File Management
The system call returns the number of bytes read in the EAX register, in case of
error, the error code is in the EAX register.
Writing to a File
For writing to a file, perform the following tasks −
The system call returns the actual number of bytes written in the EAX register, in
case of error, the error code is in the EAX register.
Closing a File
For closing a file, perform the following tasks −
The system call returns, in case of error, the error code in the EAX register.
Updating a File
For updating a file, perform the following tasks −
https://www.tutorialspoint.com/assembly_programming/assembly_file_management.htm 3/6
6/15/24, 3:59 PM Assembly - File Management
Put the system call sys_lseek () number 19, in the EAX register.
Put the file descriptor in the EBX register.
Put the reference position for the offset in the EDX register.
The system call returns, in case of error, the error code in the EAX register.
Example
The following program creates and opens a file named myfile.txt, and writes a text
'Welcome to Tutorials Point' in this file. Next, the program reads from the file and
stores the data into a buffer named info. Lastly, it displays the text as stored in info.
section .text
global _start ;must be declared for using gcc
https://www.tutorialspoint.com/assembly_programming/assembly_file_management.htm 4/6
6/15/24, 3:59 PM Assembly - File Management
mov eax, 6
mov ebx, [fd_out]
section .data
https://www.tutorialspoint.com/assembly_programming/assembly_file_management.htm 5/6
6/15/24, 3:59 PM Assembly - File Management
file_name db 'myfile.txt'
msg db 'Welcome to Tutorials Point'
len equ $-msg
section .bss
fd_out resb 1
fd_in resb 1
info resb 26
When the above code is compiled and executed, it produces the following result −
Written to file
Welcome to Tutorials Point
https://www.tutorialspoint.com/assembly_programming/assembly_file_management.htm 6/6