Robiąc miernik pojemności musiałem dokonać konwersji 32bitowej liczby bez znaku na ASCII. Ponieważ był to mały procek z 2KB flash nie chciałem sięgać po floaty, bo mogłoby to zapełnić flash.
Algorytm tak mi się spodobał, że przerobiłem go do wersji na C64 raptem 112 bajtów:
;ACME compilator
!to "convert.prg" , CBM
divisor = $02
var1 = $fb
var2 = $fc
var3 = $fd
var4 = $fe
*= $0801
;BASICLINE
!byte $0B,$08,$90,$06,$9E,$32
!byte $30,$34,$39,$00,$A0,$00
;//-----------------------------------------------------------------------------:
;// 32bit Unsigned convert BIN to ASCII
;//---------------
BIN32u_2_ASCII
ldx #3
- lda long,x
sta var1,x
dex
bpl -
ldx #10
stx divisor ;// divide by 10
dex ;// instead ldx #9
BIN_TO_ASCII:
;//------------------------------------------------------------
;//--- divide unsigned 32bit value by 8bit unsigned divisor ---
;//------------------------------------------------------------
div32u_by_8u
lda #$00
ldy #32 ;// lc = 32 (times bits to shift)
divloop ;// simple 32bit_u divide by const 8bit_u
asl var1
rol var2
rol var3
rol var4
rol
cmp divisor ;// divisor value
bcc divlp2
inc var1
sbc divisor ;// divisor value
divlp2
dey
bne divloop
;------------------------------------------
;//---------------------------
ora #$30 ;// O_o HERE we gotta our the rest of div10 :] o_O
sta OUT_ASCII_BUFF,x ;// store as ASCII NUMBER
dex
bpl BIN_TO_ASCII
inx ;// instead ldx #0
clr_lead lda OUT_ASCII_BUFF, x
cmp #$30
bne skip
cpx #9
beq put
lda #$20
put sta OUT_ASCII_BUFF,x
inx
cpx #10
bne clr_lead
skip
jsr pr_cr
ldx #0
print_it
lda OUT_ASCII_BUFF,x
cmp #$20
beq no_print
jsr $ffd2
no_print inx
cpx #10
bne print_it
pr_cr lda #$0d
jmp $ffd2
;//----------------------------
long
!byte $ff,$ff,$ff,$ff
;//-----
OUT_ASCII_BUFF