PIC Microcontoller Radix Math Method

Binary to ASCII, 16 bit to 5 digits (1 at a time) no temp register (!)

; by Rich Leggitt with tweaks by Scott Dattalo and bugfix by Dmitry Kiryashov and Nikolai Golovchenko
; given 16 bit data in HI and LO, extract decimal digits
; requires one Output register called temp, HI and LO are destroyed.
; 42 instructions and less than 269 (or 252 with known_zero) instructions executed
	clr	temp
        skp
sub10k	inc	temp
	mov	W, #10000 &	; 255
	sub	LO, W

;Scott Dattalo says:
;If you have a ram location that's known to be zero, then
;the following [the IF] can be replaced with [the ELSE]
IFNDEF known_zero
	mov	W, #10000 >	;> 8
	sb	C
;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;        addlw 1         ; this sucks
	mov	Hack, W
	mov	W, #1	; this sucks
	add	W, Hack
	sub	HI, W
ELSE
	mov	W, <<known_zero
;*** WARNING: Manual replacement required for "SUBLW k" instruction (w = k - w). Check if previous instruction is a skip instruction. 
	sublw   (10000>>8)+1	;bugfix by Dmitry Kiryashov and Nikolai Golovchenko
	sub	Hi, W
ENDIF
;*** WARNING: MPASM macro BC is not supported yet. Replace manually.
        bc sub10k               ;9*7=63/8*7=56 inst in loop for 60900 (worst)
        output(temp);

	mov	W, #10
	mov	temp, W
add1K	dec	temp
	mov	W, #1000 &	; 255
	add	LO, W

;Scott Dattalo says:
;If you have a ram location that's known to be zero, then
;the following [the IF] can be replaced with [the ELSE]
IFNDEF known_zero
	mov	W, #1000 >	;> 8
	snb	C
;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;        addlw 1
	mov	Hack, W
	mov	W, #1
	add	W, Hack
	add	HI, W
ELSE
	mov	W, <<known_zero
;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;	addlw 1000 >> 8
	mov	Hack, W
	mov	W, #1000 >	;> 8
	add	W, Hack
	add	HI, W
ENDIF
;*** WARNING: MPASM macro BNC is not supported yet. Replace manually.
        bnc add1k               ;9*10=90/8*10=80 inst in loop for 60900
        output(temp);

;Scott takes over here
	clr	temp
	mov	W, #100
	skp            ;is this a valid macro? I'd write goto $+2 ...
sub100
	inc	temp
	sub	LO, W
	sb	C
	jmp	sub100

	dec	HI
	sb	HI.7	;Check msb instead of carry for underflow.
	jmp	sub100	;4 inst per loop to 200 then 7 per loop to 900.
			;Total 64(?) in loop for worst case

;at this point, HI = 0xff, and  0 <= LO <= 99

        output(temp)

	mov	W, #10
	mov	temp, W
add10	dec	temp
	add	LO, W
;*** WARNING: MPASM macro BNC is not supported yet. Replace manually.
        bnc add10               ;40 inst in loop for worst case.
        output(temp);
        output(LO);
	ret