printf(): Add %c, add test for %c and args on stack

This commit is contained in:
2025-07-08 13:03:17 +02:00
parent 3b1ce7ca40
commit 4e7f086bff
2 changed files with 57 additions and 10 deletions

View File

@@ -59,6 +59,7 @@ puts:
; Return value: Amount of printed characters
; Supported specifiers:
; %% Literal percentage sign
; %c Single character
; %s String
; <!> 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
@@ -112,6 +113,8 @@ printf:
je .wrapup
cmp byte [rdi + 1], '%'
je .rep_pct
cmp byte [rdi + 1], 'c'
je .rep_c
cmp byte [rdi + 1], 's'
je .rep_s
@@ -125,11 +128,19 @@ printf:
;--- '%%' ---;
.rep_pct:
mov [printfBuff+r10], byte '%'
add rdi, 2
add r10, 1
add r11, 1
jmp .process
mov sil, '%'
dec rdx
jmp .insertChar
;--- '%c' ---;
.rep_c:
cmp rdx, 4
ja .c_fromStack
mov rsi, [printfArgs + SIZE_QWORD * rdx]
jmp .insertChar
.c_fromStack:
mov rsi, [rbp + RBP_OFFSET_CALLER + ((rdx-5) * SIZE_QWORD)]
jmp .insertChar
;--- '%s' ---;
.rep_s:
@@ -159,6 +170,15 @@ printf:
add rdi, 2
jmp .process
;--- Insert char to buffer ---;
.insertChar:
mov [printfBuff + r10], sil
add rdi, 2
add r10, 1
add r11, 1
inc rdx
jmp .process
.flushBuffer:
push rdi
push rsi

View File

@@ -16,11 +16,11 @@ extern strclr
section .rodata
TEST_print equ 0
TEST_puts equ 0
TEST_printf equ 0
TEST_printf equ 1
TEST_strlen equ 0
TEST_strcpy equ 0
TEST_strcat equ 0
TEST_strclr equ 1
TEST_strclr equ 0
str1 db "Hello, world!",EOS
str2 db "Hello, World!",EOS
@@ -42,6 +42,17 @@ section .rodata
printf2Str db "Are %s doing %s?",NL,EOS
printf2Str1 db "you",EOS
printf2Str2 db "okay",EOS
printf3 db TAB,"printf(",DQUO,"%c%c%c%c%c there %c%c %s%c\n",DQUO,"): ",NL,TAB,TAB,EOS
printf3Str db "%c%c%c%c%c there %c%c %s%c",NL,EOS
printf3C1 equ 'H'
printf3C2 equ 'e'
printf3C3 equ 'l'
printf3C4 equ 'l'
printf3C5 equ 'o'
printf3C6 equ 'm'
printf3C7 equ 'y'
printf3Str1 db "friend",EOS
printf3C8 equ '!'
; strlen()
strlenStr1 db "Hello",EOS
@@ -114,6 +125,22 @@ _start:
lea rsi, [rel printf2Str1]
lea rdx, [rel printf2Str2]
call printf
; TEST 3
lea rdi, [rel printf3]
call print
lea rdi, [rel printf3Str]
mov rsi, printf3C1
mov rdx, printf3C2
mov rcx, printf3C3
mov r8, printf3C4
mov r9, printf3C5
push printf3C8
push printf3Str1
push printf3C7
push printf3C6
call printf
add rsp, SIZE_QWORD * 4
%endif
;---