Hex to BCD conversion (0-99) Part Deux
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.
-
1

1 Comment
Recommended Comments