From e718e1534bdd89601c325461888f3b6cf07c2ffb Mon Sep 17 00:00:00 2001 From: Kwarde Date: Tue, 15 Jul 2025 16:39:19 +0200 Subject: [PATCH] printf: Add format specifier %p --- src/console.asm | 9 +++++++++ src/tests.asm | 16 ++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/console.asm b/src/console.asm index 701243e..cc23377 100644 --- a/src/console.asm +++ b/src/console.asm @@ -69,6 +69,8 @@ puts: ; %X Unsigned integer, printed as hexadecimal number (uppercase) ; %b Unsigned integer, printed as binary number ; %o Unsigned integer, printed as octal number +; %p Print pointer, but truly alias for %#x -- this printf/udec processes all arguments as qwords so both %x and %p (and other specifiers) will always print full true value. +; That is also why %l/%ll is not supported - it's not needed. ; %s String ; %#{x} Use prefix for printed number (non-decimal). Supported specifiers: x, X, b, o. Works in combination with width specifier (see below) ; %[#]n* Pad left, n chars (maximum 64). Supported specifiers: d, i, u, x, X, b, o @@ -281,6 +283,8 @@ printf: je .rep_b cmp byte [rdi + 1], 'o' je .rep_o + cmp byte [rdi + 1], 'p' + je .rep_p cmp byte [rdi + 1], 's' je .rep_s @@ -329,6 +333,11 @@ printf: .rep_o: process_arg 8, 0, utoa + ;--- '%p' ---; + .rep_p: + mov r15, 1 ;always force prefix, no matter if %p or %#p was used. Do not override padding though + process_arg 16, 0, utoa + ;--- '%s' ---; .rep_s: load_arg rsi diff --git a/src/tests.asm b/src/tests.asm index daf35a6..76427a3 100644 --- a/src/tests.asm +++ b/src/tests.asm @@ -99,9 +99,10 @@ section .rodata printf12Str db "\nRAX\t%064b\nEAX\t\t\t\t\t%032b\n AX\t\t\t\t\t\t\t%016b\n AH\t\t\t\t\t\t\t%08b\t\n AL\t\t\t\t\t\t\t\t%08b\n",EOS printf13 db TAB,"printf(",DQUO,"%o | %8o | %08o\n",DQUO,", 1500, 1500, 1500): ",NL,TAB,TAB,EOS printf13Str db "%o | %8o | %08o\n",EOS - printf14 db TAB,"printf(",DQUO,"%#b | %#08b | %#x | %#8X\n | %#o",DQUO,", 8, 8, 0xABCDEF, 0x12345678, 15): ",NL,TAB,TAB,EOS + printf14 db TAB,"printf(",DQUO,"%#b | %#08b | %#x | %#8X | %#o\n",DQUO,", 8, 8, 0xABCDEF, 0x12345678, 15): ",NL,TAB,TAB,EOS printf14Str db "%#b | %#08b | %#x | %#8X | %#o\n",EOS - + printf15 db TAB,"printf(",DQUO,"%p | %#p | %8p | %08p | %#08p\n",DQUO,", 0xabc, 0xabc, 0xabc, 0xabc, 0xabc): ",NL,TAB,TAB,EOS + printf15Str db "%p | %#p | %8p | %08p | %#08p\n",EOS ; strlen() msgStrlen db NL,"TEST strlen()",NL,EOS strlen1 db TAB,"strlen(str1): %d",NL,EOS @@ -396,6 +397,17 @@ _start: mov r8, 0x12345678 mov r9, 15 call printf + + ; TEST 15 + lea rdi, [rel printf15] + call print + lea rdi, [rel printf15Str] + mov rsi, 0xabc + mov rdx, 0xabc + mov rcx, 0xabc + mov r8, 0xabc + mov r9, 0xabc + call printf %endif ;---