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

BCD to Binary


Omegamatrix

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
    adc Temp
    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.

 

  • Like 1

0 Comments


Recommended Comments

There are no comments to display.

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...