Basic Assembler
Basic Assembler
Basic Assembler
Lecture 3
Part 1
Assembly Language Programming
and Debugging
Dr. Tao Li 1
Reading Assignment
• Microcontrollers and Microcomputers: Chapter
5
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
Dr. Tao Li 2
Assembly Language
• 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
Dr. Tao Li 3
The Assembler
• 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
Dr. Tao Li 5
Assembly Source Code Fields
• 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 $1000
Use label to
ORG directive
specify target
helps
address
determine
(relative
actual address
address)
Dr. Tao Li 6
Assembly Source Code Fields
• 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+1
Numeric
• Comment field Label
constant
– 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
Dr. Tao Li 7
Assembly Source Code Fields
• 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.
Dr. Tao Li 8
Macro Assembler
• 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
Assembler directives
Dr. Tao Li 9
Macros and Subroutines
• 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
Dr. Tao Li 11
Cross and Native Assemblers
• Cross assembler: for microcontroller applications,
different computers are often used to edit and assemble
the programs. (i.e. cross platforms)
• Assembler output:
– Executable file or object file
– Assembler listing (source code + assembled code)
– Symbol table, cross-reference listing…
Dr. Tao Li 12
Code Location Problem
• Tied to the hardware’s specific memory map
• In a dedicated application system, code is located in
ROM (read-only, non-volatile); data is in RAM
Dr. Tao Li 13
Code Location Problem
• Absolute assemblers
– The code on slide #5, where
the ORG directive specifies
the code location, can be
assembled with an absolute
assembler
• Major disadvantage
– The source file MUST contain
ALL the source code to be in
the program, i.e. all code must
be assembled with any change
Dr. Tao Li 14
Code Location Problem
• Relocatable assemblers
– Source code file need not have the complete program, nor
does it need to have location information, or ORG
statements
Dr. Tao Li 15
Code Location Problem
• The linker program
– Takes modules that have been assembled by a
relocatable assembler, links them together, and locates all
addresses
Dr. Tao Li 16
Code Location Problem
Dr. Tao Li 17
Code Location Problem
Dr. Tao Li 18
Code Location Problem
• 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
Dr. Tao Li 19
Code Location Problem
• 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
Dr. Tao Li 20
Program Debugging
• 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!
Dr. Tao Li 21
Program Debugging
Dr. Tao Li 22
Program Debugging
• 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
Dr. Tao Li 23
Debugging Program Flow/Logic
• 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
Dr. Tao Li 24
Debugging Data Elements
• 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
Dr. Tao Li 25
EEL 4744C: Microprocessor Applications
Lecture 3
Part 2
68HC12 Assembly Program
Dr. Tao Li 26
Reading Assignment
• 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
Dr. Tao Li 27
Hello World! (Assembler Listing)
1 ; Example program to print "Hello World"
2 ; Source File: hello.asm
3 ; Author: F. M. Cady
4 ; Created: 4/97
5 ; Constant equates
0000 6 EOS: EQU 0 ; End of string
7 ; Debug-12 Monitor equates
0000 8 printf: EQU $FE06 ; Print a string
9 ; Memory map equates
0000 10 PROG: EQU $0800 ; RAM in the EVB
0000 11 DATA: EQU $0900 ; Middle of RAM
0000 12 STACK: EQU $0a00 ; Stack pointer
0800 13 ORG PROG ; Locate the program
14 ; Initialize stack pointer
0800 CF0A00 15 lds #STACK
16 ; Print Hello World! string
0803 CC080C 17 ldd #HELLO ; Pass the adr of the string
0806 FEFE06 18 ldx printf ; The adr of the printf
routine
0809 1500 19 jsr 0,x
20 ; Return to the monitor
080B 3F 21 swi
22 ; Define the string to print
080C 48656C6C 23 HELLO: DC.B 'Hello World!',EOS
6F20576F Dr. Tao Li 28
726C6421
00
Assembler Source Code Field
• 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)
Dr. Tao Li 29
68HC12 Assembler Program
• Assembler pseudo-ops:
– ORG: set assembler’s location counter (e.g. ORG $1000)
Dr. Tao Li 30
Example: ORG
Dr. Tao Li 31
Example: EQU
Dr. Tao Li 33
68HC12 Assembler Program
• Assembler directives: (see textbook Table 5-5 for
complete list)
– Exact syntax varies by assembler program used;
most interesting options for conditional assembly
and macros
– Directives are invoked by placing a /, # or $ as the
first character of a line followed by the directive and
any parameters
– Conditional assembly directives permit different
sections of source code to be assembled depending
on setting of condition;
Dr. Tao Li 35
68HC12 Assembler Program
• Assembler directives: Macros
– Assembler directives MACRO and MACROEND
encapsulate the code
Dr. Tao Li 36
Example: MACRO Parameters
1 ; Macro definition for a variable arithmetic
2 ; shift left of the A register
0000 3 $MACRO alsa_n num
4 ; Shift the A register left num bits
5 ; where n is a parameter in the macro call.
6 ; Save B to set up a loop counter
7 pshb ; Save B on the stack
8 ldab #%1
9 loop: asla ; Shift the A reg
10 dbne b,loop ; Decr and branch if
11 ; not zero
12 pulb ; restore the B reg
0000 13 $MACROEND
14 ;
15 ;
16 ; The macro call is with a parameter
0000 macro 17 alsa_n 3
0000 37 18 PSHB
0001 C603 19 LDAB #%1
0003 48 20 LOOP: ASLA
0004 0431FC 21 DBNE B,LOOP
0007 33 22 PULB
Dr. Tao Li 37
Assembler Output Files
• Assembler listing (includes a symbol table) and
executable (S-Record file)
– Assembler listing
Loc [Cycles] Obj Code Line Source Line
Dr. Tao Li 38
Example: Symbol Table
… … …
5 ; Constant equates
0000 6 EOS: EQU 0 ; End of string
7 ; Debug-12 Monitor equates
0000 8 printf: EQU $FE06 ; Print a string
9 ; Memory map equates
0000 10 PROG: EQU $0800 ; RAM in the EVB
0000 11 DATA: EQU $0900 ; Middle of RAM
0000 12 STACK: EQU $0a00 ; Stack pointer
0800 13 ORG PROG ; Locate the program
14 ; Initialize stack pointer
0800 CF0A00 15 lds #STACK
16 ; Print Hello World! string
0803 CC080C 17 ldd #HELLO ; Pass the adr of the string
0806 FEFE06 18 ldx printf ; The adr of the printf routine
0809 1500 19 jsr 0,x
20 ; Return to the monitor Symbol Table
080B 3F 21 swi DATA 0900
22 ; Define the string to print EOS 0000
080C 48656C6C 23 HELLO: DB 'Hello World!',EOS
6F20576F
HELLO 080C
726C6421 PRINTF FE06
00 PROG 0800
STACK 0A00
Dr. Tao Li 39