From cd565f3bfac64c7c625de518f1e44f589d1f9980 Mon Sep 17 00:00:00 2001 From: Kwarde Date: Tue, 8 Jul 2025 17:37:18 +0200 Subject: [PATCH] printf: Adds specifiers %d and %u --- src/console.asm | 43 ++++++++++++++++++++++++++++++++++++++++++- src/tests.asm | 16 ++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/console.asm b/src/console.asm index df3dae6..286980a 100644 --- a/src/console.asm +++ b/src/console.asm @@ -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 diff --git a/src/tests.asm b/src/tests.asm index b7b23e2..2ce7eb2 100644 --- a/src/tests.asm +++ b/src/tests.asm @@ -56,6 +56,8 @@ section .rodata printf3C7 equ 'y' printf3Str1 db "friend",EOS 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() strlenStr1 db "Hello",EOS @@ -117,6 +119,10 @@ _start: call int2str mov rdi, -569384 call uint2str + mov rdi, 0xFFFFFFFFFFFFFFFF + call int2str + mov rdi, 0xFFFFFFFFFFFFFFFF + call uint2str %endif ;--- @@ -155,6 +161,16 @@ _start: push printf3C6 call printf 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 ;---