Вы находитесь на странице: 1из 14

1

EE3170/CC/Lecture#9 1
EE 3170 Microcontroller
Applications
Lecture 9: Introduction to Assembly Language Programming
- Miller 3.1- 3.3
Based on slides for ECE3170 by Profs. Davis, Kieckhafer, Tan, and Cischke
EE3170/CC/Lecture#9 2
Question 3 of Quiz 1
Fill in the table below. First find the addressing mode used in each instruction,
and then provide the value of the registers after each instruction.
PC=$C000(the beginning address of the first instruction), X=C200, ($0010)=
$AC, ($00FE)=$56; ($C20A)=$AA, ($C210)=$3A before the first instruction.
AC C008 Direct addressing ldaa $10
3A C006 I ndex addressing ldab $10,X
$56 C004 Direct addressing ldaa $FE
$10 C002 I mmediate
addressing
ldaa #$10
B A PC Addressing Mode I nstruction
EE3170/CC/Lecture#9 3
Question 4 of HW2
EE3170/CC/Lecture#9 4
Questions 6 & 7 of HW2
2
EE3170/CC/Lecture#9 5
Objectives
When you complete chapter 3, you will be
able to
Write assembly language instructions
Explain and use pseudo-ops
And know how theyre different from regular
instructions
Explain the assembly process
EE3170/CC/Lecture#9 6
Whats Wrong with Machine Language (ML) Programming?
Details of ML Programming
Looking up opcodes
Picking addressing modes
Remembering addresses
Calculating offsets
Problems
Tedious
Mechanical
Error prone
Difficult to read and debug
EE3170/CC/Lecture#9 7
Assembly Language Programming
Assembly language is a more readable
language
Similar to the mnemonic code to represent
machine language;
W/ lots of additional features aid in writing
correct, readable, efficient code easily;
written w/ a text editor and usually named w/
the extension .asmto identify them.
EE3170/CC/Lecture#9 8
Assembler
An assembler is a program
translate assemblylanguage into object code.
Object code is not exactlymachine language, nor is it easily
human-readable.
contains machine language, and additional information that allows
separate object files, or modules, to be linked together to form a
larger program.
Object files are usually named w/ extension .obj.
Assemblers optionally produce a listing file
That is the most human-readable & useful version of the code,
It is usually named with extension .lst.
3
EE3170/CC/Lecture#9 9
Linker & Loader
A linker is a program that can combine, or link,
several object modules together to produce a
downloadable file.
The standard format for this file is Motorolas s19 format.
It is still not exactly machine language.
It contains enough information for a loader programto
write desired ML program &data values into desired
memorylocations.
Given a program w/ s19 format,
we can download it to the Evaluation Board to run or
we can execute it using HC11 simulator software.
EE3170/CC/Lecture#9 10
Thus We Turn to an Assembler
Assembler =a program to handle all the tedious
mechanical translations
Allows you to use:
symbolic opcodes
symbolic operand values
symbolic addresses
The Assembler
keeps track of the numerical values of all symbols
translates symbolic values into numerical values
EE3170/CC/Lecture#9 11
We Start with a Source File
Assembly Language Source Code, including
Symbolic Instructions, data, and addresses
translated to Machine Language
to be downloaded to targetprocessor
Assembler Directives
commands from you to the assembler itself
not translated or downloaded to the target
Comments
lots and lots of nice semantically meaningful comments
EE3170/CC/Lecture#9 12
Assembler Then Makes an Object File/Load Module
Machine Language image of the program
J ust binary numbers
Loading information
Address(es) to start loading at
code section
data section
Etc
Error Correcting Codes to detect corrupted
object file optional
4
EE3170/CC/Lecture#9 13
General Program Development Process
Create your assembly language program
Assemble the program
Link your assembled code w/
other separately assembled modules
library modules
Download the assembled & linked code to processor
Execute and test the program
Repeat ad nauseum until it is supposedly debugged
EE3170/CC/Lecture#9 14
Development Process
EE3170/CC/Lecture#9 15
What are Basic AL Building Blocks
Symbols =Alphanumeric names for numbers
reserved keywords
opcodes, register names (e.g. LDAA, ABA, A, B, X)
User-defined symbols
names for variables, constants, addresses, etc
Standard Motorola AL Symbols
Max of 6 characters
Pure alphanumeric (no -, _, etc)
Start with a letter
Case insensitive
EE3170/CC/Lecture#9 16
AL Building Blocks
Expressions
Can embed an arithmetic expression in an
instruction
At assembly time:
All values used in the expression must be known (no
run-time dependent values)
The expression must evaluate to a constant
Can use previously defined symbols
e.g. (assume base& lengthare previously defined)
LDX #(2*length)
LDAA (base+length+3)/2
5
EE3170/CC/Lecture#9 17
What are Parts of an Assembly Language
Instruction?
Each assembly language statement has
up to four fields. In order these are
labelan optional name you supply
op codeone of the HC11 op codes or a
pseudo op code for assembly (discussed
later)
operandaddress or data
commentsyour explanations
EE3170/CC/Lecture#9 18
What is a Label?
A label is a way to name an instruction or
its location.
For example, retn
Labels can be made of letters, numbers and a
few special characters.
A label may be used only once in a
program.
Otherwise it would be associated with two
locations.
EE3170/CC/Lecture#9 19
What is a Label?
Also called user symbols,
can be used to refer to memory addresses or data values.
EQU directive associate label w/ data values,
while a label on any other line is associated w/ current address on
that line.
locations in the data segment or statements in the programsegment
can be referred to by label.
Notes:
Labels must begin in the first column (the trailing : is not part of the
label and is optional).
They can have from 1 to 6 alphanumeric characters beginning with
an alphabetic character.
Labels are case-sensitive (?).
A, B, X, or Y (upper or lower case) are not allowed.
All labels must be unique.
EE3170/CC/Lecture#9 20
What is the Op Code?
This is easy because weve been working
with opcodes for weeks.
The opcode is one of Motorolas mnemonics
for the HC11.
or it can be a special code for the assembler, a
pseudo op code, aka, assembler directive
(discussed later)
6
EE3170/CC/Lecture#9 21
What is the Operand Field?
The opcode will often require one or two
operands.
The operand field also includes the
addressing mode.
Operands can be registers, memory
locations, numeric expressions, etc.
An expression is a combination of symbols,
constants, algebraic operators and
parentheses.
EE3170/CC/Lecture#9 22
How About Comments?
Comments are remarks you write to
document your program.
They should explain the purpose of an
instruction, not just mimic the code.
clra Clear the A register Bad
clra Initialize the sum Good
Full line comments start with an asterisk
(*) in the first column.
EE3170/CC/Lecture#9 23
Examples of Assembly Language Fields
Label Opcd Oprnd Comment
Loop aba Increment sum
ldy #400 Initialize pointer
beq Loop Repeat loop ops
EE3170/CC/Lecture#9 24
Syntax Variations
Labels are explicit or inferred
Many assemblers require labels to end with a colon (: )
Some labels are symbolic addresses
Motorola takes anything starting in Col.1 as a label
Most assemblers use a symbol to start a comment
Typically the whole rest of the line is comment
Motorola AL is slightly different
A whole-line comment starts with * in column 1
Comments after instructions do not need a *
I would make it a habit to start comments with *
7
EE3170/CC/Lecture#9 25
What are Pseudo-Ops?
Pseudo-ops or assembler directives are
commands to the assembler.
define labels &allocate data storage space, among
other things.
Most exist only in assembly language &hence
have no binary translation.
They allow you to say
Where in memory to store the code
Where in memory to store data
Where to store a constant and what its value is
The values of user-defined symbols
EE3170/CC/Lecture#9 26
What are the Most Important Pseudo-Ops?
EQU equals
Associates the label in the first field with the value in the operand field.
Good for defining constants.
ORG set origin
Resets the assemblers current address pointer; subsequent directives and
instructions begin here.
RMB reserve memory bytes
Reserves (skips over) the number of memory bytes specified in the
operand field and associates the label with the first address reserved. Use
to define variables.
FCB, FDB form constant, double byte
Stores a constant byte or double-byte value at the current address and
associates the label with that address. Use to define variables with initial
values.
END end of input
Tells the assembler to stop here (even if theres more code).
EE3170/CC/Lecture#9 27
Motorola Directives in Detail
equ =EQUate a symbol to a value
Syntax: label EQU expression
Label is mandatory
Label becomes a symbol usable in the code
Assembler translates labelinto value of expression
Example:
PI equ 31416
START equ $2000
EE3170/CC/Lecture#9 28
Motorola Directives in Detail
ORG =ORiGin- set current address (location counter: assembler
analog of program counter)
syntax: ORG expression
sets current address to value of the expression
starts loading following instructions at address
e.g.
ORG $2000
LDAA #value
There will almost always be at least one org statement in every
program.
Multiple org statements are a-okay.
And in fact, are usually necessary. Code and data needs to go in
different places
8
EE3170/CC/Lecture#9 29
Motorola Directives in Detail
rmb =Reserve Memory Bytes
Syntax: [label] rmb expression
Reserves expression bytes of memory for data
Increments location counter by expression
Does not initialize value of those bytes
Example: org $4000
array rmb $FF
r mb will state reserving data bytes from current value of
Location Counter.
its important to know what it is when you start using r mb.
How can you know? - Set it yourself, using an or g!
The label is technically optional, but youll want to use it.
Otherwise, it will be hard to find the memory again.
EE3170/CC/Lecture#9 30
Motorola Directives in Detail
fcb =Form Constant Byte
Syntax: [label] FCB expression, expression,...
Evaluates each expression
Stores those values in consecutive bytes
Increments location counter past stored bytes
Example:
fcb $27,$FF,5+2
EE3170/CC/Lecture#9 31
Motorola Directives in Detail
fdb - fcbs Cousin
fdb =Form Double Byte
Syntax:
[label] fdb expression, expression,...
Evaluates each 16-bit expression
Stores those values in consecutive bytes
Increments location counter past stored bytes
Example:
fdb $27FF, 5+2
Comments on fdb/fcb
Labels are not quite as important for these as r mb, depending
on the application.
The primary difference between r mb and f cb is that f cb
initializes the data.
f db shows up a lot later in the course.
EE3170/CC/Lecture#9 32
Motorola Directives in Detail
end =end of program
Syntax: end
Tells assembler to stop assembling
Doesnt need to be at the end of the code, but the assembler
wont process anything past the end directive.
Not the same as the STOPinstruction (which is a real processor
instruction)
Asterisk as an operand
a symbol for current value of location counter
e.g. start equ *
symbol start gets value of current address.
9
EE3170/CC/Lecture#9 33
How Do We Develop
an Assembly Language Program?
We create source code with an editor.
An assembler translates our source code into
object code.
A cross assembler (THRSim11) develops code on one
computer for another computer.
We test our program with a debugger.
A loader puts our program into the computers
memory.
EPROM
RAM
EE3170/CC/Lecture#9 34
How Does an Assembler Process Our Program?
The assembler processes one statement
at a time.
It makes two complete passes through the
source code.
Hence we call it a two-pass assembler.
EE3170/CC/Lecture#9 35
What Happens in the First Pass?
First pass:
Location counter (assembly analog of
program counter) updates instruction
addresses.
Assembly opcodes translated to numeric.
Addresses calculated where possible.
Symbol table created with symbols and
addresses.
EE3170/CC/Lecture#9 36
What Happens in the Second Pass?
Second pass:
Object code produced
Symbol table completed
Program listing is produced
Errors flagged, such as
illegal opcode
undefined symbol
branch address out of range
10
EE3170/CC/Lecture#9 37
What Exactly Does Assembler Do in These Passes?
The assembler
starts with the org statement and sets the location
counter (lc) equal to the address given
looks at each succeeding statement
puts label in symbol table with corr. address
verifies a valid op code
calculates effective address(es)
translates it to machine code (hex or binary)
updates the clc by statement length
generates any needed error statements
ends with the end statement
EE3170/CC/Lecture#9 38
What are Common Assembly Errors?
Cant program address
Youve tried to use unavailable memory.
To Fix: Move your org statement
Branch too far error
Youve tried to branch beyond -128 to 127
To Fix: Change bsr to j sr (May require substantial code changes).
Operand error
You may have misspelled the operand, tried an inapplicable addressing mode, or
misused an expression
Label previously defined error
You may have used one label in several locations.
To Fix: Change one of them.
Undefined opcode error
You have misspelled the opcode.
To Fix:
Fix the typo (l daaa ->l daa)
Make sure youre not using an instruction only supported by the HC12.
If the line doesnt have a label, make sure theres a space in the first column!
EE3170/CC/Lecture#9 39
What Are the Assemblers Outputs?
Symbol table
symbolic names with corresponding addresses
shows the values of all the labels weve defined.
Listing file
addresses
object code
copy of source code
number of cycles (optional)
TheLog.rtf (Texas)
EE3170/CC/Lecture#9 40
Listing File
Information for the user
Helpful for debugging
Source code,
Corresponding object code
Address for each instruction and data
Symbol table
Any error messages generated
Often cryptic
Should at least list the address of the error
11
EE3170/CC/Lecture#9 41
The Listing File
Main part of a listing file produced by assembler
Notes:
Lines are numbered in the first column (good for reference).
Hex addresses are in second column.
Machine code (op codes and operands) is next, with one instruction per line.
Assembly language source code is also listed.
Addressing modes have been automatically determined.
EE3170/CC/Lecture#9 42
Hand Assembly
We will hand assemble the following program
We will develop the symbol table in the first
pass. This requires
determining the addressing mode
finding the length of each instruction and
calculating the address of each instruction.
We will find the object code during the second
pass.
EE3170/CC/Lecture#9 43
Pass 1 - Find Addressing Modes
PORTC equ $1003
DDRC equ $1007
org $E000 * ROM
main lds #$00FF *SP in lower RAM
bsr init
loop staa PORTC *output
inca
bra loop
init ldaa #$FF
staa DDRC *outputs
clra
rts
org $FFFE * ROM
fdb main * reset vector
EE3170/CC/Lecture#9 44
Find Addressing Modes
PORTC equ $1003
DDRC equ $1007
org $E000 * ROM
main lds #$00FF *SP in lower RAM immediate
bsr init relative
loop staa PORTC *output extended
inca inherent
bra loop relative
init ldaa #$FF immediate
staa DDRC *outputs extended
clra inherent
rts inherent
org $FFFE * ROM
fdb main * reset vector
12
EE3170/CC/Lecture#9 45
Pass 1- Find Instruction Lengths
PORTC equ $1003
DDRC equ $1007
org $E000 * ROM
main lds #$00FF *SP in lower RAM
bsr init
loop staa PORTC*output
inca
bra loop
init ldaa #$FF
staa DDRC *outputs
clra
rts
org $FFFE * ROM
fdb main * reset vector
EE3170/CC/Lecture#9 46
Pass 1 - Find Instruction Lengths
PORTC equ $1003
DDRC equ $1007
org $E000 * ROM
main lds #$00FF *SP in lower RAM immediate 3
bsr init relative 2
loop staa PORTC *output extended 3
inca inherent 1
bra loop relative 2
Init ldaa #$FF immediate 2
staa DDRC *outputs extended 3
clra inherent 1
rts inherent 1
org $FFFE * ROM
fdb main * reset vector
EE3170/CC/Lecture#9 47
Pass 1- Find Addresses
PORTC equ $1003
DDRC equ $1007
org $E000 * ROM
main lds #$00FF *SP in lower RAM
bsr init
loop staa PORTC *output
inca
bra loop
init ldaa #$FF
staa DDRC *outputs
clra
rts
org $FFFE * ROM
fdb main * reset vector
EE3170/CC/Lecture#9 48
Pass 1 - Find Addresses
1003 PORTC equ $1003
1007 DDRC equ $1007
org $E000 * ROM
E000 main lds #$00FF *SP in lower RAM
E003 bsr init
E005 loop staa PORTC *output
E008 inca
E009 bra loop
E00B init ldaa #$FF
E00D staa DDRC *outputs
E010 clra
E011 rts
org $FFFE * ROM
FFFE fdb main * reset vector
13
EE3170/CC/Lecture#9 49
Develop Symbol TablePass 1
DDRC 1007
PORTC 1003
init E00B
loop E005
main E000
EE3170/CC/Lecture#9 50
Pass 2 - Create Object Code
1003 PORTC equ $1003
1007 DDRC equ $1007
org $E000 * ROM
E000 main lds #$00FF *SP in lower RAM
E003 bsr init
E005 loop staa PORTC *output
E008 inca
E009 bra loop
E00B init ldaa #$FF
E00D staa DDRC *outputs
E010 clra
E011 rts
org $FFFE * ROM
FFFE fdb main * reset vector
EE3170/CC/Lecture#9 51
Pass 2 - Create Object Code
1003 PORTC equ $1003
1007 DDRC equ $1007
org $E000 * ROM
E000 8E00FF main lds #$00FF * SP in lower RAM
E003 8D06 bsr init
E005 B71003 loop staa PORTC * output
E008 4A inca
E009 20FA bra loop
E00B 86FF init ldaa #$FF
E00D B71007 staa DDRC * outputs
E010 4F clra
E011 39 rts
org $FFFE * ROM
FFFE fdb main * reset vector
EE3170/CC/Lecture#9 52
What is an Evaluation Board?
An evaluation board is useful in embedded
software development.
Allows real-time interaction with hardware.
Programs for ROM or EPROM are put in RAM
for easy debugging
Board includes debugger.
start execution from specified address
read or change microcomputer registers, RAM, or I/O
set breakpoints at desired addresses
14
EE3170/CC/Lecture#9 53
What is a Simulator?
A simulator emulates a microprocessor and
its external components.
It allows running the program without actual
hardware.
Examples: TExaS, THRSim11
EE3170/CC/Lecture#9 54
Hardware Setup
Host Computer or Assembly System (e.g. PC)
Stores files
Runs the assembler
Downloads assembled code to the microcontroller
aka target processor
Tells target processor to start program
Uploads results for storage and display
Usually has a target processor simulator
useful for initial debugging
allows you to examine simulated registers, memory, etc
EE3170/CC/Lecture#9 55
Summary: General Program Development Process
Create your assembly language program
Assemble the program
Link your assembled code w/
other separately assembled modules
library modules
Download the assembled & linked code to processor
Execute and test the program
Repeat until supposedly debugged

Вам также может понравиться