Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming...

35
Chapter 4 Programming and Problem Solving 2/14/14

Transcript of Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming...

Page 1: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

Chapter 4

Programming and Problem Solving 2/14/14

Page 2: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

Flowcharting

o  Flowchart n  A graphical representation

of processes (tasks) to be performed and the sequence to be followed in solving computational problem

Page 3: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

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.

Page 4: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

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;

Page 5: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

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!

Page 6: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

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.

Page 7: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

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.

Page 8: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

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.

Page 9: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

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

Page 10: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

Assembler Directives - Examples

Page 11: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

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!`

Page 12: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

Using MPLAB IDE - Introduction

Page 13: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

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

Page 14: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

Writing a Program Using an Assembler

o  The assembly language program includes: n  Program in mnemonics n  Assembler directives n  Comments

Page 15: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

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

Page 16: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

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

Page 17: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

View Registers and Source Program in MPLAB Simulator

Label

Data bytes

Define labels and const

Starting memory address

Page 18: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

How to start a GOOD program:

Page 19: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

Up to Here!

Page 20: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

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.

Page 21: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

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

Page 22: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

Using MPLAB IDE (3 of 6)

n  Step 2: Select a Language Toolsuite: Microchip MPASM Toolsuite àNext

Page 23: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

Using MPLAB IDE (4 of 6)

n  Step 3. Name Your Project: Illust4-4 Addition with Carry Check Browse à MyProj\Ch04 à Next

Page 24: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

Using MPLAB IDE (5 of 6)

n  Step 4: Add à Add Source Files à Next

Page 25: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

Using MPLAB IDE (6 of 6)

n  Summary à Finish

Make Sure You Select ABSOLUTE ADDRESSING

Page 26: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

Project Window

Page 27: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

List of Files Generated by MPLAB Assembler

Page 28: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

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

Page 29: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

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

Page 30: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

View Registers and Source Program in MPLAB Simulator

Page 31: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

View Registers, Source Program, and Program Memory in MPLAB Simulator

Page 32: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

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

Page 33: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

Single-Step Technique

Page 34: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

Breakpoint Technique

Page 35: Chapter 4web.sonoma.edu › ... › 310_arm › lectures › chapter4_.pdf · Chapter 4 Programming and Problem Solving 2/14/14. Flowcharting o Flowchart ! A graphical representation

Tracing Code