Download Assembly Language Programming - Lecture Slides | ECE 372 and more Study notes Microprocessors in PDF only on Docsity!
1
Chapter 2: PIC18 Assembly
Language Programming
Original slides by Han-Way Huang
Modified by Susan Lysecky
2
2.1 Objectives
z Explain the structure of an assembly language program
z Use assembler directives to allocate memory blocks and define constants
z Write assembly programs to perform simple arithmetic operations
z Write program loops to perform repetitive operations
z Use a flowchart to describe program flow
z Create time delays of any length using program loops
3
2.3 Assembly Language Program Structure
z Components of an Assembly Program
z Assembler directives
z Assembly language instructions
z Comments
z Elements of an Assembly Language Statement
z Label
z Mnemonics
z Operands
z Comment
4
wait btfss sum,7 ; wait is a label
_again decf loop_cnt,F ; _again is a label
2.3 Assembly Language Program Structure
The Label Fields
z Must start from column 1 and followed by a tab, a space, a colon (:), or the end of a line
z Must start with an alphabetic character or underscore (_)
z May contain alphanumeric characters, underscores and question marks (?)
z May contain up to 32 characters and is case-sensitive by default
7
too_high decf mean,F,A ; prepare to search in the lower half
2.3 Assembly Language Program Structure
The Comment Field
z Optional
z A comment starts with a semicolon
z All characters to the right of the semicolon are ignored by the assembler
z Comments provide documentation to the instruction or assembler directives
z A comment may explain the function of a single statement or the function of a group of
instructions
label mnemonic operand field
comment
8
2.4 Assembler Directives
z Look like instructions in the assembly language program
z Most directives tell assembler to do something other than create machine code
z Provide programmer with a way to tell assembler how to process the assembly code
z MPASM directives
z Control directives – permit sections of conditionally assembled code
z Data directives – control allocation of memory and refer to data items symbolically
z Listing directives – control MPASM listing file format
z Macro directives – control execution and data allocation within macro body definitions
z Object file directives – used when creating an object file
9
if
else
endif
2.4 Assembler Directives
Control Directives
z Directives for conditional assembly
if version == 100 movlw D’10’ movwf io1,A else movlw D’26’ movwf io2,A endif
end ; end of program
10
2.4 Assembler Directives
Control Directives
z Declares the beginning of a section of
program code
z If no label is specified, the section is
named “.code”
z The starting address of the section is
either included in the directive or assigned
at link time if not specified in the directive
z Defines a text substitution
z Wherever name is encountered in the
code, string is substituted
z Delete a substitution string
[
reset code 0x
goto start
// defines a text substitution string
#define []
#define loop_cnt 30
#define sum3(x,y,z) (x + y + z)
#define seed 103
// deletes a substitution string
#undefine
13
db ,…, ; define 1 or multiple byte values
db “text_string” ; define a string
dw ,…, ; define 1 or multiple word constants
dw “text_string” ; define a string
dt , …, ; generates a series of retlw instructions
2.4 Assembler Directives
Data Directives
z Data Directives
14
led_pat db 0x30,0x80,0x6D,9x40,0x79,0x20,0x33,0x10,0x5B,0x
msg1 db “Please enter your choice (1/2):”,
array dw 0x1234,0x2300,0x40,0x
msg2 dw “The humidity is “,
results dt 1,2,3,4,
sum_hi set 0x
sum_lo set 0x
TH equ 200
TL equ 30
2.4 Assembler Directives
Data Directives
z Data Directive Examples
15
macro
endm
exitm
expand
local
noexpand
2.4 Assembler Directives
Macro Directives
z What is a macro?
z A group of instructions that are grouped
together and assigned a name
z One or multiple arguments can be input to a
macro
z By entering the macro name, the same group
of instructions can be duplicated in any place
of the program
z User program is made more readable by using
macros
z User becomes more productive by saving the
text entering time
endm
16
// Macro Definition Example
eeritual macro ; macro name is eeritual
movlw 0x55 ; instruction 1
movwf EECON2 ; instruction 2
movlw 0xAA ; instruction 3
movwf EECON2 ; instruction 4
endm
// Macro Call Example
eeritual ; this macro call causes the
; assembler to insert
; instruction 1 … instruction 4
2.4 Assembler Directives
Macro Directives
z Macro definition and call example
19
reset org 0x goto start … start …
led_pat org 0x1000 ; led_pat has the value of 0x db 0x7E,0x30,0x6D,0x79,0x33,0x5B,0x5F,0x70,0x7F,0x7B
2.4 Assembler Directives
Object File Directives
z sets the program origin for subsequent code
at the address defined in .
z
[
20
processor p18F8680 ; set processor type to PIC18F
2.4 Assembler Directives
Object File Directives
z sets the processor type
processor <processor_type>
21
2.5 Representing the Program Logic
z Program Development Procedure
z Problem definition
z Algorithm development using pseudo
code or flowchart
z Converting algorithm into assembly
instruction sequence
z Testing program using normal data,
marginal data, and erroneous data
z Algorithm provides overall plan for
solving problem as well as
documentation to the software to be
developed
z Many representations
Flowchart Symbols
Process^ Terminal
Input or output
Decision
yes
no
Subroutine
A
B
A
on-page connector
off-page connector
22
org 0x0000 ; program starting address after power on reset goto start org 0x … ; high-priority interrupt service routine org 0x … ; low-priority interrupt service routine start … … ; your program end
2.6 A Template for Writing Assembly Programs
25
Byte Order Issue
z This issue concerns how bytes are stored for multi-byte numbers
z The big-endian method stores the most significant byte at the lowest address and
stores the least significant byte in the highest address
z The little-endian method stores the most significant byte of the number at the
highest address and stores the least significant byte of the number in the lowest
address
z The 32-bit number 0x12345678 will stored as follows with two methods
Big-Endian Method Little-Endian Method
address
value
P P+1 P+2 P+3 P P+1 P+2 P+
12 34 56 78 78 56 34 12 (in hex)
26
Example 2.4 Write a program that adds the three numbers stored in data registers at 0x20, 0x30,
and 0x40 and places the sum in data register at 0x50.
2.8 Writing Programs to Perform Arithmetic Computations
Example 2.
Algorithm:
Step 1 Load the number stored at 0x20 into the WREG register
Step 2 Add the number stored at 0x30 and the number in the WREG register and leave the sum in the WREG register
Step 3 Add the number stored at 0x40 and the number in the WREG register and leave the sum in the WREG register
Step 4 Store the contents of the WREG register in the memory location at 0x
#include <p18F8720.inc> ; can be other processor org 0x goto start org 0x retfie org 0x retfie start movf 0x20,W,A ; WREG ← [0x20] addwf 0x30,W,A ; WREG ← [0x20]+[0x30] addwf 0x40,W,A ; WREG ← [0x20]+[0x30]+[0x40] movwf 0x50,A ; 0x50 ← sum (in WREG) end
z Microcontroller executes instruction sequentially in
normal condition
z PIC18 has a 21-bit program counter (PC) which is divided into three registers: PCL, PCH, and PCU z PCL can be accessed directly z PCH and PCU are not directly accessible
z One can accessed the values of PCH and PCU
indirectly by accessing the PCLATH and PCLATU
z Reading the PCL will cause the values of PCH and PCU to be copied into the PCLATH and PCLATU z Writing the PCL will cause the values of PCLATCH and PCLATU to be written into the PCH and PCU
z In normal program execution, the PC value is
incremented by either 2 or 4
z To implement a program loop, the processor needs to change the PC value by a value other than 2 or 4
38
2.9 Program Loops
Changing the Program Counter
z Instructions for changing the program counter
z Destination of a branch or goto instruction is normally specified by a label
39
2.9 Program Loops
Changing the Program Counter
z Instructions for changing the program counter (continued)
incf f,d,a // increment file register f
decf f,d,a // decrement file register f
z Instructions for changing register value by 1
40
i_cnt equ PRODL ; use PRODL as loop count clrf i_cnt,A i_loop … … ; i_cnt is incremented in the loop movlw n cpfseq i_cnt,A ; compare i_cnt with WREG and skip if equal goto i_loop ; executed when i_cnt ≠ loop limit
2.9 Program Loops
Changing the Program Counter
z Examples of program loops that execute n times
n equ 20 ; n has the value of 20 lp_cnt set 0x10 ; assign file register 0x10 to lp_cnt … movlw n movwf lp_cnt ; prepare to repeat the loop for n times loop … ; program loop … ; “ decfsz lp_cnt,F,A ; decrement lp_cnt and skip if equal to 0 goto loop ; executed if lp_cnt ≠ 0