PIC Microcontoller Range Check Math Methods

Test if a value is in a certain range

W = value

addlw 255 - Hi
;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;addlw (Hi - Lo) + 1
	mov	Hack, W
	mov	W, #(Hi - Lo) + 1
	add	W, Hack

Carry is set if W is in range Lo - Hi

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: ;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. ;

        addlw (255 - Hi) & 0ffh
	mov	Hack, W
	mov	W, #(255 - Hi) &	; 0ffh
	add	W, Hack
;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;        addlw ((Hi - Lo) + 1) & 0ffh
	mov	Hack, W
	mov	W, #((Hi - Lo) + 1) &	; 0ffh
	add	W, Hack

For example:

Lower = 50
Upper = 60

;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;addlw 255 - Hi equates to addlw 195
	mov	Hack, W
	mov	W, #255 - Hi equates to addlw 195
	add	W, Hack
;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;addlw (Hi - Lo) + 1 equates to addlw 11
	mov	Hack, W
	mov	W, #(Hi - Lo) + 1 equates to addlw 11
	add	W, Hack

W = 50          ; lower limit

;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;addlw 195       ; w = 245
	mov	Hack, W
	mov	W, #195	; w = 245
	add	W, Hack
;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;addlw 11        ; w = 0, C = 1, result = OK
	mov	Hack, W
	mov	W, #11	; w = 0, C = 1, result = OK
	add	W, Hack

W = 55          ; in range

;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;addlw 195       ; w = 250
	mov	Hack, W
	mov	W, #195	; w = 250
	add	W, Hack
;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;addlw 11        ; w = 5, C = 1, result = OK
	mov	Hack, W
	mov	W, #11	; w = 5, C = 1, result = OK
	add	W, Hack

W = 60          ; upper limit

;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;addlw 195       ; w = 255
	mov	Hack, W
	mov	W, #195	; w = 255
	add	W, Hack
;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;addlw 11        ; w = 11, C = 1, result = OK
	mov	Hack, W
	mov	W, #11	; w = 11, C = 1, result = OK
	add	W, Hack

W = 49          ; under minimum

;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;addlw 195       ; w = 244
	mov	Hack, W
	mov	W, #195	; w = 244
	add	W, Hack
;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;addlw 11        ; w = 255, C = 0, result = Not OK
	mov	Hack, W
	mov	W, #11	; w = 255, C = 0, result = Not OK
	add	W, Hack

W = 61          ; over maximum

;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;addlw 195       ; w = 0
	mov	Hack, W
	mov	W, #195	; w = 0
	add	W, Hack
;*** WARNING: ADDLW was expanded in three instructions! Check if previous instruction is a skip instruction. 
;addlw 11        ; w = 11, C = 0, result = Not OK
	mov	Hack, W
	mov	W, #11	; w = 11, C = 0, result = Not OK
	add	W, Hack