File I/O
open
Syscall 2 (open) takes a filename, flags, and a mode. The flags are constants from fcntl.h: O_RDONLY is 0, O_WRONLY is 1, O_CREAT is 0x40, O_TRUNC is 0x200:
%define SYS_OPEN 2 %define O_RDONLY 0 mov rax, SYS_OPEN mov rdi, filename mov rsi, O_RDONLY syscall ; rax = file descriptor
read
Syscall 0 (read) takes the descriptor, a buffer, and a count, and returns the bytes read. Zero means end of file, and a negative value means an error:
%define SYS_READ 0 mov rax, SYS_READ mov rdi, fd mov rsi, buf mov rdx, buflen syscall ; rax = bytes read
close
Syscall 3 (close) releases the descriptor. Close every descriptor you open, including on the error path, or the process runs out of descriptors. Write (syscall 1) works exactly like the stdout version, with the descriptor in RDI.
This complete program puts the three calls together: it creates note.txt, writes one line and closes it, then prints a marker:
default rel %define SYS_OPEN 2 %define SYS_WRITE 1 %define SYS_CLOSE 3 %define SYS_EXIT 60 %define O_WRONLY 1 %define O_CREAT 0x40 %define O_TRUNC 0x200 section .data filename db "note.txt", 0 text db "file", 10 ok db "written", 10 section .text global _start _start: mov rax, SYS_OPEN mov rdi, filename mov rsi, O_WRONLY | O_CREAT | O_TRUNC mov rdx, 0o644 syscall test rax, rax js failed mov rdi, rax ; the descriptor mov rax, SYS_WRITE mov rsi, text mov rdx, 5 syscall mov rax, SYS_CLOSE syscall mov rax, SYS_WRITE mov rdi, 1 mov rsi, ok mov rdx, 8 syscall mov rax, SYS_EXIT xor rdi, rdi syscall failed: mov rax, SYS_EXIT mov rdi, 1 syscall
nasm -f elf64 -o file.o file.asm
ld -o file file.o
./file
echo $?
written
0
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which syscall number opens a file on x86-64 Linux?
Answer
2
0 is read, 1 is write, 2 is open and 3 is close.
-
Which flag value means O_RDONLY?
Answer
0
Read-only is the zero flag; write-only is 1.
-
Which flag creates the file when it does not exist?
Answer
O_CREAT
O_CREAT is 0x40, and the mode argument then supplies the permissions.
-
What does O_TRUNC do to an existing file?
Answer
It empties the file first
O_TRUNC cuts the file to zero length when it is opened for writing.
-
What does read return at the end of a file?
Answer
0
Zero bytes read means end of file; a negative value is an error.
-
Which descriptor does a program close when it closes stdout?
Answer
1
0 is stdin, 1 is stdout and 2 is stderr.
-
What does this program print?
default rel %define SYS_OPEN 2 %define SYS_WRITE 1 %define SYS_CLOSE 3 %define SYS_EXIT 60 %define O_WRONLY 1 %define O_CREAT 0x40 %define O_TRUNC 0x200 section .data filename db "note.txt", 0 text db "file", 10 ok db "written", 10 section .text global _start _start: mov rax, SYS_OPEN mov rdi, filename mov rsi, O_WRONLY | O_CREAT | O_TRUNC mov rdx, 0o644 syscall test rax, rax js failed mov rdi, rax mov rax, SYS_WRITE mov rsi, text mov rdx, 5 syscall mov rax, SYS_CLOSE syscall mov rax, SYS_WRITE mov rdi, 1 mov rsi, ok mov rdx, 8 syscall mov rax, SYS_EXIT xor rdi, rdi syscall failed: mov rax, SYS_EXIT mov rdi, 1 syscallAnswer
written
The first write goes into the file, and the second write prints the marker to stdout.
-
Which register holds the descriptor returned by open?
Answer
RAX
The result of a syscall comes back in RAX, so the program moves it into RDI for the next call.
-
What is the mode argument of open used for?
Answer
The permissions of a newly created file
The mode is read only when O_CREAT makes a new file; 0o644 is a common value.
-
Why must every descriptor be closed?
Answer
A process has a limited number of descriptors
Leaking descriptors eventually makes open fail with EMFILE.
The short snippets on this page are fragments: they show the three calls. The complete program was assembled with NASM 3.01, linked with GNU ld 2.45.1, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev