From ebca30bb0ad44603627c6486d4fdf122189e3466 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Tue, 6 Aug 2024 14:22:43 +0800 Subject: [PATCH] replace %d with 666 kinda works --- src/macros.asm | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/macros.asm b/src/macros.asm index 23f3e2a..0508787 100644 --- a/src/macros.asm +++ b/src/macros.asm @@ -50,11 +50,12 @@ ; printdf [format str] [int] push_registers + ; TODO: do not segfault when we write out of those bounds + %define MAX_PRINTF_LEN 2048 + ; rax = is null terminated format str str_to_stack %1 - %define MAX_PRINTF_LEN 10 - sub rsp, MAX_PRINTF_LEN ; r10 is index in final output string mov r10, 0 @@ -62,7 +63,7 @@ lea r11, [rbp-MAX_PRINTF_LEN] mov rcx, 0 -.printf_fmt_char_loop: +%%printf_fmt_char_loop: mov r9b, byte [rax+rcx] inc rcx @@ -71,8 +72,31 @@ mov byte [r11+r10], r9b inc r10 + cmp r9b, '%' + jne %%printf_fmt_char_loop_check_repeat + +%%printf_fmt_char_loop_got_percentage: + mov r9b, byte [rax+rcx] + cmp r9b, 'd' + jne %%printf_fmt_char_loop_check_repeat + +%%printf_fmt_char_loop_got_fmt_d: + + ; overwrite the %d + dec r10 + inc rcx + + mov byte [r11+r10], '6' + inc r10 + mov byte [r11+r10], '6' + inc r10 + mov byte [r11+r10], '6' + inc r10 + +%%printf_fmt_char_loop_check_repeat: + cmp r9b, 0 - jne .printf_fmt_char_loop + jne %%printf_fmt_char_loop ; print output buffer printn r11, r10 @@ -80,6 +104,10 @@ ; frees stack string ; and copy buffer mov rsp, rbp + + ; TODO: support \n escape sequence + call print_newline + pop_registers %endmacro