OTHERWISE, the result should be in REG2 AND REG3=0
Download
Report
Transcript OTHERWISE, the result should be in REG2 AND REG3=0
Chapter 4
Programming and
Problem Solving
Flowcharting
Flowchart
A graphical representation
of processes (tasks) to be
performed and the
sequence to be followed in
solving computational
problem
Example 4.1 (1 of 2)
Write instructions to load two bytes (37H and
92H) in data registers REG0 and REG1. Add
the bytes and store the sum in REG2.
Steps
Load the two bytes in data registers REG0 and
REG1.
Add the bytes.
Save the sum in data register REG2.
Example 4.1 (2 of 2)
ORG
REG0
REG1
REG2
0x20
EQU
EQU
EQU
Start
Process
0x00
0x01
0x02
MOVLW 0x37
MOVWF REG0,0
MOVLW 0x92
MOVWF REG1,0
ADDWF REG0,0
MOVWF REG2, 0
SLEEP
Change the program: if the sum is larger than
50H then the result should be in REG3 AND
REG2=0; OTHERWISE, the result should be in
REG2 AND REG3=0;
What is wrong with this?
ORG
REG0
REG1
REG2
REG3
0x20
EQU
EQU
EQU
EQU
0x00
0x01
0x02
0x03
COMPREG
CONST
EQU
EQU
MOVLW
MOVWF
CONST
COMPREG,0
MOVLW
MOVWF
MOVLW
MOVWF
ADDWF
0x37
REG0,0
0x92
REG1,0
REG0,0
CPFSLT COMPREG,0
BRA WR_REG3
BRA WR_REG2
WR_REG3:
MOVWF
REG3, 0
BRA DONE_PROG
WR_REG2:
MOVWF
REG2, 0
DONE_PROG:
SLEEP
0x10
0x50
;the result is in W
Steps in Writing and Executing
Assembly Language Program
Analyze the problem.
Draw a flowchart.
Convert the flowchart in mnemonics.
Look up Hex code and assign memory addresses.
Enter the Hex code into memory of a lab training board.
Execute the program.
Debug the program if necessary.
Illustrative Program:
Addition With Carry Check
Write instructions to load two
bytes, Byte1 (F2H) and Byte2
(32H), in data registers REG0 and
REG1 respectively and add the
bytes.
If the sum generates a carry, clear
the data register REG2; otherwise,
save the sum in REG2.
Illustrative Program:
Addition With Carry Check (1 of 2)
Write instructions to load two
bytes, Byte1 (F2H) and Byte2
(32H), in data registers REG0 and
REG1 respectively and add the
bytes.
If the sum generates a carry, clear
the data register REG2; otherwise,
save the sum in REG2.
Integrated Development
Environment (IDE)
Steps in using IDE
Editing – generating the source code
Assembling – Converting mnemonics into hex and binary; generates the
object file
Linking – uses the object file and generates the binary code
Downloading – transfers the binary code (instructions) to the memory of the
target device
Executing – perform the tasks specified by instruction codes
Simulation – Execute the program on PC (also called the simulator)
Debugging – Going through the program step-by-step to find logical
problems in the instruction set
Writing a Program Using an Assembler
The assembly language program includes:
Program in mnemonics
Assembler directives
Comments
Assembly Language Format (1 of 2)
Typical statement of an assembly language
source code has four fields:
Label
Opcode (operation code)
Operand (data, register, or memory address to be
operated on)
Comment
Assembly Language Format (2 of 2)
Format example
Label
Opcode
Operand
START:
MOVLW
0xF2
↑
Space
Or Colon
Comment
;Load F2H in W
↑
↑
↑
Space Space Semicolon
Assembler Directives
ORG
Origin
END
End of assembly
EQU
Equate
SET
Defines an assembler variable
#INCLUDE Include resources from available library
RADIX
Number format
DB
Define byte
DW
Define word
CBLOCK Define a block of constants
ENDC
End of block of constants
RES
Reserve memory
View Registers and Source Program
in MPLAB
Simulator
Data
Define labels
and const
bytes
Starting
memory
address
Label
Format of Radixes
Hexadecimal
Decimal
D`200`
Binary
0x0F
H`4F`
4F
4FH
B`1001`
ASCII
`This stuff are interesting!`
Using MPLAB IDE to Write,
Assemble, and Build Project (1 of 6)
Write source code using MPLAB editor.
Create a new project.
Select language tool suite.
Name your project.
Add files to assemble.
Build the project.
Using MPLAB IDE (2 of 6)
To create a new project
Step 1: Open MPLAB
IDE Select Project
Project Wizard
Select Device
PIC18F452 Next
Using MPLAB IDE (3 of 6)
Step 2: Select
a Language
Toolsuite:
Microchip
MPASM
Toolsuite
Next
Using MPLAB IDE (4 of 6)
Step 3. Name
Your Project:
Illust4-4
Addition with
Carry Check
Browse
MyProj\Ch04
Next
Using MPLAB IDE (5 of 6)
Step 4: Add
Add
Source Files
Next
Using MPLAB IDE (6 of 6)
Summary
Finish
Project Window
List of Files Generated
by MPLAB Assembler
Understanding the List File
List file generated primarily for documentation
Includes seven columns
Memory addresses where binary code is stored
Hex code
Line numbers
Contents of source file
Labels
Opcode
Operands
Comments
Executing a Program Using
Simulator
Steps in setting up MPLAB simulator
Select Debugger Select tool MPLABSIM
Select Debugger Settings Change
frequency if necessary
Select View Watch Add registers to
observe
View Registers and Source Program
in MPLAB Simulator
View Registers, Source Program,
and Program Memory in MPLAB
Simulator
Debugging a Program
Single-step technique
Breakpoint technique
Enables user to execute one instruction at a time and
observe registers for expected results
Enables user to execute a group of instructions at a time
and observe registers for expected results
Tracing code
MPLAB can track execution of each instruction and
display data which can be examined for errors
Single-Step Technique
Breakpoint Technique
Tracing Code
Assembler Directive
Pseudo-code instructions
Define constants, labels, where to assemble a program, reserves memory for
data
Directives do not translate to machine language do not require memory
assignment (come for free!)
Example BYTE EQU 0x02
Label BYTE is being equated to value 2Hex
Example ORG 20H
Assemble the program starting at location 20H
Number Representation