printf(): Allow padding (spaces or zeroes) for %b

This commit is contained in:
2025-07-12 07:48:21 +02:00
parent 971b1cbb86
commit ee17ec68e0
4 changed files with 88 additions and 36 deletions

View File

@@ -84,6 +84,7 @@ puts:
; r10* Keeps track of current index of printfBuff
; 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)
printf:
%macro load_arg 1
cmp rdx, 4
@@ -100,9 +101,13 @@ printf:
push rax
push rdx
push rdi
push r9
push r10
%endmacro
%macro pop_regs 0
pop r10
pop r9
pop rdi
pop rdx
pop rax
@@ -112,6 +117,8 @@ printf:
; entry:
push rbp
mov rbp, rsp
push r12
push r13
cmp byte [rdi], EOS
je .emptyStr
@@ -148,14 +155,16 @@ printf:
je .wrapup
cmp byte [rdi + 1], '%'
je .rep_pct
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
cmp byte [rdi + 1], '1' ;zero padding later!
xor r13, r13
mov r12, 1
cmp byte [rdi + 1], '0'
jb .noPadding
cmove r13, r12
cmp byte [rdi + 1], '9'
jg .noPadding
inc rdi ;point to after '%'
xor rcx, rcx ;length of padding number (set to 0 first)
xor r12, r12
xor rcx, rcx
inc rdi
.findPaddingNumLen:
cmp byte [rdi], EOS
je .wrapup
@@ -167,11 +176,10 @@ printf:
inc rdi
jmp .findPaddingNumLen
.getPadding:
dec rdi ;dont break .noPadding below (for cmp rdi+1)
dec rdi
push rdi
push rax
xor rax, rax
xor r12, r12 ;total len
mov r9, 1
.getPaddingLoop:
xor rax, rax
@@ -186,10 +194,10 @@ printf:
mov r9, rax
loop .getPaddingLoop
pop rax
pop rdi ;r12 should now store padding length
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; OK so at this point, padding (spaces, zeroes not yet supported) length is calculated correctly and string is replaced correctly.
; Now what remains is to actually add the spaces (and zeroes, eventually)
pop rdi
test r13, r13
jz .noPadding
not r12
.noPadding:
cmp byte [rdi + 1], 'c'
je .rep_c
@@ -270,6 +278,20 @@ printf:
load_arg rsi
push_regs
mov rdi, rsi
;
mov rdx, 0
test r12, r12
jz .callB2S
test r12, r12
js .notB2S
jmp .callB2S
.notB2S:
not r12
mov rdx, 1
jmp .callB2S
;
.callB2S:
mov rsi, r12
call bin2str
mov rsi, rax
pop_regs
@@ -342,6 +364,7 @@ printf:
xor rax, rax
.quit:
pop r13
pop r12
leave
ret