!pr2
!lm12
!rm75
Adding Decimal Values from ASCII Strings...Bob Sander-Cederlof

The program below shows a nifty way to add two decimal values together and get the result as an ASCII string, without ever converting decimal to binary or binary to decimal.

The example shows two six-character values being added, but any length would work the same.  For simplicity's sake I used a leading zero format, and allow no signs or decimal points.  Fancier features can wait for more cerebral times.

The beautiful part is the way the 6502's carry flag works.  On entering the add loop, I clear carry.  Then I add a pair of digits, preserving the ASCII code.  If the sum is more than "9" ($39), the CMP will leave carry set, prepared for subtracting 10 at line 1160.  After subtracting 10, carry will be set (because the SBC caused no borrow).  This carry then propagates to the next digit.

Strictly speaking, I should allow the sum to be one digit longer than the addend and augend strings, and store the final carry value there.  Any reasonably useful version would also allow leading blanks and decimal points, be callable as an &-routine with string parameters, automatically handle non-aligned decimal points, and allow negative numbers.  I'll try all these for next month.
