36.1 Standard Instruction Set

The standard PIC18 instruction set adds many enhancements to the previous PIC® MCU instruction sets, while maintaining an easy migration from these PIC® MCU instruction sets. Most instructions are a single program memory word (16 bits), but there are four instructions that require two program memory locations.

Each single-word instruction is a 16-bit word divided into an opcode, which specifies the instruction type and one or more operands, which further specify the operation of the instruction.

The instruction set is highly orthogonal and is grouped into four basic categories:

  • Byte-oriented operations
  • Bit-oriented operations
  • Literal operations
  • Control operations

The PIC18 instruction set summary in Table 36-2 lists byte-oriented, bit-oriented, literal and control operations. Table 36-1 shows the opcode field descriptions.

Most byte-oriented instructions have three operands:

  1. The file register (specified by ‘f’)
  2. The destination of the result (specified by ‘d’)
  3. The accessed memory (specified by ‘a’)

The file register designator ‘f’ specifies which file register is to be used by the instruction. The destination designator ‘d’ specifies where the result of the operation is to be placed. If ‘d’ is zero, the result is placed in the WREG register. If ‘d’ is one, the result is placed in the file register specified in the instruction.

All bit-oriented instructions have three operands:

  1. The file register (specified by ‘f’)
  2. The bit in the file register (specified by ‘b’)
  3. The accessed memory (specified by ‘a’)

The bit field designator ‘b’ selects the number of the bit affected by the operation, while the file register designator ‘f’ represents the number of the file in which the bit is located.

The literal instructions may use some of the following operands:

  • A literal value to be loaded into a file register (specified by ‘k’)
  • The desired FSR register to load the literal value into (specified by ‘f’)
  • No operand required 
(specified by ‘—’)

The control instructions may use some of the following operands:

  • A program memory address (specified by ‘n’)
  • The mode of the CALL or RETURN instructions 
(specified by ‘s’)
  • The mode of the table read and table write instructions (specified by ‘m’)
  • No operand required 
(specified by ‘—’)

All instructions are a single word, except for four double-word instructions. These instructions were made double-word to contain the required information in 32 bits. In the second word, the four MSbs are ‘1’s. If this second word is executed as an instruction (by itself), it will execute as a NOP.

All single-word instructions are executed in a single instruction cycle, unless a conditional test is true or the Program Counter is changed as a result of the instruction. In these cases, the execution takes two instruction cycles, with the additional instruction cycle(s) executed as a NOP.

The double-word instructions execute in two instruction cycles.

One instruction cycle consists of four oscillator periods. Thus, for an oscillator frequency of 4 MHz, the normal instruction execution time is 1 μs. If a conditional test is true, or the Program Counter is changed as a result of an instruction, the instruction execution time is 2 μs. Two-word branch instructions (if true) would take 3 μs.

Figure 36-1 shows the general formats that the instructions can have. All examples use the convention ‘nnh’ to represent a hexadecimal number.

The Instruction Set Summary, shown in Table 36-2, lists the standard instructions recognized by the Microchip Assembler (MPASMTM).

Standard Instruction Set
 provides a description of each instruction.

Table 36-1. Opcode Field Descriptions
FieldDescription
a
RAM access bit

a = 0: RAM location in Access RAM (BSR register is ignored)

a = 1: RAM bank is specified by BSR register

bbb
Bit address within an 8-bit file register (0 to 7).
BSR
Bank Select Register. Used to select the current RAM bank.
C, DC, Z, OV, N
ALU Status bits: Carry, Digit Carry, Zero, Overflow, Negative.
d
Destination select bit

d = 0: store result in WREG

d = 1: store result in file register f

destDestination: either the WREG register or the specified register file location.
f
8-bit Register file address (00h to FFh) or 2-bit FSR designator (0h to 3h).
fs
12-bit Register file address (000h to FFFh). This is the source address.
fd
12-bit Register file address (000h to FFFh). This is the destination address.
GIE
Global Interrupt Enable bit.
k
Literal field, constant data or label (may be either an 8-bit, 12-bit or a 20-bit value).
label
Label name.
mm
The mode of the TBLPTR register for the table read and table write instructions.
Only used with table read and table write instructions:
*No change to register (such as TBLPTR with table reads and writes)
*+Post-Increment register (such as TBLPTR with table reads and writes)
*-Post-Decrement register (such as TBLPTR with table reads and writes)
+*Pre-Increment register (such as TBLPTR with table reads and writes)
n
The relative address (2’s complement number) for relative branch instructions or the direct address for 
CALL/BRANCH and RETURN instructions.
PC
Program Counter.
PCL
Program Counter Low Byte.
PCH
Program Counter High Byte.
PCLATH
Program Counter High Byte Latch.
PCLATU
Program Counter Upper Byte Latch.
PD
Power-down bit.
PRODH
Product of Multiply High Byte.
PRODL
Product of Multiply Low Byte.
s
Fast Call/Return mode select bit

s = 0: do not update into/from shadow registers

