printf: Add support for \n and \t

This commit is contained in:
2025-07-14 13:48:40 +02:00
parent c544ef8c58
commit cae828bc9a
2 changed files with 37 additions and 1 deletions

View File

@@ -75,6 +75,10 @@ puts:
; *0N* Pad left with zeroes, N chars (maximum 64). Supported *: d, i, u, x, X, b
; <!> Unsupported specifiers are printed as-is
; <!> For all specifiers (except %%) an argument is expected. Mismatch between arguments given and specifiers provided will lead to issues
;
; Supported special characters:
; \n New line
; \t Tab
; Used registers:
; rax* (ret) amount of printed characters
; rdi* (arg) pointer to format[] to format and print >> pointer to buffer
@@ -143,6 +147,8 @@ printf:
cmp r10, printfBuffLen-1
je .flushBuffer
.flushReturn_0:
cmp byte [rdi], '\'
je .asciiReplacement
cmp byte [rdi], '%'
je .argReplacement
mov r8b, [rdi]
@@ -152,6 +158,32 @@ printf:
inc rdi
jmp .process
;-- Replace special characters --;
.asciiReplacement:
cmp byte [rdi + 1], EOS
je .wrapup
mov r8, NL
cmp byte [rdi + 1], 'n'
cmove r9, r8
je .replaceAscii
mov r8, TAB
cmp byte [rdi + 1], 't'
cmove r9, r8
je .replaceAscii
jmp .invalidReplacement
.replaceAscii:
mov r8b, r9b
mov r9b, 2
cmp r10, printfBuffLen-1
je .flushBuffer
.flushReturn_2:
mov [printfBuff + r10], r8b
add rdi, 2
inc r10
jmp .process
;-- Replace specifiers --;
.argReplacement:
cmp byte [rdi + 1], EOS
je .wrapup
@@ -216,6 +248,7 @@ printf:
je .rep_s
;--- Invalid ---;
.invalidReplacement:
mov r9w, word [rdi]
mov [printfBuff+r10], r9w
add rdi, 2
@@ -346,6 +379,8 @@ printf:
je .flushReturn_0
cmp r9b, 1
je .flushReturn_1
cmp r9b, 2
je .flushReturn_2
.wrapup:
mov rax, NR_write