Fix printf args always being first arg when using padding, improved/added tests. NEW FOUND BUG: full qword byte misses 3 characters when using padding

This commit is contained in:
2025-07-13 15:03:59 +02:00
parent 1971c5b897
commit 483d9185de
2 changed files with 70 additions and 44 deletions

View File

@@ -79,7 +79,7 @@ puts:
; rax* (ret) amount of printed characters
; rdi* (arg) pointer to format[] to format and print >> pointer to buffer
; rsi* (optional arg) >> Used for inserting strings to buffer
; rdx* (optional arg) >> Keeps track of amount of processed format specifiers
; rdx* (optional arg) >> misc
; rcx* (optional arg) >> Stores arg for %x/%X
; r8* (optional arg) >> Used for moving characters
; r9* (optional arg) >> Keeps track of where to jump after flushing buffer
@@ -87,21 +87,20 @@ puts:
; r11* Keeps track of total characters (return value)
; r12 Padding for numbers (0=nopadding, >0=padding w/ spaces, NOT >0=padding w/ zeroes)
; r13 Padding: zeroes (=1) or spaces (=0)
; r14 Keeps track of amount of processed format specifiers
printf:
%macro load_arg 1
cmp rdx, 4
cmp r14, 4
ja %%fromStack
mov %1, [printfArgs + SIZE_QWORD * rdx]
mov %1, [printfArgs + SIZE_QWORD * r14]
jmp %%continue
%%fromStack:
mov %1, [rbp + RBP_OFFSET_CALLER + ((rdx-5) * SIZE_QWORD)]
mov %1, [rbp + RBP_OFFSET_CALLER + ((r14-5) * SIZE_QWORD)]
%%continue:
%endmacro
%macro push_regs 0
sub rsp, SIZE_QWORD
push rax
push rdx
push rdi
push r9
push r10
@@ -111,9 +110,7 @@ printf:
pop r10
pop r9
pop rdi
pop rdx
pop rax
add rsp, SIZE_QWORD
%endmacro
; entry:
@@ -121,6 +118,8 @@ printf:
mov rbp, rsp
push r12
push r13
push r14
sub rsp, SIZE_QWORD
cmp byte [rdi], EOS
je .emptyStr
@@ -135,6 +134,7 @@ printf:
xor rdx, rdx
xor r10, r10
xor r11, r11
xor r14, r14
.process:
cmp byte [rdi], EOS
@@ -226,7 +226,7 @@ printf:
;--- '%%' ---;
.rep_pct:
mov sil, '%'
dec rdx
dec r14
jmp .insertChar
;--- '%c' ---;
@@ -280,7 +280,6 @@ printf:
;--- '%b' ---;
.rep_b:
cmp rdx, 4
load_arg rsi
push_regs
mov rdi, rsi
@@ -310,8 +309,10 @@ printf:
inc r11
jmp .insertString
.endInsertString:
inc rdx
inc r14
add rdi, 2
xor r12, r12
xor r13, r13
jmp .process
;--- Insert char to buffer ---;
@@ -320,7 +321,9 @@ printf:
add rdi, 2
add r10, 1
add r11, 1
inc rdx
xor r12, r12
xor r13, r13
inc r14
jmp .process
.flushBuffer:
@@ -358,6 +361,8 @@ printf:
xor rax, rax
.quit:
add rsp, SIZE_QWORD
pop r14
pop r13
pop r12
leave