6502 Shorts - BCD to binary. By Lee Davison
[Back]
I'd not thought about this until recently when I needed it. Here's what I came up with....The code.
I don't think this will win any prizes but it works (which is always a bonus). The byte temp can be anywhere but is probably best in page 0.
TAX ; copy BCD value AND #$F0 ; mask top nibble LSR ; /2 (/16*8) STA temp ; save it LSR ; /4 (/16*4) LSR ; /8 (/16*2) ADC temp ; add /2 (carry always clear) ; ((n/16*8)+(n/16*2) = (n/16*10)) STA temp ; save it TXA ; get original back AND #$0F ; mask low nibble ADC temp ; add shifted (carry always clear)This is seventeen bytes long (assuming temp is in page 0) which makes it just about the right size for this section.
There is no error checking and it is up to the calling routine to ensure that the source byte is a valid BCD byte. If it isn't nothing bad happens, you just get the sum of the low nibble and the high nibble times ten.