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