Assembly Programming
TOPIC 3
Able to explain the structure of an assembly
language program
Able to use assembler directives to allocate
memory blocks and constants
Learning Able to write assembly programs to perform
simple arithmetic operations
Outcomes Able to write program loops to perform repetitive
operations
Able to use a flowchart to describe program flow
Assembly Program Development
Editor - MPLab
Source Module
Object Module /
Machine code
Micro-controller Board
Source module ASM file for PIC18 ( .ASM) Listing file produced by MPLAB IDE ( .LST) Object module/load module ( .HEX file for PIC18)
Example of MPLAB output
Terminal A
Flowchart
Process Subroutine
Input or
output B
• Flowchart is:
• A form of program documentation. off-page connector
• A tool for developing program logic
flow. yes
Decision A
on-page connector
no
Pseudo Code/Algorithm
• Algorithm Representation
Step 1
…
Step 2
…
Step 3
…
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.
[0x20]→WREG
[0X30]+WREG→W
[0X40]+WREG→W
WREG→[0X50]
• Source Statement Format
PIC18 (Syntax)
• Label
Assembly • Operation
• Operand
Language • Comment
Label Operation operand ; comment
Source Statement
Format-Label Field
Must start from column 1 and followed by
a tab, a space, a colon(:),or the end of a
line. Wait: btfss sum,7 ; Wait is a label
Must start with an alphabetic character or
underscore (_). _again decf loop_cnt,F; _again is a label
May contain alphanumeric characters,
underscores and question marks(?).
May contain up to 32 characters and is
case-sensitive by default.
Source Statement Format- Mnemonic/Operation 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
Source Statement Format- Operand Field
➢The operand (s) follows the instruction mnemonic.
➢Provides the operands for an instruction or arguments for an assembler directive.
➢Must be separated from the mnemonic field by one or more spaces or tabs.
➢Multiple operands are separated by commas.
movff 0x30,0x400 ; “0x30,0x400” is the operand field
decf loop_cnt,F ; label loop_cnt is the operand
true equ 1 ; ‘1’ is the argument for equ
Source Statement Format-Comment Field
➢ Is optional
➢ A comment starts with a semicolon(;)
➢ All characters to the right of the semicolon are ignored by the assembler
➢ Comments provide documentation to the instruction or assembler directives
➢ A comment may explain the function of a single statement or the function of a group of instructions
too_high ADDWF 0X51,F,A ; prepare to search in the lower half
“too_high” is a label
“ADDWF” is a mnemonic
“0X51,F,A” is the operand field
“; prepare to search in the lower half” is a comment
Assembly Program Template
org 0x0000 ; program starting address
goto start ; after power on reset
org 0x08 ; high-priority interrupt
goto Hi_Intr ;service routine
org 0x18 ; low-priority interrupt
goto Low_Intr;service routine
start …
… ; your program
end
Program Template Before Interrupts Have Been
Covered
org 0x0000 ; program starting address after power on reset
goto start
org 0x08
retfie ; high-priority interrupt service routine
org 0x18
retfie ; low-priority interrupt service routine
start …
… ; your program
end
Always write sufficient Comments in
your Assembly Program so that your
program is self-explained and
documented for later modification!