Unit 3 PowerPoint Slides

EET 2261 Unit 3
Assembly Language; Load, Store, &
Move Instructions
Read Almy, Chapters 5 and 6.
Homework #3 and Lab #3 due next
Quiz next week.
Assembly Language
In Lab #2 we programmed the HCS12 using
machine language (also called machine
From here on, we’ll program the HCS12
chip using assembly language, and we’ll
use CodeWarrior’s built-in assembler to
translate from assembly language to
machine language.
C versus Assembly Language
C is a popular high-level programming
language that can be used to program the
HCS12, but in this course we’re using
assembly language (a low-level language)
to program the HCS12.
Our textbook focuses on assembly
language, but occasionally refers to how
you would do something in a C program.
(For example, see page 49.) You can ignore
all such references to the C language.
Computer Programming
Most programming today is done in high-level
languages, which can run on various machines.
High-level programs are easier to write and
maintain than assembly programs or machine code.
Assembly language is more convenient
than machine language. Assembly
language is used today for many
applications because it executes fast and
efficiently. But it must be written for a
specific processor.
Early computers were programmed in
machine language. Machine language
is tedious to write and prone to errors.
th th
Floyd, Digital
High-level language
• Closer to human language
• Portable
Assembly language
• English-like terms representing
binary code
• Machine dependent
Machine language
• Binary code (1s and 0s)
• Machine dependent
Computer hardwar e (the “machine”)
• Memory (RAM, ROM)
• Disk drives
• Input/Output
© 2009 Pearson Education, Upper Saddle River, NJ 07458. All Rights Reserved
•This block of code adds two numbers together
and stores the result in memory.
High-Level Language
FirstNumber = 30
LDAA #30
SecondNumber = 21
LDAB #21
Sum = FirstNumber+SecondNumber ABA
STAA $1022
$86 1E
$C6 15
$18 06
$7A 10 22
•The third column is the only one the
microcontroller understands. The BASIC or
Assembly programs must be translated to
machine code before being executed.
Computer Programming: Compilers & Assemblers
High-level languages are machine-independent. The source code is
translated to machine code by a compiler for the specific processor being
High-level language
(Source program)
Machine language
(Object program)
Assembly language must be written for the specific processor being used,
so the programmer must understand details of how this processor
operates. An assembler translates the source code to machine code.
Assembly language
(Source program)
th th
Floyd, Digital
Machine language
(Object program)
© 2009 Pearson Education, Upper Saddle River, NJ 07458. All Rights Reserved
Review: Registers Inside the
Review: Mnemonics and Opcodes
In assembly language, an HCS12
instruction contains a mnemonic, possibly
followed by one or more numbers.
Example: In the instruction LDAA #05,
LDAA is the mnemonic.
When an instruction is translated into
machine code, the mnemonic is replaced by
an opcode.
Example: The instruction LDAA #05
becomes $8605 in machine code, because
$86 is the opcode for LDAA.
Review: Operands
Most HCS12 instructions require the
programmer to specify one or two
operands, which are the data to be
operated on.
• The LDAA instruction loads some
number into accumulator A. The number
that’s to be loaded is the operand.
• In the instruction LDAA #05, the operand
is 5.
A Complete Assembly-Language
Let’s write a program that:
1. Loads 30 into Accumulator A.
2. Loads 21 into Accumulator B.
3. Adds the two accumulators, placing the
sum in Accumulator A.
4. Stores the sum in memory location
5. Sits forever on a self-pointing branch
In addition to the instructions that do these
steps, our program will include several lines
called assembler directives.
Our Assembly-Language Program
ORG $2000
;Load program at $2000.
Entry: LDAA #30
LDAB #21
STAA $1022
;Number of apples.
;Number of oranges.
;Total pieces of fruit.
;Store the result.
Note: The line starting with the word “Entry” must not be
indented from the left margin. The other lines must be
•Any text to the right of a semicolon is a
comment. Comments are ignored by the
assembler, so they have no effect on your
program’s operation. But they are important for
documenting your program so that you and
other people can understand how it works.
•The program on the previous slide has five
comments, all at the end of a line. If the first
symbol in a line is a semicolon, that entire line
is a comment.
Assembler Directives
•In the previous program, ABSENTRY, ORG, and END are
assembler directives.
•These tell the assembler something about how to
assemble our program. They’re not instructions that
the HCS12 executes.
•For example,
ORG $2000
tells the assembler where in memory it should load the
code that follows this line.
Where to Find Explanations of
Assembler Directives?
•Since assembler directives such as
ABSENTRY and ORG are not HCS12
instructions, they’re not listed in the Instruction
Summary Table on pages 377-380 of the CPU
Reference Manual.
•So where are they explained? CodeWarrior’s
Help system gives detailed information on all
assembler directives. See next slide…
In CodeWarrior, select Help > CodeWarrior
Help from the menus
•Another useful assembler directive, EQU (Equate), lets
us name locations in memory so that we can refer to
them by name.
MySum: EQU $1022
This directive tells the assembler that wherever in our
program we type MySum, the assembler should replace
MySum with $1022.
•Makes your programs easier to read and maintain.
Our Assembly-Language Program,
Using EQU
MySum: EQU $1022
ORG $2000
;Load program at $2000.
Entry: LDAA #30
LDAB #21
;Number of apples.
;Number of oranges.
;Total pieces of fruit.
;Store the result.
Some Other Assembler Directives
•Another assembler directive that we’ll use in a
few weeks:
•DC (Define Constant)
•Warning: There is some variation between
assembler directives from one assembler to another.
Not all of the directives listed on page 38 of the
textbook are recognized by CodeWarrior’s assembler.
Fields in a Line
•Each line in an assembly-language program has four
fields, not all of which are required in every line:
•Operation, which may be an HCS12 instruction or
an assembler directive.
•Only labels may appear in a line’s leftmost column.
;Number of parking spaces.
;Add deposit to balance.
Rules for Labels
•You can choose your own labels, but there are a few
rules to follow:
•A label must start with a letter (but it can contain
numbers after the first letter).
•Start2 is a valid label, but 2Start is not.
•A label cannot contain spaces. (Use underscores
instead of spaces.)
•Go_here is a valid label, but Go here is not.
•A label cannot be the same as instruction mnemonics
or assembler directives.
•ABA and ORG are not valid labels.
•To make your program easier to read and understand,
choose meaningful labels.
•LED_on is better than Label.
Program Header
•I will expect you to start each program with a
program header that lists the program’s name,
function, author, and date:
; Name:
; Function: Adds two numbers and stores the
; Author:
Nick Reeder
; Date:
• All lines in a program header are comments,
so they don’t affect the program’s operation.
Four Ways to Specify a Number
•The HCS12 assembler gives us four ways to
represent a number:
•Hex, using $ prefix
•Binary, using % prefix
•Decimal, using no prefix
•ASCII, using single quote marks around a
•See next slide for examples.
Example: Four Ways to Specify a
•The following four statements all load the
same number into Accumulator A.
•LDAA #$41
•LDAA #%01000001
•LDAA #65
•LDAA #’A’
Review: Categories of Instructions
The Instruction Set Summary table lists
instructions alphabetically. Sometimes it’s
more useful to have instructions grouped
into categories of similar instructions.
Examples of categories:
Load and Store Instructions
Addition and Subtraction Instructions
Boolean Logic Instructions
See Section 5 (starting on p. 55) of the
HCS12 CPU Reference Manual.
Instructions that Load, Store,
Transfer, Exchange, or Move Data
These instructions simply copy data from
one place to another.
Load instructions copy data from memory to a
Store instructions copy data from a register to
Transfer instructions and Exchange
instructions copy data from one register to
another register.
Move instructions copy data from one place in
memory to another place in memory.
Load Instructions
(Table from p. 56 of the HCS12 CPU Reference Manual.)
Big-Endian Versus Little-Endian
For instructions that load a two-byte register
from memory, the HCS12 loads the loweraddressed memory byte into the high-order
byte of the register.
This is known as the big-endian
convention, and it’s used by all Freescale
Intel processors use the opposite
convention, which is called little-endian.
Store Instructions
(Table from p. 57 of the HCS12 CPU Reference Manual.)
The big-endian convention applies here too
for the instructions that store two-byte
Transfer and Exchange
(Table from p. 58 of the HCS12 CPU Reference Manual.)
Move Instructions
(Table from p. 58 of the HCS12 CPU Reference Manual.)
A Note About the Book’s Examples
The textbook gives lots of good examples of
transfer, exchange, and move instructions. But
CodeWarrior will give an error if you type the
example code exactly as it appears in the book.
CodeWarrior’s assembler (unlike some other
assemblers) requires a comma between the two
registers or memory locations.
Example: The first example on page 63 is
tfr A B
In CodeWarrior, you must type it as
tfr A,B
Clear Instructions
(Table from p. 63 of the HCS12 CPU Reference Manual.)
Review: Addressing Modes
The HCS12’s six addressing modes are:
• Inherent
• Immediate
• Direct
• Extended
• Indexed (which has several variations)
• Relative
We’ve studied the first four. Now let’s begin
to look at indexed addressing mode.
Indexed Addressing Mode
Indexed addressing mode comes in at least
five variations:
• Constant offset indexed addressing
• Auto pre/post decrement/increment
indexed addressing
• Accumulator offset indexed addressing
• Constant indirect indexed addressing
• Accumulator D indirect indexed addressing
For now, we’ll just look at the first of these.
More detail is available on pages 50-54 in the
textbook or the section starting on p. 34 of the HCS12
CPU Reference Manual.
The Original Indexed Addressing
In older microcontrollers (including the
Freescale HC11) “indexed addressing”
meant what we’re calling constant offset
indexed addressing. The HCS12 added
the other variations.
Variation #1: Constant Offset
Indexed Addressing Mode
In constant offset indexed addressing
mode, the operand’s address is found by
adding a constant offset to the contents of
an index register (usually IX or IY, but
possibly also SP or PC).
Example: The instruction
uses Index Register X, with 3 as the
constant offset.
If Index Register X contains $1500, then this
instruction loads Accumulator A with the
contents of memory location $1503.
Simple Example of Indexed
LDX #$1500
LDY #$1600
Why Is This Useful?
It might be hard for you to see at this point
why you’d ever want to use indexed
addressing. But it turns out to be very
useful, as we’ll see in a couple of weeks.
(First you need to learn about loops.)

similar documents