Searching \ for 'Thanks Dan Larson subtract smaller ' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=thanks+dan+larson
Search entire site for: 'Thanks Dan Larson subtract smaller'.

No exact or substring matches. trying for part
PICList Thread
'Thanks Dan Larson subtract smaller # from Larger #'
1999\11\03@132111 by John Considine

flavicon
face
Very ingenious,  I never would have though of the two's complement after
already having done the original subtraction.

Thanks.

Dan Larson wrote:

{Quote hidden}

1999\11\04@053403 by Caisson

flavicon
face
> Van: John Considine <spam_OUTjconsiTakeThisOuTspamMAGICNET.NET>
> Aan: .....PICLISTKILLspamspam@spam@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
{Quote hidden}

1999\11\04@180531 by John Considine

flavicon
face
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
below.

Thanks.

movf         TEMP, W
subwf       TEMP1, W
btfss         STATUS, C
goto          DONE
comf         W
incf           TEMP1, W

Caisson wrote:

{Quote hidden}

1999\11\04@183720 by Dan Larson

flavicon
face
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:


{Quote hidden}

More... (looser matching)
- Last day of these posts
- In 1999 , 2000 only
- Today
- New search...