from-scratch/lang/src/file.asm

49 lines
931 B
NASM

section .data
file_error_msg db "Could not open file: "
file_error_msg_len equ $ - file_error_msg
section .text
global fopen_read
extern strlen
extern eprint_str
extern panic
extern error_to_str
extern eprint_error
;; Opens file for reading:
;; rdi: pointer to filename (null-terminated)
fopen_read:
mov rax, 2 ; syscall: open
mov rsi, 0 ; flags: O_RDONLY
mov rdx, 0 ; mode
syscall
cmp rax, 0
jl .file_error
ret ;fd in rax
.file_error:
push rdi
mov rdi, rax
call eprint_error
lea rdi, [rel file_error_msg]
mov rsi, file_error_msg_len
call eprint_str
pop rdi
call strlen ; get length of filename
mov rsi, rax ; r9 = filename length
call eprint_str
mov rdi, 10
push rdi
mov rdi, rsp
mov rsi, 1
call eprint_str
pop rdi
call panic