printf->__INTERNAL_fmt. FD passed via RAX. fwrite now allows specifiers and variable arguments.

This commit is contained in:
2025-07-26 00:20:25 +02:00
parent d713fdf556
commit 6b45b84ab1
3 changed files with 68 additions and 29 deletions

View File

@@ -1,5 +1,7 @@
%include "src/constants.asm"
extern __INTERNAL_fmt
extern strlen
extern umask_get
@@ -76,7 +78,7 @@ fopen:
call fexist
pop rsi
pop rdi
mov rax, 1
test rax, 1
jz %%createFile
jmp %1
%%createFile:
@@ -86,7 +88,7 @@ fopen:
pop rdi
push rdi
not rax
mov rsi, 00666q
mov rsi, 0666q
and rsi, rax
mov rax, NR_creat
syscall
@@ -155,6 +157,7 @@ fopen:
; rax* (ret)
; rdi (arg) Pointer to opened file
fclose:
sub rsp, SIZE_QWORD
cmp rdi, 3
jl .invalid_fd ;disallow closing stdin,stdout,stderr
mov rax, NR_close
@@ -165,6 +168,7 @@ fclose:
mov rax, -EINVAL
.quit:
add rsp, SIZE_QWORD
ret
;----- fexist(*file[]) -----;
@@ -174,7 +178,7 @@ fclose:
; rax* (ret)
; rdi (arg) Pointer to string holding (path+)file name
fexist:
;sub rsp, SIZE_QWORD
sub rsp, SIZE_QWORD
mov rsi, 'r'
call fopen
test rax, rax
@@ -186,35 +190,32 @@ fexist:
.not_exist:
xor rax, rax
.quit:
;add rsp, SIZE_QWORD
add rsp, SIZE_QWORD
ret
;--- fwrite(*filePointer, *data[]) -----;
; Writes data to given file
;--- fwrite(*filePointer, *data[], ...) -----;
; Writes data to given file. Allows format specifiers and variable arguments (see `__INTERNAL_fmt` for supported specifiers)
; Return value: Length of given string(data[]), 0 if nothing was written (eg invalid file or file not writable, or data[] is empty)
; Used registers:
; rax* (ret)
; rdi (arg) Pointer to file
; rsi (arg) Data to write
; r8* Backs up rdi
; r9* Backs up rsi
fwrite:
sub rsp, SIZE_QWORD
push rbp
mov rbp, rsp
cmp rdi, 2
jg .cnt
xor rax, rax
cmp rdi, 3
jl .quit
mov r8, rdi
mov r9, rsi
mov rdi, rsi
call strlen
mov rdx, rax
mov rax, NR_write
mov rdi, r8
mov rsi, r9
syscall
.quit:
add rsp, SIZE_QWORD
leave
ret
.cnt:
mov rax, rdi
mov rdi, rsi
mov rsi, rdx
mov rdx, rcx
mov rcx, r8
mov r8, r9
mov r9, [rbp + RBP_OFFSET_CALLER]
call __INTERNAL_fmt
leave
ret