John Payson has asked me to forward this message to the list. -Andy ------- Forwarded Message From John Payson Follows ------- Subject: Re: How to decrement W To: fastfwd@ix.netcom.com Date: Sat, 13 Jul 1996 16:15:02 -0500 (CDT) From: "John Payson" > > I new to PIC programming. I'm having trouble figuring out how to > > decrement W. Anyone care to help me out? > > If you're using a 12-bit PIC (16C5x), which doesn't have the "ADDLW" > instruction, you can do this: > > MOVWF TEMP ;Store W temporarily in a file register. > > MOVLW 1 ;Subtract 1 from that register, leaving the > SUBWF TEMP,W ;result in W. Hmm... this seems a little long and complicated. If there is no need to preserve TEMP or flags, simply use: movwf temp decf temp,w If you need to preserve flags, use movwf temp decfsz temp,w nop ; Or figure something useful to put here If you need to preserve temp, you could use either: ; ; ; W temp addwf temp ; W W+t subwf temp,w ; t W+t decf temp ; t W-1+t subwf temp,w ; W-1 W-1+t subwf temp ; W-1 t [which I came up with] or you could use one of these: ; ; ; W temp subwf temp,w ; t-W t decf temp ; t-W t-1 subwf temp,w ; W-1 t-1 incf temp ; W-1 t or the similar ; ; ; W temp incf temp ; W t+1 subwf temp,w ; t-W+1 t+1 decf temp ; t-W+1 t subwf temp,w ; W-1 t both of which were produced [in addition to my solution] by a little QBASIC program I wrote [at bottom of message]. Note that if you for some reason wanted to subtract one from both the W and some other register, you could use: ; ; ; W temp subwf temp,w ; t-W t decf temp ; t-W t-1 subwf temp,w ; W-1 t-1 Anyway, for the QBASIC program y'all have been waiting for... here it is. It's pretty kludged, but I wrote it just for some little dabbling, not for any real workhorse stuff... proglen = 5 numops = 6 DIM inst(proglen) PRINT PRINT "Scanning:" Foo: i = 0 WHILE inst(i) >= numops inst(i) = 1 i = i + 1 WEND inst(i) = inst(i) + 1 ww = 1: wt = 0: wc = 0: tw = 0: tt = 1: tc = 0 FOR i = 0 TO proglen - 1 ON inst(i) GOSUB aww, awf, sww, swf, incf, decf NEXT IF ww = 1 AND wt = 0 AND wc = 1 AND tw = 0 AND tt = 1 AND tc = 0 THEN 'IF ww = -1 AND wt = 0 AND wc = 0 AND tw = 0 AND tt = 1 AND tc = 0 THEN FOR i = 0 TO proglen - 1 PRINT inst(i); NEXT PRINT END IF IF inst(proglen) = 0 THEN GOTO Foo END aww: ww = tw + ww: wt = tt + wt: wc = tc + wc: RETURN awf: tw = tw + ww: tt = tt + wt: tc = tc + wc: RETURN sww: ww = tw - ww: wt = tt - wt: wc = tc - wc: RETURN swf: tw = tw - ww: tt = tt - wt: tc = tc - wc: RETURN incf: tc = tc + 1: RETURN decf: tc = tc - 1: RETURN ------ End of Forwarded Message from John Payson --------- Andrew Warren - fastfwd@ix.netcom.com Fast Forward Engineering, Vista, California http://www.geocities.com/SiliconValley/2499