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

@@ -1,5 +1,7 @@
%include "src/constants.asm"
extern clamp
section .bss
cnvtBuff resb 67
cnvtBuffRev resb 67
@@ -178,24 +180,30 @@ hex2str:
lea rax, [rel cnvtBuff]
ret
;----- bin2str(uint) -----;
;----- bin2str(uint, padLen, bool padZeroes) -----;
; Converts an unsigned integer to a string (binary output)
; Return value: Pointer to string containing the converted integer
; Used registers:
; rax* uint stored for div >> (ret) pointer to cnvtBuff[]
; rcx* Counts length of created string
; rdx* modulo as calculated by div >> character storage for cnvtBuff
; rdx* (arg) If padLen>0, use Zeroes (rdx!=0) or spaces (rdx=0) >> modulo as calculated by div >> character storage for cnvtBuff
; rdi* (arg) integer to convert to string
; rsi* Points to cnvtBuff for writing characters
; rsi* (arg) Padding length
; r8* Dividor for div
; r9* Points to cnvtBuff for writing characters
; r10* padZeroes arg (since rdx used by DIV)
bin2str:
lea rsi, [rel cnvtBuffRev]
mov r10, ' '
mov r9, '0'
test rdx, rdx
cmovnz r10, r9
lea r9, [rel cnvtBuffRev]
test rdi, rdi
jnz .notZero
lea rsi, [rel cnvtBuff]
mov word [rsi], '0b'
mov byte [rsi + 2], '0'
mov byte [rsi + 3], EOS
lea r9, [rel cnvtBuff]
mov word [r9], '0b'
mov byte [r9 + 2], '0'
mov byte [r9 + 3], EOS
jmp .quit
.notZero:
@@ -206,20 +214,38 @@ bin2str:
mov r8, 2
div r8
add rdx, '0'
mov [rsi], dl
inc rsi
mov [r9], dl
inc r9
inc rcx
test rax, rax
jnz .convert
mov word [rsi], 'b0'
add rcx, 2
inc rsi
sub rsi, rcx
cmp rsi, rcx
jl .noPadding
mov r8, rcx
push rsi
push rdx
mov rdi, rsi
xor rsi, rsi
mov rdx, 64
call clamp
pop rdx
pop rsi
mov rcx, rax
.paddingLoop:
mov byte [r9], r10b
inc r9
inc r8
loop .paddingLoop
mov rcx, r8
.noPadding:
dec r9
lea rdi, [rel cnvtBuff]
.makeStringLoop:
mov al, [rsi]
mov al, [r9]
mov [rdi], al
inc rdi
dec rsi
dec r9
loop .makeStringLoop
mov byte [rdi], EOS