Download presentation
1
Programming and Problem Solving
Chapter 4 Programming and Problem Solving
2
Flowcharting Flowchart
A graphical representation of processes (tasks) to be performed and the sequence to be followed in solving computational problem
3
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.
4
Example 4.1 (2 of 2) Start ORG 0x20 Process REG0 EQU 0x00
MOVLW 0x37 MOVWF REG0,0 MOVLW 0x92 MOVWF REG1,0 ADDWF REG0,0 MOVWF REG2, 0 SLEEP Process 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;
5
What is wrong with this? ORG 0x20 REG0 EQU 0x00 REG1 EQU 0x01
COMPREG EQU 0x10 CONST EQU 0x50 MOVLW CONST MOVWF COMPREG,0 MOVLW 0x37 MOVWF REG0,0 MOVLW 0x92 MOVWF REG1,0 ADDWF REG0,0 ;the result is in W 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
6
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.
7
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.
8
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. 8
9
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
10
Writing a Program Using an Assembler
The assembly language program includes: Program in mnemonics Assembler directives Comments
11
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
12
Assembly Language Format (2 of 2)
Format example Label Opcode Operand Comment START: MOVLW 0xF2 ;Load F2H in W ↑ ↑ ↑ ↑ Space Space Space Semicolon Or Colon
13
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
14
View Registers and Source Program in MPLAB Simulator
Data bytes Define labels and const Starting memory address Label
15
Format of Radixes Hexadecimal Decimal Binary ASCII 0x0F H`4F` 4F 4FH
`This stuff are interesting!`
16
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.
17
Using MPLAB IDE (2 of 6) To create a new project
Step 1: Open MPLAB IDE Select Project Project Wizard Select Device PIC18F452 Next
18
Using MPLAB IDE (3 of 6) Step 2: Select a Language Toolsuite: Microchip MPASM Toolsuite Next
19
Using MPLAB IDE (4 of 6) Step 3. Name Your Project: Illust4-4 Addition with Carry Check Browse MyProj\Ch04 Next
20
Using MPLAB IDE (5 of 6) Step 4: Add Add Source Files Next
21
Using MPLAB IDE (6 of 6) Summary Finish
22
Project Window
23
List of Files Generated by MPLAB Assembler
24
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
25
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
26
View Registers and Source Program in MPLAB Simulator
27
View Registers, Source Program, and Program Memory in MPLAB Simulator
28
Debugging a Program Single-step technique Breakpoint technique
Enables user to execute one instruction at a time and observe registers for expected results Breakpoint technique 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
29
Single-Step Technique
30
Breakpoint Technique
31
Tracing Code
32
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
33
Number Representation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.