;	***** Parallax PIC16C71 Assembler example file *****
;		   (formatted with 8-space tabs)
;
; NOTE: Assembler is case-insensitive, except within strings
;

;
;
; Equates
;
expression		=	0
fr			=	0h				;0-07fh
fr1			=	0h				;0-07fh
fr2			=	0h				;0-07fh
d			=	0				;0 or 1
bit_number		=	0				;0-07h
bit_addr		=	fr.bit_number			;0-03ffh
bit_addr1		=	bit_addr			;0-03ffh
bit_addr2		=	bit_addr+1			;0-03ffh
port_fr			=	6h				;5 or 6
literal			=	0h				;0-0ffh
address			=	0h				;0-07ffh
;
;
; Directives
;
symbol_1	equ	expression	;Equate a symbol to a value
symbol_2	=	expression	;Same as EQU
		org	expression	;Set code origin
		ds	expression	;Define space in bytes
		device	lp_osc,wdt_off,pwrt_off,protect_on	;Define device
		id	'Fred'		;Set device id to 'Fred'
		;include 'stuff.asm'	;Include another source file
;
;
; Data Types
;
			retw	100		;decimal 100
			retw	27h		;hex 27
			retw	0B6h		;hex B6
			retw	10101110b	;binary 10101110
			retw	'A'		;ASCII value 'A' (041h)
			retw	$		;current origin value

			djnz	fr,$		;use $ for delay loops, etc.
			jb	bit_addr,$
;
;
; Operators and expressions (resolved strictly from left to right)
;
; . = BIT ADDRESS
; & = AND
; | = OR
; ^ = XOR
; + = ADD
; - = SUBTRACT
; * = MULTIPLY
; / = DIVIDE
; <<= SHIFT LEFT
; >>= SHIFT RIGHT
; < = HIGH BYTE
; > = LOW BYTE
;
			setb	fr.7			;BIT ADDRESS  function
			mov	w,#expression&7Fh	;AND function
			or	w,#expression|40h	;OR function
			inc	fr2+1			;ADD function
			mov	w,#Routine_2-Routine_1	;SUBTRACT function
			retw	expression*3		;MULTIPLY function
			mov	w,#$/100h		;DIVIDE function
			mov	w,#bit_number<<5	;SHIFT LEFT 5 bits
			call	Routine_3>>3		;SHIFT RIGHT 3 bits
			mov	w,#String<		;HIGH BYTE function
			mov	w,#String>		;LOW BYTE function
;
;
; Symbols can be up to 32 characters and may contain alpha, numeric, '_', and
; ':' characters.  They must not begin with a numeric character.
;
; Local symbols
;
Routine_1		mov	w,#100		;A local symbol can be called
			mov	fr,W		;...within its regular-symbol
:Loop			call	Some_Routine	;...boundary by its own name.
			djnz	fr,:Loop
			ret

Routine_2		mov	w,#200		;Local symbols can be used over
			mov	fr,W		;...and over again.
:Loop			call	Some_Routine
			djnz	fr,:Loop
			ret

Routine_3		mov	w,#250		;A local symbol can be called
			mov	fr,W		;...outside its regular-symbol
			jmp	Routine_2:Loop	;...boundary as shown.

Some_Routine		ret
;
;
; Using DS to allocate sequential variables
;
			org	8	;Set variables' origin.
byte_variable_1		ds	1	;List symbolic variables with length.
byte_variable_2		ds	1	;This way, equate 'stacking' is avoided
byte_variable_3		ds	1	;...in naming sequential variables.
word_variable_1		ds	2	;Also, variables can be moved without
word_variable_2		ds	2	;...typing new equate operands.
word_variable_3		ds	2	;Of course, no code is generated.
string_variable		ds	8

			org	40h	;Now that variables are declared,
					;...set up code origin.
;
;
; Using RETW to define complex strings
;
String			retw	'This string has an inverted end chr.'^80h
			retw	0FFh^'This string has an inversed first chr.'
			retw	'This string ends with 0.',0
			retw	1,'This string starts with 1.'
