Random Terrain, on Fri Sep 10, 2010 10:33 PM, said:
How can I make sure a variable is always a
BCD compliant number? The following will work at the beginning, but as the number grows, the score will be incorrect or become garbled as you would expect:
a = a + $05 : score = score + a
A for-next loop will solve the problem:
a = a + $05 : for temp5 = 1 to a : score = score + 1 : next
But is that the best solution? Is there some special magic with AND or OR that could be done to make sure the variable is always a BCD compliant number as long as it is less than 100?
Thanks.
I don't know what you're doing but making sure "a" is BCD compliant may not be enough.
You may have to make "a" BCD. That is treat, it as BCD.
If you never add more than 6 to "a" then it's relatively simple, mask off the digits
individually and test if they're greater than 9 and if a digit is greater than 9 add
6 to it, starting with the ones digit. If you add more than 6 it starts getting messy.
If you do add more than 6 there's the possibility of a carry out of the ones digit which
you'd have to detect independent of what ever you add to the tens digit.
Probably be easiest to treat the digits seperately, ie add the digits one at a time,
add the ones digit then adjust, then add the tens digit and adjust.
I'm not that familiar with batari Basic but it would be something like:
if (a & $0F) > 9 then a = a + 6
if (a & $F0) > $90 then a = a + $60
Edit:
That code is just to make "a" BCD compliant
(it doesn't convert binary to BCD)
Here's code to add "n" to "a" (I hope

)
"n" doesn't (necessarily) need to be BCD
(if "n" is a single hex digit 0-F it should work)
Not sure if this is proper batari Basic
temp = (a & $0F) + (n & $0F) : REM seperate ones digit and add them
: REM result in temp so they can be
: REM decimal adjusted seperately
if temp > 9 then temp = temp + 6 : REM decimal adjust the ones digit
a = (a & $F0) + temp + (n & $F0) : REM add it all together
if a > $9F then a = a + $60 : REM decimal adjust the tens digit
Edited by bogax, Sat Sep 11, 2010 2:46 AM.