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