Ron Kreymborg wrote: > > movf new+1,w ; subtract lsbs > > subwf old+1,w > > movf new,w ; subtract msbs > > btfss status,c ; including any carry > > addlw 1 > > subwf old,w > > btfsc status,c ; new smaller? > > goto smaller ; yes > > goto larger ; no I replied: > your code works for Marcel's application (12-bit subtraction), but > it fails on 16-bit numbers when "new" is greater than 0xFF00. > > A routine that works for all 16-bit numbers is: > > MOVF NEWLO,W > SUBWF OLDLO,W > MOVF NEWHI,W > SKPC > INCFSZ NEWHI,W > SUBWF OLDHI,W > BC NEW_IS_LARGER_OR_EQUAL > GOTO NEW_IS_SMALLER and Ron responded: > it works fine when new > 0xff00. No it doesn't, Ron. Your code works when new is less than 0xFF00: ; NEWHI = 00, NEWLO = 20. ; OLDHI = 00, OLDLO = 10. movf new+1,w ; W = 20, CARRY = unknown. subwf old+1,w ; W = F0, CARRY = 0. movf new,w ; W = 00, CARRY = 0. btfss status,c ; -- skip not taken -- addlw 1 ; W = 01, CARRY = 0. subwf old,w ; W = FF, CARRY = 0. btfsc status,c ; -- skip is taken -- goto smaller ; -- this instruction is skipped -- goto larger ; -- this branch is taken -- But it fails when new is > 0xFF00: ; NEWHI = FF, NEWLO = 20. ; OLDHI = FF, OLDLO = 10. movf new+1,w ; W = 20, CARRY = unknown. subwf old+1,w ; W = F0, CARRY = 0. movf new,w ; W = FF, CARRY = 0. btfss status,c ; -- skip not taken -- addlw 1 ; W = 00, CARRY = 1. subwf old,w ; W = FF, CARRY = 1. btfsc status,c ; -- skip not taken -- goto smaller ; -- this branch is taken -- goto larger ; -- this branch is NOT taken -- > However, a quick check of your code would indicate that, for > example, 65329 < 1104. A less-quick check would've shown that my code ALWAYS gave the wrong answer, since I erroneously swapped OLD and NEW as I transcribed it from a message I wrote a couple of years ago. Sorry about that... It should have read: MOVF OLDLO,W SUBWF NEWLO,W MOVF OLDHI,W SKPC INCFSZ OLDHI,W SUBWF NEWHI,W BC NEW_IS_LARGER_OR_EQUAL GOTO NEW_IS_SMALLER > I guess the argument here is whether we are dealing with signed > numbers or not. Marcel didn't mention it, and I assumed they > weren't. My code assumes unsigned numbers, too. -Andy === Andrew Warren - fastfwd@ix.netcom.com === Fast Forward Engineering - Vista, California === http://www.geocities.com/SiliconValley/2499