Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming...
Transcript of Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming...
Chapter 4
Programming and Problem Solving 2/14/14
Flowcharting
o Flowchart n 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)
o Write instructions to load two bytes (37H and 92H) in data registers REG0 and REG1. Add the bytes and store the sum in REG2.
o Steps n Load the two bytes in data registers REG0 and
REG1. n Add the bytes. n Save the sum in data register REG2.
Example 4.1 (2 of 2)
ORG 0x20 REG0 EQU 0x00 REG1 EQU 0x01 REG2 EQU 0x02
MOVLW 0x37 MOVWF REG0,0 MOVLW 0x92 MOVWF REG1,0 ADDWF REG0,0 MOVWF REG2, 0 SLEEP
Processà
Startà
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 does it do? Is it correct? ORG 0x20 REG0 EQU 0x00 REG1 EQU 0x01 REG2 EQU 0x02 REG3 EQU 0x03
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
Find: Reg0, Reg1, Reg2, RegA, RegB, Reg10, Reg11, W
Find: Reg0, Reg1, Reg2, RegA, RegB, Reg10, Reg11, W
Draw the flowchart after you complete the program!
Steps in Writing and Executing Assembly Language Program
o Analyze the problem. o Draw a flowchart. o Convert the flowchart in mnemonics. o Look up Hex code and assign memory addresses. o Enter the Hex code into memory of a lab training board. o Execute the program. o Debug the program if necessary.
Illustrative Program: Addition With Carry Check
o Write instructions to load two bytes, Byte1 (F2H) and Byte2 (32H), in data registers REG0 and REG1 respectively and add the bytes.
o 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)
o Write instructions to load two bytes, Byte1 (F2H) and Byte2 (32H), in data registers REG0 and REG1 respectively and add the bytes.
o If the sum generates a carry, clear the data register REG2; otherwise, save the sum in REG2.
Assembler Directives o Assembly Language Format:
n Label Opcode Operand Comment n STATRT: MOVLW 0xF2 ;Load Operation
o Directives, also called pseudocodes, are instructions to the assembler n Define constants, labels, where to assemble a program, reserves memory for data n Different types:
o Object File (e.g., CODE, UDATA) o Control (e.g., #define, #include, EQU, ORG) o List (e.g., nolist, page, space) o Data (e.g., data, db, de, dw – all have to do with declaration)
o Directives do not translate to machine language à do not require memory assignment (come for free!) n Example BYTE EQU 0x02
o Label BYTE is being equated to value 2Hex n Example ORG 20H
o Assemble the program starting at location 20H
Assembler Directives - Examples
Format of Radixes o Hexadecimal
n 0x0F n H`4F` n 4F n 4FH
o Decimal n D`200`
o Binary n B`1001`
o ASCII n `This stuff are interesting!`
Using MPLAB IDE - Introduction
Integrated Development Environment (IDE) o Steps in using IDE
n Editing – generating the source code n Assembling – Converting mnemonics into hex and binary; generates the
object file n Linking – uses the object file and generates the binary code n Downloading – transfers the binary code (instructions) to the memory of the
target device n Executing – perform the tasks specified by instruction codes n Simulation – Execute the program on PC (also called the simulator) n Debugging – Going through the program step-by-step to find logical
problems in the instruction set
Writing a Program Using an Assembler
o The assembly language program includes: n Program in mnemonics n Assembler directives n Comments
Assembly Language Format (1 of 2)
o Typical statement of an assembly language source code has four fields: n Label n Opcode (operation code) n Operand (data, register, or memory address to be
operated on) n Comment
Assembly Language Format (2 of 2)
o Format example Label Opcode Operand Comment START: MOVLW 0xF2 ;Load F2H in W
↑ ↑ ↑ ↑ Space Space Space Semicolon Or Colon
View Registers and Source Program in MPLAB Simulator
Label
Data bytes
Define labels and const
Starting memory address
How to start a GOOD program:
Up to Here!
Using MPLAB IDE to Write, Assemble, and Build Project (1 of 6)
o Write source code using MPLAB editor. o Create a new project. o Select language tool suite. o Name your project. o Add files to assemble. o Build the project.
Using MPLAB IDE (2 of 6)
o To create a new project n Step 1: Open MPLAB
IDEà Select Project à Project Wizard à Select Deviceà PIC18F452 àNext
Using MPLAB IDE (3 of 6)
n Step 2: Select a Language Toolsuite: Microchip MPASM Toolsuite àNext
Using MPLAB IDE (4 of 6)
n Step 3. Name Your Project: Illust4-4 Addition with Carry Check Browse à MyProj\Ch04 à Next
Using MPLAB IDE (5 of 6)
n Step 4: Add à Add Source Files à Next
Using MPLAB IDE (6 of 6)
n Summary à Finish
Make Sure You Select ABSOLUTE ADDRESSING
Project Window
List of Files Generated by MPLAB Assembler
Understanding the List File o List file generated primarily for documentation o Includes seven columns
n Memory addresses where binary code is stored n Hex code n Line numbers n Contents of source file
o Labels o Opcode o Operands o Comments
Executing a Program Using Simulator o Steps in setting up MPLAB simulator
n Select Debugger à Select toolà MPLABSIM n Select Debugger à Settings à Change
frequency if necessary n 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 o Single-step technique
n Enables user to execute one instruction at a time and observe registers for expected results
o Breakpoint technique n Enables user to execute a group of instructions at a time
and observe registers for expected results o Tracing code
n MPLAB can track execution of each instruction and display data which can be examined for errors
Single-Step Technique
Breakpoint Technique
Tracing Code