Hex to BCD conversion (0-99)
I can't sleep. I'm consumed by this problem and I keep thinking about it...
Tonight I discovered some of Batari's solutions for hex to bcd conversion here and here. They are very compact solutions, and neat. I started to wonder if a constant (un-looped) cycle approach could be found without using tables. Then the insomnia crept in. I decided to have a go at translating hex values of 0-99 (BCD range for a byte).
The approach I took was to modify my divide by 10 routine so that the result was multiplied by 6, and then added it to the initial value. From there I optimized it so that some of the shifts could be cut out, and removed the correction value (adc #13) from the divide routine, because that is only required for values 130 or more (and we are just doing 0-99).
Here is what I came up with:
;Hex2Bcd (good 0-99) ;24 bytes, 39 cycles ;You can also use LSR for all ROR's here... sta temp lsr adc temp ror lsr lsr adc temp ror adc temp ror lsr and #$3C sta temp2 lsr adc temp2 adc temp
I wrote a real short verification program. Basically just a macro that pumps out all the results and stuffs them into zero page ram, which can easily been seen in Stella's debugger. I did a visual verification.
- 4
3 Comments
Recommended Comments