PIC mid range ALU
Embedded Systems
PIC16F84A Instruction Set
Dr. Nasser Halasa
Second Semester 2017-2018
Block Diagram of the PIC 16 Series ALU
Assembly language syntax Basic Assembly Program Structure
An assembly language program consists of statements. The Assembly language is made up of two(2) types of
syntax of an assembly language program statement obeys statements:
the following rules:
Assembler Directive:
- Only one statement is written per line.
Inform the assembler about the program and the
- Each statement is either an instruction or an assembler
directive. environment and NOT be translated into machine code.
- Each instruction has an op-code and possibly one, two or Executable Instruction:
no operands at all.
- An op-code is known as mnemonic.
One of the processor's valid instructions which can be
- Each mnemonic represents a single machine instruction. translated into machine code form by the assembler.
- Operands provide the data to work with.
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.
Files that the Assembler
Assembler Directive Assembler File Structure (Simple Form) (e.g. MPASM) generates
Assembler Directive Summary of Action
list implement a listing option*
For us, this will be
#include include additional source file MPASM Executable File
org set program origin .hex
equ define an assembly constant; this
allows us to assign a value to a label
Source File List File
end end program block Assembler
.asm .lst
Radix Example Representation
Decimal D‘255’
Written by you, as a text
Hexadecimal H‘8d’ or Ox8d file, in Assembler format
Error File
Octal O‘574’ .err
Binary B‘01011100’
ASCII ‘G’ or A‘G’
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
Instruction format – Label Instruction format - Opcode
Labels may be up to 32 characters long. This field consists of a symbolic operation code,
By default they are case sensitive, but case known as op-code.
sensitivity may be overridden by a command-line The opcode describes the operation.
option (/c). Symbolic op-codes (known as mnemonic) are
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.
Example: Example: BSF (Bit Set f)
Here NOP CLRW (Clear W)
GOTO Here
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
Example MPASM Assembler Source Code PIC16F84A Instruction set
Each microcontroller family has its own set of
instructions, which carry out essentially the same set of
operations, but using different syntax.
The PIC16F84A only has 35 instructions. Each instruction
is 14-bit words.
This instruction set organized by functional groups is
listed in Table 1.
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
Table 2.1- PIC instruction set :
PIC instruction set – description convention
Byte-oriented file register operations
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
Another Example Examples
Write instructions to swap (exchange) the values Write instructions to perform the following:
stored in locations 0x20 and 0x21 1. add 15 to the value stored in memory location 0x29 and store the
result in the same location
move the value in location MOVF 0x20, 0 2. select bank 1
0x20 to a temporary location MOVWF 0x23
3. Store 0x26 in register TRISB
move the value in location
0x21 to location 0x20 MOVF 0x21, 0 4. Store the value of the STATUS register in location 0x40
MOVWF 0x20
move the value in the 5. Jump to program memory location 0x0132 if Z flag is 1 otherwise
temporary location to MOVF 0x23, 0 continue program execution
location 0x21 MOVWF 0x21 6. clear TRISB<3:0> and set TRISB<7:4> to ones
7. clear TRISB<3:0> without affecting the other bits of TRISB
8. If the value in location 0x35 is 16, set it to zero, otherwise
increment it
15
Assembler Assembler
For the MCU to understand the instruction, it has to be
translated to machine language Example: translate the following machine language instruction to
Example: translate the following instruction to machine assembly: 0x19C4
language: 0x19C 4 = 01 1001 1100 0100
MOVLW 0xF3 BTFSC 0x44, 3
11 0000 1111 0011 = 0x30F3 0x16B5
BSF 0x2E, 4 BSF 0x35, 5
0x2A3C
01 0010 0010 1110 = 0x122E
IORWF 0x3A , 1 ?
Example Assembler directives
What does the following code do ? Assembler directives are commands to the assembler
CLRF 0x06 Directives are not part of the instruction set
So, they are not instructions for the PIC
loop INCF 0x06,1
They are not translated to machine language
BTFSS 0x06 ,3 They simplify writing the program
GOTO loop
CLRF 0x06
16
Assembler Directive: EQU Assembler Directive: ORG
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
Other Assembler directives Example
#include "name.h" Initialize W to zero, and keep adding 0x10 to the W
– replaces this line with the contents of the file name.h register until the result in W is zero
– Microchip provides header files containing all ORG 0x000
register names with their addresses STATUS EQU 0x03
Z EQU 2
end CLRW
– after the last instruction in the program l o o p A D D LW 0 x 1 0
B T FS S S TAT US , Z
instructions after “end” directive are not assembled GOTO loop
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