assembly language programming for the mc68hc11. assembly language programming recall the 4 design...
TRANSCRIPT
AssemblyLanguage
Programmingfor the
MC68HC11
Assembly language programming Recall the 4 design levels for software development:
– Application
– High Level Language
– Assembly
– Machine code (often called object code)
Machine code programs are the only ones that can be directly executed on the processor -- programs in the higher 3 levels must be converted to machine code CONTD
Assembly language programming
– High level language programs must be compiled
» Synthesize an assembly-level intermediate form
» Convert the intermediate form to machine code
– Assembly language programs must be assembled
» Translated into machine code
» The synthesis part of compiling is not required
» One-to-one translation of instructions
Assembly language programming
Assembly level programming– Advantages (over HLL programs)
» Faster
» Less memory
» Enhanced control over the hardware
– Disadvantages (vs. HLL programming)
» Coding requires more "talent"
» Life cycle support more difficult
» Program tied to a specific HW platform
--- Not portable
Overview of the AS11 assembler
Assembly language programs consist of 3 types of statements:
» Instructions that will be executed by the microprocessor
» Comments to document the program operation for humans
» Assembler directives (pseudo-operations) that tell the assembler what to do
CONTD
Overview of the AS11 assembler
Instructions:– Format is shown below:
label operation operand ;comment
– Labels
» Optional
» Must start in column 1 and begin with an alphabetic character
» No more than 15 characters long
» Delimit with a space or colon
» May be on a line by itself
Overview of the AS11 assembler
– Operation Operand
» The instruction mnemonic from the instruction set followed by any needed operands
» The opcode cannot start in column 1 of the source file
» It a label is present, there must be at least one space or a colon between it and the opcode
– Comments
» Comments in an instruction line are delimited by a semicolon
CONTD
Overview of the AS11 assembler
» Everything to the right of a semicolon is treated as a comment (ignored by the assembler)
» If a line starts with a semicolon or an asterisk in column 1, the entire line is treated as a comment
» Use enough comments to explain program function -- not so many as to "flood" reader
» Use meaningful comments; the following is not very useful
LDDA #$FC ; load $FC into A
Overview of the AS11 assembler
– Directives and pseudo-operations
» These statements cause the assembler to perform certain actions -- they are converted directly to executable code
» ORG xxxx Place the next byte of the program at address $xxxx The programmer uses this to place logical program groups into different memory areas
CONTD
Overview of the AS11 assembler
» EQU
label EQU xxxx The label is assigned the constant value xxxxThe programmer uses EQU to give meaningful names to constant numeric data – enhances code readability Examples:
TRUE: EQU 1
MAXINT EQU $FF
Overview of the AS11 assembler
» Memory allocation You should always allocate any memory locations that you use
– Variables (RAM)– Constants (ROM/EEPROM)– Strings (ROM/EEPROM)
FCC ‘ASCII characters’– Declare a string(Form Constant Characters)– Assembler converts characters toASCII– Example:string1: FCC ‘This is string1.’
Overview of the AS11 assembler
FDB word,word, . . .– Declare 16-bit constants(Form Double Bytes)– Words may be constants, symbols, or expressions– If you have more than one word, they must be separated by commas, with no spaces between them– Example:Jump_Table: FDB $E000,$E010,$E020
CONTD
Overview of the AS11 assembler
FCB byte,byte, . . .– Declare 8-bit constants(Form Constant Bytes)– Bytes may be constants, symbols, or expressions– Separate bytes with commas, no paces between them– Example:sqr_tbl: FCB $0,$1,$4,$9,$10
Overview of the AS11 assembler
RMB size– Declare storage for variables(Reserve Memory Bytes)– Reserves size number of bytes (bytes are not initialized)– Size may be constant, symbol, or Expression – Example: Declare a 128-byte buffer at address $100 ORG $100 Buffer: RMB 128
Overview of the AS11 assembler
» INCLUDE “filename”» INCLUDE <filename>
Include the specified file into the source code Useful for initialization and definition of sections of code that are common across many programs For example, I/O register names
» Output control PAGE OPT option
– Available options: l, nol, c, noc,contc, cre, s, crlf, nnf, p50
Overview of the AS11 assembler
» Conditional assembly: IFD symbol IFND symbol ELSE ENDIF
– Checks if the symbol has beendefined or not defined– Each IFD or IFND must have amatching ENDIF– ELSE statements are optional
CONTD
Overview of the AS11 assembler
– IF/ELSE/ENDIF blocks may be
nested
– Format:
IFD symbol
; code, more IFs, comments, etc.
ELSE
; code, more IFs, comments, etc.
ENDIF
Overview of the AS11 assembler
The assembly process
– Most assemblers perform their job by making 2 “passes” over the source code
– Pass 1 identifies all symbolic references to memory locations, and to the starting locations of all instructions
» Symbol table is built during Pass 1
CONTD
Overview of the AS11 assembler
Contains name and value for each symbol
Symbols may be labels or they may be created with EQU directives
– Pass 2 converts all symbolic references to absolute memory references and produces the final object code
» Uses the symbol table information
Overview of the AS11 assembler
Assembler first pass [Sho87] Assembler second pass [Sho87]
Overview of the AS11 assembler– Assembler outputs:
» Object code: A specification of the actual bytes that will be placed in the HC11’s memory to be executed
On our systems, this is an ASCII file where binary code is represented as strings of hex equivalents
– Motorola S-records format
– Intel hex format
CONTD
Overview of the AS11 assembler
Must be "loaded" into memory, at which time the ASCII-encoded hex digits are mapped to actual memory contents
» Listing file: composite file giving both the source assembly code and the corresponding memory addresses and their contents
This file is particularly useful for debugging coding errors
Overview of the AS11 assembler
General Format of Listing File
<address> <code> <source line no.> <source line>
– address = Starting address in memory of the instruction
– code = Hex digits that are the values put into the memory locations
– source line no. = line number
CONTD
Overview of the AS11 assembler
– source line = a ssembly source code instruction
– Other things that can be seen: Comments, cycle count, symbol table at the end of file, etc.
Example:
018A 8612 203 LDAA #$12
018C 9634 204 LDAA $34
018E B65678 205 LDAA $5678
Overview of the AS11 assembler
Example: Write a program to find the minimum value in a list of unsigned integers.
Assume that the starting address of the list is stored in memory location START, and the length of the list is stored in location LENGTH. Store the minimum value in memory location RESULT.
CONTD
Overview of the AS11 assembler
Pseudocode:
Set min = MAX_INT;
for each item in the list
{
if (list item < min)
{
min = list item
}
}
Store the min in RESULT
Overview of the AS11 assembler
One implementation (in C):
int *START;
int LENGTH;
int RESULT;
int *ptr;
int count;
int min;
min = MAX_INT;count = LENGTH;ptr = START;while (count != 0){if (*ptr < min){min = *ptr;}ptr++;count--;}RESULT = min;
Overview of the AS11 assembler;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Program: FIND_MIN -- Finds the minimum value of
; a list of unsigned integers.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
MAX_INT EQU $FF ; largest unsigned integer
ORG $100
START RMB 2 ; starting address of list
LENGTH RMB 1 ; length of list
RESULT RMB 1 ; minimum value
ORG $E000
FIND_MIN:CONTD
Overview of the AS11 assemblerFIND_MIN:LDAA #MAX_INT ; ACCA = min = MAX_INTLDAB LENGTH ; ACCB = count = LENGTHLDX START ; IX = ptr = START ;LOOP:CMPA 0,X ; compare list item with minBLS END_LOOP ; branch if min <= list itemLDAA 0,X ; else, update minEND_LOOP:INX ; increment list pointerDECB ; decrement counterBNE LOOP ; and repeat if counter > 0 ;STAA RESULT ; store minimum value
Overview of the AS11 assembler
Overview of the AS11 assembler
Disassembly
– Translate from object code/machine code back to source code
– You usually lose symbols, labels, etc.
– Example: (problem 12, chapt. 2 in text)
E000 7F 50 00 7C 50 00 CE 10 - 00 18 CE 20 00 08 18 09
E010 A6 00 18 A7 00 78 50 00 - 24 F3 01 01 01 01 01 01
CONTD
Overview of the AS11 assembler
CLR $5000
INC $5000
LDX #$1000
LDY #$2000
LABEL:
INX
DEY
LDAA 0,X
STAA 0,Y
LSL $5000
BCC LABEL
– What does this program do?
Overview of the AS11 assembler
Example: Exercise 13 from Chapt. 2– Write a program to clear the first 20 bytes of
RAM (set them to zero)
» One solution:
ORG $B600
LDX #$00 ; start with address $00
Loop:
CLR $0,X ; clear the byte
INX ; increment addr pointer
CPX #20 ; addr < 20?
BLO Loop ; branch if so
Overview of the AS11 assembler
Example: Exercise 13 from Chapt. 2– A slightly different solution
ORG $B600
LDX #19 ; s tart with end of block
Loop:
CLR $0,X ; clear the byte
DEX ; decrement addr pointer
BNE Loop ; branch if not done
CLR $0,X ; why do we need this?
Overview of the AS11 assembler
Example:
– Write a routine that will delay for a specified number of milliseconds. (The number of milliseconds will be stored in memory location DELAY_VALUE). Assume the system uses an 8 MHz crystal. The delay should be accurate to +/- 1%.
» Remember that the E-clock, or system clock, is the internal clock frequency of the processor.
CONTD
Overview of the AS11 assembler
E-clock = crystal frequency / 4
= 8 MHz / 4
= 2 MHz Each clock period is therefore 0.5 µsec 1 ms = 2000 clock cycles So, we need to write a routine that willtake 2000 clock cycles (+/- 20 cycles) to
execute
Overview of the AS11 assembler
Solution(?):DELAY_VALUE RMB 1
DELAY:
LDAA #LOOP_CNT ; 2 cycles
LOOP:
DECA ; 2 cycles
BNE LOOP ; 3 cycles
;
DEC DELAY_VALUE ; 6 cycles
BNE DELAY ; 3 cycles
– What value should we use for LOOP_CNT?
Overview of the AS11 assembler Corrected solution:DELAY_VALUE RMB 1DELAY:
LDAA #LOOP_CNT ; 2 cyclesLOOP:
NOP ; 2 cyclesNOP ; 2 cyclesDECA ; 2 cyclesBNE LOOP ; 3 cycles;DEC DELAY_VALUE ; 6 cyclesBNE DELAY ; 3 cycles
– Now what should LOOP_CNT be?