Vous êtes sur la page 1sur 9

Codewarrior Assembler

Jerome K. Butler SMU Electrical Engineering jkb@lyle.smu.edu March 4, 2009

Assembly Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Statements and Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 User Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Source Statement Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Fields Continued . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Assembler Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Freescale Assembler Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Freescale Assembler Directives Cont . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Codewarrior Assembler Directive: Absentry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Codewarrior Assembler Directive: DC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Codewarrior Assembler Directive: DC & DS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Assembled DC Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Codewarrior Assembler Directive: EQU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Codewarrior Assembler Directive: ORG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Codewarrior Assembler Directive: Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Codewarrior Assembly Directive: Section Cont. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Codewarrior Assembler Directive: XDEF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 OFFSET - Create absolute symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 OFFSET Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Review Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Review Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Assembly Language
s s s

Generic Assembler Concepts (Slides 3 - 8) Codewarrior Assembler Specics Review EE 3381 Microprocessors jkb 5 slide 2

Statements and Numbers


s s s s

Source Statement is a single line of the source program. Source statement may contain various numbers and symbols. Numbers and symbols are connected by operators to make a sentence. Spaces, tabs are important. They serve as delimiters or separators.

Numbers are represented by decimal, hexadecimal, or binary. Decimal Decimal Hexadecimal Hexadecimal Binary 10 -100 $10 $ABCD %1001 A positive number A negative number Preceeded by $ Binary number preceeded by %

Comment: Always use the preceding dollar or percent signs. Common Mistake The assembly process converts negative numbers to 2s complement numbers. EE 3381 Microprocessors jkb 5 slide 3

User Symbols
MNEMONICS - LDAA, ldaa, STAA, staa, ... Symbols consists of alphanumeric characters. The 1st character must be alphabetic. User dened symbols May not consist of the single characters A, B, X, Y. Also do not dene symbols after the mnemonics. Each symbol must be unique Examples: TIME, INIT4, A444. Incorrect: 3ABC, 125, A Combination of symbols, +, -, * and / are called expressions. Example: ABC + 1 ; add 1 to the symbol ABC EE 3381 Microprocessors jkb 5 slide 4

Source Statement Format


Each source statement man contain up to four elds of information (left to right). 1. Label eld 2. Operation eld 3. Operand eld 4. Comment eld Fields are separated by spaces or tabs (white spaces), some elds may be blank. Labela Op Operand Comment The label always represents a MEMORY ADDRESS!
While using the Codewarrior assembler a colon is postxed to the label eld, however, if the label begins in column 1, the assembler allows the colon to be dropped. For good and consistent coding, we will always postx the colon to the label.
a

EE 3381 Microprocessors

jkb 5 slide 5

Fields
LABEL User dened symbol represents an address. Label starts in column 1.

OPERATION FIELD Instruction mnemonics Example START CLRA START is the label and CLRA is the operation. OPERAND FIELD Depends on the operator. May be empty, symbol, number, or expression. Example LOOP LDAA NUMBER ;a Comment LOOP is the label, LDAA is the operation, NUMBER is the operand, and Commnet is an optional comment.
The semicolon is used in blank lines and always preceeds a comment. Some assemblers use a star, *, to designate a blank line or the beginning of a comment.
a

EE 3381 Microprocessors

jkb 5 slide 6

Fields Continued
Addressing Mode Inherent Immediate Extended Direct Indexed Prog. Rel. Operand Format None #Operand Operand Operand Operand,X Operand Operand Type None Number 2-byte address 1-byte address Offset Offset jkb 5 slide 7

EE 3381 Microprocessors

Assembler Directives
Directives control the operation of the assembler. For example, the ORGa directive puts an address in the assembler location counter that species the location of the code in memory. ORG expression Example: ORG $C800 ORG 25
a

The ORG assembler directive will be discussed in more detail in slide 16.

EE 3381 Microprocessors

jkb 5 slide 8

Freescale Assembler Directives


Discussion will now focus on the the assembler directives in the HC(S)12 Assembler Manual. All assembler directives are listed below, however, only the ones necessary for our present needs will be covered. ABSENTRY - Application entry point BASE - Set number base DC - Dene Constant DS - Dene Space END - End assembly ENDIF - End conditional assembly EQU - Equate symbol value FAIL - Generate Error message IF - Conditional assembly INCLUDE - Inc txt from le EE 3381 Microprocessors ALIGN - Align Location Counter CLIST - List conditional assembly DCB - Dene Constant Block ELSE - Conditional assembly ENDFOR - End of FOR block ENDM - End macro denition EVEN - Force word alignment FOR - Repeat assembly block IFcc - Conditional assembly LIST - Enable Listing jkb 5 slide 9

