; ======================================================================
; CS617 CHAPTER 03 - DERIVED NATIVE FP MODULUS
;
; BCD_MOD is NEW project composition, not donor code.
;
; r001 contract:
;   input A: $10-$18, nonnegative integer-valued native FP
;   input B: $0-$8, positive integer-valued native FP
;   output : $10-$18 = A mod B
;
; Definition used:
;   A mod B = A - FIX(A / B) * B
;
; Dependencies:
;   BCD_DIV  accepted Chapter 03 native division wrapper
;   &H0A82   low-ROM FIX service (truncate FP toward zero)
;   BCD_MUL  accepted Chapter 03 native multiplication wrapper
;   BCD_SUB  accepted Chapter 03 native subtraction wrapper
;
; Provenance boundary:
;   This routine is derived Chapter 03 code. No donor-byte identity claim
;   is made for BCD_MOD itself.
; ======================================================================
BCD_MOD:
; Save original A ($10-$18), then original B ($0-$8).
        PHSM    $18,8
        PHS     $10
        PHSM    $8,8
        PHS     $0

; q = FIX(A / B), left in $10-$18.
        CAL     BCD_DIV
        CAL     &H0A82

; Restore B into $0-$8 and form q*B in $10-$18.
        PPS     $0
        PPSM    $1,8
        CAL     BCD_MUL

; Move q*B to operand B, restore original A, and subtract.
        LDM     $0,$10,8
        LD      $8,$18
        PPS     $10
        PPSM    $11,8
        CAL     BCD_SUB
        RTN
