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

View File

@@ -104,7 +104,7 @@ section .rodata
printf11 db TAB,"printf(",DQUO,"%X | %8X | %08X\n",DQUO,", 0xAB0F, 0xBA0F, 0xAB0F): ",NL,TAB,TAB,EOS
printf11Str db "%X | %8X | %08X",NL,EOS
printf12 db TAB,"rax=0x1234567890ABCDEF",NL,TAB,"printf(",DQUO,"\nRAX = %064b\nEAX = %64b\n AX = %56b \n AH = %64b\n AL = %64b\n",DQUO,", rax, eax, ax, ah, al): ",NL,TAB,TAB,EOS
printf12Str db NL,"RAX = %064b",NL,"EAX = %64b",NL," AX = %64b",NL," AH = %56b ",NL," AL = %64b",NL,EOS
printf12Str db "\nRAX = %064b\nEAX = %64b\n AX = %64b\n AH = %56b\t\n AL = %64b\n",EOS
; strlen()
msgStrlen db NL,"TEST strlen()",NL,EOS
@@ -834,6 +834,7 @@ _start:
call printf
; TEST 6
lea rdi, [rel str1]
lea rsi, [rel str5]
call strcmp