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

Hex to BCD conversion (0-99) Part Deux


Omegamatrix

2,046 views

Continuing with Hex to BCD routines, tonight I took a look at using a hybrid table approach.

 

 

;Hex2Bcd (good 0-99)
;26 bytes, 30 cycles
    sta  temp        ;3  @3
    lsr              ;2  @5
    lsr              ;2  @7
    lsr              ;2  @9
    lsr              ;2  @11
    tax              ;2  @13
    lda  temp        ;3  @16
    and  #$0F        ;2  @18
    clc              ;2  @20
    sed              ;2  @22
    adc  #0          ;2  @24
    adc  BcdTab,X    ;4  @28
    cld              ;2  @30

BcdTab:
  .byte $00,$16,$32,$48,$64,$80,$96
 

 

 

The advantages of this routine is that it is 9 cycles quicker then what I wrote before, and uses 1 less temporary register. The disadvantages are that one of the index registers has to be used, and it takes 2 more bytes, but at 26 bytes that is mostly negligible.

 

 

The routine is straight forward. You look up the BCD conversion of the upper nibble value, and add it to a BCD conversion of the value of the lower nibble. In decimal mode it is easy to correct $0A-$0F to be become $10-$15 (using adc #0).

 

 

I like both routines I wrote, their use just depends on your need.

  • Like 1

1 Comment


Recommended Comments

I was thinking some more about this tonight, and if the user has Y register available then they can claw back 2 bytes and shave 2 cycles. This makes it the same amount of bytes as the original attempt, but also makes it the fastest routine yet (excluding a straight forward look-up table).

;Hex2Bcd (good 0-99)
;24 bytes, 28 cycles
    tay              ;2  @2
    lsr              ;2  @4
    lsr              ;2  @6
    lsr              ;2  @8
    lsr              ;2  @10
    tax              ;2  @12
    tya              ;2  @14
    and  #$0F        ;2  @16
    sed              ;2  @18
    clc              ;2  @20
    adc  #0          ;2  @22
    adc  BcdTab,X    ;4  @26
    cld              ;2  @28



BcdTab:
  .byte $00,$16,$32,$48,$64,$80,$96
Link to comment
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...