Jump to content





Hex to BCD conversion (0-99) Part Trois

Posted by Omegamatrix, in 6502 Math Routines 01 June 2015 · 677 views

Hex BCD Hex Conversion
I've covered some Hex to Decimal conversions like 0-255 and 0-65535 already in my blog, and have had done some previous solutions to 0-99 as well. Today I was thinking about converting a single byte from a hex value to BCD (0-99) once again. I came up with this routine which is the shortest, simplest, and fastest yet.
;Hex2Bcd (good 0-99)
;22 bytes, 26 cycles
    tay              ;2  @2
    lsr              ;2  @4
    lsr              ;2  @6
    lsr              ;2  @8
    lsr              ;2  @10
    tax              ;2  @12
    tya              ;2  @14
    sed              ;2  @16
    clc              ;2  @18
    adc   #0         ;2  @20
    adc   BcdTab,X   ;4  @24
    cld              ;2  @26



BcdTab:
    .byte $00,$06,$12,$18,$24,$30,$36
The code can be modified to preserve Y by using PHA/PLA, but that will cost you more cycles of course.
 
 
This routine is quite similar to the one I left in the comments of Hex to Decimal (0-99) part deux. I was able to eliminate the AND #$0F, and that made all the difference. In the code the ADC #0 corrects the low nibble to BCD if the low nibbles value is greater than nine. Looking at the BcdTab you can see that it increases in BCD values of six. This is used to correct the final value with the high nibble, and that is pretty much it. :)




Fastest is a 100 byte lookup table, but I guess you know that.

  • Report

Fastest is a 100 byte lookup table, but I guess you know that.

Yep. What I'm aiming for in almost all of my routines is the best solution without using a lot of bytes, but still maintaining the speed. The best examples of this are in my Unsigned Integer Division Routines for the 6502.

  • Report

August 2018

S M T W T F S
   1234
567891011
12131415161718
19 20 2122232425
262728293031 

Recent Comments

Latest Visitors

0 user(s) viewing

0 members, 0 guests, 0 anonymous users