;
;
;********************************
;*				*
;*   PARALLAX INSTRUCTION SET	*
;*				*
;********************************
;
;
; Byte-oriented operations
;
		mov	w,#literal		;mov's
		mov	w,fr
		mov	fr,w
		mov	fr,#literal
		mov	fr1,fr2

		add	w,#literal		;add's
		add	w,fr
		add	fr,w
		add	fr,#literal
		add	fr1,fr2

		mov	w,#literal-w		;sub's
		mov	w,fr-w
		sub	fr,w
		sub	fr,#literal
		sub	fr1,fr2

		and	w,#literal		;and's
		and	w,fr
		and	fr,w
		and	fr,#literal
		and	fr1,fr2

		or	w,#literal		;or's
		or	w,fr
		or	fr,w
		or	fr,#literal
		or	fr1,fr2

		xor	w,#literal		;xor's
		xor	w,fr
		xor	fr,w
		xor	fr,#literal
		xor	fr1,fr2

		clr	w			;clear's
		clr	fr

		mov	w,++fr			;inc's
		inc	fr

		mov	w,--fr			;dec's
		dec	fr

		mov	w,>>fr			;right shifts
		rr	fr

		mov	w,<<fr			;left shifts
		rl	fr

		mov	w,<>fr			;nibble swaps
		swap	fr

		not	w			;not's
		mov	w,/fr
		not	fr

		test	w			;tests
		test	fr
;
;
; Bit-oriented operations
;
		clrb	bit_addr		;bit set/clear's
		clc
		clz

		setb	bit_addr
		stc
		stz

		movb	bit_addr1,bit_addr2	;bit moves
		movb	bit_addr1,/bit_addr2
;
;
; Inc/dec conditional branches
;
		movsz	w,++fr			;inc/dec with skip if zero
		incsz	fr
		ijnz	fr,address

		movsz	w,--fr
		decsz	fr
		djnz	fr,address
;
;
; Compare-conditional branches
;
		csa	fr,#literal		;compare skips
		csa	fr1,fr2
		csae	fr,#literal
		csae	fr1,fr2
		csb	fr,#literal
		csb	fr1,fr2
		csbe	fr,#literal
		csbe	fr1,fr2
		cse	fr,#literal
		cse	fr1,fr2
		csne	fr,#literal
		csne	fr1,fr2

		cja	fr,#literal,address	;compare jumps
		cja	fr1,fr2,address
		cjae	fr,#literal,address
		cjae	fr1,fr2,address
		cjb	fr,#literal,address
		cjb	fr1,fr2,address
		cjbe	fr,#literal,address
		cjbe	fr1,fr2,address
		cje	fr,#literal,address
		cje	fr1,fr2,address
		cjne	fr,#literal,address
		cjne	fr1,fr2,address
;
;
; Bit-conditional branches
;
		sb	bit_addr		;bit skips
		sc
		sz

		snb	bit_addr
		snc
		snz

		jb	bit_addr,address	;bit jumps
		jc	address
		jz	address

		jnb	bit_addr,address
		jnc	address
		jnz	address
;
;
; Unconditional branches
;
		skip				;skip

		jmp	address			;jumps
		jmp	pc+w
		jmp	w

		call	address			;call/return
		ret
		retw	'String'|80h

		reti				;return from interrupt
;
;
; I/O and control operations
;
		mov	!port_fr,w		;tristate registers
		mov	!port_fr,#literal
		mov	!port_fr,fr

		mov	!option,w		;option register
		mov	!option,#literal
		mov	!option,fr

		clr	wdt			;miscellaneous
		sleep

		nop				;no operation
;
;
;****************************************
;*					*
;*    MANUFACTURER'S INSTRUCTION SET	*
;*					*
;****************************************
;
		addwf	fr,d			;byte-oriented file
		addwf	fr,w			;register operations
		addwf	fr
		andwf	fr,d
		andwf	fr,w
		andwf	fr
		clrf	fr
		clrw
		comf	fr,d
		comf	fr,w
		comf	fr
		decf	fr,d
		decf	fr,w
		decf	fr
		decfsz	fr,d
		decfsz	fr,w
		decfsz	fr
		incf	fr,d
		incf	fr,w
		incf	fr
		incfsz	fr,d
		incfsz	fr,w
		incfsz	fr
		iorwf	fr,d
		iorwf	fr,w
		iorwf	fr
		movf	fr,d
		movf	fr,w
		movf	fr
		movwf	fr
		nop
		rlf	fr,d
		rlf	fr,w
		rlf	fr
		rrf	fr,d
		rrf	fr,w
		rrf	fr
		subwf	fr,d
		subwf	fr,w
		subwf	fr
		swapf	fr,d
		swapf	fr,w
		swapf	fr
		xorwf	fr,d
		xorwf	fr,w
		xorwf	fr

		bcf	fr,bit_number			;bit-oriented file
		bcf	bit_addr			;register operations
		bsf	fr,bit_number
		bsf	bit_addr
		btfsc	fr,bit_number
		btfsc	bit_addr
		btfss	fr,bit_number
		btfss	bit_addr

		addlw	literal				;literal and control
		andlw	literal				;operations
		call	address
		clrwdt
		goto	address
		iorlw	literal
		movlw	literal
		option
		retfie
		retlw	literal
		return
		sleep
		sublw	literal
		tris	port_fr
		xorlw	literal

program_start
