printf(): Start adding padding support (right now just detect it and calculate padding length)

This commit is contained in:
2025-07-11 00:04:30 +02:00
parent 13d14f7a1f
commit 73e3918d39
2 changed files with 55 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
%include "src/constants.asm"
extern printf
section .rodata
msg db "%133769666b",NL,EOS
section .text
global _start
_start:
lea rdi, [rel msg]
mov rsi, 6
call printf

View File

@@ -83,6 +83,7 @@ puts:
; r9* (optional arg) >> Keeps track of where to jump after flushing buffer
; 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)
printf:
%macro load_arg 1
cmp rdx, 4
@@ -147,6 +148,49 @@ printf:
je .wrapup
cmp byte [rdi + 1], '%'
je .rep_pct
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
cmp byte [rdi + 1], '1' ;zero padding later!
jb .noPadding
cmp byte [rdi + 1], '9'
jg .noPadding
inc rdi ;point to after '%'
xor rcx, rcx ;length of padding number (set to 0 first)
.findPaddingNumLen:
cmp byte [rdi], EOS
je .wrapup
cmp byte [rdi], '0'
jb .getPadding
cmp byte [rdi], '9'
ja .getPadding
inc rcx
inc rdi
jmp .findPaddingNumLen
.getPadding:
dec rdi ;dont break .noPadding below (for cmp rdi+1)
push rdi
push rax
xor rax, rax
xor r12, r12 ;total len
mov r9, 1
.getPaddingLoop:
xor rax, rax
mov al, byte [rdi]
sub al, '0'
imul r9
add r12, rax
dec rdi
mov rax, r9
mov r9, 10
imul r9
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)
.noPadding:
cmp byte [rdi + 1], 'c'
je .rep_c
cmp byte [rdi + 1], 'i'
@@ -298,5 +342,6 @@ printf:
xor rax, rax
.quit:
pop r12
leave
ret