Chapter 2 Lecture 3&4 ASM
Chapter 2 Lecture 3&4 ASM
Chapter 2
No.2-1
The PIC18 Microcontroller
- Assembler directives
- Comments
No.2-2
The PIC18 Microcontroller
- Label
- Mnemonics
- Operands
- Comment
No.2-3
The PIC18 Microcontroller
Label Field
No.2-4
The PIC18 Microcontroller
Mnemonic Field
- Can be an assembly instruction mnemonic or
assembly directive
- Must begin in column two or greater
- Must be separated from the label by a colon, one or
more spaces or tabs
addlw 0x10 ; addlw is the mnemonic field
loop incf 0x30,W,A ; incf is a mnemonic
false equ 0 ; equ is the mnemonic field
No.2-5
The PIC18 Microcontroller
No.2-6
The PIC18 Microcontroller
Comment field
- Is optional
No.2-7
The PIC18 Microcontroller
“too_high” is a label
“decf” is a mnemonic
No.2-8
The PIC18 Microcontroller
Assembler Directives
- Control directives
- Data directives
- Listing directives
- Macro directives
No.2-9
The PIC18 Microcontroller
Control Directives
Example.
if version == 100
movlw D‟10‟
movwf io_1,A
else
movlw D‟26‟
movwf io_2,A
endif
No.2-10
The PIC18 Microcontroller
#define loop_cnt 30
#define sum3(x,y,z) (x + y + z)
#define seed 103
No.2-11
The PIC18 Microcontroller
radix <default_radix>
- sets the default radix for data expression
- the default radix values are: hex, dec, or oct
No.2-12
The PIC18 Microcontroller
while <expr>
endw
- The lines between while and endw are assembled as long as <expr> is true.
Data Directives
db <expr>,…,<expr> ; define 1 or multiple byte values
db “text_string” ; define a string
dw <expr>,…,<expr> ; define 1 or multiple word constants
dw “text_string” ; define a string
dt <expr>, …, <expr> ; generates a series of retlw instructions
<label> set <expr> ; assign a value (<expr>) to label
<label> equ <expr> ; defines a constant
No.2-13
The PIC18 Microcontroller
led_pat db 0x30,0x80,0x6D,9x40,0x79,0x20,0x33,0x10,0x5B,0x08
array dw 0x1234,0x2300,0x40,0x33
results dt 1,2,3,4,5
TH equ 200
TL equ 30
No.2-14
The PIC18 Microcontroller
What is a macro?
Macro Directives
macro
endm
exitm
No.2-15
The PIC18 Microcontroller
No.2-16
The PIC18 Microcontroller
No.2-17
The PIC18 Microcontroller
banksel <label>
- generate the instruction sequence to set active data bank to the one where
<label> is located
- <label> must have been defined before the banksel directive is invoked.
No.2-18
The PIC18 Microcontroller
- sets the program origin for subsequent code at the address defined in <expr>.
- <label> will be assigned the value of <expr>.
No.2-19
The PIC18 Microcontroller
No.2-20
The PIC18 Microcontroller
Algorithm Representation
Step 1
…
Step 2
…
Step 3
…
No.2-21
The PIC18 Microcontroller
Flowchart Symbols
Terminal A
Process Subroutine
Input or
output B
off-page connector
yes
Decision A
on-page connector
no
No.2-22
The PIC18 Microcontroller
No.2-23
The PIC18 Microcontroller
No.2-24
The PIC18 Microcontroller
Case Issue
- The PIC18 instructions can be written in either uppercase or lowercase.
- MPASM allows the user to include “p18Fxxxx.inc” file to provide register
definitions for the specific processor.
- All special function registers and bits are defined in uppercase.
- The convention followed in this text is: using lowercase for instructions and
directives, using uppercase for special function registers.
No.2-25
The PIC18 Microcontroller
No.2-26
The PIC18 Microcontroller
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.
Solution:
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 0x50.
No.2-27
The PIC18 Microcontroller
No.2-28
The PIC18 Microcontroller
Example 2.5 Write a program to add two 24-bit numbers stored at 0x10~0x12 and
0x13~0x15 and leave the sum at 0x20..0x22.
Solution:
#include <p18F8720.inc>
org 0x00
goto start
org 0x08
retfie
org 0x18
retfie
start movf 0x10,W,A ; WREG [0x10]
addwf 0x13,W,A ; WREG [0x13] + [0x10]
movwf 0x20,A ; 0x20 [0x10] + [0x13]
movf 0x11,W,A ; WREG [0x11]
addwfc 0x14,W,A ; WREG [0x11] + [0x14] + C flag
movwf 0x21,A ; 0x21 [WREG]
movf 0x12,W,A ; WREG [0x12]
addwfc 0x15,W,A ; WREG [0x12] + [0x15] + C flag
movwf 0x22,A ; 0x22 [WREG]
end
No.2-29
The PIC18 Microcontroller
No.2-30
The PIC18 Microcontroller
#include <p18F8720.inc>
org 0x00
goto start
org 0x08
retfie
org 0x18
retfie
start movlw 0x05 ; WREG 0x05
subwf 0x10,F,A ; 0x10 [0x10] – 0x05
subwf 0x11,F,A ; 0x11 [0x11] – 0x05
subwf 0x12,F,A ; 0x12 [0x12] – 0x05
subwf 0x13,F,A ; 0x13 [0x13] – 0x05
end
No.2-31
The PIC18 Microcontroller
Example 2.7 Write a program that subtracts the number stored at 0x20..0x23
from the number stored at 0x10..0x13 and leaves the difference at 0x30..0x33.
Solution:
Start
WREG [0x20]
WREG [0x21]
Three-operand
0x31 [0x11] - [WREG] - B
subtraction
WREG [0x22]
Three-operand
0x32 [0x12] - [WREG] - B subtraction
WREG [0x23]
Three-operand
0x33 [0x13] - [WREG] - B subtraction
Stop
No.2-32
The PIC18 Microcontroller
No.2-33
The PIC18 Microcontroller
No.2-34
The PIC18 Microcontroller
Example 2.9 Write an instruction sequence that adds the decimal numbers stored at
0x10...0x13 and 0x14...0x17 and stores the sum in 0x20..0x23.
Solution:
#include <p18F8720.inc>
…
start movf 0x10,W ; add the least significant byte
addwf 0x14,W ; “
daw ; adjust for valid BCD
movwf 0x20 ; save in the destination
movf 0x11 ; add the second to least significant byte
addwfc 0x15,W ; “
daw ; “
movwf 0x21 ; “
movf 0x12 ; add the second to most significant byte
addwfc 0x16 ; “
daw ; “
movwf 0x22 ; “
movf 0x13 ; add the most significant byte
addwfc 0x17 ; “
daw ; “
movwf 0x23 ; “
end
No.2-35
The PIC18 Microcontroller
Multiplication
- PIC18 has two instructions for 8-bit multiplication: mulwf f and mullw k.
- The products are stored in the PRODH:PRODL register pair.
- The multiplication of numbers larger than 8 bits must be synthesized.
- The following instruction sequence performs 8-bit multiplication operation:
movf 0x10,W,A
mulwf 0x11,A
movff PRODH,0x21 ; upper byte of the product
movff PRODL,0x20 ; lower byte of the product
- To perform multiplication operation on numbers longer than 8 bits, the operand must be
broken down into 8-bit chunks. Multiple 8-bit multiplications are performed and the
resultant partial products are aligned properly and added together.
- Two 16-bit numbers P and Q can be broken down into as follows:
P = PHPL
Q = QHQL
No.2-36
The PIC18 Microcontroller
Note: msb stands for most significant byte and lsb stands for least significant byte
No.2-37
The PIC18 Microcontroller
Instruction sequence to multiply two numbers that are stored at N:N+1 and M:M+1:
movf M+1,W,A
mulwf N+1,A ; compute MH NH
movff PRODL,PR+2
movff PRODH,PR+3
movf M,W,A ; compute ML NL
mulwf N,A
movff PRODL,PR
movff PRODH,PR+1
movf M,W,A
mulwf N+1,A ; compute ML NH
movf PRODL,W,A ; add ML NH to PR
addwf PR+1,F,A ; "
movf PRODH,W,A ; "
addwfc PR+2,F,A ; "
movlw 0 ; "
addwfc PR+3,F,A ; add carry
movf M+1,W,A
mulwf N,A ; compute MH NL
movf PRODL,W,A ; add MH NL to PR
No.2-38
The PIC18 Microcontroller
MNxPQS3S2S1S0
No.2-39
The PIC18 Microcontroller
Program Loops
1. Do S forever
No.2-40
The PIC18 Microcontroller
i i1 i i2
no no
i i2 ? i i1 ?
yes yes
S S
ii+1 i i- 1
No.2-41
The PIC18 Microcontroller
3. while C do S
true
C S
false
4. repeat S until C
initialize C
true
C
fals
e
Figure 2.8 The Repeat ... Until looping construct
No.2-42
The PIC18 Microcontroller
- PIC18 has a 21-bit program counter (PC) which is divided into three
- PCL can be accessed directly. However, PCH and PCU are not directly
accessible.
- One can accessed the values of PCH and PCU indirectly by accessing
No.2-43
The PIC18 Microcontroller
- Reading the PCL will cause the values of PCH and PCU to be
- Writing the PCL will cause the values of PCLATCH and PCLATU
2 or 4.
No.2-44
The PIC18 Microcontroller
BCC n: jump to the instruction with address equals to PC+2+n if the condition code
CC is true.
CC can be any one of the following:
C: C flag is set to 1
NC: C flag is 0
N: N flag is set to 1 which indicates that the previous operation result was negative
NN: N flag is 0 which indicates non-negative condition
NOV: V flag is 0 which indicates there is no overflow condition
NZ: Z flag is 0 which indicates the previous operation result was not zero
OV: V flag is 1 which indicates the previous operation caused an overflow
Z: Z flag is 1 which indicates the previous operation result was zero
No.2-45
The PIC18 Microcontroller
incf f,d,a
decf f,d,a
No.2-46
The PIC18 Microcontroller
Example 1
No.2-47
The PIC18 Microcontroller
Example 2
No.2-48
The PIC18 Microcontroller
Example 2.12 Write a program to compute 1 + 2 + 3 + … + n and save the sum at 0x00
and 0x01.
Solution:
1. Program logic
Start
i1
sum 0
yes
i > n?
no
sum sum + i
i i+ 1
Stop
No.2-49
The PIC18 Microcontroller
#include <p18F8720.inc>
radix dec
n equ D'50'
sum_hi set 0x01 ; high byte of sum
sum_lo set 0x00 ; low byte of sum
i set 0x02 ; loop index i
org 0x00 ; reset vector
goto start
org 0x08
retfie
org 0x18
retfie
start clrf sum_hi,A ; initialize sum to 0
clrf sum_lo,A ; "
clrf i,A ; initialize i to 0
incf i,F,A ; i starts from 1
sum_lp movlw n ; place n in WREG
cpfsgt i,A ; compare i with n and skip if i > n
bra add_lp ; perform addition when i 50
bra exit_sum ; it is done when i > 50
No.2-50
The PIC18 Microcontroller
No.2-51
The PIC18 Microcontroller
Start
Example 2.13
Write a program
arr_max arr[0]
to find the i1
largest element
stored in the no
array that is i n?
yes
stored in data
memory no
arr[i] > arr_max?
locations from
0x10 to 0x5F. yes
arr_max arr[i]
i i+ 1
Stop
No.2-52
The PIC18 Microcontroller
No.2-53
The PIC18 Microcontroller
No.2-54
The PIC18 Microcontroller
Program memory
Table pointer
Table latch
TBLPTRU TBLPTRH TBLPTRL
TABLAT
No.2-55
The PIC18 Microcontroller
Program memory
Table pointer
Table latch
TBLPTRU TBLPTRH TBLPTRL
TABLAT
• TBLPTRU (6 bits)
• TBLPTRH (8 bits)
• TBLPTRL (8 bits)
No.2-56
The PIC18 Microcontroller
No.2-57
The PIC18 Microcontroller
tblrd
No.2-58
The PIC18 Microcontroller
Logic Instructions
No.2-59
The PIC18 Microcontroller
movlw B‟11000001‟
iorwf PORTA,F,A
movlw B‟11101001
andwf PORTB,F,A
movlw B‟10101010‟
xorwf PORTC
No.2-60
The PIC18 Microcontroller
Example 2.16 Write a program to find out the number of elements in an array of 8-bit
elements that are a multiple of 8. The array is in the program memory.
Solution:
Start
yes
count count + 1
ii-1
no
i = 0?
yes
Stop
No.2-61
The PIC18 Microcontroller
#include <p18F8720.inc>
ilimit equ 0x20 ; loop index limit
count set 0x00
ii set 0x01 ; loop index
mask equ 0x07 ; used to masked upper five bits
org 0x00
goto start
… ; interrupt service routines
start clrf count,A
movlw ilimit
movwf ii ; initialize ii to ilimit
movlw upper array
movwf TBLPTRU,A
movlw high array
movwf TBLPTRH,A
movlw low array
movwf TBLPTRL,A
movlw mask
i_loop tblrd*+ ; read an array element into TABLAT
andwf TABLAT,F,A
bnz next ; branch if not a multiple of 8
No.2-62
The PIC18 Microcontroller
No.2-63
The PIC18 Microcontroller
- The PIC18 uses a crystal oscillator or a RC circuit to generate the clock signal
needed to control its operation.
- The instruction execution time is measured by using the instruction cycle
clock.
- One instruction cycle is equal to four times the crystal oscillator clock period.
- Select an appropriate instruction that will take a multiple of 10 or 20
instruction cycles to execute.
- A desirable time delay is created by repeating the chosen instruction sequence
for certain number of times.
No.2-64
The PIC18 Microcontroller
radix dec
loop_cnt equ PRODL
movlw 250
movwf loop_cnt,A
again dup_nop 17 ; insert 17 nop instructions
decfsz loop_cnt,F,A ; 1 instruction cycle
bra again ; 2 instruction cycles
No.2-65
The PIC18 Microcontroller
Example 2.18 Write a program to create a time delay of 100 ms for the demo
board that uses a 40 MHz crystal oscillator to operate.
Solution: Repeat the previous instruction sequence for 200 times can create a
100 ms time delay.
radix dec
lp_cnt1 equ 0x21
lp_cnt2 equ 0x22
movlw 200
movwf lp_cnt1,A
loop1 movlw 250
movwf lp_cnt2,A
loop2 dup_nop 17 ; 17 instruction cycles
decfsz lp_cnt2,F,A ; 1 instruction cycle (2 when [lp_cnt1] = 0)
bra loop2 ; 2 instruction cycles
decfsz lp_cnt1,F,A
bra loop1
No.2-66
The PIC18 Microcontroller
Rotate Instructions
7 6 5 4 3 2 1 0 C
7 6 5 4 3 2 1 0
No.2-67
The PIC18 Microcontroller
C 7 6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
No.2-68
The PIC18 Microcontroller
Example 2.19 Compute the new values of the data register 0x10 and the C flag after the
execution of the rlcf 0x10,F,A instruction. [0x10] = 0xA9, C = 1
Solution:
The result is
0 1 0 1 0 1 0 0 1
Original value New value
[0x10] = 1010 1001 [0x10] = 01010010
1 0 1 0 1 0 0 1 0 C=0 C=1
Example 2.20 Compute the new values of the data register 0x10 and the C flag after the
execution of the rrcf 0x10,F,A instruction. [0x10] = 0xC7, C = 1
Solution:
The result is
1 1 0 0 0 1 1 1 1
Original value New value
[0x10] = 1100 0111 [0x10] = 1110 0011
1 1 1 0 0 0 1 1 1 C=1 C=1
No.2-69
The PIC18 Microcontroller
Example 2.21 Compute the new values of the data memory location 0x10 after the
execution of the rrncf 0x10,F,A instruction and the rlncf 0x10,F,A instruction,
respectively. [0x10] = 0x6E
Solution:
The result is
0 1 1 0 1 1 1 0
original value new value
[0x10] = 0110 1110 [0x10] = 0011 0111
0 0 1 1 0 1 1 1
The result is
0 1 1 0 1 1 1 0
Before After
[0x10] = 0110 1110 [0x10] = 1101 1100
1 1 0 1 1 1 0 0
No.2-70
The PIC18 Microcontroller
Examples
No.2-71
The PIC18 Microcontroller
No.2-72
The PIC18 Microcontroller
No.2-73