PIC instruction set
Download
Report
Transcript PIC instruction set
TOPIC 3
INTRODUCTION TO
PIC ASSEMBLY LANGUAGE
Prepared by :
Puziah Yahaya
JKE, POLISAS / DEC 2010
E4160 – Microprocessor & Microcontroller System
Learning Outcomes
2
At the end of this topic, students should be able to:
Differentiate high level, assembly and machine language
Define the terms : opcode/mnemonic, operand, label, comment,
machine code, pseudo-operation
Classify PIC16F877 instruction set into the following groups: data
movement, arithmetic, logic, branch and other instructions.
Explain the function of PIC16F877 status register and the
changes to the flags when related instructions are executed.
Explain the stack process according to LIFO & FIFO concept and
the instructions related to stack.
Programming Language
3
The lowest-level language is called
Machine languages. It means those
languages which are closer to the
understanding of machine rather than
human beings. A machine language thus
comprises a string of binary O’s and 1’s.
Machine language is actually a coded set
of instructions for a particular CPU
(Central Processing Unit), and it is also
known as a machine code.
A machine language is designed to be
used by a computer without the need of
translation.
Machine Language
4
Disadvantage :
1. It is a machine dependent programming language.
Machine dependent means the program designed in one
type of machine or computer could not be run on other
type of computer or machine. So programs designed in
the machine language in one computer are not easily
portable to other computers.
2. It is a very difficult language to understand and learn.
If there is any problem in the program, written in machine
language, then it is very difficult to find out the correct
mistake.
Assembly language
5
Assembly language is one level above the machine
language. (Both Machine and Assembly language are
considered low-level language)
It uses certain predefined symbolic codes instead of binary
codes. These symbolic codes are called mnemonics.
Assembly language programs are translated into machine
language by a program called an assembler.
Assembler Process
High-level language
6
High-Level Language overcomes the limitation of
writing a program in Machine and Assembly
language as it is difficult and time consuming.
In High-Level Language, the programs can be written
using simple English words. Examples of High-Level
Language are BASIC, Fortran, COBOL, C, C++.
Programs written in high-level languages are
translated into machine language by a compiler.
Assembly language syntax
7
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
8
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.
Assembler directive
9
Assembler directives are instructions that are directed to
the assembler to do a specific thing.
It is not translated into machine code.
(Assembler directives are executed by the assembler at
assembly 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.
Example of assembler directives
10
EQU - Equate
Assigns a value to a symbol (same as = ) e.g.
TRISA
EQU
0x85
ORG - Origin
Sets the current origin to a new value. This is used to set the program or register address
during assembly. For example, ORG 0x00 tells the assembler to assemble all subsequent
code starting at address 0000H.
INCLUDE
END
An 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, to
add the standard header file for the PIC16F877 device to your assembly code, use:
#INCLUDE P16F877.INC
This directive is the last statement in an assembly language program. The END
directive terminates an assembly language program.
PIC Assembly Code
11
Consists of 4 fields:
1
3
operand
2
4
f = Source : name of special-purpose register or RAM variable
F(W) = Destination :
F – destination is f
W – destination is Working Register
Instruction format – Label
12
A label is used to represent a line or group of code, or a
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
begin with a leading underscore and number.
e.g. _2NDLOOP
be an assembler reserved word (mnemonic, directive, etc.).
Instruction format – Label
13
Labels may be up to 32 characters long.
By default they are case sensitive, but case
sensitivity may be overridden by a command-line
option (/c).
If a colon is used when defining a label, it is treated
as a label operator and not part of the label itself.
Example:
Here
NOP
GOTO Here
Instruction format - Opcode
14
This field consists of a symbolic operation code,
known as op-code.
The opcode describes the operation.
Symbolic op-codes (known as mnemonic) are
translated into machine language opcode.
Mnemonics are not case sensitive.
Example:
BSF
(Bit Set f)
CLRW (Clear W)
Instruction format - Operand Field
15
Operands give information to the instruction on the data
that should be used and the storage location for the
instruction.
Operands must be separated from mnemonics by one or
more spaces, or tabs.
It may has one, two or no operands at all. Multiple
operands 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 – Comment Field
16
Comments are text explaining the operation of a line or
lines of code.
A semicolon (;) marks the beginning of a comment
A semicolon in the beginning of a line makes it all a
comment line. All characters following the semicolon are
ignored through the end of the line.
Good programming practice dictates the use of a
comment on almost every line.
Example:
;Statement line with a comment field
BSF PortA,0
;set pin 0 of PortA
Example MPASM Assembler Source Code
17
PIC16F877A Instruction set
18
Each microcontroller family has its own set of
instructions, which carry out essentially the same set of
operations, but using different syntax.
The PIC16F877 only has 35 instructions. Each instruction
is 14-bit words.
This instruction set organized by functional groups is
listed in Table 1.
Table 1: PIC instruction set by functional groups
19
Instruction Type
Definition
Examples
MOVE
The contents of a register are copied
to another.
MOVF, MOVWF, MOVLW
REGISTER
Register operations affect only a
single register, and all except CLRW
(clear W) operate on file registers.
CLRW, CLRF, DECF, INCF,
SWAPF, COMF, RLF, RRF,
BCF, BSF
ARITHMETIC
Addition and subtraction in binary
ADDWF, ADDLW, SUBWF,
gives the same result as in decimal or SUBLW
hex. .
LOGIC
Logic operations are carried out on
bit pairs in two numbers to give the
result which would be obtained if
they were fed to the corresponding
logic gate
ANDWF, ANDLW, IORWF,
IORLW, XORWF, XORLW
TEST, SKIP & JUMP
make decisions (conditional program
branches) which depend on some
input condition or the result of a
calculation
BTFSC, BTFSS, DECFSZ,
INCFSZ, GOTO, CALL,
RETURN, RETLW, RETFIE
CONTROL
NOP, SLEEP, CLRWDT
PIC16F877A Instruction set
20
Some instructions with alternate result destinations. The default destination
for the result of an operation is the file register, but the working register W
is sometimes an option.
The instruction set can also organized by operational groups as shown in
Table 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
Table 2.1- PIC instruction set :
Byte-oriented file register operations
21
Table 2.2 - PIC instruction set :
Bit-oriented file register operations
22
Table 2.3 - PIC instruction set :
23
Literal and Control Operations
PIC instruction set – description convention
24
ADDLW
25
ADDWF
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Stack
58
A stack is a Last In, First Out ( LIFO )
data structure
Stacks are used to temporarily store
items in memory (RAM)
/ Pull
When an item enters the stack it is said
to be pushed on the stack. When an item
leaves the stack, it is said to be pulled
off the stack.
58
59
In contrast to the Queue (Q):
only
one pointer to top element.
PUSH and POP/PULL in different directions.
wrap-around is not needed.
(PUSH)
Direction of
PULL operation
59
The two fundamental operations:
60
1. Push
- push an element on the top
- PUSH should first decrement SP,
$00FB
$00FC
then store the data,
-
$00FD
$00FE
Instruction used :
$00FF
Example:
Note: [ ] means the location pointed by SP
60
2. Pull
61
- pull the top element from the stack
- removes an item from the top of the
list, and returns this value to the caller.
- Instruction used :
- PULL should first read the data,
10
X = $10, SP=[SP]+1
67
08
then increment SP.
- Example:
61
Note: [ [ ] ] means the content of location pointed by SP
FIFO
62
FIFO is an acronym for First In, First Out.
This expression describes the principle of a queue (Q) : what
comes in first is handled first, what comes in next waits until the
first is finished, etc.
It is a very common data structure used for I/O interfacing
Example :The ASCII codes are PUT in a FIFO whenever you hit
the key. A FIFO is also used when you ask the computer to
print a file.
FIFO Concept
63
FIFO/Queue operation:
Needs
two pointers – PUTPT and GETPT
PUTPT and GETPT in same directions.
wrap-around (items go on the rear and come off of the front of
the queue. ) is needed.
References
64
PIC Instruction Set and Some Tips for Programming,
Dr. Charles J. Kim, Howard University
Microchip PIC16F87X Data Sheet
Getting Started with Development Tools, Microchip.com
Stack, http://en.wikipedia.org