bcdconvert
;This converts hex1:hex0 to bcd. The top half of hex1 must be zero
;the result is in bcd1:bcd0
;The maximum output is 4096
clrf bcd1
movlw 0x0f
subwf hex1, w
bnz comp_4000
movlw 0xa0
subwf hex0, w
comp_4000
bnc no_4000
sub_4000
bsf bcd1, 6
movlw 0xa0
subwf hex0, f
movlw 0x0f
subwfb hex1, f
no_4000
movlw 0x07
subwf hex1, w
bnz comp_2000
movlw 0xd0
subwf hex0, w
comp_2000
bnc no_2000
sub_2000
bsf bcd1, 5
movlw 0xd0
subwf hex0, f
movlw 0x07
subwfb hex1, f
no_2000
movlw 0x03
subwf hex1, w
bnc no_800 ;as 800 and 1000 share 3 as the first hex digit
bnz comp_1000
movlw 0xe8
subwf hex0, w
comp_1000
bnc no_1000
sub_1000
bsf bcd1, 4
movlw 0xe8
subwf hex0, f
movlw 0x03
subwfb hex1, f
no_1000
bcd_convert_1000
movlw 0x03
subwf hex1, w
bnz comp_800
movlw 0x20
subwf hex0, w
comp_800
bnc no_800
sub_800
bsf bcd1, 3
movlw 0x20
subwf hex0, f
movlw 0x03
subwfb hex1, f
no_800
movlw 0x01
subwf hex1, w
bnz comp_400
movlw 0x90
subwf hex0, w
comp_400
bnc no_400
sub_400
bsf bcd1, 2
movlw 0x90
subwf hex0, f
movlw 0x01
subwfb hex1, f
bcd_convert_400
no_400 ;the top byte can only be 0 or 1 here
btfsc hex1, 0
bra sub_200
bcd_convert_256
movlw 0xc8
subwf hex0, w
bnc no_200
sub_200
bsf bcd1, 1
movlw 0xc8
subwf hex0, f
movlw 0x00
subwfb hex1, f
bcd_convert_200
no_200 ;the top byte can be ignored from now on
movlw 0x64
subwf hex0, w
bnc no_100
bsf bcd1, 0
movlw 0x64
subwf hex0
no_100
movf hex0, w
bcd_convert_w
;if we multiply by d'51' and add d'52', and divide by 2, the msb is the number divided by 10
movwf hex0
mullw d'51'
movlw d'52'
addwf prodl, f
movlw 0x00
addwfc prodh, f ;this should clear the carry bit
rrcf prodh, w ;now w is the input/10
movwf bcd0
mullw d'10'
movf prodl, w
subwf hex0, f
swapf bcd0, w
iorwf hex0, w
movwf bcd0
return