exit() instead of ret in main

This commit is contained in:
janis 2025-10-16 19:14:28 +02:00
parent 3d4cfc8e2b
commit 7bc428caf3
Signed by: janis
SSH key fingerprint: SHA256:bB1qbbqmDXZNT0KKD5c2Dfjg53JGhj7B3CFcLIzSqq8

View file

@ -1,7 +1,6 @@
;; Compile with: ;; Compile with:
;; nasm -f elf64 main.asm -o main.o ;; nasm -f elf64 main.asm -o main.o
;; RO section
section .data section .data
hello_msg db "Hello, World!", 10 hello_msg db "Hello, World!", 10
hello_msg_len equ $ - hello_msg hello_msg_len equ $ - hello_msg
@ -32,7 +31,10 @@ _start:
mov rdx, rax ; filename pointer mov rdx, rax ; filename pointer
call fopen_read call fopen_read
mov rax, 42 mov rax, 42
ret ; exit with success
mov rdi, 0 ; status:
mov rax, 60 ; syscall: exit
syscall
.no_filename: .no_filename:
call die call die
@ -54,9 +56,7 @@ die:
;; Writes a string to stderr: ;; Writes a string to stderr:
;; rdx: pointer to string ;; rdx: pointer to string
;; rcx: length of string ;; rcx: length of string
eprint_str: ; print string-length pair to stderr eprint_str:
; str pointer in rdx
; length in rcx
mov rax, 1 ; syscall: write mov rax, 1 ; syscall: write
mov rdi, 2 ; fd: stderr mov rdi, 2 ; fd: stderr
mov rsi, rdx ; buf: str mov rsi, rdx ; buf: str
@ -93,7 +93,7 @@ memcpy:
ret ret
;; Opens file for reading: ;; Opens file for reading:
;; rdx: pointer to filename (null-terminated) ;; rdx: pointer to filename (null-terminated)
fopen_read: fopen_read:
mov rax, 2 ; syscall: open mov rax, 2 ; syscall: open
mov rdi, rdx ; filename mov rdi, rdx ; filename
@ -103,42 +103,43 @@ fopen_read:
cmp rax, 0 cmp rax, 0
jl .file_error jl .file_error
ret ;fd in rax ret ;fd in rax
.file_error: .file_error:
; allocate stack space for error message
mov rdx, rdi ; filename is in rdi mov rdx, rdi ; filename is in rdi
call strlen ; get length of filename call strlen ; get length of filename
mov r9, rax mov r9, rax ; r9 = filename length
mov rsi, r9 mov rsi, r9
add rsi, file_error_msg_len ; add length of error message prefix add rsi, file_error_msg_len ; + prefix
add rsi, 1 ; for new line add rsi, 1 ; + newline
; align to 16 bytes
add rsi, 15 add rsi, 15
and rsi, -16 and rsi, -16 ; align up to 16
sub rsp, rsi ; alloca sub rsp, rsi ; allocate buffer
push rsi ; save length for dealloca push rsi ; save allocation size
; copy file_error_msg ; copy file_error_msg
lea rdx, [rsp + 8] lea rdx, [rsp + 8]
mov rcx, file_error_msg mov rcx, file_error_msg
mov r8, file_error_msg_len mov r8, file_error_msg_len
call memcpy call memcpy
; copy filename ; copy filename
lea rdx, [rsp + 8 + file_error_msg_len] lea rdx, [rsp + 8 + file_error_msg_len]
mov rcx, rdi mov rcx, rdi
mov r8, r9 mov r8, r9
call memcpy call memcpy
; trailing newline ; trailing newline
lea rdx, [rsp + 8 + file_error_msg_len + r9] lea rdx, [rsp + 8 + file_error_msg_len + r9]
mov byte [rdx], 10 mov byte [rdx], 10
; print error message ; print error message
lea rdx, [rsp + 8] lea rdx, [rsp + 8]
mov rcx, r9 mov rcx, file_error_msg_len
add rcx, file_error_msg_len add rcx, r9
add rcx, 1 add rcx, 1 ; include newline
call eprint_str call eprint_str
pop rsi pop rsi
add rsp, rsi ; dealloca add rsp, rsi ; dealloc
ret ret