Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

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.

As in all assembly-level programming, computation is performed in an “accumulator” scratch register,


which is modified as needed by the different processor instructions.

The basic instruction format uses square brackets to frame each instruction:

[instruction argument]

Program location labels are defined by plain text, with no brackets:

program-label

Constants are used to define memory locations for both built-in registers and variable storage. A con-
stant is defined like this:

[const name address]

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:

• C - The carry flag, which is set when an instruction overflows a register.


• Z - The zero flag, which is set when the result of an instruction is “0”.

PIC Assembly Language Reference - 243


What follows, is a list of all instructions currently supported in the PIC Assembler. The instructions
are organized by their functionality, for ease of reference when the exact name is not known. The list
below represents the order in which their detailed descriptions and examples of use can be found on
the subsequent pages:

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

244 - PIC Assembly Language Reference


Bit Manipulation
bset - bit set 259
bclr - bit clear 259
btss - bit test skip if set 260
btsc - bit test skip if clear 260

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

PIC Assembly Language Reference - 245


bsr - branch to subroutine
Program Flow

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]

The address argument is a number corresponding to a program memory location, usually


specified by the name of a location label.

Status Flags Affected


None

rts - return from subroutine


Program Flow

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]

Status Flags Affected


None

246 - PIC Assembly Language Reference


rtv - return from subroutine with value
Program Flow

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]

The number argument is the value to return.

Status Flags Affected


None

bra - unconditional branch


Program Flow

Description
Branches to a given memory location.

Usage Format

[bra address]

The address argument is a number corresponding to a program memory location, usually


specified by the name of a location label.

Status Flags Affected


None

PIC Assembly Language Reference - 247


rti - return from interrupt
Program Flow

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]

Status Flags Affected


None

lda - load accumulator


Register Manipulation

Description
Loads the accumulator with the contents of a register.

Usage Format

[lda register]

The register argument is the address of any register.

Status Flags Affected


Z

ldan - load accumulator with number


Register Manipulation

Description
Loads the accumulator with a number.

248 - PIC Assembly Language Reference


Usage Format

[ldan number]

The number argument is the value to be loaded into the accumulator.

Status Flags Affected


None

sta - store accumulator


Register Manipulation

Description
Stores the contents of the accumulator in a register.

Usage Format

[lda register]

The register argument is the address of any register.

Status Flags Affected


Z

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]

PIC Assembly Language Reference - 249


The register argument is the address of any register.

Status Flags Affected


Z

clr - clear
Register Manipulation

Description
Sets the contents of a register to “0”.

Usage Format

[clr register]

The register argument is the address of any register.

Status Flags Affected


Z

clra - clear accumulator


Register Manipulation

Description
Sets the contents of the accumulator to “0”.

Usage Format

[clra]

Status Flags Affected


Z

250 - PIC Assembly Language Reference


inc - increment
Register Manipulation

Description
Increments the contents of a register and stores the result in the register.

Usage Format

[inc register]

The register argument is the address of any register.

Status Flags Affected


Z

linc - load and increment


Register Manipulation

Description
Increments the contents of a register and stores the result in the accumulator.

Usage Format

[linc register]

The register argument is the address of any register.

Status Flags Affected


Z

PIC Assembly Language Reference - 251


incsz - increment skip if zero
Register Manipulation

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]

The register argument is the address of any register.

Status Flags Affected


None

lincsz - load and increment skip if zero


Register Manipulation

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]

The register argument is the address of any register.

Status Flags Affected


None

252 - PIC Assembly Language Reference


dec - decrement
Register Manipulation

Description
Decrements the contents of a register and stores the result in the register.

Usage Format

[dec register]

The register argument is the address of any register.

Status Flags Affected


Z

ldec - load and decrement


Register Manipulation

Description
Decrements the contents of a register and stores the result in the accumulator.

Usage Format

[ldec register]

The register argument is the address of any register.

Status Flags Affected


Z

PIC Assembly Language Reference - 253


decsz - decrement skip if zero
Register Manipulation

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]

The register argument is the address of any register.

Status Flags Affected


None

ldecsz - load and decrement skip if zero


Register Manipulation

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]

The register argument is the address of any register.

Status Flags Affected


None

254 - PIC Assembly Language Reference


rol - rotate left
Register Manipulation

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]

The register argument is the address of any register.

Status Flags Affected


C

lrol - load and rotate left


Register Manipulation

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]

The register argument is the address of any register.

Status Flags Affected


C

PIC Assembly Language Reference - 255


ror - rotate right
Register Manipulation

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]

The register argument is the address of any register.

Status Flags Affected


C

lror - load and rotate right


Register Manipulation

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]

The register argument is the address of any register.

Status Flags Affected


C

256 - PIC Assembly Language Reference


com - complement
Register Manipulation

