Hi Peter, If you combine the increment and decrement code with the range check, it might be more efficient: ; Increment lower 2 bits of File, 0->1->2->0 movlw 1 btfsc File, 1 movlw -2 addwf File, f ; Decrement lower 2 bits of File, 0->2->1->0 movf File, w andlw 0x03 movlw -1 skpnz movlw 2 addwf File, f Nikolai > Subject: Re: [PIC]: Modulo 3 code > From: Peter L. Peres (plp@ACTCOM.CO.IL) > Date: Sat Jun 15 2002 - 12:07:42 PDT > On Sat, 15 Jun 2002, Scott Dattalo wrote: >>On Sat, 15 Jun 2002, Peter L. Peres wrote: >> >>> Hi, >>> >>> I need to make a modulo 3 counter in a file. I've come up with this code: >>> >>> movlw 0x03 >>> andwf File,w ; >>> >>> xorlw 0x03 ; 11 -> 00 >>> btfss STATUS,Z >>> xorlw 0x03 ; undo xorlw above >>> >>> movwf File >>> >>> I actually need the movwf at the end to be like this because I have to add >>> in some flags. Surely there is a MUCH shorter way to do this ... >>> >>> Peter >>> >>> PS: The code has to be constant run time >> how 'bout >> >> rrf File,w >> andwf File,w >> andlw 1 >> addwf File,f >> >>Scott > I did not specify clearly, my mistake. File contains flags excepting the > lowest two bits. It can count up or down. The down counting is already > handled elsewhere (in a similar way), so is the up counting. > This is because the counter switches between base 3 and base 4 at runtime. > The only thing is I need to fix is the state 11 which does not exist in > base 3. Thus my code. Sorry about being unclear. > This code is clearer I hope: > movf File,w > andlw 0x03 > xorlw 0x03 > movlw 0xFC > btfsc STATUS,Z > andwf File,f > On the second look it is not that long ... thanks anyway. > Peter -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu