[stella] Hex to BCD conversion routine

Subject: [stella] Hex to BCD conversion routine
From: Kevin Horton <khorton@xxxxxxxxxx>
Date: Sat, 10 Feb 2001 18:43:49 -0500
Here's my favourite hex to BCD routine for the 6502.  I use Table Assembler,
so if you think the code looks a bit funny, that's why. :-)

I've used this on all my 6502 projects and it works great and requires very little
space, execution time, and it requires no temp variable storage.


".block" is the directive to reserve 1 byte of memory.

;HEX2BCD.ASM
;How to use:
;
;Load your binary number between 0000 and ffff into inp0 and inp1.
;MSB goes into inp1.

;run conv_bcd and get your output in packed BCD format in bcd_out0-2
; bcd_out2 is MSB, only lower nybble is used.
;

bcd_out0:  .block 1
bcd_out1:  .block 1
bcd_out2:  .block 1
inp0:      .block 1
inp1:      .block 1


conv_bcd: ldx #16 lda #0 sta bcd_out0 sta bcd_out1 sta bcd_out2 ;clear result regs

b2bcd:     clc
           rol inp0
           rol inp1
           rol bcd_out2
           rol bcd_out1
           rol bcd_out0    ;rotate results
           dex
           beq bc_done
           ldy #bcd_out2
           jsr bcdfix
           ldy #bcd_out1
           jsr bcdfix
           ldy #bcd_out0
           jsr bcdfix
           jmp b2bcd

bcdfix:    lda 0,y
           clc
           adc #003h
           and #008h
           beq bcd_f2
           lda 0,y
           clc
           adc #003h
           sta 0,y

bcd_f2:    lda 0,y
           clc
           adc #030h
           and #080h
           beq bc_done
           lda 0,y
           clc
           adc #030h
           sta 0,y

bc_done: rts


- Archives (includes files) at http://www.biglist.com/lists/stella/archives/ Unsub & more at http://www.biglist.com/lists/stella/

Current Thread