From a4855c3b75934ce678c3212071aeaa096ee78bd9 Mon Sep 17 00:00:00 2001 From: Kwarde Date: Tue, 15 Jul 2025 10:45:01 +0200 Subject: [PATCH] printf: Macro process_arg to decrease repeated code --- src/console.asm | 74 +++++++++++++++---------------------------------- 1 file changed, 22 insertions(+), 52 deletions(-) diff --git a/src/console.asm b/src/console.asm index 4c1ae52..cabb2ea 100644 --- a/src/console.asm +++ b/src/console.asm @@ -120,6 +120,20 @@ printf: add rsp, SIZE_QWORD %endmacro + %macro process_arg 3 ;process_arg [base] [upper/lowercase (r8)] [itoa/utoa] + load_arg rsi + push_regs + mov rdi, rsi + mov rsi, %1 + mov rdx, r12 + mov rcx, r13 + mov r8, %2 + call %3 + mov rsi, rax + pop_regs + jmp .insertString + %endmacro + ; entry: push rbp mov rbp, rsp @@ -275,71 +289,27 @@ printf: ;--- '%i' / '%d' / '%u' ---; .rep_d: - load_arg rsi - push_regs - mov rdx, r12 - mov rcx, r13 cmp byte [rdi + 1], 'u' - je .callUTOA - mov rdi, rsi - mov rsi, 10 - call itoa - jmp .pr - .callUTOA: - mov rdi, rsi - mov rsi, 10 - mov r8, 0 - call utoa - .pr: - mov rsi, rax - pop_regs - jmp .insertString + je .unsigned + process_arg 10, 0, itoa + .unsigned: + process_arg 10, 0, utoa ;--- '%x' / '%X' ---; .rep_x: - load_arg rsi - push_regs - mov rdx, r12 - mov rcx, r13 - mov r8, 0 cmp byte [rdi + 1], 'x' je .lower - mov r8, 1 + process_arg 16, 1, utoa .lower: - mov rdi, rsi - mov rsi, 16 - call utoa - mov rsi, rax - pop_regs - jmp .insertString + process_arg 16, 0, utoa ;--- '%b' ---; .rep_b: - load_arg rsi - push_regs - mov rdi, rsi - mov rsi, 2 - mov rdx, r12 - mov rcx, r13 - mov r8, 0 - call utoa - mov rsi, rax - pop_regs - jmp .insertString + process_arg 2, 0, utoa ;--- '%o' ---; .rep_o: - load_arg rsi - push_regs - mov rdi, rsi - mov rsi, 8 - mov rdx, r12 - mov rcx, r13 - mov r8, 0 - call utoa - mov rsi, rax - pop_regs - jmp .insertString + process_arg 8, 0, utoa ;--- '%s' ---; .rep_s: