; Hex to BCD
; converts a hex number (eg. $10) to its BCD representation (eg. $16).
; Input: a = hex number
; Output: a = BCD number
; Clobbers: b,c
HexToBCD:
   ld c,a  ; Original (hex) number
   ld b,8  ; How many bits
   xor a   ; Output (BCD) number, starts at 0
-: sla c   ; shift c into carry
   adc a,a
   daa     ; Decimal adjust a, so shift = BCD x2 plus carry
   djnz -  ; Repeat for 8 bits



Return to top
0.108s