# SXMicrocontroller Range Check Program FlowMethods

## Test if a value is in a certain range

Reg = value

```	;offset Reg value so that Hi value in Reg becomes 255
mov	W, #255 - Hi
add	Reg, W		;Reg is influenced!

;add to offset value the span value plus one
mov	W, #(Hi - Lo) + 1
```

Carry is set if Lo < Reg < Hi.

If Reg should be restored:

```	mov	W, #Hi - 255	;restore Reg
```

It works for all values of Hi and Lo although it is not optimal for 255 and 0. It even works for ranges that wrap around 255 back to 0. For instance, if you set Lo to 240 and Hi to 15, it will match 0..15 and also 240..255. And it works for signed numbers. You can set Lo to -3 and Hi to 5 to match -3..+5.

The only requirement is that your assembler must truncate literals to eight bits. If not, you can still use it by masking the literals yourself:

```	mov	W, #(255 - Hi) & \$FF
mov	W, #((Hi - Lo) + 1) & \$FF
```

For example:

```Lower = 50
Upper = 60
mov	W, #195	;255 - Hi = 255 - 60 = 195
mov	W, #11		;(Hi - Lo) + 1

W = 50          ; lower limit
mov	W, #195
add	Reg, W		;Reg = 50 + 195 = 245
mov	W, #11
add	W, Reg		;w = 245 + 11 = 0, C = 1, result = OK

W = 55          ; in range
mov	W, #195
add	Reg, W		;Reg = 55 + 195 = 250
mov	W, #11
add	W, Reg		;w = 250 + 11 = 5, C = 1, result = OK

W = 60          ; upper limit
mov	W, #195
add	Reg, W		;Reg = 60 + 195 = 255
mov	W, #11
add	W, Reg		;w = 255 + 11 = 10, C = 1, result = OK

W = 49          ; under minimum
mov	W, #195
add	Reg, W		;Reg = 49 + 195 = 244
mov	W, #11
add	W, Reg		;w = 244 + 11 = 255, C = 0, result = Not OK

W = 61          ; over maximum
mov	W, #195
add	Reg, W		;Reg = 61 + 195 = 0
mov	W, #11
add	W, Reg		;w = 0 + 11 = 11, C = 0, result = Not OK
```

Code:

• Harold Hallikainen (who, by the way, provides the FCC rules on the web at his site http://www.hallikainen.org/FCC/FccRules/, and makes a darn fine line of lighting controllers (DMX etc..) at http://www.dovesystems.com/) posted this nice little range check sample. It is good because it works with min and max in ram rather than hard coded...
```WindowTest macro value, min, max
;	call	as WindowTest value
; and cleared if not (wtfail).
; The key to this is the mov w,fr-w instruction.
; Carry =1 if the result is greater than 0 (carry=1 is positive result). So,
;	carry=1 if f=>w
;	carry=0 if f<w or w>f
local wtexit			; local labels in macro
; For min, we want value=>min,
;  so we'll put value in f and min in w
mov	W, min		; Get minimum
mov	W, value-w	; w=value-min. Result positive (carry set) if value=>min
jnc	wtexit		; If no carry, we're below min. Exit with fail (carry clear)
; For max, we want max=>value,
;  so we'll put max in f, value in w
mov	W, value	; Get current value in w
mov	W, max-w	; w = max-value. Result positive (carry set)
;  if max=>value. Exit with carry set
wtexit
endm```
+

 file: /Techref/scenix/lib/flow/rangechk_sx.htm, 3KB, , updated: 2004/6/10 14:40, local time: 2023/3/29 19:53, TOP NEW HELP FIND:  34.238.189.240:LOG IN

 ©2023 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?Please DO link to this page! Digg it! / MAKE! SX Microcontroller Range Check Program Flow Methods

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant"

.