Description
Complements the contents of a register and stores the result in the register.

Usage Format

[com register]

The register argument is the address of any register.

Status Flags Affected


Z

lcom - load and complement


Register Manipulation

Description
Complements the contents of a register and stores the result in the accumulator.

Usage Format

[lcom register]

The register argument is the address of any register.

Status Flags Affected


Z

PIC Assembly Language Reference - 257


swap - swap
Register Manipulation

Description
The upper and lower halves of a register are swapped and the result is stored in the register.

Usage Format

[swap register]

The register argument is the address of any register.

Status Flags Affected


None

lswap - load and swap


Register Manipulation

Description
The upper and lower halves of a register are swapped and the result is stored in the accumula-
tor.

Usage Format

[lswap register]

The register argument is the address of any register.

Status Flags Affected


None

258 - PIC Assembly Language Reference


bset - bit set
Bit Manipulation

Description
Makes the value of a given bit of a register equal to “1”.

Usage Format

[bset bit register]

The bit argument is any bit number (from 0 to 7) and the register argument is the address of
any register.

Status Flags Affected


None

bclr - bit clear


Bit Manipulation

Description
Makes the value of a given bit of a register equal to “0”.

Usage Format

[bclr bit register]

The bit argument is any bit number (from 0 to 7) and the register argument is the address of
any register.

Status Flags Affected


None

PIC Assembly Language Reference - 259


btss - bit test skip if set
Bit Manipulation

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

[btss bit register]

The bit argument is any bit number (from 0 to 7) and the register argument is the address of
any register.

Status Flags Affected


None

btsc - bit test skip if clear


Bit Manipulation

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

[btsc bit register]

The bit argument is any bit number (from 0 to 7) and the register argument is the address of
any register.

Status Flags Affected


None

260 - PIC Assembly Language Reference


add - add
Arithmetic

Description
Adds the contents of the accumulator to a reguster and stores the result in the accumulator.

Usage Format

[add register]

The register argument is the address of any register.

Status Flags Affected


Z, C

addm - add to memory


Arithmetic

Description
Adds the contents of the accumulator to a register and stores the result in the register.

Usage Format

[addm register]

The register argument is the address of any register.

Status Flags Affected


Z, C

PIC Assembly Language Reference - 261


addn - add number
Arithmetic

Description
Adds the conents of the accumulator to a number and stores the result in the accumulator.

Usage Format

[addn number]

The number argument is the value to be added to.

Status Flags Affected


Z, C

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]

The register argument is the address of any register.

Status Flags Affected


Z, C (C=0 means result is negative)

262 - PIC Assembly Language Reference


subm - subtract from memory
Arithmetic

Description
Subtracts the contents of the accumulator from a register and stores the result in the register.

Usage Format

[subm register]

The register argument is the address of any register.

Status Flags Affected


Z, C (C=0 means result is negative)

subn - subtract number


Arithmetic

Description
Subtracts the contents of the accumulator from a number and stores the result in the accu-
mulator.

Usage Format

[subn number]

The number argument is the value to be subtracted from.

Status Flags Affected


Z, C (C=0 means result is negative)

PIC Assembly Language Reference - 263


and - and
Logic

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]

The register argument is the address of any register.

Status Flags Affected


Z, C

andm - and memory


Logic
Description
Performs a bit-wise “and” of the contents of the accumulator with a reguster and stores the
result in the register.

Usage Format

[andm register]

The register argument is the address of any register.

Status Flags Affected


Z

264 - PIC Assembly Language Reference


andn - and number
Logic

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]

The number argument is the value to “and” the accumulator with.

Status Flags Affected


Z

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]

The register argument is the address of any register.

Status Flags Affected


Z, C

PIC Assembly Language Reference - 265


orm - or memory
Logic

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]

The register argument is the address of any register.

Status Flags Affected


Z

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]

The number argument is the value to “or” the accumulator with.

Status Flags Affected


Z

266 - PIC Assembly Language Reference


xor - exclusive or
Logic

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]

The register argument is the address of any register.

Status Flags Affected


Z, C

xorm - exclusive or memory


Logic

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]

The register argument is the address of any register.

Status Flags Affected


Z

PIC Assembly Language Reference - 267


xorn - exclusive 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

[xorn number]

The number argument is the value to “xor” the accumulator with.

Status Flags Affected


Z

nop - no operation
System Commands

Description
Does nothing. Usually used to perform precise timing operations.

Usage Format

[nop]

Status Flags Affected


None

268 - PIC Assembly Language Reference


sleep - sleep
System Commands

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]

Status Flags Affected


None

clrwdt - clear watchdog timer


System Commands

Description
Resets the value of the internal watchdog timer.

Usage Format

[clrwdt]

Status Flags Affected


None

PIC Assembly Language Reference - 269

You might also like