Rich Leggitt 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 - fastfwd@ix.netcom.com === Fast Forward Engineering - Vista, California === http://www.geocities.com/SiliconValley/2499 === === The reports of my demise have been greatly exaggerated.