piclist 1995\12\08\080506a >
Thread: Negative numbers?
face BY : Clyde Smith-Stubbs email (remove spam text)

> Here is my problem: How do I check for a <0 (negative) value in a PIC

Simple! Just test bit 7 of the register! But, if you're interested in the
result of a subtract, you might need to look instead at the carry flag.
It all depends on exactly what you're doing.

A useful trick too is that if you want to do a signed comparison, you
have a problem with overflow if you just subtract a constant value
from a signed number, but you can do a signed comparison with an
unsigned subtract if you remap the range of values. IOW if your
byte value is equivalent to -128 through +127, by remapping this
to a range of 0-255 you can use unsigned arithmetic (and just
use the carry and zero flags). Now here's the trick; the
remapping just involves xoring the high bit of each number, so
you do something like:

       movlw   80h
       xorwf   reg,f
       movlw   bound ^ 80h
       subwf   reg,w

Now you can just test carry as a sign bit (but note that it's
inverted! It's not a borrow) and the z flag for equality.

Clyde Smith-Stubbs       | HI-TECH Software,       | Voice: +61 7 3300 5011
clydespamspam.....hitech.com.au      | P.O. Box 103, Alderley, | Fax:   +61 7 3300 5246
http://www.hitech.com.au  | QLD, 4051, AUSTRALIA.   | BBS:   +61 7 3300 5235
FREE! Download our shareware (FREE for noncommercial use) MS-DOS C Compiler!
            Point your Web browser at http://www.hitech.com.au/

In reply to: <9512081035.AA03659@trojan.hitech.com.au> from "Steven Addison" at Dec 8, 95 10:39:42 am
See also: www.piclist.com/techref/index.htm?key=negative+numbers
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) Negative numbers?

month overview.

new search...