From 1c5ba65f49cb786d38b0ed919bbc485487535f6b Mon Sep 17 00:00:00 2001 From: Kwarde Date: Thu, 17 Jul 2025 14:56:35 +0200 Subject: [PATCH] Correctly set padding length when using prefix (fixes #1) --- src/convert.asm | 8 +++++++- src/tests.asm | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/convert.asm b/src/convert.asm index b182b72..3045b04 100644 --- a/src/convert.asm +++ b/src/convert.asm @@ -256,7 +256,6 @@ utoa: test r15, r15 ;prefix now? (padding with spaces; r15 is zero) jnz .checkPadding insert_prefix - add r14, 2 .checkPadding: ; Substract length of converted number from padLen @@ -264,6 +263,13 @@ utoa: cmp r14, 0 jle .checkPrefix mov rcx, r14 + test r13, r13 + jns .addPadding + test r15, r15 + jz .addPadding + sub rcx, 2 + cmp rcx, 2 + jl .checkPrefix .addPadding: mov byte [r10], r11b inc r10 diff --git a/src/tests.asm b/src/tests.asm index d07f20a..09c04a6 100644 --- a/src/tests.asm +++ b/src/tests.asm @@ -103,6 +103,8 @@ section .rodata printf14Str db "%#b | %#08b | %#x | %#8X | %#o\n",EOS printf15 db TAB,"printf(",DQUO,"%p | %#p | %8p | %#8p | %08p | %#08p\n",DQUO,", str5, str5, str5, str5, strBuff1, strBuff1): ",NL,TAB,TAB,EOS printf15Str db "%p | %#p | %8p | %#8p | %08p | %#08p\n",EOS + printf16 db TAB,"printf(",DQUO,"%x | %#10x | %10x | %#10x | %010x | %#010x\n",DQUO,", str5, str5, str5, str5, strBuff1, strBuff1): ",NL,TAB,TAB,EOS + printf16Str db "%x | %#10x | %10x | %#10x | %010x | %#010x\n",EOS ; strlen() msgStrlen db NL,"TEST strlen()",NL,EOS strlen1 db TAB,"strlen(str1): %d",NL,EOS @@ -411,6 +413,20 @@ _start: push strBuff1 call printf add rsp, SIZE_QWORD * 2 + + ; TEST 16 + lea rdi, [rel printf16] + call print + lea rdi, [rel printf16Str] + lea rsi, [rel str5] + lea rdx, [rel str5] + lea rcx, [rel str5] + lea r8, [rel str5] + lea r9, [rel strBuff1] + sub rsp, SIZE_QWORD + push strBuff1 + call printf + add rsp, SIZE_QWORD * 2 %endif ;---