Searching \ for 'Decrement 4 byte variables' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=decrement+byte+variables
Search entire site for: 'Decrement 4 byte variables'.

Truncated match.
PICList Thread
'Decrement 4 byte variables'
2000\02\05@002550 by Rich Leggitt

picon face
Hi all, I need a fast routine that, given a pointer to a 32 bit counter in
W, decrements it and returns some flag set if the whole thing is zero. I
cooked up this godawful thing, any better ideas out there?

TIA - Rich

; given W=address of little-endian 32 bit variable, decrement the
; variable, return Z set if it is zero
dec32   movwf FSR       ; point FSR to variable LSB
       movlw 1
       subwf INDF,f    ; v[0] -= 1
       skpnz           ; if now 0,
       goto tst32      ; go check the others
       skpnc           ; else if no borrow,
       return          ; we are done
       incf FSR,f      ;
       subwf INDF,f    ; v[1] -= 1
       skpnc           ; if no borrow,
       goto dec32x     ; exit
       incf FSR,f      ;
       subwf INDF,f    ; v[2] -= 1
       skpnc           ; if no borrow,
       goto dec32x     ; exit
       incf FSR,f      ;
       subwf INDF,f    ; v[3] -= 1
dec32x  bcf STATUS,Z    ; ensure Z is clear
       return          ;

; here, v[0] just dec'd to 0, see if the rest are as well
tst32   incf FSR,f      ;
       movf INDF,w     ; w = v[1]
       incf FSR,f      ;
       iorwf INDF,w    ; w |= v[2]
       incf FSR,f      ;
       iorwf INDF,w    ; w |= v[3]
       return          ; return Z set if v == zero

2000\02\05@043836 by Andrew Warren

face
flavicon
face
Rich Leggitt <spam_OUTPICLISTTakeThisOuTspamMITVMA.MIT.EDU> wrote:

> Hi all, I need a fast routine that, given a pointer to a 32 bit
> counter in W, decrements it and returns some flag set if the whole
> thing is zero. I cooked up this godawful thing, any better ideas out
> there?
>
> [godawful thing snipped]

Rich:

This routine IS a little clunky -- I'm sure that if it weren't so
late here, I could do better -- but it's faster than the one you
posted.

If the LSB is beteen 2 and 255 when the routine is called, it takes
11 cycles (including CALL/RETURN overhead).

When the LSB is equal to 1, it takes 20 cycles (including CALL/RETURN
overhead).

When the LSB is equal to 0, it takes 12-19 cycles (including
CALL/RETURN overhead).

Unfortunately, it doesn't return the "Zero" status in the Z flag; it
uses the Carry flag instead (C=0 if the result is non-zero, C=1 if
the result is zero):

   DEC32:

       CLRC            ;ASSUME RESULT WILL BE NON-ZERO.

       MOVWF   FSR     ;POINT AT LSB.
       DECF    INDF    ;DECREMENT IT.
       INCFSZ  INDF,W  ;BORROW?
       GOTO    CHECK0  ;IF NOT, GO CHECK FOR ZERO.

       INCF    FSR     ;OTHERWISE, POINT AT THE NEXT BYTE.
       DECF    INDF    ;DECREMENT IT.
       INCFSZ  INDF,W  ;BORROW?
       RETURN          ;IF NOT, RETURN (SINCE WE ALREADY KNOW
                       ;THAT THE LSB IS NON-ZERO).

       INCF    FSR     ;OTHERWISE, POINT AT THE NEXT BYTE.
       DECF    INDF    ;DECREMENT IT.
       INCFSZ  INDF,W  ;BORROW?
       RETURN          ;IF NOT, RETURN (SINCE WE ALREADY KNOW
                       ;THAT THE LOW 2 BYTES ARE NON-ZERO).

       INCF    FSR     ;OTHERWISE, POINT AT THE MSB.
       DECF    INDF    ;DECREMENT IT.
       RETURN          ;RETURN (SINCE WE ALREADY KNOW THAT
                       ;THE OTHER 3 BYTES ARE NON-ZERO).

   CHECK0:

       SKPZ            ;IF THE LSB IS 0, SKIP AHEAD (Z FLAG
                       ;WAS SET BY THE "DECF").

       RETURN          ;OTHERWISE, RETURN.

       INCF    FSR     ;LSB = 0.  -OR- THE REMAINING 3 BYTES
       MOVF    INDF,W  ;TOGETHER.
       INCF    FSR     ;
       IORWF   INDF,W  ;
       INCF    FSR     ;
       IORWF   INDF,W  ;

       SKPNZ           ;IF ANY OF THE HIGH 3 BYTES ARE
                       ;NON-ZERO, SKIP AHEAD.

       SETC            ;OTHERWISE, ALL 4 BYTES ARE ZERO.
                       ;SET THE CARRY FLAG.

       RETURN          ;RETURN WITH C=1 IF ALL 4 BYTES ARE
                       ;ZERO, C=0 OTHERWISE.

-Andy


=== Andrew Warren - .....fastfwdKILLspamspam@spam@ix.netcom.com
=== Fast Forward Engineering - Vista, California
=== http://www.geocities.com/SiliconValley/2499
===
=== The reports of my demise have been greatly exaggerated.

2000\02\06@000907 by Rich Leggitt

picon face
On Sat, 5 Feb 2000, Andrew Warren wrote:

[[godawful thing snipped] snipped]

>         DECF    INDF    ;DECREMENT IT.
>         INCFSZ  INDF,W  ;BORROW?

Slick. Thanks, Andy.

More... (looser matching)
- Last day of these posts
- In 2000 , 2001 only
- Today
- New search...