printf(): Always print full string instead of throwing an error; print printfBuff when full and start at index 0 again

This commit is contained in:
2025-06-27 18:57:54 +02:00
parent e49c203c34
commit 9982eabe7c

View File

@ -6,9 +6,7 @@ extern itoa
section .rodata
bNL db NL
bufferLength equ 4096
ERR_buffLen db "<!> ERROR: Failed to complete printf(), reached buffer length!",NL,EOS
lERR_buffLen equ $-ERR_buffLen-1
bufferLength equ 128
; Errors (perror)
perrorMsg db "%s: %s",NL,EOS
@ -88,8 +86,10 @@ printf:
push rbp
mov rbp, rsp
push r12
push r14
push r12
push r13 ;total length of string
push r14 ;current index of printfBuff
push r15 ;where to return after flush
xor r10, r10
xor r14, r14
@ -97,8 +97,10 @@ printf:
.makeStr:
cmp byte [rdi], 0x0
je .finish
mov r15, 1
cmp r14, bufferLength-1
je .finish
je .flush
.continue_after_flush_1:
cmp byte [rdi], '%'
je .replaceArg
mov r12b, byte [rdi]
@ -212,8 +214,10 @@ printf:
;--- Move fetched data to buffer ---;
.insertLoop:
mov r15, 2
cmp r14, bufferLength-1
je .finish
je .flush
.continue_after_flush_2:
cmp byte [rsi], 0x0
je .s0f
mov r12b, byte [rsi]
@ -231,6 +235,34 @@ printf:
inc rdi
inc r11
jmp .makeStr
.flush:
push rdi
push rsi
push rdx
push rcx
push r8
push r9
mov rax, NR_write
mov rdi, 1
lea rsi, [rel printfBuff]
mov rdx, r14
mov byte [r11], 0x0
syscall
pop r9
pop r8
pop rcx
pop rdx
pop rsi
pop rdi
add r13, r14
xor r14, r14
lea r11, [rel printfBuff]
cmp r15, 1
je .continue_after_flush_1
cmp r15, 2
je .continue_after_flush_2
.finish:
mov byte [r11], 0x0
@ -238,19 +270,12 @@ printf:
mov rdi, 1
lea rsi, [rel printfBuff]
mov rdx, r14
add r13, r14
syscall
mov rax, r14
cmp r14, bufferLength-1
jl .final
mov rax, NR_write
mov rdi, 2
lea rsi, [rel ERR_buffLen]
mov rdx, lERR_buffLen
syscall
.final:
mov rax, r13
pop r15
pop r14
pop r13
pop r12
leave