printf: Macro process_arg to decrease repeated code

This commit is contained in:
2025-07-15 10:45:01 +02:00
parent 351f8058f9
commit a4855c3b75

View File

@@ -120,6 +120,20 @@ printf:
add rsp, SIZE_QWORD add rsp, SIZE_QWORD
%endmacro %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: ; entry:
push rbp push rbp
mov rbp, rsp mov rbp, rsp
@@ -275,71 +289,27 @@ printf:
;--- '%i' / '%d' / '%u' ---; ;--- '%i' / '%d' / '%u' ---;
.rep_d: .rep_d:
load_arg rsi
push_regs
mov rdx, r12
mov rcx, r13
cmp byte [rdi + 1], 'u' cmp byte [rdi + 1], 'u'
je .callUTOA je .unsigned
mov rdi, rsi process_arg 10, 0, itoa
mov rsi, 10 .unsigned:
call itoa process_arg 10, 0, utoa
jmp .pr
.callUTOA:
mov rdi, rsi
mov rsi, 10
mov r8, 0
call utoa
.pr:
mov rsi, rax
pop_regs
jmp .insertString
;--- '%x' / '%X' ---; ;--- '%x' / '%X' ---;
.rep_x: .rep_x:
load_arg rsi
push_regs
mov rdx, r12
mov rcx, r13
mov r8, 0
cmp byte [rdi + 1], 'x' cmp byte [rdi + 1], 'x'
je .lower je .lower
mov r8, 1 process_arg 16, 1, utoa
.lower: .lower:
mov rdi, rsi process_arg 16, 0, utoa
mov rsi, 16
call utoa
mov rsi, rax
pop_regs
jmp .insertString
;--- '%b' ---; ;--- '%b' ---;
.rep_b: .rep_b:
load_arg rsi process_arg 2, 0, utoa
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
;--- '%o' ---; ;--- '%o' ---;
.rep_o: .rep_o:
load_arg rsi process_arg 8, 0, utoa
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
;--- '%s' ---; ;--- '%s' ---;
.rep_s: .rep_s: