Adds fwrite()

This commit is contained in:
2025-07-25 14:16:20 +02:00
parent a7c1c6bdb5
commit 5434e48ee4
2 changed files with 87 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
%include "src/constants.asm"
extern strlen
extern umask_get
section .rodata
@@ -51,6 +52,7 @@ section .text
global fopen
global fclose
global fexist
global fwrite
;----- fopen(*file[], char mode) -----;
; Opens a file
@@ -146,7 +148,7 @@ fopen:
add rsp, SIZE_QWORD
ret
;----- fclose(file) -----;
;----- fclose(*filePointer) -----;
; Closes an opened file
; Return value: 0 if file closed. Negative error number if an error occured
; Used registers:
@@ -167,11 +169,12 @@ fclose:
;----- fexist(*file[]) -----;
; Checks if given file exists (attempts to open the file)
; Return value: 0- File closed succesfully. -errno (negative error number) if an error occured
; Return value: 1- File exists. 0- File does not exist or is a directory
; Used registers:
; rax* (ret)
; rdi (arg) Pointer to string holding (path+)file name
fexist:
;sub rsp, SIZE_QWORD
mov rsi, 'r'
call fopen
test rax, rax
@@ -183,4 +186,35 @@ fexist:
.not_exist:
xor rax, rax
.quit:
;add rsp, SIZE_QWORD
ret
;--- fwrite(*filePointer, *data[]) -----;
; Writes data to given file
; 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
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
ret