Docsity

Assembly Language Programming - Lecture Slides | ECE 372, Study notes of Microprocessors

Toggle sidebar
bg1
1
1
Chapter 2: PIC18 Assembly
Language Programming
Original slides by Han-Way Huang
Modified by Susan Lysecky
2
2.1 Objectives
zExplain the structure of an assembly language program
zUse assembler directives to allocate memory blocks and define constants
zWrite assembly programs to perform simple arithmetic operations
zWrite program loops to perform repetitive operations
zUse a flowchart to describe program flow
zCreate time delays of any length using program loops
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

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

27

#include <p18F8720.inc>

org 0x goto start org 0x retfie org 0x 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

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.

2.8 Writing Programs to Perform Arithmetic Computations

Example 2.

28

Example 2.6 Write a program to subtract 5 from memory locations 0x10 to 0x13.

2.8 Writing Programs to Perform Arithmetic Computations

Example 2.

Algorithm:

Step 1. Place 5 in the WREG register.

Step 2. Subtract WREG from the memory location 0x10 and leave the difference in the memory location 0x10.

Step 3. Subtract WREG from the memory location 0x11 and leave the difference in the memory location 0x11.

Step 4. Subtract WREG from the memory location 0x12 and leave the difference in the memory location 0x12.

Step 5. Subtract WREG from the memory location 0x13 and leave the difference in the memory location 0x13.

#include <p18F8720.inc>

org 0x goto start org 0x retfie org 0x retfie

start movlw 0x05 ; WREG ← 0x subwf 0x10,F,A ; 0x10 ← [0x10] – 0x subwf 0x11,F,A ; 0x11 ← [0x11] – 0x subwf 0x12,F,A ; 0x12 ← [0x12] – 0x subwf 0x13,F,A ; 0x13 ← [0x13] – 0x end

31

// Let data register 0x24 and 0x25 holds BCD numbers, the following instruction // sequence adds these two BCD numbers and saves the sum in 0x

2.8 Writing Programs to Perform Arithmetic Computations

z Binary Coded Decimal (BCD) Addition

z Decimal digits are encoded using 4 bits

z Two decimal digits are packed into a byte in memory

z After each addition, one needs to use the daw instruction to adjust and correct the result

movf 0x24, W, A addwf 0x25, W, A daw movwf 0x30, A

32

#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

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.

2.8 Writing Programs to Perform Arithmetic Computations

Example 2.

33

movf 0x10,W,A mulwf 0x11,A movff PRODH,0x21 ; upper byte of the product movff PRODL,0x20 ; lower byte of the product

2.8 Writing Programs to Perform Arithmetic Computations

Multiplication

z PIC18 has two instructions for 8-bit

multiplication

z mulwf f and mullw k.

z The products are stored in the PRODH:PRODL

register pair

z The multiplication of numbers larger than 8 bits

must be synthesized

z To perform multiplication operation on

numbers longer than 8 bits, the operand must

be broken down into 8-bit chunks

z Multiple 8-bit multiplications are performed

and the resultant partial products are aligned

properly and added together

z Two 16-bit numbers P and Q can be broken

down into as follows:

z P = PHPL

z Q = QHQL

34

2.8 Writing Programs to Perform Arithmetic Computations

Multiplication

z Instruction sequence to multiply two numbers that

are stored at N:N+1 and M:M+1:

movwf N,A ; " movf M+1,W,A mulwf N+1,A ; compute MH × NH movff PRODL,PR+ movff PRODH,PR+ movf M,W,A ; compute ML × NL mulwf N,A movff PRODL,PR movff PRODH,PR+ movf M,W,A mulwf N+1,A ; compute M (^) L × NH movf PRODL,W,A ; add M (^) L × 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 addwf PR+1,F,A ; “ movf PRODH,W,A ; “ addwfc PR+2,F,A ; “ movlw 0 ; “ addwfc PR+3,F,A ; add carry nop end

8-bit 8-bit 8-bit 8-bit

R + 3 R + 2 R + 1 R

upper byte

upper byte

upper byte

upper byte

lower byte

lower byte

lower byte

lower byte

ADDR

partial product PLQ^ L

partial product PH Q (^) L

partial product PLQ (^) H

partial product PH Q (^) H

Final product PQ

msb lsb

16-bit by 16-bit multiplication

37

2.9 Program Loops

Changing the Program Counter

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