On Thu, 4 Nov 1999 16:51:40 -0500, John Considine wrote: >Thanks for that info Caisson. I would have been trying to figure out why my >values weren't coming out correct. Sorry for the oversight.... I was using this technique myself, but on the upper byte of 16-bit values. I did not show the 16-bit math to avoid any confusion. The modification that Rudy Wieser pointed out is essentially providing a 9-bit signed result. Here is my 16-bit subtract and absolute value. ; 16-bit signed subtract, RESULT = VALUE1 - VALUE2 MOVF VALUE1,W ; MSB MOVWF RESULT ; MOVF VALUE1 + 1,W ; LSB MOVWF RESULT + 1 ; MOVF VALUE2 + 1,W ; LSB SUBWF RESULT + 1,F MOVF VALUE2,W ; MSB, VALUE2 into W BTFSS STATUS,C ; borrow from sub of LSB INCFSZ VALUE2,W ; if borrow, MSB VALUE2+1 into W, SUBWF RESULT,F ; sub MSB, skipped if borrow results in zero ; C valid, but Z not ; compute absolute value of 16-bit signed result BTFSS RESULT,7 ; MSB GOTO NO2COMP ; no 2's complement needed, skip around COMF RESULT + 1,F ; LSB COMF RESULT,F ; MSB INCF RESULT + 1,F ; LSB BTFSC STATUS,Z INCF RESULT,F ; MSB NO2COMP: > >Per my understanding. If I do a subtraction and the C bit is 1, everything >went okay and use the result. Otherwise C is 0 and the borrow bit occurred and >I then can use two's complement to get it corrected. Is this correct as shown >below. > >Thanks. > >movf TEMP, W > subwf TEMP1, W > btfss STATUS, C > goto DONE > comf W > incf TEMP1, W > >Caisson wrote: > >> > Van: John Considine >> > Aan: PICLIST@MITVMA.MIT.EDU >> > Onderwerp: Re: Thanks Dan Larson subtract smaller # from Larger # >> > Datum: woensdag 3 november 1999 19:21 >> >> This "solution" only works for numbers upto 127. Subtracting 2 from 200 >> would give a *valid* number, but with the sign-flag set. So the "solution" >> would mangle it into something else :-( Where the Sign-flag is the >> Over/underflow for small numbers (0 thru 127), the Carry is the Overflow >> for large numbers (0 thru 255) ... >> >> You can still use the Two-complement trick though, just replace the check >> for the Sign-flag with a check for the Borrow-flag ( NOT Carry-flag). >> >> Regards, >> Rudy Wieser >> >> > Very ingenious, I never would have though of the two's complement after >> > already having done the original subtraction. >> > >> > Thanks. >> > >> > Dan Larson wrote: >> > >> > > On Wed, 3 Nov 1999 10:08:20 -0500, John Considine wrote: >> > > >> > > >I am trying to make sure that I subtract a smaller number from a >> larger >> > > >number. However, when the subwf does the two's complement apparently >> it >> > > >sets the C flag. For example, Value1 = H'5C' and Value2 = H'72' >> causes >> > > >C to be set. Even though 72 is greater than 5C. >> > > > >> > > >How does everybody else check that a smaller number is being >> subtracted >> > > >from a larger number? >> > > >> > > How about just doing the subtract, regardless of operand order. >> > > Then, check the sign bit (bit seven). If bit 7 is set, complement >> > > and increment the result (two's complement). If bit 7 is not set, >> > > then leave the result alone. The result will be the absolute value >> > > of the difference. Is this what you want? >> > > >> > > ; Using three RAM location and preserving Value1 and value2 >> > > >> > > movf Value1, W >> > > movwf Result >> > > movf Value2, W >> > > subwf Result, F >> > > btfss Result, 7 >> > > goto Done >> > > comf Result, F >> > > incf Result, F >> > > Done: >> > > >> > > ; Using two RAM locations and destroying Value2 >> > > >> > > movf Value1, W >> > > subwf Value2, F >> > > btfss Value2, 7 >> > > goto Done >> > > comf Value2, F >> > > incf Value2, F >> > > Done: >> > > >> > > I hope this helps you... >> > > >> > > Dan >