I recently posted a bit of HC11 assembly code for converting unpacked BCD to binary, and I thank those that took time to look at it and for the replacement code that someone offered.
I figure the code that does the opposite is probably just as inefficient, so here we go.
; Convert binary data to BCD values
bin2BCD LDX f2BCD
LDAB f2bytes
LDAA #$00
func02_1 STAA $00,X
DEX
DECB
BNE func02_1
INX
STX f2BCD
LDAA #$80
STAA f2bitpos
func02_2 LDX f2binary
LDAA $00,X
PSHA
DEX
STX f2binary
func02_3 PULA
PSHA
ANDA f2bitpos
BEQ func02_5
LDX f2BCD
LDAB f2bytes
LDAA $00,X
ADDA #$01
func02_4 DAA
STAA $00,X
DECB
BEQ func02_5
INX
LDAA $00,X
ADCA #$00
BRA func02_4
func02_5 LDAA f2bits
CMPA #$01
BEQ func02_7
LDX f2BCD
LDAB f2bytes
LDAA $00,X
ADDA $00,X
func02_6 DAA
STAA $00,X
DECB
BEQ func02_7
INX
LDAA $00,X
ADCA $00,X
BRA func02_6
func02_7 DEC f2bits
BEQ func02_8
LSR f2bitpos
BNE func02_3
PULA
LDAA #$80
STAA f2bitpos
BRA func02_2
func02_8 PULA
RTS
As I mentioned in the other thread, this routine has a few prerequisites before being called:
f2binary - 16-bit address of the source binary data
f2BCD - 16-bit address of the packed BCD output (these are stored in the same sequence as the readout as mentioned in the other thread; 7/6, 5/4, 3/2, 1/0)
f2bytes - I assume this is a byte counter (only 2 values are used: 2 or 4)
f2bits - I assume this is a bit counter (only 2 values are used: 8 or 24 - corresponding to above values)