; BRANCH offset (in w), jump table
; Jumps to the routine corresponding to offset in the jump	table.
; Makes evaluating compound logic expressions easy.
; Since this example uses data straight from port RA, it could move RA
; into w, then go to the jump table. However, we're using a separate variable
; to demonstrate the technique.	

; Device data and reset vector
	device	pic16c55,xt_osc,wdt_off,protect_off
	reset	start

	org	8
flags	ds	1	; Variable to hold bits.
temp	ds	1	; Variables for delay
temp2	ds	1		; in Action.
A	=	flags.0		; The elements of our
B	=	flags.1		; compound logic expression.
C_	=	flags.2		; "C" is carry bit
D	=	flags.3

	org	0

start	mov	!ra, #255	; All inputs for switches.
	mov	!rb, #0		; All outputs for LEDs
	clr	rb		; Ensure that LEDs are off.
	clr	flags		; Initialize flags.

; For the sake of the example, we'll move each bit into place separately.
; However, always look for opportunities to move several bits at a time.
; For example, the four movb operations below could be replaced with
; mov flags,ra. Make sure that you strip off any extraneous bits if you use

; a port that is wider than the desired number of bits.

Get_bits
	movb	A,ra.0		; Move bits of ra into flags.
	movb	B,ra.1
	movb	C_,ra.2
	movb	D,ra.3

; We want to jump to Action IF A AND (B OR C) BUT NOT D.
; That corresponds to the bit patterns 0111, 0101 and 0011,
; (see diagram below) table entries 7, 5 and 3.

;	Bits:	|	D	|	C_	|	B	|	A	|
;	Flags:	| flags.3 | flags.2 | flags.1 | flags.0 |


; BRANCH (cont)

Branch	mov	w,flags		; Move flags into w.
	jmp	pc+w		; Table of 16 possible actions.
	jmp	Get_bits	; DCBA=0000: try again.
	jmp	Get_bits	; DCBA=0001: try again.
	jmp	Get_bits	; DCBA=0010: try again.
	jmp	Action		; DCBA=0011: Action!
	jmp	Get_bits	; DCBA=0100: try again.
	jmp	Action		; DCBA=0101: Action!
	jmp	Get_bits	; DCBA=0110: try again.
	jmp	Action		; DCBA=0111: Action!
	jmp	Get_bits	; DCBA=1000: try again.
	jmp	Get_bits	; DCBA=1001: try again.
	jmp	Get_bits	; DCBA=1010: try again.
	jmp	Get_bits	; DCBA=1011: try again.
	jmp	Get_bits	; DCBA=1100: try again.
	jmp	Get_bits	; DCBA=1101: try again.
	jmp	Get_bits	; DCBA=1110: try again.
	jmp	Get_bits	; DCBA=1111: try again.

; When the switches on port RA are in the correct combination,
; the LEDs on port RB blink.
Action	XOR	rb,#255		; Toggle LEDs.
:loop	djnz	temp,:loop	; Short delay.
	djnz	temp2,:loop
	jmp	Get_bits	; Resume checking RA.
