Professional Documents
Culture Documents
Appendix D: PIC Assembly Language Reference
Appendix D: PIC Assembly Language Reference
PIC Assembly is the lowest-level programming lanugage for Microchip PIC microcontrollers. These
processors are used on the LogoChip and LogoChip modules, the LogoBoard, the PIC Foundation,
and every layer in the Tower system.
When using PIC Assembly, user programs are entered on a desktop computer and assembled into byte
codes that are transferred to the PIC processor through the serial port of the host computer.
Our assembly language differs in some ways from the commercially-available one. It is designed to be
more human-readable than the standard instruction set, and can be easily modified to support other
processors while retaining its user-friendly naming scheme.
The basic instruction format uses square brackets to frame each instruction:
[instruction argument]
program-label
Constants are used to define memory locations for both built-in registers and variable storage. A con-
stant is defined like this:
Memory addresses can be denoted either in decimal or hexidecimal format, as indicated by placing a
“$” character in front of the number.
The status register, at memory location “$03”, is a special one on the processor. It has user-readable
flags that represent different conditions, which can be triggered by the results of various instructions.
In most cases, the two status flags that one would be most concerned with are:
Program Flow
bsr - branch subroutine 246
rts - return from subroutine 246
rtv - return from subroutine with value 247
bra - unconditional branch 247
rti - return from interrupt 248
Register Manipulation
lda - load accumulator 248
ldan - load accumulator with number 248
sta - store accumulator 249
tst - test 249
clr - clear 250
clr - clear accumulator 250
inc - increment 251
linc - load and increment 251
incsz - increment skip if zero 252
lincsz - load and increment skip if zero 252
dec - decrement 253
ldec - load and decrement 253
decsz - decrement skip if zero 254
ldecsz - load and decrement skip if zero 254
rol - rotate left 255
lrol - load and rotate left 255
ror - rotate right 256
lror - load and rotate right 256
com - complement 257
lcom - load and complement 257
swap - swap 258
lswap - load and swap 258
Arithmetic
add - add 261
addm - add memory 261
addn - add number 262
sub - subtract 262
subm - subtract memory 263
subn - subtract number 263
Logic
and - and 264
andm - and memory 264
andn - and number 265
or - or 265
orm - or memory 266
orn - or number 266
xor - xor 267
xorm - xor memory 267
xorn - xor number 268
System Commands
nop - no operation 268
sleep - sleep 269
clrwdt - clear watchdog timer 269
Description
Branches to a subroutine located at a given memory location. The value of the program coun-
ter incremented by one is pushed onto the stack.
Usage Format
[bsr address]
Description
Returns from a subroutine. The top value on the stack is popped and loaded into the program
counter, returning to the program location of the most recently called bsr instruction.
Usage Format
[rts]
Description
Returns from a subroutine with a value stored in the accumulator. The top value on the stack
is popped and loaded into the program counter, returning to the program location of the most
recently called bsr instruction.
Usage Format
[rtv number]
Description
Branches to a given memory location.
Usage Format
[bra address]
Description
Returns from an interrupt. The top value on the stack is popped and loaded into the program
counter, returning to the program location at the time of interrupt.
Usage Format
[rti]
Description
Loads the accumulator with the contents of a register.
Usage Format
[lda register]
Description
Loads the accumulator with a number.
[ldan number]
Description
Stores the contents of the accumulator in a register.
Usage Format
[lda register]
tst - test
Register Manipulation
Description
Tests the value of a register and sets the zero flag if the contents of the register is equal to
“0”.
Usage Format
[tst register]
clr - clear
Register Manipulation
Description
Sets the contents of a register to “0”.
Usage Format
[clr register]
Description
Sets the contents of the accumulator to “0”.
Usage Format
[clra]
Description
Increments the contents of a register and stores the result in the register.
Usage Format
[inc register]
Description
Increments the contents of a register and stores the result in the accumulator.
Usage Format
[linc register]
Description
Increments the contents of a register and stores the result in the register. If the result of the
operation is zero, the next program intsruction is skipped.
Usage Format
[incsz register]
Description
Increments the contents of a register and stores the result in the accumulator. If the result of
the operation is zero, the next program intsruction is skipped.
Usage Format
[lincsz register]
Description
Decrements the contents of a register and stores the result in the register.
Usage Format
[dec register]
Description
Decrements the contents of a register and stores the result in the accumulator.
Usage Format
[ldec register]
Description
Decrements the contents of a register and stores the result in the register. If the result of the
operation is zero, the next program instruction is skipped.
Usage Format
[decsz register]
Description
Decrements the contents of a register and stores the result in the accumulator. If the result
of the operation is zero, the next program intsruction is skipped.
Usage Format
[ldecsz register]
Description
The contents of a register are rotated to the left through the carry flag and the result is stored
in the register.
Usage Format
[rol register]
Description
The contents of a register are rotated to the left through the carry flag and the result is stored
in the accumulator.
Usage Format
[lrol register]
Description
The contents of a register are rotated to the right through the carry flag and the result is stored
in the register.
Usage Format
[ror register]
Description
The contents of a register are rotated to the right through the carry flag and the result is stored
in the accumulator.
Usage Format
[lror register]
Description
Complements the contents of a register and stores the result in the register.
Usage Format
[com register]
Description
Complements the contents of a register and stores the result in the accumulator.
Usage Format
[lcom register]
Description
The upper and lower halves of a register are swapped and the result is stored in the register.
Usage Format
[swap register]
Description
The upper and lower halves of a register are swapped and the result is stored in the accumula-
tor.
Usage Format
[lswap register]
Description
Makes the value of a given bit of a register equal to “1”.
Usage Format
The bit argument is any bit number (from 0 to 7) and the register argument is the address of
any register.
Description
Makes the value of a given bit of a register equal to “0”.
Usage Format
The bit argument is any bit number (from 0 to 7) and the register argument is the address of
any register.
Description
Tests the value of a given bit of a register. If it is equal to “1,” the next program instruction
is skipped.
Usage Format
The bit argument is any bit number (from 0 to 7) and the register argument is the address of
any register.
Description
Tests the value of a given bit of a register. If it is equal to “0,” the next program instruction
is skipped.
Usage Format
The bit argument is any bit number (from 0 to 7) and the register argument is the address of
any register.
Description
Adds the contents of the accumulator to a reguster and stores the result in the accumulator.
Usage Format
[add register]
Description
Adds the contents of the accumulator to a register and stores the result in the register.
Usage Format
[addm register]
Description
Adds the conents of the accumulator to a number and stores the result in the accumulator.
Usage Format
[addn number]
sub - subtract
Arithmetic
Description
Subtracts the contents of the accumulator from a register and stores the result in the accu-
mulator.
Usage Format
[sub register]
Description
Subtracts the contents of the accumulator from a register and stores the result in the register.
Usage Format
[subm register]
Description
Subtracts the contents of the accumulator from a number and stores the result in the accu-
mulator.
Usage Format
[subn number]
Description
Performs a bit-wise “and” of the contents of the accumulator with a reguster and stores the
result in the accumulator.
Usage Format
[and register]
Usage Format
[andm register]
Description
Performs a bit-wise “and” of the contents of the accumulator with a number and stores the
result in the accumulator.
Usage Format
[andn number]
or - or
Logic
Description
Performs a bit-wise “or” of the contents of the accumulator with a reguster and stores the
result in the accumulator.
Usage Format
[or register]
Description
Performs a bit-wise “or” of the contents of the accumulator with a reguster and stores the
result in the register.
Usage Format
[orm register]
orn - or number
Logic
Description
Performs a bit-wise “or” of the contents of the accumulator with a number and stores the
result in the accumulator.
Usage Format
[orn number]
Description
Performs a bit-wise “xor” of the contents of the accumulator with a reguster and stores the
result in the accumulator.
Usage Format
[xor register]
Description
Performs a bit-wise “xor” of the contents of the accumulator with a reguster and stores the
result in the register.
Usage Format
[xorm register]
Description
Performs a bit-wise “or” of the contents of the accumulator with a number and stores the
result in the accumulator.
Usage Format
[xorn number]
nop - no operation
System Commands
Description
Does nothing. Usually used to perform precise timing operations.
Usage Format
[nop]
Description
Puts the processor to sleep, a mode where its power consumption is significantly reduced. It
cannot perform any computation while in this state, but can be awakened by interrupts.
Usage Format
[sleep]
Description
Resets the value of the internal watchdog timer.
Usage Format
[clrwdt]