The following code subtracts one number (ARG1) from another (ARG2), both of which are packed BCD format and of equal (but varying) length. The result is stored in the location of the minuend (HL):
BCDPAK: LD B,COUNT ;LENGTH OF TWO ARGUMENTS LD HL,ARG1 ;ADDRESS OF MINUEND LD DE,ARG2 ;ADDRESS OF SUBTRAHEND AND A ;CLEAR CARRY MINUS: LD A,(DE) ;SUBTRAHEND TO ACCUMULATOR SBC A (HL) ;SUBTRACT (HL) FROM ACCUMULATOR DAA ;ADJUST RESULT TO DECIMAL CODED VALUE LD (HL),A ;STORE RESULT INC DE ;ADVANCE MEMORY POINTERS INC HL DJNZ MINUS ;DEC B, LOOP UNTIL B = 0
Source: Adapted from Zaks' Programming the Z80