Jump to content
  • entries
    17
  • comments
    28
  • views
    32,096

Hex to BCD conversion (0-99) Part Trois


Omegamatrix

1,295 views

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. :)

  • Like 1

2 Comments


Recommended Comments

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...