Chapter 10. Assembly

When all else fails, one can resort to inline assembly. This can be in the form of a single statement:

      ASM ...
or an entire block:


Using assembly should be a last resort -- it is needed only when either a feature is not possible using JALv2 (for example, the TRIS and OPTION codes), or when speed is of the essence. JALv2 includes the entire assembly language set in the PIC16F87x data sheet, several instructions from earlier micro controllers, and several common macros. There is some support for the 16 bit keywords.

To guarantee the correct data bank is selected when accessing a file register, use one of the following:

      BANK opcode ...

      BANK f
The former takes the file register from the command, the later takes it directly.

Similarly, to guarantee the correct page bits are set (for GOTO or CALL), use one of the following:

      PAGE opcode ...

      PAGE lbl
Again, the former takes the label from the command, the later takes it directly.

Normally, the codes to set or clear the bank or page bits are only generated when necessary. If the bits are already in the correct states, no further commands are generated. If you need to guarantee the codes are always generated, use the following pragmas:

The former will keep any page bits, the later and bank bits. These affect the entire sub-program in which they are declared.

To declare a local label for use in CALLs and/or GOTOs:

      LOCAL identifier[',' identifier2...]
Once declared, a label is inserted into the assembly block by making it the first part of a statement, followed by a ':':

      identifier: opcode...
The available opcodes are listed below. For a full description see the appropriate data sheet.

Note that when using inline assembly you should not modify the bank or page registers, FSR, or BSR. If these are modified, it is the programmers responsibility to return them to their original states.

10.1. Available Op-codes

The following abbreviations are used:

b -- bit number, 0 <= b <= 7
d -- destination, 'f' or 'w'
f -- file register or variable
n -- literal value, 0 <= n <= 255 unless otherwise noted
k -- label or constant

Note that not all opcodes are available on all devices. Check the datasheet for a complete description.

addwf f,d
addwfc f,d
andwf f,d
clrf f
comf f,d
decf f,d
decfsz f,d
incf f,d
incfsz f,d
iorwf f,d
movf f,d
movwf f
rlf f,d
rlcf f,d
rlncf f,d
rrf f,d
rrcf f,d
rrncf f,d
subwf f,d
swapf f,d
xorwf f,d
bcf f,b
bsf f,b
btfsc f,b
btfss f,b
addlw n
andlw n
call k
goto k
iorlw n
movlw n
retlw n
sublw n
xorlw n
tblrd { * | *+ | *- | +* }
tblwt { * | *+ | *- | +* }
tris n (5 <= n <= 9)