printf: Adds specifiers %d and %u

This commit is contained in:
2025-07-08 17:37:18 +02:00
parent 72409116f5
commit cd565f3bfa
2 changed files with 58 additions and 1 deletions

View File

@@ -1,6 +1,8 @@
%include "src/constants.asm" %include "src/constants.asm"
extern strlen extern strlen
extern int2str
extern uint2str
section .rodata section .rodata
mNL db NL mNL db NL
@@ -60,6 +62,8 @@ puts:
; Supported specifiers: ; Supported specifiers:
; %% Literal percentage sign ; %% Literal percentage sign
; %c Single character ; %c Single character
; %d Signed integer, printed as decimal
; %u Unsigned integer, printed as decimal
; %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
@@ -68,7 +72,7 @@ puts:
; rdi* (arg) pointer to format[] to format and print ; rdi* (arg) pointer to format[] to format and print
; rsi* (optional arg) >> Used for inserting strings to buffer ; rsi* (optional arg) >> Used for inserting strings to buffer
; rdx* (optional arg) >> Keeps track of amount of processed format specifiers ; rdx* (optional arg) >> Keeps track of amount of processed format specifiers
; rcx (optional arg) ; rcx* (optional arg)
; r8* (optional arg) >> Used for moving characters ; r8* (optional arg) >> Used for moving characters
; r9* (optional arg) >> Keeps track of where to jump after flushing buffer ; r9* (optional arg) >> Keeps track of where to jump after flushing buffer
; r10* Keeps track of current index of printfBuff ; r10* Keeps track of current index of printfBuff
@@ -115,6 +119,10 @@ printf:
je .rep_pct je .rep_pct
cmp byte [rdi + 1], 'c' cmp byte [rdi + 1], 'c'
je .rep_c je .rep_c
cmp byte [rdi + 1], 'd'
je .rep_d
cmp byte [rdi + 1], 'u'
je .rep_d
cmp byte [rdi + 1], 's' cmp byte [rdi + 1], 's'
je .rep_s je .rep_s
@@ -142,6 +150,39 @@ printf:
mov rsi, [rbp + RBP_OFFSET_CALLER + ((rdx-5) * SIZE_QWORD)] mov rsi, [rbp + RBP_OFFSET_CALLER + ((rdx-5) * SIZE_QWORD)]
jmp .insertChar jmp .insertChar
;--- '%d' / '%u' ---;
.rep_d:
cmp rdx, 4
ja .d_fromStack
mov rsi, [printfArgs + SIZE_QWORD * rdx]
jmp .checkINTorUINT
.d_fromStack:
mov rsi, [rbp + RBP_OFFSET_CALLER + ((rdx-5) * SIZE_QWORD)]
.checkINTorUINT:
push rax
push rcx
push rdx
push rdi
;push rsi
push r8
cmp byte [rdi + 1], 'd'
je .callINT2STR
mov rdi, rsi
call uint2str
jmp .loadConvertedStr
.callINT2STR:
mov rdi, rsi
call int2str
.loadConvertedStr:
mov rsi, rax
pop r8
;pop rsi
pop rdi
pop rdx
pop rcx
pop rax
jmp .insertString
;--- '%s' ---; ;--- '%s' ---;
.rep_s: .rep_s:
cmp rdx, 4 cmp rdx, 4

View File

@@ -56,6 +56,8 @@ section .rodata
printf3C7 equ 'y' printf3C7 equ 'y'
printf3Str1 db "friend",EOS printf3Str1 db "friend",EOS
printf3C8 equ '!' printf3C8 equ '!'
printf4 db TAB,"printf(",DQUO,"%d|%u , %d|%u\n",DQUO,", -50, -50, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF): ",NL,TAB,TAB,EOS
printf4Str db "%d|%u , %d|%u",NL,EOS
; strlen() ; strlen()
strlenStr1 db "Hello",EOS strlenStr1 db "Hello",EOS
@@ -117,6 +119,10 @@ _start:
call int2str call int2str
mov rdi, -569384 mov rdi, -569384
call uint2str call uint2str
mov rdi, 0xFFFFFFFFFFFFFFFF
call int2str
mov rdi, 0xFFFFFFFFFFFFFFFF
call uint2str
%endif %endif
;--- ;---
@@ -155,6 +161,16 @@ _start:
push printf3C6 push printf3C6
call printf call printf
add rsp, SIZE_QWORD * 4 add rsp, SIZE_QWORD * 4
; TEST 4
lea rdi, [rel printf4]
call print
lea rdi, [rel printf4Str]
mov rsi, -50
mov rdx, -50
mov rcx, 0xFFFFFFFFFFFFFFFF
mov r8, 0xFFFFFFFFFFFFFFFF
call printf
%endif %endif
;--- ;---