you wrote: > >Here's an excerpt from a "comparison" tip page I'm working on: > > >Testing that a value lies within a range > > >In this example, the value in the W register will be tested to determine > >whether it lies within the range specified by the constants loval and hival > >(inclusive). In other words, we're testing loval <= W <= hival. > > > addlw 256-hival > > addlw (hival-loval)+1 > > btfsc status,cf > > goto inrange > > Let's try this with w = 254, hival = 255, loval = 253. > > 254 + (256 - 255) = 255 > 255 + (255 - 253 + 1) = 258 > which generates a carry. > > Now try w = 75, hival = 100, loval = 50 > > 75 + (256 - 100) = 231 > 231 + (100 - 50 +1) = 252 > which does not generate a carry. > > In both cases the starting value is in range. Did I miss something? > The quote code is a bit broken. You want to :- 1. Subtract the lo value, which should not generate a carry 2. Subtract the difference, which should generate a carry i.e. addlw 256-loval btfsc status,cf goto outofrange addlw 256-(hival-loval) btfss status,cf goto outofrange inrange: You will want +1's in various places depending on the range being >= or > and <= or = the limits. There is probably a way with one less goto by making the sums both generate carry, and skip the second add if not set... but its too early in the morning and I havn't had my cup of coffee yet. -- _ (_) _| _ . _ _ Tel +44 973 222257 ( )(_|( |(_|| ) Fax UK 0500 222258 E&OE