Embedded Systems: Assembly Language Syntax
Embedded Systems: Assembly Language Syntax
Embedded Systems
PIC16F84A Instruction Set
1
Assembler directive Example of assembler directives
Assembler directives are instructions that are directed to EQU - Equate
Assigns a value to a symbol (same as = ) e.g. TRISA EQU 0x85
the assembler to do a specific thing.
It is not translated into machine code. ORG - Origin
Sets the current origin to a new value. This is used to set the program or register address
(Assembler directives are executed by the assembler at during assembly. For example, ORG 0x00 tells the assembler to assemble all subsequent
code starting at address 0000H.
assembly time, not by the CPU at run time).
Directives can be used to : INCLUDE
An assembler include, or header, file is any file containing valid assembly code.
Link symbolic names to actual values. Usually, the file contains device-specific register and bit assignments. This file may be
Set up pre-defined constants. “included” in the code so that it may be reused by many programs. As an example, to
add the standard header file for the PIC16F84A device to your assembly code, use:
Allocate storage for data in memory.
#INCLUDE P16F84A.INC
Control the assembly process.
END
Include additional source files. This directive is the last statement in an assembly language program. The END
starting address for the program. directive terminates an assembly language program.
2
PIC Assembly Code Instruction format – Label
Consists of 4 fields: 3
A label is used to represent a line or group of code, or a
1 2 operand 4 constant value. It is needed for branching instructions.
Labels should start in column 1. They may be followed by a
colon (:), space, tab or the end of line.
Labels must begin with an alpha character or an under bar
(_) and may contain alphanumeric characters, the under bar
and the question mark.
Labels must not:
begin with two leading underscores, e.g. __temp
f = Source : name of special-purpose register or RAM variable
begin with a leading underscore and number.
F(W) = Destination :
e.g. _2NDLOOP
F – destination is f
be an assembler reserved word (mnemonic, directive, etc.).
W – destination is Working Register
If a colon is used when defining a label, it is treated translated into machine language opcode.
as a label operator and not part of the label itself. Mnemonics are not case sensitive.
3
Instruction format - Operand Field Instruction format – Comment Field
Operands give information to the instruction on the data Comments are text explaining the operation of a line or
that should be used and the storage location for the lines of code.
instruction. A semicolon (;) marks the beginning of a comment
Operands must be separated from mnemonics by one or A semicolon in the beginning of a line makes it all a
more spaces, or tabs. comment line. All characters following the semicolon are
It may has one, two or no operands at all. Multiple ignored through the end of the line.
operands must be separated by commas. Good programming practice dictates the use of a
Examples of instructions with different operand fields comment on almost every line.
NOP ; Instruction with no operand Example:
ANDLW 0x34 ; Instruction with one operand ;Statement line with a comment field
ADDWF FSR,1 ; Instruction with two operand BSF PortA,0 ;set pin 0 of PortA
4
Table 1: PIC instruction set by functional groups
Instruction Type Definition Examples PIC16F84A Instruction set
MOVE The contents of a register are copied MOVF, MOVWF, MOVLW
to another.
Some instructions with alternate result destinations. The default destination
REGISTER Register operations affect only a CLRW, CLRF, DECF, INCF,
for the result of an operation is the file register, but the working register W
single register, and all except CLRW SWAPF, COMF, RLF, RRF, is sometimes an option.
(clear W) operate on file registers. BCF, BSF
The instruction set can also organized by operational groups as shown in
ARITHMETIC Addition and subtraction in binary ADDWF, ADDLW, SUBWF, Table 2.1 – Table 2.3.
gives the same result as in decimal or SUBLW
There are three basic categories:
hex. .
Byte-Oriented Instruction:
LOGIC Logic operations are carried out on ANDWF, ANDLW, IORWF, F: File Register (or RAM)
bit pairs in two numbers to give the IORLW, XORWF, XORLW D: Destination
result which would be obtained if
D=0: Destination W
they were fed to the corresponding
D=1: Destination File Register
logic gate
Bit-Oriented Instruction:
TEST, SKIP & JUMP make decisions (conditional program BTFSC, BTFSS, DECFSZ, F: Register File where the Bit is located
branches) which depend on some INCFSZ, GOTO, CALL, B: Bit Field
input condition or the result of a RETURN, RETLW, RETFIE
Literal and Control Operation:
calculation
K: 8-bit constant
CONTROL NOP, SLEEP, CLRWDT
5
Table 2.2 - PIC instruction set : Table 2.3 - PIC instruction set :
Bit-oriented file register operations Literal and Control Operations
ADDLW ADDWF
6
7
8
9
10
11
12
13
Example
Describe briefly the operation of the following
instructions:
CLRW
MOVLW 0x05
MOVWF 0x0C
INCF 0x0C, 1
MOVF 0x0C, 0
ADDLW 0x32
MOVWF 0x0D
14
Example Example
Write instructions to add the values stored in locations 0x10 Write instructions to perform the following calculation and
and 0x11 and store the result in location 0x12 store the result in location 0x25
1. Move value from location 0x10 into W register 0x11 + 0x32 – 0x2E
MOVF 0 x10, 0
1. Add the value in location 0x11 to the value of the W register MOVLW 0x11
ADDWF 0x11, 0 ADDLW 0x32
1. Move the result from W register to location 0x12 MOVWF 0x25
MOVWF 0x12
MOVLW 0x2E
SUBWF 0x25, 1
15
Assembler Assembler
What does the following code do ? Assembler directives are commands to the assembler
16
EQU: is an assembler directive used to define a ORG is a directive for the assembler to specify the location of
constant the instruction in program memory
Example:
for example: a g e E Q U 0 x 0 F
here the assembler will find and replace all age EQU 0x0F
ORG 0x01E
occurrences of 'age' with 0x0F MOVLW D'23'
After downloading
MOVLW D'23' MOVWF age
age EQU 0x0F equivalent code to the PIC
MOVLW D'23' instructions
MOVWF age MOVWF 0x0F
17
Example
#include "p16f84a.inc"
SUM EQU 0x20
Write a program that adds
CNTR EQU 0x21
the numbers from 1 to 10 ORG 0x000
GOTO START
and stores the result in ORG 0x005
START MOVLW D'10'
location 0x20
MOVWF CNTR
CLRW
LOOP ADDWF CNTR, 0
DECFSZ CNTR, 1
GOTO LOOP
MOVWF SUM
END
18