Decimal Subtraction

Each 4 bit data quantity may be treated as a decimal number as long as it represents one of the decimal digits 0 through 9. The TCS (transfer carry subtract) and DAA (decimal adjust accumulator) may be used to subtract two decimal numbers and produce a decimal number. The TCS instruction permits subtraction of multi-digit decimal numbers.

The process consists of generating the ten’s complement of the subtrahend digit (the difference between the subtrahend digit and 10 decimal), and adding the result to the minuend digit. For instance, to subtract 2 from 7, the ten’s complement of 2 (10 - 2 = 8) is added to 7, producing 15 decimal which, when truncated to a 4 bit quantity gives 5 (the required result). If a borrow was generated by the previous subtraction, the 9’s complement of the subtrahend digit is produced to compensate for the borrow.

In detail, the procedure for subtracting one multi-digit decimal number from another is as follows:

1 Set the carry bit to 1 indicating no borrow.

2 Use the TCS instruction to set the accumulator to either 9 or 10 decimal.

3 Subtract the subtrahend digit from the accumulator, producing either the 9’ s or 10’ s complement.

4 Set the carry bit to 0.

5 Add the minuend digit to the accumulator.

6 Use the DAA instruction to make sure the result in the accumulator is in decimal format, and to indicate a borrow in the carry bit if one occurred.

7 Save this resuIt.

8 If there are more digits to subtract, goto step 2, otherwise stop.

Example: Perform the decimal subtraction

5 1
3 8 -
---
1 3

1 Set the carry bit to 1

2 TCS sets accumulator = 1010b and carry = O

3 Subtract the subtrahend digit 8 from the accumulator

Accumulator   =   1 0 1 0
        ~ 8   =   0 1 1 1
    ~ Carry   =         1
                ---------
Result            0 0 1 0

4 Set the carry bit to 0

5 Add minuend digit 1 to accumulator

Accumulator   =   0 0 1 0
          1   =   0 1 1 1
      Carry   =         0
                ---------
Result            0 0 1 1
Carry         0

6 DAA leaves accumulator = 3 = first digit of result, and carry = 0, indicating that a borrow occurred

7 TCS sets accumulator =1001B and carry = 0

8 Subtract the subtrahend digit 3 from the accumulator

Accumulator   =   1 0 0 1
        ~ 3   =   1 1 0 0
    ~ Carry   =         1
                ---------
Result            0 1 1 0

9 Set carry = 0

10 Add minuend digit 5 to accumulator

Accumulator   =   0 1 1 0
          5   =   0 1 0 1
      Carry   =         0
                ---------
Result            1 0 1 1
Carry         0

11 DAA adds 6 to accumulator and sets carry = 1, indicating that no borrow occurred.

Accumulator   =   1 0 1 1
          6   =   0 1 1 0
                ---------
Result            0 0 0 1
Carry         1

Therefore the result of subtracting 38 from 51 is 13.

Example Code (subroutine)

The following subroutine will subtract one 16 digit decimal number from another, using the following assumptions.

  • The minuend is stored least significant digit first in DATA RAM chip 0, register O.

  • The subtrahend is stored least significant digit first in DATA RAM chip 0, register 1.

  • The result will be stored least significant digit first in DATA RAM chip 0, register 0 replacing the minuend.

  • Index register 8 will count the number of digits (up to 16) which have been subtracted.

SD,     FIM      2P      0       / REG PAIR 2P = RAM CHIP 0, REG 0
        FIM      3P      16      / REG PAIR 3P = RAM CHIP 0

        CLB
        XCH      8               / SET DIGIT COUNTER = 0
        STC                      / SET CARRY = 1
SD1,    TCS                      / ACCUMULATOR = 9 OR 10
        SRC     3P               / SELECT RAM REG 1
        SBM                      / PRODUCE 9's OR 10's COMPLEMENT
        CLC                      / SET CARRY = 0
        SRC     2P               / SELECT RAM REG 0
        ADM                      / ADD MINUEND TO ACCUMULATOR
        DAA                      / ADJUST ACCUMULATOR
        WRM                      / WRITE RESULT TO REG 0
        INC     5                / ADDRESS NEXT CHARACTER OF RAM REG 0
        INC     7                / ADDRESS NEXT CHARACTER OF RAM REG 1
        ISZ     8       SD1      / BRANCH IF DIGIT COUNTER < 16 (NON-ZERO)
DN      BBL     0

Glossary

Term

Definition

minuend

The number that the subtrahend is being subtracted from

subtrahend

The number that is being subtracted