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

@@ -11,6 +11,8 @@ section .bss
printfBuff resb printfBuffLen
printfArgs resq 5 ;5=>rsi,rdx,rcx,r8,r9
section .text
global __INTERNAL_fmt
global print
global puts
global printf
@@ -94,13 +96,23 @@ puts:
; r12 Padding length
; r13 Bitmask, 1 = insert specifier yes/no, 2 = use zeroes for padding yes/no
; r14 Keeps track of amount of processed format specifiers
; r15 Stores fd to write output to (passed to __INTERNAL_fmt via RAX)
printf:
mov rax, FD_stdout
jmp __INTERNAL_fmt
__INTERNAL_fmt:
%macro load_arg 1
cmp r14, 4
ja %%fromStack
mov %1, [printfArgs + SIZE_QWORD * r14]
jmp %%continue
%%fromStack:
cmp r15, FD_stdout
je %%stackNoShift
mov %1, [rbp + (RBP_OFFSET_CALLER*2 + SIZE_QWORD) + ((r14-5) * SIZE_QWORD)] ;non-printf functions calling __INTERNAL_fmt require RDI to be FD, all args shifted and rbp+RBP_OFFSET_CALLER to r9. Also requires function prologue and a CALL to __INTERNAL_fmt (not a JMP like in printf); hence the RBP_OFFSET_CALLER*2
jmp %%continue
%%stackNoShift:
mov %1, [rbp + RBP_OFFSET_CALLER + ((r14-5) * SIZE_QWORD)]
%%continue:
%endmacro
@@ -152,10 +164,11 @@ printf:
; entry:
push rbp
mov rbp, rsp
sub rsp, SIZE_QWORD
push r12
push r13
push r14
push r15
mov r15, rax
cmp byte [rdi], EOS
je .emptyStr
@@ -414,7 +427,7 @@ printf:
push rdx
push r11
mov rax, NR_write
mov rdi, FD_stdout
mov rdi, r15
lea rsi, [rel printfBuff]
mov rdx, r10
syscall
@@ -435,7 +448,7 @@ printf:
.wrapup:
mov rax, NR_write
mov rdi, FD_stdout
mov rdi, r15
lea rsi, [rel printfBuff]
mov rdx, r10
mov r10, r11
@@ -447,9 +460,9 @@ printf:
xor rax, rax
.quit:
pop r15
pop r14
pop r13
pop r12
add rsp, SIZE_QWORD
leave
ret