From 68ee2828c1d47038c195e5c7b9943807a9c9d47e Mon Sep 17 00:00:00 2001 From: Kwarde Date: Thu, 31 Jul 2025 18:22:26 +0200 Subject: [PATCH] Make fetching args from stack (__INTERNAL_fmt) bit clearer --- src/console.asm | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/console.asm b/src/console.asm index 6f64a4a..3c9c2c0 100644 --- a/src/console.asm +++ b/src/console.asm @@ -128,6 +128,9 @@ eprintf: ; To achieve that, pass ~RAX (thus ~pointer_to_array) and push length of array to the stack (from within function wrapper). See format() __INTERNAL_fmt: %macro load_arg 1 + %define OFFSET_SHIFT_NONE (RBP_OFFSET_CALLER) ; offset from rbp to get pushed args from original caller + %define OFFSET_SHIFT_FD (RBP_OFFSET_CALLER*2 + SIZE_QWORD) ; ^same, but account for function wrapper (which has func prologue and CALL to __INTERNAL_fmt) + %define OFFSET_SHIFT_MEM (RBP_OFFSET_CALLER*2 + SIZE_QWORD*4) ; ^same, but account for arg len (of array passed to rax) pushed to stack (+ stack alignment) cmp r14, 4 ja %%fromStack mov %1, [__fmt_Args + SIZE_QWORD * r14] @@ -137,16 +140,13 @@ __INTERNAL_fmt: jle %%stackNoShift test rbx, rbx jnz %%stackShift_format - mov %1, [rbp + (RBP_OFFSET_CALLER*2 + SIZE_QWORD) + ((r14-5) * SIZE_QWORD)] ;Offset when called from wrapper like fwrite() (shifted args, output is to FD) + mov %1, [rbp + OFFSET_SHIFT_FD + ((r14 - 5) * SIZE_QWORD)] jmp %%continue %%stackShift_format: - mov %1, [rbp + (RBP_OFFSET_CALLER*2 + SIZE_QWORD*4) + ((r14-5) * SIZE_QWORD)] ;Offset when called from wrapper like format() (shifted args, output is memory; extra push (+stack offset) for length of array) - ;Not entirely sure why I need to do SIZE_QWORD*4 though, should be *3 (because of one push and one stack offset: 16 bytes) - ;At %%fromStack I was also not sure why (according to comment at description: '+8 to account for possible R9 arg becoming a stack arg'); this does not apply - ;It 1*8 is needed it checks out: 1 + 2 + that extra 1 = 4. Still not sure where the extra 8 is coming from. + mov %1, [rbp + OFFSET_SHIFT_MEM + ((r14 -5) * SIZE_QWORD)] jmp %%continue %%stackNoShift: - mov %1, [rbp + RBP_OFFSET_CALLER + ((r14-5) * SIZE_QWORD)] ;Offset when called from wrapper like printf (no shifted args) + mov %1, [rbp + OFFSET_SHIFT_NONE + ((r14 - 5) * SIZE_QWORD)] %%continue: %endmacro