IGNORED

# BCD issue

## Recommended Posts

This is a dumb question I'm sure , but I am having trouble with a BCD calculation.

I am using the Collect Tutorial 2 digit score display , which uses BCD encoded values.

In the past , all of the numbers I have used have been obtainable by

sed

clc

lda numberbcd

adc #10   ( some fixed value)

sta numberbcd

But now I am trying to subtract two variables and store as BCD. No combination I have tried works.   ( having both BCD before arithmetic or converting after )

Any help would be appreciated !

ES

##### Share on other sites

44 minutes ago, easmith said:

sed

clc

lda numberbcd

adc #10   ( some fixed value)

sta numberbcd

Not exactly your question, but I figured I'd point out that for BCD operations you use the hex representations of the decimal numbers. So "ADC #\$10" instead of "ADC 10". The latter will mess up your BCD math for any operand greater than 9. (since \$0=0, \$1=1, ..., \$9=9)

Subtraction with BCD math isn't any more complicated than addition (use SEC instead of CLC), so I'm wondering if your problem isn't with the issue I've mentioned above.

##### Share on other sites

Yup...gotta be \$hex values only.   Decimal mode then treats \$00 - \$99 as base 10 values 0 - 99.  You also cannot use any other method other than ADC and SBC when doing your math (unless you know for a fact that it won't result in a carry or borrow from either column).

##### Share on other sites

What about subtracting RAM variables x and y ?

##### Share on other sites

Variables, Rom data, registers, doesn't matter.  They need to contain numeric digits only when being used for BCD.  Those that hold values only up to 9 could be safe in certain cases when BCD is not invoked (when it's guaranteed that a carry or borrow isn't going to happen to/from the 1's column in the result).

##### Share on other sites

If X or Y do contain \$hex values, you'd need to convert them to decimal before BCD arithmetic.  It might be easier to use a conversion table in Rom for such a case.  It's wiser just not to use those specific variables for anything BUT decimal mode, so no conversion is necessary.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.