No exact or substring matches. trying for part
'Thanks Dan Larson subtract smaller # from Larger #'
Very ingenious, I never would have though of the two's complement after
already having done the original subtraction.
Dan Larson wrote:
|> Van: John Considine <MAGICNET.NET> jconsi
> Aan: MITVMA.MIT.EDUPICLIST
> 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).
> Very ingenious, I never would have though of the two's complement after
> already having done the original subtraction.
> 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
> > >number. However, when the subwf does the two's complement apparently
> > >sets the C flag. For example, Value1 = H'5C' and Value2 = H'72'
> > >C to be set. Even though 72 is greater than 5C.
> > >
> > >How does everybody else check that a smaller number is being
Thanks for that info Caisson. I would have been trying to figure out why my
values weren't coming out correct.
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
movf TEMP, W
subwf TEMP1, W
btfss STATUS, C
incf TEMP1, W
|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
; 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
INCF RESULT,F ; MSB
More... (looser matching)
- Last day of these posts
- In 1999
, 2000 only
- New search...