Add support for '%%' in printf()

This commit is contained in:
2025-06-25 14:21:46 +02:00
parent 1e095bec77
commit 5ead6ebdb1
2 changed files with 10 additions and 2 deletions

View File

@ -46,7 +46,7 @@ puts:
leave leave
ret ret
;----- printf(const char* string, ...) -----; ;----- printf(const char* string, ...) -----;
; Currently only supports specifiers: %d, %c, %s ; Currently only supports specifiers: %d, %c, %s, %%
; Return value: Amount of printed characters ; Return value: Amount of printed characters
printf: printf:
push rbp push rbp
@ -71,6 +71,8 @@ printf:
push rdi push rdi
cmp byte [rdi+1], 'd' cmp byte [rdi+1], 'd'
je .rep_d je .rep_d
cmp byte [rdi+1], '%'
je .rep_pct
cmp byte [rdi+1], 'c' cmp byte [rdi+1], 'c'
je .rep_c je .rep_c
cmp byte [rdi+1], 's' cmp byte [rdi+1], 's'
@ -124,6 +126,12 @@ printf:
mov rsi, rax mov rsi, rax
jmp .insertLoop jmp .insertLoop
;--- %% ---;
.rep_pct:
mov rdi, '%'
dec r10 ;r10 is increased at the end, but not needed because '%%' requires no arg
jmp .charToStr
;--- %c ---; ;--- %c ---;
.rep_c: .rep_c:
cmp r10, 0 cmp r10, 0

View File

@ -70,7 +70,7 @@ section .rodata
msgPuts db NL,NL,"# puts()",NL,EOS msgPuts db NL,NL,"# puts()",NL,EOS
msgPuts1 db "puts() test",EOS msgPuts1 db "puts() test",EOS
; printf() ; printf()
testStr db "Testing: %s, %s, %s, %s, %s, %s, %s, %s, %d, %s",NL,EOS testStr db "Testing: %s, %%, %s, %s, %s, %s, %s, %s, %s, %d, %s",NL,EOS
tS1 db "one",EOS tS1 db "one",EOS
tS2 db "two",EOS tS2 db "two",EOS
tS3 db "three",EOS tS3 db "three",EOS