=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Date: Sat, 11 Mar 2000 20:25:36
From: Nikolai Golovchenko
To: pic microcontroller discussion list
Subject: Re: How to write code that no one will laugh at [OT]
--------------------------------------------------------------------------------
How about this one?
Saves 18 cycles for the first digit, 25 for the second, and 12 for the
fourth. Same(?) for the third. For the case without known_zero it saves
even more. The cost is bigger code.
Conditional compiling directives tested in MPLAB :) James, I can't
figure how you counted executed instructions?
Thanks Scott, your code is very instructive.
;Total execution time:
;worst case: 47 + 66 + 58 + 39 + 2 = 213 cycles(with known zero)
;worst case: 53 + 73 + 58 + 39 + 2 = 226 cycles(without known zero)
;Code size: 68
clrf temp
sub30k
movlw 3
addwf temp, f
movlw low(30000)
subwf Lo, f
IFNDEF known_zero
movlw high(30000)
skpc
movlw high(30000) + 1
subwf Hi, f
ELSE
rlf known_zero, w
sublw high(30000) + 1
subwf Hi, f
ENDIF
skpnc
goto sub30k
add10k
decf temp, f
movlw low(10000)
addwf Lo, f
IFNDEF known_zero
movlw high(10000)
skpnc
movlw high(10000) + 1
addwf Hi, f
ELSE
rlf known_zero, w
addlw high(10000)
addwf Hi, f
ENDIF
skpc
goto add10k
; Output(temp) ;output temp = TenK
;worst case: 10 * 3 + 9 * 3 - 1 = 47 (with known zero)
;worst case: 11 * 3 + 10 * 3 - 1 = 53 (without known zero)
clrf temp
sub3k
movlw 3
addwf temp, f
movlw low(3000)
subwf Lo, f
IFNDEF known_zero
movlw high(3000)
skpc
movlw high(3000) + 1
subwf Hi, f
ELSE
rlf known_zero, w
sublw high(3000) + 1
subwf Hi, f
ENDIF
skpnc
goto sub3k
add1k
decf temp, f
movlw low(1000)
addwf Lo, f
IFNDEF known_zero
movlw high(1000)
skpnc
movlw high(1000) + 1
addwf Hi, f
ELSE
rlf known_zero, w
addlw high(1000)
addwf Hi, f
ENDIF
skpc
goto add1k
; Output(temp) ;output temp = Thou
;worst case: 10 * 4 + 9 * 3 - 1 = 66 (with known zero)
;worst case: 11 * 4 + 10 * 3 - 1 = 73 (without known zero)
clrf temp
sub300
movlw 3
addwf temp, f
movlw low(300)
subwf Lo, f
IFNDEF known_zero
movlw high(300)
skpc
movlw high(300) + 1
subwf Hi, f
ELSE
rlf known_zero, w
sublw high(300) + 1
subwf Hi, f
ENDIF
skpnc
goto sub300
movlw 100
add100
decf temp, f
addwf Lo, f
skpc
goto add100
incf Hi, f
btfsc Hi, 7
goto add100
; Output(temp) ;output temp = Hund
;worst case: 10 * 4 + 5 * 3 + 3 = 59
clrf temp
movlw 30
sub30
incf temp, f
subwf Lo, f
skpnc
goto sub30
movfw temp
rlf temp, f
addwf temp, f
movlw 10
add10
decf temp, f
addwf Lo, f
skpc
goto add10
; Output(temp) ;output temp = Tens
;worst case: 5 * 4 + 5 * 3 + 4 = 39
; Output(Lo) ;output temp = Ones
Nikolai
On Friday, March 10, 2000 James Newton wrote:
> Nice. Thanks Scott.
> The 8 bit to BCD routine is 28 instructions by my count. I have it at
> http://techref.massmind.org/microchip/math/radix/b2bhp-8b3d
> and I think it would require another temp... well... no I guess you could
> reuse HI. It would also need extra (trivial) code to separate tens and ones.
> Summary:
> As I don't know the MChip syntax for conditional compilation, I have:
> ; by Rich Leggitt with tweaks by Scott Dattalo
> ; given 16 bit data in HI and LO, extract decimal digits
> ; requires one Output register called temp, HI and LO are destroyed.
> ; 42? instructions less than 217 (or 199 with known_zero) instructions
> executed
> clrf temp
> skp
> sub10k incf temp,f
> movlw 10000 & 255
> subwf LO,f
> ;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
> movlw 10000 >> 8
> skpc
> addlw 1 ; this sucks
> subwf HI,f
> ELSE
> rlf known_zero,w
> addlw 10000 >> 8
> subwf HI,f
> ENDIF
> bc sub10k ;48/39 inst in loop for 60900 (worst)
> output(temp);
> movlw 10
> movwf temp
> add1K decf temp,f
> movlw 1000 & 255
> addwf LO,f
> ;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
> movlw 1000 >> 8
> skpnc
> addlw 1
> addwf HI,f
> ELSE
> rlf known_zero,w
> addlw 1000 >> 8
> addwf HI,f
> ENDIF
> bnc add1k ;72/63 inst in loop for 60900
> output(temp);
> ;Scott takes over here
> clrf temp
> movlw 100
> skp ;is this a valid macro? I'd write goto $+2 ...
> sub100
> incf temp,f
> subwf LO,f
> skpc
> goto sub100
> decf HI,f
> btfss HI,7 ;Check msb instead of carry for underflow.
> goto sub100 ;4 inst per loop to 200 then 7 per loop to 900.
> ;Total 57(?) in loop for worst case
> ;at this point, HI = 0xff, and 0 <= LO <= 99
> output(temp)
> movlw 10
> movwf temp
> add10 decf temp,f
> addwf LO,f
> bnc add10 ;27 inst in loop for worst case.
> output(temp);
> output(LO);
> return
> ---
> James Newton mailto:jamesnewton@geocities.com 1-619-652-0593
> http://techref.massmind.org NEW! FINALLY A REAL NAME!
> Members can add private/public comments/pages ($0 TANSTAAFL web hosting)
> -----Original Message-----
> From: Scott Dattalo [mailto:scott@dattalo.com]
> Sent: Friday, March 10, 2000 10:27
> To: James Newton
> Cc: PICLIST@MITVMA.MIT.EDU
> Subject: Re: How to write code that no one will laugh at [OT]
> Importance: Low
> I added a few things that'll speed it up...