Freescale Assembler Directives Cont


LLEN - Set Line Length LONGEVEN - Forcing Long-Word alignment MACRO - Begin macro denition MEXIT - Terminate Macro Expansion MLIST - List macro expansions NOLIST - Disable Listing MLIST - List macro expansions NOLIST - Disable Listing OFFSET - Create absolute symbols ORG - Set Location Counter PAGE - Insert Page break PLEN - Set Page Length RAD50 - Rad50-encoded str consts SECTION - Declare Relocatable Section SET - Set Symbol Value SPC - Insert Blank Lines TABS - Set Tab Length TITLE - Provide Listing Title XDEF - External Symbol Denition XREF - External Symbol Reference XREFB - External Reference for Symbols located on the Direct EE 3381 Microprocessors jkb 5 slide 10

Codewarrior Assembler Directive: Absentry


ABSENTRY - Application entry point This directive is used to specify the application Entry Point when the Assembler directly generates an absolute le. The -FA2 assembly option - ELF/DWARF 2.0 Absolute File - must be enabled. Using this directive, the entry point of the assembly application is written in the header of the generated absolute le. When this le is loaded in the debugger, the line where the entry point label is dened is highlighted in the source window. This directive is ignored when the Assembler generates an object le. EE 3381 Microprocessors jkb 5 slide 11

Codewarrior Assembler Directive: DC


DC - Dene Constanta Syntaxb

[<label>] DC[<.size>] expression [, <expression>]...


Synonyms

DCW (= 2 byte DCs), DCL (= 4 byte DCs), FCB (= DC.B), FDB (= 2 byte DCs), FQB (= 4 byte DCs) The DC directive denes constants in memory. It can have one or more <expression> operands, which are separated by commas. The <expression> can contain an actual value (binary, octal, decimal, hexadecimal, or ASCII). Alternatively, the <expression> can be a symbol or expression that can be
evaluated by the Assembler as an absolute or simple relocatable expression. One memory block is allocated and initialized for each expression.
a b

See HC(S)12 Assembler Manual, page 288. Something with brackets [] implies it is optional, and not part of RTL.

EE 3381 Microprocessors

jkb 5 slide 12

Codewarrior Assembler Directive: DC & DS


DC - Dene Constant DS - Dene Space Examples: Dene Constanta CONST: DC.B CON1: DC.B TABLE: DC.B

15 $F $27, 12, LENGTH+1

;Puts 0F into memory. ; Puts 0F into memory. ; Puts 27, 0C, 22 into memory.

Dene Constant Words WHAT: DC.W Hello LARGE: DC.W $0C3D Dene Storage
a

; Puts 48, 65, 6C, 6C, 6F into memory. ;Puts 0C, 3D into memory. 2 5 ; Reserves 2 bytes of memory. ; Reserves 5 words or 10 bytes of memory.

NEW1: NEW2:

DS.B DS.W

Assume Length has a value of $21

EE 3381 Microprocessors

jkb 5 slide 13

Assembled DC Directive
Label: DC.B "ABCDE" DC.B %1010, @12, 1,$A
Freescale HC12-Assembler (c) Copyright Freescale 1987-2008 Abs. Rel. ---- ---1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 Loc Obj. code -------------Source line ----------;The Freescale Assembler ; Example of simple assembler directives ; Source 5.1.asm ; Jerome K. Butler ; February 16, 2009 ; ORG $4000 ; ORG will start code $4000 Label: DC.B "ABCDE" DC.B %1010, @12, 1,$A

a4000 4142 4344 4004 45 a4005 0A0A 010A

EE 3381 Microprocessors

jkb 5 slide 14

Codewarrior Assembler Directive: EQU


EQU - Equates a symbol to a numerical value.

symbola EQU expression EQU is similar to #dene something in C/CPP, such as, #dene PI 314 The compiler simply replaces the string PI with the number 314. Example: PI EQU 314 Makes the code: LDD #PI Equivalent to: LDD #314
a

There is no : after symbol.

EE 3381 Microprocessors

jkb 5 slide 15

Codewarrior Assembler Directive: ORG


