On Thu, Sep 18, 1997 at 01:24:45PM -0400, Frank Dalton wrote: > Does anyone have efficient code that will simply > COMPARE two 16-bit SIGNED integers without too > much temporary register usage? Not really a newbie question - it's harder than it looks. I'm giving away the family jools here :-) The problem is that subtraction of signed numbers can cause overflow; the solution is to make them unsigned, i.e. add 32768 to each number, thus bringing them into the range 0-65535, and now you can subtract without fear of overflow, using the carry flag as the sign of the result. Adding 32768 is as easy as toggling the most significant bit of the number. Here's sample (compiler-generated) code for testing i <= j which is actually turned around to be !(j < i) ;x.c: 6: if(i <= j) movf _j+1,w ;get the most significant byte xorlw 128 ;make it an unsigned number movwf btemp ;save movf _i+1,w ;ditto the other operand xorlw 128 subwf btemp,w ;subtract to compare movf _i,w ;get ready to compare low bytes btfsc 3,2 ;if hi bytes not equal, we're done subwf _j,w btfss 3,0 ;now C bit is set if i <= j > Is there anywhere I can look for the answers to > these kinds of questions so that I don't have to Nope, this is the right place. Of course you can save all this heartache by using a comp... oops, better not be too blatant! Cheers, Clyde -- Clyde Smith-Stubbs | HI-TECH Software Email: clyde@htsoft.com | Phone Fax WWW: http://www.htsoft.com/ | USA: (408) 490 2885 (408) 490 2885 PGP: finger clyde@htsoft.com | AUS: +61 7 3354 2411 +61 7 3354 2422 --------------------------------------------------------------------------- ANSI C for the PIC! Now shipping! See www.htsoft.com for more info.