s = 1: certain registers loaded into/from shadow registers (Fast mode)

TBLPTR
21-bit Table Pointer (points to a Program Memory location).
TABLAT
8-bit Table Latch.
TO
Time-out bit.
TOS
Top-of-Stack.
u
Unused or unchanged.
WDT
Watchdog Timer.
WREG
Working register (accumulator).
x
Don’t care (‘0’ or ‘1’). The assembler will generate code with x = 0. It is the recommended form of use for compatibility with all Microchip software tools.
zs
7-bit offset value for indirect addressing of register files (source).
zd
7-bit offset value for indirect addressing of register files (destination).
{ }Optional argument.
[text]Indicates an indexed address.
(text)
The contents of text.
[expr]<n>
Specifies bit n of the register indicated by the pointer expr.
Assigned to.
< >
Register bit field.
In the set of.
italicsUser defined term (font is Courier).
Figure 36-1. General Format for Instructions
Table 36-2. Instruction Set
Mnemonic,

Operands

DescriptionCycles16-Bit Instruction WordStatus

Affected

Notes
MSbLSb
BYTE-ORIENTED OPERATIONS
ADDWFf, d, aAdd WREG and f1
0010
01da
ffff
ffff
C, DC, Z, OV, N1, 2
ADDWFCf, d, aAdd WREG and CARRY bit to f1
0010
00da
ffff
ffff
C, DC, Z, OV, N1, 2
ANDWFf, d, aAND WREG with f1
0001
01da
ffff
ffff
Z, N1, 2
CLRFf, aClear f1
0110
101a
ffff
ffff
Z2
COMFf, d, aComplement f1
0001
11da
ffff
ffff
Z, N1, 2
CPFSEQf, aCompare f with WREG, skip =1 (2 or 3)
0110
001a
ffff
ffff
None4
CPFSGTf, aCompare f with WREG, skip >1 (2 or 3)
0110
010a
ffff
ffff
None4
CPFSLTf, aCompare f with WREG, skip <1 (2 or 3)
0110
000a
ffff
ffff
None1, 2
DECFf, d, aDecrement f1
0000
01da
ffff
ffff
C, DC, Z, OV, N1, 2, 3, 4
DECFSZf, d, aDecrement f, Skip if 01 (2 or 3)
0010
11da
ffff
ffff
None1, 2, 3, 4
DCFSNZf, d, aDecrement f, Skip if Not 01 (2 or 3)
0100
11da
ffff
ffff
None1, 2
INCFf, d, aIncrement f1
0010
10da
ffff
ffff
C, DC, Z, OV, N1, 2, 3, 4
INCFSZf, d, aIncrement f, Skip if 01 (2 or 3)
0011
11da
ffff
ffff
None4
INFSNZf, d, aIncrement f, Skip if Not 01 (2 or 3)
0100
11da
ffff
ffff
None1, 2
IORWFf, d, aInclusive OR WREG with f1
0001
00da
ffff
ffff
Z, N1, 2
MOVFf, d, aMove f1
0101
00da
ffff
ffff
Z, N1
MOVFFfs, fdMove fs (source) to 1st word
2
1100
ffff
ffff
ffff
None
fd (destination) 2nd word
1111
ffff
ffff
ffff
MOVWFf, aMove WREG to f1
0110
111a
ffff
ffff
None
MULWFf, aMultiply WREG with f1
0000
001a
ffff
ffff
None1, 2
NEGFf, aNegate f1
0110
110a
ffff
ffff
C, DC, Z, OV, N
RLCFf, d, aRotate Left f through Carry1
0011
01da
ffff
ffff
C, Z, N1, 2
RLNCFf, d, aRotate Left f (No Carry)1
0100
01da
ffff
ffff
Z, N
RRCFf, d, aRotate Right f through Carry1
0011
00da
ffff
ffff
C, Z, N
RRNCFf, d, aRotate Right f (No Carry)1
0100
00da
ffff
ffff
Z, N
SETFf, aSet f1
0110
00da
ffff
ffff
None1, 2
SUBFWBf, d, aSubtract f from WREG with 
 borrow1
0101
01da
ffff
ffff
C, DC, Z, OV, N
SUBWFf, d, aSubtract WREG from f1
0101
11da
ffff
ffff
C, DC, Z, OV, N1, 2
SUBWFBf, d, aSubtract WREG from f with 
 borrow1
0101
10da
ffff
ffff
C, DC, Z, OV, N
SWAPFf, d, aSwap nibbles in f1
0011
10da
ffff
ffff
None4
TSTFSZf, aTest f, skip if 01 (2 or 3)
0110
011a
ffff
ffff
None1, 2

XORWF

f, d, a

Exclusive OR WREG with f

1

0001
10da
ffff
ffff

Z, N

BIT-ORIENTED OPERATIONS
BCFf, b, aBit Clear f1
1001
bbba
ffff
ffff
None1, 2
BSFf, b, aBit Set f1
1000
bbba
ffff
ffff
None1, 2
BTFSCf, b, aBit Test f, Skip if Clear1 (2 or 3)
1011
bbba
ffff
ffff
None3, 4
BTFSSf, b, aBit Test f, Skip if Set1 (2 or 3)
1010
bbba
ffff
ffff
None3, 4