ORG - Set Location Countera The ORG directive sets the location counter to the value specied by its object. Subsequent statements are assigned memory locations starting with the new location counter value. The object must be absolute and may not contain any forward, undened, or external references. The ORG directive generates an internal section, which is absolute. Example: ORG $2000 Subsequent code will be loaded starting at memory location $2000.
a

See HC(S)12 Assembler Manual, page 330

EE 3381 Microprocessors

jkb 5 slide 16

Codewarrior Assembler Directive: Section


SECTION - Declare Relocatable Sectiona <name>: SECTION [SHORT][<number>] This directive declares a relocatable section and initializes the location counter for the following code. The rst SECTION directive for a section sets the location counter to zero. Subsequent SECTION directives for that section restore the location counter to the value that follows the address of the last code in the section. <name> is the name assigned to the section. Two SECTION directives with the same name specied refer to the same section, and the resulting code or data will be contiguous for the same section <name>.
Software and Hardware Engineering, page 79 and HC(S)12 Assembler Manual, page 338. SECTION directive is used for relocatable programs as opposed to absolute where one puts code, date, etc., at specic locations.
a

EE 3381 Microprocessors

jkb 5 slide 17

Codewarrior Assembly Directive: Section Cont.


The code, constant data, and data that may change usually go into different sections. Recall that there are multiple storage elements such as ROM and RAM. The rule for what goes where is stated below. A SECTION is a code section when it contains at least one assembly instruction. It is considered to be a constant section if it contains only DC or DCB directives. Thus, the section will be located in ROM. A section is considered to be a data section when it contains at least a DS directive or if it is empty; the section will be located in RAM. EE 3381 Microprocessors jkb 5 slide 18

Codewarrior Assembler Directive: XDEF


XDEF - External Symbol Denitiona XDEF [.<size>] <label>, [<label>], ... <size> = B, W(default), or L This directive species labels dened in the current module that are to be passed to the linker as labels that can be referenced by other modules linked to the current module. The number of symbols enumerated in an XDEF directive is only limited by the memory available at assembly time.
a

HC(S)12 Assembler Manual, page 344.

EE 3381 Microprocessors

jkb 5 slide 19

Codewarrior Assembler Directive: OFFSET


OFFSET - Create absolute symbolsa OFFSET [<expression>] The OFFSET directive declares an offset section and initializes the location counter to the value specied in <expression>. The <expression> must be absolute and may not contain references to external, undened or forward dened labels. An offset section is useful to simulate data structures or a stack frame. See example on the next slide.
a

HC(S)12 Assembler Manual, page 328.

EE 3381 Microprocessors

jkb 5 slide 20

OFFSET Example
Freescale HC12-Assembler (c) Copyright Freescale 1987-2008 Abs. Rel. ---- ---1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 Loc Obj. code -------------0000 0001 0003 0000 0007 0000 Source line ----------OFFSET ID: DS.B COUNT: DS.W VALUE: DS.L SIZE: EQU ;; DataSec: SECTION Struct: DS.B ;; CodeSec: SECTION entry: LDX LDAA STAA INC INCA STAA

0 1 1 1 * SIZE

0000 0003 0005 0007 0009 000A

CExx xx 8600 6A00 6201 42 6A03

#Struct #0 ID,X COUNT,X VALUE,X

EE 3381 Microprocessors

jkb 5 slide 21

Review Question
This question is similar to 5.13/page 103 Give the result in hexadecimal for each of the following assembler expressions. [k] MS MASK EQU %11110000 LS MASK EQU %00001111 MOST EQU 255 NEG EQU -128 SIX EQU 6 TEN EQU 10 DATA1: DS.B 10 DATA2: DC.B 10 EE 3381 Microprocessors jkb 5 slide 22

Review Question
Freescale HC12-Assembler (c) Copyright Freescale 1987-2008 Abs. Rel. ---- ---1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 Loc Obj. code -------------0000 0000 0000 FFFF 0000 0000 a2000 a200A 0A a200B 10 a200C 0A 00F0 000F 00FF FF80 0006 000A Source line ----------;; ORG $2000 MS_MASK EQU %11110000 LS_MASK EQU %00001111 MOST EQU 255 NEG1 EQU -128 SIX EQU 6 TEN EQU 10 DATA1: DS.B 10 DATA2: DC.B 10 Q1: DC.B SIX+TEN Q2: DC.B DATA2-DATA1 ;; This is not [DATA2]-[DATA1].

EE 3381 Microprocessors

jkb 5 slide 23

Vous aimerez peut-être aussi