Megalextoria
Retro computing and gaming, sci-fi books, tv and movies and other geeky stuff.

Home » Digital Archaeology » Computer Arcana » Commodore » Commodore 8-bit » Assembly Question; BCD to Decimal
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Assembly Question; BCD to Decimal [message #199532] Tue, 14 April 2009 15:45 Go to next message
JB is currently offline  JB
Messages: 42
Registered: January 2013
Karma: 0
Member
I am trying to convert a BCD # into a regular 1 byte number in
assembly with no luck so far.

Ex. if the BCD # was 24 I want to have the decimal equivilant in say
location $2 which would equal in hex #$18

Any help is appreciated.
Re: Assembly Question; BCD to Decimal [message #199534 is a reply to message #199532] Tue, 14 April 2009 16:55 Go to previous messageGo to next message
Dombo is currently offline  Dombo
Messages: 210
Registered: December 2011
Karma: 0
Senior Member
JB schreef:
> I am trying to convert a BCD # into a regular 1 byte number in
> assembly with no luck so far.
>
> Ex. if the BCD # was 24 I want to have the decimal equivilant in say
> location $2 which would equal in hex #$18
>
> Any help is appreciated.

You would have to implement the following formula:

bin = 10*(upper_BCD_nibble) + lower_BCD_nibble

My 6502 is a bit rusty (after 20 years), but I'll give it a try:

LDA $02 ; Read BCD number from $02
TAX ; Save copy in X
AND #$0F ; Extract lower nibble from BCD
STA $02 ; And store it at $02
TXA ; Put BCD number back in accumulator
AND #$F0 ; Extract upper nibble from BCD
LSR ; Accumulator equals now 8*upper_BCD_nibble
TAX ; Save it for later
CLC
ADC $02 ; Add it to the number at $02
STA $02
TXA ; Restore accumulator to 8*upper_BCD_nibble
LSR
LSR ; Accumulator equals now 2*upper_BCD_nibble
CLC
ADC $02 ; Add it to the number at $02
STA $02 ; $02 now contains the binary representation
RTS


See also: http://6502.org/source/integers/32bcdbin.htm
Re: Assembly Question; BCD to Decimal [message #199535 is a reply to message #199534] Tue, 14 April 2009 18:40 Go to previous messageGo to next message
Anonymous
Karma:
Originally posted by: mrtinbspam@gmail.com

On 14 Apr., 22:55, Dombo <do...@disposable.invalid> wrote:
> JB schreef:
>
>> I am trying to convert a BCD # into a regular 1 byte number in
>> assembly with no luck so far.
>
>> Ex.  if the BCD # was 24 I want to have the decimal equivilant in say
>> location $2 which would equal in hex #$18
>
>> Any help is appreciated.
>
> You would have to implement the following formula:
>
>         bin = 10*(upper_BCD_nibble) + lower_BCD_nibble
>
> My 6502 is a bit rusty (after 20 years), but I'll give it a try:
>
>         LDA $02         ; Read BCD number from $02
>         TAX             ; Save copy in X
>         AND #$0F        ; Extract lower nibble from BCD
>         STA $02         ; And store it at $02
>         TXA             ; Put BCD number back in accumulator
>         AND #$F0        ; Extract upper nibble from BCD
>         LSR             ; Accumulator equals now 8*upper_BCD_nibble
>         TAX             ; Save it for later
>         CLC            
>         ADC $02         ; Add it to the number at $02
>         STA $02
>         TXA             ; Restore accumulator to 8*upper_BCD_nibble
>         LSR
>         LSR             ; Accumulator equals now 2*upper_BCD_nibble
>         CLC
>         ADC $02         ; Add it to the number at $02
>         STA $02         ; $02 now contains the binary representation
>         RTS
>
> See also:http://6502.org/source/integers/32bcdbin.htm

You might also read the 3-part article which can be downloaded from
GEnie Commodore File Library. It is a very thorough explanasion of
everything BCD on 6502.

The files are:
#10733 BCD.PART1.ARC Binary Coded Decimals tutorial.
#10708 BCD.PART2.ARC Part II on binary coded decimals.
#10703 BCD.PART3.ARC BCD part III tutorial.

Files can be downloaded at <http://cbmfiles.com/genie/
InformationListing.html>. The files are archived with ARC, and is most
easily unarchived with cbmconvert at <http://zimmers.net/anonftp/pub/
cbm/crossplatform/converters/unix/index.html>.

mrtinb
Denmark
Re: Assembly Question; BCD to Decimal [message #199536 is a reply to message #199535] Tue, 14 April 2009 20:18 Go to previous messageGo to next message
JB is currently offline  JB
Messages: 42
Registered: January 2013
Karma: 0
Member
On Apr 14, 6:40 pm, "mrtinbs...@gmail.com" <mrtinbs...@gmail.com>
wrote:
> On 14 Apr., 22:55, Dombo <do...@disposable.invalid> wrote:
>
>
>
>
>
>> JB schreef:
>
>>> I am trying to convert a BCD # into a regular 1 byte number in
>>> assembly with no luck so far.
>
>>> Ex.  if the BCD # was 24 I want to have the decimal equivilant in say
>>> location $2 which would equal in hex #$18
>
>>> Any help is appreciated.
>
>> You would have to implement the following formula:
>
>>         bin = 10*(upper_BCD_nibble) + lower_BCD_nibble
>
>> My 6502 is a bit rusty (after 20 years), but I'll give it a try:
>
>>         LDA $02         ; Read BCD number from $02
>>         TAX             ; Save copy in X
>>         AND #$0F        ; Extract lower nibble from BCD
>>         STA $02         ; And store it at $02
>>         TXA             ; Put BCD number back in accumulator
>>         AND #$F0        ; Extract upper nibble from BCD
>>         LSR             ; Accumulator equals now 8*upper_BCD_nibble
>>         TAX             ; Save it for later
>>         CLC            
>>         ADC $02         ; Add it to the number at $02
>>         STA $02
>>         TXA             ; Restore accumulator to 8*upper_BCD_nibble
>>         LSR
>>         LSR             ; Accumulator equals now 2*upper_BCD_nibble
>>         CLC
>>         ADC $02         ; Add it to the number at $02
>>         STA $02         ; $02 now contains the binary representation
>>         RTS
>
>> See also:http://6502.org/source/integers/32bcdbin.htm
>
> You might also read the 3-part article which can be downloaded from
> GEnie Commodore File Library. It is a very thorough explanasion of
> everything BCD on 6502.
>
> The files are:
> #10733 BCD.PART1.ARC Binary Coded Decimals tutorial.
> #10708 BCD.PART2.ARC Part II on binary coded decimals.
> #10703 BCD.PART3.ARC BCD part III tutorial.
>
> Files can be downloaded at <http://cbmfiles.com/genie/
> InformationListing.html>. The files are archived with ARC, and is most
> easily unarchived with cbmconvert at <http://zimmers.net/anonftp/pub/
> cbm/crossplatform/converters/unix/index.html>.
>
> mrtinb
> Denmark- Hide quoted text -
>
> - Show quoted text -

Thanks, I appreciate the help. Got it working!
Re: Assembly Question; BCD to Decimal [message #199544 is a reply to message #199534] Wed, 15 April 2009 12:15 Go to previous message
Anonymous
Karma:
Originally posted by: bogaxg

Dombo wrote:
>
> My 6502 is a bit rusty (after 20 years), but I'll give it a try:
>
> LDA $02 ; Read BCD number from $02
> TAX ; Save copy in X
> AND #$0F ; Extract lower nibble from BCD
> STA $02 ; And store it at $02
> TXA ; Put BCD number back in accumulator
> AND #$F0 ; Extract upper nibble from BCD
> LSR ; Accumulator equals now 8*upper_BCD_nibble
> TAX ; Save it for later
> CLC
> ADC $02 ; Add it to the number at $02
> STA $02
> TXA ; Restore accumulator to 8*upper_BCD_nibble
> LSR
> LSR ; Accumulator equals now 2*upper_BCD_nibble
> CLC
> ADC $02 ; Add it to the number at $02
> STA $02 ; $02 now contains the binary representation
> RTS
>
>
> See also: http://6502.org/source/integers/32bcdbin.htm

OK rusty ;)
nothing really wrong with your code but I can't resist nipicking ,,
you don't need the clc's since you just got through shifting zero(s)
in to the carry
(and personally I'd use the stack instead of messing up x unless
cycles were
really tight in which case it might be better to use a zero page
location)
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Run-time GEOS module?
Next Topic: Compilers used for Quetzal?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Thu Apr 25 01:09:32 EDT 2024

Total time taken to generate the page: 0.06843 seconds