1 Embedded Systems PIC16F84A Instruction Set Dr. Nasser Halasa Second Semester 2017-2018 PIC mid range ALU Block Diagram of the PIC 16 Series ALU Assembly language syntax An assembly language program consists of statements. The syntax of an assembly language program statement obeys the following rules: - Only one statement is written per line. - Each statement is either an instruction or an assembler directive. - Each instruction has an op-code and possibly one, two or no operands at all. - An op-code is known as mnemonic. - Each mnemonic represents a single machine instruction. - Operands provide the data to work with. Basic Assembly Program Structure Assembly language is made up of two(2) types of statements: Assembler Directive: Inform the assembler about the program and the environment and NOT be translated into machine code. Executable Instruction: One of the processor's valid instructions which can be translated into machine code form by the assembler.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
Embedded Systems
PIC16F84A Instruction Set
Dr. Nasser HalasaSecond Semester 2017-2018
PIC mid range ALU
Block Diagram of the PIC 16 Series ALU
Assembly language syntax
An assembly language program consists of statements. Thesyntax of an assembly language program statement obeysthe following rules:
- Only one statement is written per line.- Each statement is either an instruction or an assembler
directive.- Each instruction has an op-code and possibly one, two or
no operands at all.- An op-code is known as mnemonic.- Each mnemonic represents a single machine instruction.- Operands provide the data to work with.
Basic Assembly Program Structure
Assembly language is made up of two(2) types ofstatements:
Assembler Directive:
Inform the assembler about the program and theenvironment and NOT be translated into machine code.
Executable Instruction:
One of the processor's valid instructions which can betranslated into machine code form by the assembler.
2
Assembler directives are instructions that are directed tothe assembler to do a specific thing.
It is not translated into machine code.(Assembler directives are executed by the assembler atassembly time, not by the CPU at run time).
Directives can be used to : Link symbolic names to actual values. Set up pre-defined constants. Allocate storage for data in memory. Control the assembly process. Include additional source files. starting address for the program.
Assembler directive
EQU - EquateAssigns a value to a symbol (same as = ) e.g. TRISA EQU 0x85
ORG - OriginSets the current origin to a new value. This is used to set the program or register addressduring assembly. For example, ORG 0x00 tells the assembler to assemble all subsequentcode starting at address 0000H.
INCLUDEAn assembler include, or header, file is any file containing valid assembly code.Usually, the file contains device-specific register and bit assignments. This file may be“included” in the code so that it may be reused by many programs. As an example, toadd the standard header file for the PIC16F84A device to your assembly code, use:
#INCLUDE P16F84A.INC
ENDThis directive is the last statement in an assembly language program. The ENDdirective terminates an assembly language program.
Example of assembler directives
Assembler Directive
Assembler Directive Summary of Action
list implement a listing option*
#include include additional source file
org set program origin
equ define an assembly constant; thisallows us to assign a value to a label
end end program block
Radix Example Representation
Decimal D‘255’
Hexadecimal H‘8d’ or Ox8d
Octal O‘574’
Binary B‘01011100’
ASCII ‘G’ or A‘G’
Assembler File Structure (Simple Form)
For us, this will beMPASM
Written by you, as a textfile, in Assembler format
Files that the Assembler(e.g. MPASM) generates
Source File Assembler
Executable File
List File
Error File
.asm
.hex
.lst
.err
3
PIC Assembly Code
Consists of 4 fields:
f = Source : name of special-purpose register or RAM variable
F(W) = Destination :F – destination is f
W – destination is Working Register
operand1
42
3
Instruction format – Label
A label is used to represent a line or group of code, or aconstant value. It is needed for branching instructions.
Labels should start in column 1. They may be followed by acolon (:), 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 barand the question mark.
Labels must not: begin with two leading underscores, e.g. __temp begin with a leading underscore and number.
e.g. _2NDLOOP be an assembler reserved word (mnemonic, directive, etc.).
Labels may be up to 32 characters long.
By default they are case sensitive, but casesensitivity may be overridden by a command-lineoption (/c).
If a colon is used when defining a label, it is treatedas a label operator and not part of the label itself.
Example:
Here NOP
GOTO Here
Instruction format – Label Instruction format - Opcode
This field consists of a symbolic operation code,known as op-code.
The opcode describes the operation.
Symbolic op-codes (known as mnemonic) aretranslated into machine language opcode.
Mnemonics are not case sensitive.
Example: BSF (Bit Set f)
CLRW (Clear W)
4
Operands give information to the instruction on the datathat should be used and the storage location for theinstruction.
Operands must be separated from mnemonics by one ormore spaces, or tabs.
It may has one, two or no operands at all. Multipleoperands must be separated by commas.
Examples of instructions with different operand fields
NOP ; Instruction with no operand
ANDLW 0x34 ; Instruction with one operand
ADDWF FSR,1 ; Instruction with two operand
Instruction format - Operand Field Instruction format – Comment Field
Comments are text explaining the operation of a line orlines of code.
A semicolon (;) marks the beginning of a comment
A semicolon in the beginning of a line makes it all acomment line. All characters following the semicolon areignored through the end of the line.
Good programming practice dictates the use of acomment on almost every line.Example:
;Statement line with a comment field
BSF PortA,0 ;set pin 0 of PortA
Example MPASM Assembler Source Code PIC16F84A Instruction set
Each microcontroller family has its own set ofinstructions, which carry out essentially the same set ofoperations, but using different syntax.
The PIC16F84A only has 35 instructions. Each instructionis 14-bit words.
This instruction set organized by functional groups islisted in Table 1.
5
Table 1: PIC instruction set by functional groupsInstruction Type Definition Examples
MOVE The contents of a register are copiedto another.
MOVF, MOVWF, MOVLW
REGISTER Register operations affect only asingle register, and all except CLRW(clear W) operate on file registers.
ARITHMETIC Addition and subtraction in binarygives the same result as in decimal orhex. .
ADDWF, ADDLW, SUBWF,SUBLW
LOGIC Logic operations are carried out onbit pairs in two numbers to give theresult which would be obtained ifthey were fed to the correspondinglogic gate
ANDWF, ANDLW, IORWF,IORLW, XORWF, XORLW
TEST, SKIP & JUMP make decisions (conditional programbranches) which depend on someinput condition or the result of acalculation
Some instructions with alternate result destinations. The default destinationfor the result of an operation is the file register, but the working register Wis sometimes an option.
The instruction set can also organized by operational groups as shown inTable 2.1 – Table 2.3.
There are three basic categories: Byte-Oriented Instruction: F: File Register (or RAM) D: Destination
D=0: Destination W D=1: Destination File Register
Bit-Oriented Instruction: F: Register File where the Bit is located B: Bit Field
Literal and Control Operation: K: 8-bit constant
PIC16F84A Instruction set
PIC instruction set – description conventionTable 2.1- PIC instruction set :
Byte-oriented file register operations
6
Table 2.2 - PIC instruction set :Bit-oriented file register operations
Table 2.3 - PIC instruction set :
Literal and Control Operations
ADDLW ADDWF
7
8
9
10
11
12
13
14
Example
Describe briefly the operation of the followinginstructions: