Assembly Language Programming and Debugging - Lecture Notes | EEL 4744, Study notes of Microprocessors

Material Type: Notes; Professor: Li; Class: MICROPROCESSOR APPLIC; Subject: ENGINEERING: ELECTRICAL; University: University of Florida; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-u68-1
koofers-user-u68-1 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Dr. Tao Li 1
EEL 4744C: Microprocessor Applications
Lecture 3
Part 1
Assembly Language Programming
and Debugging
Dr. Tao Li 2
Microcontrollers and Microcomputers: Chapter
5
Or
Software and Hardware Engineering (new
version): Chapter 2.4, Chapter 5.3, Ch apter 6
Plus
CPU 12 Reference Manual: Chapters 1, 2, and 3
Reading Assignment
Dr. Tao Li 3
Gives you a better appreciation of how the
hardware works than high-level languages
Resources are given by the programmer’s
model
These include registers, hardware resources in
CPU, and memory used for data storage
Look through the instructions in a category,
find the mnemonic for the required ope ration,
then find the correct addressing mode
Assembly Language
Dr. Tao Li 4
Converts (assembles) a source file into binary
codes (machine codes) executed by the
computer
1-to-1 correspondence between assembly language
statements and the machine code in memory
Assembly source code fields: 4 of these
Label Field
Operation Code Field
Operand Field
Comment Field
The Assembler
Dr. Tao Li 5
Label field
Optional and provides a symbolic memory ref erence
Represents the address of the first byte of an
instruction or a data element
Also used to define constants
Usually starts with an alph abetic character, may
contain digits and other characters
Operation code field
Contains either a mnemonic for the operation or an
assembler directive or pseudo-operation
Mnemonics are assembled into code to be placed into
memory
Assembler directives or pseudo-operations are
instructions to direct the assembler how to do its job
Assembly Source Code Fields
Dr. Tao Li 6
Operation code field (continued)
In the following code exam ple, the ORG (for
origination) directive specifies where the code is
located in memory
When assembler sees the ORG directive, it sets the
value of current location counter to $1000
Assembly Source Code Fields
Use label to
specify target
address
(relative
address)
ORG directive
helps
determine
actual address
pf3
pf4
pf5

Partial preview of the text

Download Assembly Language Programming and Debugging - Lecture Notes | EEL 4744 and more Study notes Microprocessors in PDF only on Docsity!

Dr. Tao Li 1

EEL 4744C: Microprocessor Applications

Lecture 3

Part 1

Assembly Language Programming

and Debugging

Dr. Tao Li 2

• Microcontrollers and Microcomputers: Chapter

Or

• Software and Hardware Engineering (new

version): Chapter 2.4, Chapter 5.3, Chapter 6

Plus

• CPU 12 Reference Manual: Chapters 1, 2, and 3

Reading Assignment

Dr. Tao Li 3

• Gives you a better appreciation of how the

hardware works than high-level languages

• Resources are given by the programmer’s

model

• These include registers, hardware resources in

CPU, and memory used for data storage

• Look through the instructions in a category,

find the mnemonic for the required operation,

then find the correct addressing mode

Assembly Language

Dr. Tao Li 4

• Converts (assembles) a source file into binary

codes (machine codes) executed by the

computer

– 1-to-1 correspondence between assembly language

statements and the machine code in memory

• Assembly source code fields: 4 of these

– Label Field

– Operation Code Field

– Operand Field

– Comment Field

The Assembler

Dr. Tao Li 5

• Label field

– Optional and provides a symbolic memory reference

– Represents the address of the first byte of an

instruction or a data element

– Also used to define constants

– Usually starts with an alphabetic character, may

contain digits and other characters

• Operation code field

  • Contains either a mnemonic for the operation or an

assembler directive or pseudo-operation

  • Mnemonics are assembled into code to be placed into

memory

  • Assembler directives or pseudo-operations are

instructions to direct the assembler how to do its job

Assembly Source Code Fields

Dr. Tao Li 6

• Operation code field (continued)

– In the following code example, the ORG (for

origination) directive specifies where the code is

located in memory

– When assembler sees the ORG directive, it sets the

value of current location counter to $

Assembly Source Code Fields

Use label to

specify target

address

(relative

address)

ORG directive

helps

determine

actual address

Dr. Tao Li 7

• Operand field

– Can be: names of registers, numeric/symbolic

constants, labels, algebraic expressions to be

evaluated by assembler

– The following 2 instructions load the A, B registers

from memory location DATA1 and DATA1+

• Comment field

– Ignored during assembly

– May have source code lines that are only comments

or blank

– For some assemblers, beginning of comment fields

are denoted by “;” or “*” characters

Assembly Source Code Fields

Label Numeric constant

Dr. Tao Li 8

• Assembler directives or pseudo-operations

field

– We have seen the ORG example

– There are others to define symbols, provide data in

memory locations, reserve memory locations for data

storage, define macros, etc.

Assembly Source Code Fields

Dr. Tao Li 9

• What is it?

– An assembler in which frequently used assembly

instructions can be collected into a single statement

– It makes the assembler more like a high-level

language

• 3 stages of using a macro: macro definition,

macro invocation, macro expansion

Macro Assembler

Assembler directives

Dr. Tao Li 10

• Each time a macro is invoked, the assembler expands

the macro “in line”. A subroutine code is included only

once. So the macro’s make your program larger

• A subroutine requires a call or JMP, a macro does not.

So a macro is faster than a subroutine to run

• Both allows reuse of code segments. Both make the

program easier to read, and allow changes to be made

in one place. Both hide details of the program so you

don’t need to know how it is doing something but just

what it is doing

Macros and Subroutines

Dr. Tao Li 11

• Allow symbols to be used before they are defined. This

is called a forward reference

• The assembler evaluates these symbols by making 2

passes through the source code

• On the first pass, any symbol definitions it finds are

recorded in a symbol table (containing values defined

for symbols using the EQU directive, and the memory

locations for labels, e.g. TARGET

• On the second pass, the assembler uses the symbol

table to substitute the values for the symbols

Two-Pass Assemblers

Dr. Tao Li 12

• Cross assembler: for microcontroller applications,

different computers are often used to edit and assemble

the programs. (i.e. cross platforms)

• Native assembler: one that runs on the target processor

• Assembler output:

– Executable file or object file

– Assembler listing (source code + assembled code)

– Symbol table, cross-reference listing…

Cross and Native Assemblers

Dr. Tao Li 19

• Loader program

  • Puts an executable file into the memory of a computer
  • Can take on many forms, e.g. OS as MS-DOS programs, a

modem/downloader program (cross platforms), a program

or system that burns the programmable ROM

• Assembly time

  • Refers to data values that are known at the time of

assembly

  • Includes constants (e.g. loop counters, ASCII character

codes), operand expressions, absolute addresses and

relative addresses (with relocatable assemblers)

• Linking time

  • Evaluate address/constants left by the relocatable

assembler

Code Location Problem

Dr. Tao Li 20

• Load time

  • When the executable file produced at link time

(relocatable assembler) or at assembly time (absolute

assembler) is loaded into the memory of the computer

  • Using assembler directives, it is possible to initialize the

variables in RAM at load time

• Run time

  • Refers to the time when the program is running
  • Data elements are initialized and manipulated at run time
  • Addresses can also be initialized and manipulated at run

time

Code Location Problem

Dr. Tao Li 21

• Debugging: the process of finding the clues and

interpreting these clues to find the problem

• 1st approach: synthesis

  • Try to fix the problem by changing the code somewhere
  • Wrong!

• 2nd approach: analysis

  • Try to find out what your program is doing before any fixes
  • Right!

• Every program has 2 parts: data, logic

  • Most program bugs occur in the logic

Program Debugging

Dr. Tao Li 22

Program Debugging

Dr. Tao Li 23

• Choose an input data set

• Predict what the program will do with the input data set

at each step of the program, and what the program will

do next (the program model)

• Now run the program, and look for data values and

program steps that differ from the prediction

• Once we find out where the program deviates from the

prediction, we are on track to finding the bugs and fixes

for the bugs

Program Debugging

Dr. Tao Li 24

• Program trace: stepping through the program one

statement at a time. Values of registers are shown

during each step. Data elements in memory have to be

checked manually

• Breakpoints: much like a high-level language debugger,

e.g. Gdb. These cause program flow to be interrupted,

and control transferred to the debugger (the user)

• Breakpoints can be set at statements, as well as when

certain conditions are met, e.g. data elements become

some specific values

Debugging Program Flow/Logic

Dr. Tao Li 25

• Registers: state of the registers must be known at each

step. Debugger usually will display contents of all the

registers, including the CCR. Watch for any deviating

register content values

• Memory: usually are displayed in hexadecimal

• You will most likely need a source code listing

generated by the assembler, not the original source

code file. And you will need an up-to-date one!

• Debugging plan: no universal solutions, but in general

make your code “modularly”

Debugging Data Elements

Dr. Tao Li 26

EEL 4744C: Microprocessor Applications

Lecture 3

Part 2

68HC12 Assembly Program

Dr. Tao Li 27

• Software and Hardware Engineering (old

version): Chapter 3

Or

• Software and Hardware Engineering (new

version): Chapter 5.1, 5.4, 5.5, 5.6,

Plus

• M68HC12B Family Data Sheet: Chapter 1

Reading Assignment

Dr. Tao Li 28

Hello World! (Assembler Listing)

12 ;; ExampleSource File:program hello.asm to print "Hello World" 34 ;; Author:Created: F. 4/97 M. Cady 0000 56 ; EOS:^ Constant^ EQU equates 0 ; End of string 0000 78 ; printf:^ Debug-12^ EQU Monitor^ $FE06 equates ; Print a string 0000 109 ; PROG:^ Memory^ map EQU^ equates $0800 ; RAM in the EVB 00000000 1112 DATA:STACK: EQUEQU $0900$0a00 ;; MiddleStack pointerof RAM (^0800 1314) ; Initialize ORG (^) stack PROG pointer ; Locate the program 0800 CF0A00 (^1516) ; Print Hello lds (^) World! #STACK string 08030806 CC080CFEFE06 1718 lddldx #HELLOprintf ;; PassThe adrthe ofadr the of printfthe string routine 0809 1500 19 jsr 0,x 080B 3F 2021 ;^ Return^ to swi^ the^ monitor 080C 48656C6C 2223 ; HELLO:^ Define^ the DC.B^ string 'Hello^ to^ print World!',EOS 6F20576F 726C 00 Dr. Tao Li 29

• Label: see textbook section 5.3 for how to define labels

  • A whitespace character is needed for a source code line without

label

• Opcode field: mnemonic, assembler directive or

pseudooperation, macro name

• Operand field: symbols, constants, expressions (see

textbook section 5.3 for detail

  • Special symbols: * and $ represent the current value of program counter
  • Constants: Decimal (default), Hexadecimal /w ($) prefix, Binary

/w (%) prefix, ASCII /w ‘ ’ and “ ”

  • Expressions: evaluated by assembler, used for specifying

constant only

• Comment field: ; or * (in column 1)

Assembler Source Code Field

Dr. Tao Li 30

• Assembler pseudo-ops:

  • ORG: set assembler’s location counter (e.g. ORG $1000)
  • EQU: equate symbol to value (e.g. COUNT EQU $10)
  • DS (define storage): reserve storage (e.g. ARRAY DS 10)
  • DC.B (define byte constant), DC.W (define word constant) :

define constants in memory storage (e.g. DATA1 DC.B

Pseudo-ops help the assembler

generate code for the program

68HC12 Assembler Program

Dr. Tao Li 37

Example: MACRO Parameters

12 ;; Macroshift definitionleft of the for A register a variable arithmetic 0000 34 $MACRO ; Shift (^) the alsa_n A register num left num bits 56 ;; whereSave Bn tois seta parameter up a loop in counter the macro call. 78 pshbldab (^) #%1 ; Save B on the stack 109 loop:^ asladbne^ b,loop ;;^ ShiftDecr andthe branchA^ reg if (^1112) pulb ;; notrestore zero the B reg 0000 1314 $MACROEND ; 1516 ;; The macro call is with a parameter 00000000 macro 37 1718 PSHB alsa_n 3 00010003 C603 48 1920 LDABLOOP: #%1 ASLA 00040007 0431FC 33 2122 DBNEPULB B,LOOP Dr. Tao Li 38

• Assembler listing (includes a symbol table) and

executable (S-Record file)

– Assembler listing

Loc [Cycles] Obj Code Line Source Line

– Symbol table: lists all symbols used in a program

and their values

– Cross-reference table: shows where the symbols are

defined and referenced

Assembler Output Files

Dr. Tao Li 39

Example: Symbol Table

… 5 …; … Constant equates 0000 67 EOS ; Debug-12: EQU Monitor (^0) equates; End of string 0000 89 printf ; Memory: (^) mapEQU equates $FE06 ; Print a string 00000000 1011 PROGDATA:: EQUEQU $0800$0900 ;; RAMMiddle in theof RAMEVB 00000800 1213 STACK : EQUORG (^) PROG$0a00 ;; StackLocate pointer the program 0800 CF0A00 1415 ;^ Initializelds^ stack #STACKpointer 0803 CC080C 1617 ;^ Print^ Helloldd^ World!#HELLO^ string ; Pass the adr of the string 08060809 FEFE06 1500 1819 ldxjsr printf0,x ; The adr of the printf routine 080B 3F 2021 ;^ Return^ toswi^ the^ monitor 080C 48656C6C 2223 ; HELLO^ Define: theDB^ string'Hello^ to^ print World!',EOS 6F20576F 726C 00

Symbol Table

DATA 0900

EOS 0000

HELLO 080C

PRINTF FE

PROG 0800

STACK 0A