Searching \ for 'best way for range checking ?' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page:
Search entire site for: 'best way for range checking ?'.

Truncated match.
PICList Thread
'best way for range checking ?'
1998\08\05@044001 by Stefan Sczekalla-Waldschmidt


I`m need to do something in assembler like the following pseudo code

       unsigned char a;        // Byte holds value to be compared
       unsigned char state;    // Byte holds kind of result
       const state_1 = 1;
       const state_2 = 2;
       ( ... )

       case ((a < const1) AND (a > const2))
               then state = state_1
       else case ((a < const3) AND (a > const4))
               then state = state_2
       else case ((a < const5) AND (a > const64))
               then state = state_3
       default: state = err;

The "<" and ">" could also be ">=" or "<=". The primary need
is to check for "a" being within a range.

Target Pic 16C84

Ideas ?

1998\08\05@075507 by Chip Weller


I use a macro for range checking which will branch when W is not between two
constants. This could be used as follows:

bwNotBetween MACRO x,y,label
   addlw -((y)+1)      ; make high value -1, should borrow.
   bc label            ; branch above our range (no borrow)
   addlw (y)+1-(x)     ; make low value 0, should carry here.
   bnc label           ; branch if below our range

   movf a,w
   bwNotBetween const1, const2, Label_a
       movlw state_1
       movwf state
       goto exit
   movf a,w
   bwNotBetween const1, const2, Label_b

You probably can hard code it in a few less instructions.


Stefan Sczekalla-Waldschmidt wrote:

{Quote hidden}

1998\08\05@110915 by Andrew Warren

Stefan Sczekalla-Waldschmidt <spam_OUTPICLISTTakeThisOuTspamMITVMA.MIT.EDU> wrote:

{Quote hidden}


Use Eric Smith's method:

   addlw   255-upper
   addlw   (upper-lower)+1
   bc      inrange

In your specific case, the code would look like this (I'm
assuming that "a" is stored in a register called "a_value",
and that your comparisons are ">=" and "<=":

       movlw   state_1            ;Assume that we'll set
       movwf   state              ;state to state_1.

       movf    a_value,w          ;If const2 <= a <= const1,
       addlw   255-const1         ;exit with state = state_1.
       addlw   (const1-const2)+1  ;
       bc      done               ;

       movlw   state_2            ;Otherwise, assume that
       movwf   state              ;we'll set state to state_2.

       movf    a_value,w          ;If const4 <= a <= const3,
       addlw   255-const3         ;exit with state = state_2.
       addlw   (const3-const4)+1  ;
       bc      done               ;

       movlw   state_3            ; etc...
       movwf   state              ;

       movf    a_value,w          ;
       addlw   255-const5         ;
       addlw   (const5-const6)+1  ;
       bc      done               ;

       movlw   state_err          ;Otherwise,
       movwf   state              ;state = state_err.

   done:   ....


=== Andrew Warren -
=== Fast Forward Engineering - Vista, California

1998\08\06@020838 by Dr. Imre Bartfai

I think you wanted to check whether a falls within any of ranges,
i. e.
         case ((a => const1) AND (a <= const2))

instead of what you have written. If I guess it right, here is a macro I
use to reduce the generated code size for PIC Basic Pro.

;*  MYLIB.LIB                                                   *
;*                                                              *
;*  By        : Dr. Imre B‡rtfai                                *
;*  Notice    : Copyright (c) 1998 Dr. Imre B‡rtfai             *
;*              All Rights Reserved                             *
;*  Date      : 07/16/98                                        *
;*  Version   : 1.00                                            *
;*  Revision  : a                                               *
;*  Notes     :                                                 *

;* Utility macros                                               *

;* INTVAL?BCC : Macro - check whether a var falls into an intval*
;*                                                              *
;* Input      : var    = file register                          *
;*            : lower  = lower bound of interval                *
;*            : upper  = upper bound of interval                *
;*            : label  = label to be jump if var out of interval*
;* Output     : none                                            *
;*            : W      = var - lower                            *
;*            : RWO    = W (if defined)                         *
;*                                                              *
;* Notes      : quick and dirty version;                        *
;*            : no page boundary crossing handled yet           *

INTVAL?BCC  macro  var,lower,upper,label,RWO
   if      lower >= upper
   error   'Lower bound exceeds upper bound in INTVAL?BCC'
   movf    var,W           ; fetch the value
   addlw   ~upper          ; check whether out ouf upper bound
   jc      label
   addlw   upper-lower+1   ; check whether out of lower bound
   jnc     label
     ifnb     RWO
       movwf   RWO

The 'label' must point in your case to the next 'case'.

I hope I have helped.


On Wed, 5 Aug 1998, Stefan Sczekalla-Waldschmidt wrote:

{Quote hidden}

1998\08\06@043713 by Stefan Sczekalla-Waldschmidt

Hi Andrew,

Thanks to you and all, helped by giving suggestions. Many thanks for
your pice of source. It4s made in a way that "copy+paste" to my
source the "hardest" thing still to do ...

Kind regards,

       Stefan Sczekalla-Waldschmit

1998\08\07@051148 by Claudio Rachiele IW0DZG

                  Status Distribution August 06, 1998 08:58:26

The message regarding "Re: best way for range checking ?" sent on August 06, 1998 08:58:26 was sent by

Status Recipient
         Type                To
         Native Name         .....PICLISTKILLspamspam.....MITVMA.MIT.EDU
         Foreign Native Name EraseMEPICLISTspam_OUTspamTakeThisOuTMITVMA.MIT.EDU\n\n\nINTERNET


Status Reporters
         Type                From
         Initials            CR
         Name Domain         NOTES
         Native Name         CN=Claudio Rachiele IW0DZG/OU=Italy/O=IBM@IBMIT
         Foreign Native Name CN=Claudio Rachiele IW0DZG/OU=Italy/O=IBM\nIBMIT\n\n
         Organization        IBM
         Org Unit 1          Italy
         Last Name           IW0DZG
         First Name          Claudio

Status          769
Explanation     Invalid recipient

X.400 Status    769
Explanation     Router: Unable to open mailbox file D14HUBM1/14/H/IBM Server not responding

More... (looser matching)
- Last day of these posts
- In 1998 , 1999 only
- Today
- New search...