• entries
17
28
• views
32,071

# BCD to Binary

196 views

Tonight I was scrubbing my blogs to reformat all the mangled code block entries and thought about some code I made in @Karl G's topic about BCD to Binary Routines.

Here is the last routine I made:

```; BCD value \$0 - \$99 is in A. Returns binary number 0-99 in A
BCDtoBin6:
sta Temp
and #\$F0
lsr
sta Temp2
lsr
lsr
sec
sbc Temp2
rts```

Here is @Andrew Davie's explaination of the routine:

Quote

I like this, very clever.
To explain for others...

bcd format \$XY = effectively 16X + Y (where X and Y are the decimal digits of the number)
we want decimal format XY = effectively 10X + Y
So we need to subtract 6X from the original number and we'll have our answer
The code first isolates X (and #\$F0) and then divides by 2, giving us 8X stored in Temp2
Then divides by 4, giving 2X, adds it to the original number so that's now (16+2)X + Y
And finally subtracts 8X (in Temp2), giving (16+2-8)X + Y
--> 10X + Y

Lovely.

• 1

There are no comments to display.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.