Scott, you really looked too long at comparisons :) After subtraction, a set carry flag can mean two situations - one number is bigger or equal to another. But if carry is clear, there is no ambiguity - one number is definitely bigger. So, immediate return can be made only on cleared carry. I corrected your routine below... Best regards, Nikolai Wednesday, November 28, 2001, 9:08:49 AM, Scott Dattalo wrote: > On Wed, 28 Nov 2001, Vasile Surducan wrote: >> On Tue, 27 Nov 2001, Drew Vassallo wrote: >> >> > >Compare two 16 Bit numbers, require flag when one greater than the other? >> > > >> > >Anyone have the solution? >> > >> > No, but I'm sure you can find it here: >> > >> > http://www.piclist.com/techref/microchip/compcon.htm#16_bit >> >> >> or here: http://www.geocities.com/vsurducan/electro/PIC/lib.html > I've been looking at comparisons lately. In fact I've been looking at them > too much and am starting to get sick of it (but that's another story). > I saw your page Vasile and noticed that it can be dramatically > streamlined if your're willing to change the interface ever so slightly. > ; unsigned char compare_lt_eq_gt(unsigned int a, unsigned int b) > ; > ; compare two integers. return 0 if they're equal, 1 if a>b, or 2 if a ; movf HIGH(b) subwf HIGH(a) ; first upper bytes ; skpnc ; retlw 1 ; b is less than a skpc retlw 2 ; b > a ; high(a) >= high(b) ; skpnz ; retlw 2 ; a is less than b skpz retlw 1 ; a > b ; high(a) = high(b) = 0 movf LOW(b) subwf LOW(a) ; compare lower bytes ; skpnc ; retlw 1 ; b is less than a skpc retlw 2 ; b > a ; a >= b ; skpnz ; retlw 2 ; a is less than b skpz retlw 1 ; a > b retlw 0 ; a and b are equal > However if you want to return the value in a register (like the example on > your web page) then: > clrf result > movf HIGH(b),w > incfsz result,f ;put 1 into result, note skip will not happen! > subwf HIGH(a),w ;compare high bytes > skpnc > return ; b is less than a, return 1 > rlf result,f ; result == 2 > skpz > return ; a is less than b, return 2 > movf LOW(b),w > decfsz result,f ;put 1 into result again > subwf LOW(b),w > skpnc ; b is less than a, return 1 > return > bcf result,0 ; result now = 0, assume a == b > skpnz > bsf result,1 ; result now = 2, a is less than b > return > A little obscure perhaps, but gets the job done. Note, this won't work on > the 18c core, but then again on the 18c core you'd make use of the addwfc > instruction. > Scott > -- > http://www.piclist.com hint: PICList Posts must start with ONE topic: > [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics