From 8c25b7913b345b42402fe3cceb782c5323827d3c Mon Sep 17 00:00:00 2001 From: Kwarde Date: Sun, 20 Jul 2025 22:51:01 +0200 Subject: [PATCH] printf(): Print null pointers (arg for %s = 0) as (null), fixes #4 --- src/console.asm | 17 +++++++++++++++++ src/tests.asm | 16 ++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/console.asm b/src/console.asm index 19feb2a..1f09794 100644 --- a/src/console.asm +++ b/src/console.asm @@ -359,6 +359,21 @@ printf: ;--- Insert string to buffer ---; .insertString: + test rsi, rsi + jnz .doInsertString + ;rsi = NULL: + mov r9b, 3 + cmp r10, printfBuffLen-7 + je .flushBuffer + .flushReturn_3: + mov byte [printfBuff + r10], '(' + mov qword [printfBuff + r10 + 1], 'null' + mov byte [printfBuff + r10 + 5], ')' + add r10, 6 + add r11, 6 + jmp .endInsertString + + .doInsertString: cmp byte [rsi], EOS je .endInsertString mov r9b, 1 @@ -409,6 +424,8 @@ printf: je .flushReturn_1 cmp r9b, 2 je .flushReturn_2 + cmp r9b, 3 + je .flushReturn_3 .wrapup: mov rax, NR_write diff --git a/src/tests.asm b/src/tests.asm index 5598764..b2cddf8 100644 --- a/src/tests.asm +++ b/src/tests.asm @@ -109,6 +109,8 @@ section .rodata printf16Str db "%x | %#10x | %10x | %#10x | %010x | %#010x\n",EOS printf17 db TAB,"printf(",DQUO,"Ran\dom in\v\a\li%\d test%\n",DQUO,"): ",NL,TAB,TAB,EOS printf17Str db "Ran\dom in\v\a\li%\d test%\n",EOS + printf18 db TAB,"printf(",DQUO,"atoi(12345, 9, 0, 0): %s <