BTG

f, b, a

Bit Toggle f

1

0111
bbba
ffff
ffff

None

1, 2

CONTROL OPERATIONS
BCnBranch if Carry1 (2)
1110
0010
nnnn
nnnn

None

4
BNnBranch if Negative1 (2)
1110
0110
nnnn
nnnn

None

BNCnBranch if Not Carry1 (2)
1110
0011
nnnn
nnnn

None

BNNnBranch if Not Negative1 (2)
1110
0111
nnnn
nnnn

None

BNOVnBranch if Not Overflow1 (2)
1110
0101
nnnn
nnnn

None

BNZnBranch if Not Zero1 (2)
1110
0001
nnnn
nnnn

None

BOVnBranch if Overflow1 (2)
1110
0100
nnnn
nnnn

None

BRAnBranch Unconditionally2
1101
0nnn
nnnn
nnnn

None

BZnBranch if Zero1 (2)
1110
0000
nnnn
nnnn

None

CALLk, sCall

subroutine 1st word

2
1110
110s
kkkk
kkkk

None

2nd word
1111
kkkk
kkkk
kkkk
CLRWDT

Clear Watchdog Timer1
0000
0000
0000
0100
TO, PD
DAWDecimal Adjust WREG1
0000
0000
0000
0111
C
GOTOk

Go to address 1st word

2
1110
1111
kkkk
kkkk
None
2nd word
1111
kkkk
kkkk
kkkk
NOPNo Operation1
0000
0000
0000
0000
None
NOPNo Operation1
1111
xxxx
xxxx
xxxx
None
POP

Pop top of return stack (TOS)1
0000
0000
0000
0110
None
PUSHPush top of return stack (TOS)1
0000
0000
0000
0101
None
RCALLnRelative Call2
1101
1nnn
nnnn
nnnn
None
RESETSoftware device Reset1
0000
0000
1111
1111
All
RETFIEsReturn from interrupt enable2
0000
0000
0001
000s

GIE/GIEH,

PEIE/GIEL

RETLWkReturn with literal in WREG2
0000
1100
kkkk
kkkk
None
RETURNsReturn from Subroutine2
0000
0000
0001
001s
None

SLEEP

Go into Standby mode

1

0000
0000
0000
0011

TO, PD

LITERAL OPERATIONS
ADDLWkAdd literal and WREG1
0000
1111
kkkk
kkkk
C, DC, Z, OV, N
ANDLWkAND literal with WREG1
0000
1011
kkkk
kkkk
Z, N
IORLWkInclusive OR literal with WREG1
0000
1001
kkkk
kkkk
Z, N
LFSRf, kMove literal (12-bit) 2nd word 
 2
1110
1110
00ff
kkkk
None
to FSR(f) 1st word
1111
0000
kkkk
kkkk
MOVLBkMove literal to BSR<3:0>1
0000
0001
0000
kkkk
None
MOVLWkMove literal to WREG1
0000
1110
kkkk
kkkk
None
MULLWkMultiply literal with WREG1
0000
1101
kkkk
kkkk
None
RETLWkReturn with literal in WREG2
0000
1100
kkkk
kkkk
None
SUBLWkSubtract WREG from literal1
0000
1000
kkkk
kkkk
C, DC, Z, OV, N

XORLW

k

Exclusive OR literal with WREG

1

0000
1010
kkkk
kkkk

Z, N

DATA MEMORY ↔ PROGRAM MEMORY OPERATIONS
TBLRD*Table Read2
0000
0000
0000
1000
None
TBLRD*+Table Read with post-increment
0000
0000
0000
1001
None
TBLRD*-Table Read with post-decrement
0000
0000
0000
1010
None
TBLRD+*Table Read with pre-increment
0000
0000
0000
1011
None
TBLWT*Table Write2
0000
0000
0000
1100
None
TBLWT*+Table Write with post-increment
0000
0000
0000
1101
None
TBLWT*-Table Write with post-decrement
0000
0000
0000
1110
None

TBLWT+*

Table Write with pre-increment

0000
0000
0000
1111

None

Note:
  1. When a PORT register is modified as a function of itself (e.g., MOVF PORTB, 1, 0), the value used will be that value present on the pins themselves. For example, if the data latch is ‘1’ for a pin configured as input and is driven low by an external device, the data will be written back with a ‘0’.
  2. If this instruction is executed on the TMR0 register (and where applicable, ‘d’ = 1), the prescaler will be cleared if assigned.
  3. If Program Counter (PC) is modified or a conditional test is true, the instruction requires two cycles. The second cycle is executed as a NOP.
  4. Some instructions are two-word instructions. The second word of these instructions will be executed as a NOP unless the first word of the instruction retrieves the information embedded in these 16 bits. This ensures that all program memory locations have a valid instruction.