piclist 2001\04\05\101721a >
Thread: clever rounding out there ?
www.piclist.com/techref/microchip/devices.htm?key=pic
BY : Bob Ammerman email (remove spam text)

Tony,

I like your idea of figuring out the contribution of each bit to the ones
and 10's digits. Here is a quick way to compute them:

Assume: FSR0 points to the LSByte of your value:

movlw  0
btfsc    INDF0,0
btfsc    INDF0,1
btfsc    INDF0,2
btfsc    INDF0,3
btfsc    INDF0,4
btfsc    INDF0,5
btfsc    INDF0,6
btfsc    POSTINC0,7

etc.

This will take 48 cycles/instructions to compute the 1's part.

The same pattern can be repeated to compute the 10's. Note that the four
LSBits of the first byte have no affect on the 10's so you can do this in
40cycles/instructions. Of course you'd start with the MSBits and work to the
lsbits since the code above leaves FSR0 pointing to the MSBits.

That's only 88 cycles/instructions to extract the ones and tens.

Wait a minute!!!

Another thought in computing the 1's part....
Except for the 1's bit, each bit in a nibble already has the right value,
so:

movf      INDF0,W             ; Get low nibble
andlw    0x0F
movwf   ones
swapf    INDF0,W    ; Get 2nd nibble
andlw    0x0F
btfsc      POSTINC0,0    ; Low bit set?
addlw    5                        ; Yes: it is worth 6, not 1

movf      INDF0,W    ; Get 3rd nibble
andlw    0x0F
btfsc      INDF0,0    ; Low bit set?
addlw    5                        ; Yes, it is worth 6, not 1
swapf    INDF0,W    ; Get 4th nibble
andlw    0x0F
btfsc      POSTINC0,0    ; Low bit set?
addlw    5                        ; Yes, it is worth 6, not 1

movf      INDF0,W    ; Get 5rd nibble
andlw    0x0F
btfsc      INDF0,0    ; Low bit set?
addlw    5                        ; Yes, it is worth 6, not 1
swapf    INDF0,W    ; Get 6th nibble
andlw    0x0F
btfsc      INDF0,0    ; Low bit set?
addlw    5                        ; Yes, it is worth 6, not 1

That's 28 (for the ones)+40 (for the tens) == 68 instructions/cycles instead
of 88!

Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservmitvma.mit.edu with SET PICList DIGEST in the body

<000f01c0bdda\$b663fc20\$fce8c540@CDS> 7bit