Align stack in functions that use syscall

This commit is contained in:
2025-07-01 18:19:00 +02:00
parent c80eb0b21d
commit 520f527d7e
3 changed files with 12 additions and 0 deletions

View File

@ -60,16 +60,19 @@ section .text
;----- print (char* string) -----;
; return value: N/A
print:
sub rsp, 8
call strlen
mov rdx, rax
mov rax, NR_write
mov rsi, rdi
mov rdi, FD_stdout
syscall
add rsp, 8
ret
;----- puts (char* string) -----;
; return value: N/A
puts:
sub rsp, 8
mov r10, rdi
call print
mov rdi, r10
@ -78,6 +81,7 @@ puts:
mov rsi, bNL
mov rdx, 1
syscall
add rsp, 8
ret
;----- printf(const char* string, ...) -----;
; Currently only supports specifiers: %d, %c, %s, %%

View File

@ -139,5 +139,6 @@ itoa:
;----- exit(int exit_code) -----;
; returns: nothing
exit:
sub rsp, 8
mov rax, NR_exit
syscall

View File

@ -1,4 +1,5 @@
%include "src/constants.asm"
extern strlen
section .rodata
@ -17,6 +18,7 @@ section .text
;----- fopen(const char *filename, const char mode) -----;
; return value: pointer to file or value < 0 (neg errno) if file failed to open
fopen:
sub rsp, 8
cmp sil, 'r'
je .setMode_r
cmp sil, 'w'
@ -56,10 +58,12 @@ fopen:
syscall
.quit:
add rsp, 8
ret
;----- fclose(FILE* fp) -----;
; return value: 0 on success, below 0 otherwise (neg errno)
fclose:
sub rsp, 8
cmp rdi, 3
jl .ebadf
@ -71,11 +75,13 @@ fclose:
mov rax, -9
.quit:
add rsp, 8
ret
;----- fwrite(FILE* fp, char* str) -----;
; writes a string to opened file
; return value: amount of written characters on success, below 0 otherwise (neg errno)
fwrite:
sub rsp, 8
cmp rdi, FD_stderr
jle .ebadf
@ -92,4 +98,5 @@ fwrite:
mov rax, -9
.quit:
add rsp, 8
ret