From 9982eabe7c05e102860d4691a0956f6d08acb91b Mon Sep 17 00:00:00 2001 From: Kwarde Date: Fri, 27 Jun 2025 18:57:54 +0200 Subject: [PATCH] printf(): Always print full string instead of throwing an error; print printfBuff when full and start at index 0 again --- src/console.asm | 61 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/src/console.asm b/src/console.asm index e328851..64263c7 100644 --- a/src/console.asm +++ b/src/console.asm @@ -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