0% found this document useful (0 votes)
14 views

CPE_416 Introduction-to-pic-instr_SET_NEXT_105637

The document provides an introduction to PIC assembly language, detailing the differences between high-level, assembly, and machine languages. It covers key concepts such as opcodes, operands, labels, and the instruction set of the PIC16F877 microcontroller. Additionally, it explains the stack process and the FIFO concept in data structures.

Uploaded by

olaben306
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

CPE_416 Introduction-to-pic-instr_SET_NEXT_105637

The document provides an introduction to PIC assembly language, detailing the differences between high-level, assembly, and machine languages. It covers key concepts such as opcodes, operands, labels, and the instruction set of the PIC16F877 microcontroller. Additionally, it explains the stack process and the FIFO concept in data structures.

Uploaded by

olaben306
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 66

INTRODUCTION TO

PIC ASSEMBLY
LANGUAGE

Prepared by : I.A Dauda CPE416 – Microprocessor &


Microcontroller Systems
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
3 Language
 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
7 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
8
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.
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
10
directives
 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
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
 END
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: 3
operand
1 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
16
Field
 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
18
set
 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
Instruction
functional
Definition
groups
Examples
Type
19 MOVE The contents of a register are MOVF, MOVWF,
copied to another. MOVLW
REGISTER Register operations affect CLRW, CLRF, DECF,
only a single register, and all INCF, SWAPF,
except CLRW (clear W) COMF, RLF, RRF,
operate on file registers. BCF, BSF
ARITHMETIC Addition and subtraction in ADDWF, ADDLW,
binary gives the same result SUBWF, SUBLW
as in decimal or hex. .
LOGIC Logic operations are carried ANDWF, ANDLW,
out on bit pairs in two IORWF, IORLW,
numbers to give the result XORWF, XORLW
which would be obtained if
they were fed to the
corresponding logic gate
TEST, SKIP & make decisions (conditional BTFSC, BTFSS,
JUMP program branches) which DECFSZ, INCFSZ,
PIC16F877A Instruction
20
set
 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
21 register operations
Table 2.2 - PIC instruction set :
Bit-oriented file register operations
22
Table 2.3 - PIC instruction set :
Literal and Control Operations
23
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
 Stack is a linear data structure
which follows a particular order in
which the operations are performed.
 The order may be Last In, First Out ( / Pull
LIFO ) data structure
 Stacks are used to temporarily store

items in memory (RAM)


 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):
 onlyone pointer to top element.
 PUSH and POP/PULL in different directions.
 wrap-around is not needed.

(PUSH)

Direction of
PULL operation 59
QUEUE
59

60
QUEUE
59

61
The two fundamental
60
operations:
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 62


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 :
10
67

- PULL should first read the data, 08

then increment SP.


X = $10, SP=[SP]+1

- Example:
63
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

You might also like