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 ; Return value: Amount of printed characters
; Supported specifiers: ; Supported specifiers:
; %% Literal percentage sign ; %% Literal percentage sign
; %c Single character
; %s String ; %s String
; <!> Unsupported specifiers are printed as-is ; <!> 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 ; <!> 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 je .wrapup
cmp byte [rdi + 1], '%' cmp byte [rdi + 1], '%'
je .rep_pct je .rep_pct
cmp byte [rdi + 1], 'c'
je .rep_c
cmp byte [rdi + 1], 's' cmp byte [rdi + 1], 's'
je .rep_s je .rep_s
@@ -125,11 +128,19 @@ printf:
;--- '%%' ---; ;--- '%%' ---;
.rep_pct: .rep_pct:
mov [printfBuff+r10], byte '%' mov sil, '%'
add rdi, 2 dec rdx
add r10, 1 jmp .insertChar
add r11, 1
jmp .process ;--- '%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' ---; ;--- '%s' ---;
.rep_s: .rep_s:
@@ -155,9 +166,18 @@ printf:
inc r11 inc r11
jmp .insertString jmp .insertString
.endInsertString: .endInsertString:
inc rdx inc rdx
add rdi, 2 add rdi, 2
jmp .process 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: .flushBuffer:
push rdi push rdi

View File

@@ -16,11 +16,11 @@ extern strclr
section .rodata section .rodata
TEST_print equ 0 TEST_print equ 0
TEST_puts equ 0 TEST_puts equ 0
TEST_printf equ 0 TEST_printf equ 1
TEST_strlen equ 0 TEST_strlen equ 0
TEST_strcpy equ 0 TEST_strcpy equ 0
TEST_strcat equ 0 TEST_strcat equ 0
TEST_strclr equ 1 TEST_strclr equ 0
str1 db "Hello, world!",EOS str1 db "Hello, world!",EOS
str2 db "Hello, World!",EOS str2 db "Hello, World!",EOS
@@ -42,6 +42,17 @@ section .rodata
printf2Str db "Are %s doing %s?",NL,EOS printf2Str db "Are %s doing %s?",NL,EOS
printf2Str1 db "you",EOS printf2Str1 db "you",EOS
printf2Str2 db "okay",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() ; strlen()
strlenStr1 db "Hello",EOS strlenStr1 db "Hello",EOS
@@ -114,6 +125,22 @@ _start:
lea rsi, [rel printf2Str1] lea rsi, [rel printf2Str1]
lea rdx, [rel printf2Str2] lea rdx, [rel printf2Str2]
call printf 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 %endif
;--- ;---