Adds bin2str() and printf() format specifier %b

This commit is contained in:
2025-07-10 12:19:54 +02:00
parent e0bd8bcdbd
commit 902d962068
3 changed files with 125 additions and 20 deletions

View File

@@ -1,12 +1,13 @@
%include "src/constants.asm"
section .bss
cnvtBuff resb 21
cnvtBuffRev resb 21
cnvtBuff resb 67
cnvtBuffRev resb 67
section .text
global dec2str
global udec2str
global hex2str
global bin2str
;----- dec2str(int) -----;
; Converts a signed integer to a string (decimal output)
@@ -176,3 +177,52 @@ hex2str:
.quit:
lea rax, [rel cnvtBuff]
ret
;----- bin2str(uint) -----;
; 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
; rdi* (arg) integer to convert to string
; rsi* Points to cnvtBuff for writing characters
; r8* Dividor for div
bin2str:
lea rsi, [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
jmp .quit
.notZero:
mov rax, rdi
xor rcx, rcx
.convert:
xor rdx, rdx
mov r8, 2
div r8
add rdx, '0'
mov [rsi], dl
inc rsi
inc rcx
test rax, rax
jnz .convert
mov word [rsi], 'b0'
add rcx, 2
inc rsi
lea rdi, [rel cnvtBuff]
.makeStringLoop:
mov al, [rsi]
mov [rdi], al
inc rdi
dec rsi
loop .makeStringLoop
mov byte [rdi], EOS
.quit:
lea rax, [rel cnvtBuff]
ret