[stella] Fw: Hex to BCD routine

Subject: [stella] Fw: Hex to BCD routine
From: "Andrew Davie" <adavie@xxxxxxxxxxxxx>
Date: Mon, 12 Feb 2001 18:18:42 +1100
Here's a routine for Hex to BCD that Kevin Horton sent to me... Enjoy!

From: "Kevin Horton" <khorton@xxxxxxxxxx>

> ---Original E-mail follows---
>
> 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
>
>
>

--
 _  _  _| _ _        _| _    * _                               _  ,
(_|| )(_|( (/_\/\/  (_|(_|\/(_(/_                           ,~' L_|\
                                                         ,-'        \
see my Museum of Soviet Calculators at                  (            \
http://www.taswegian.com/MOSCOW/soviet.html              \    __     /
                                                          L,~'  "\__/
                                                              @--> v


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

Current Thread