Documente Academic
Documente Profesional
Documente Cultură
The Assembler
www.freescale.com/universityprograms
Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners Freescale Semiconductor, Inc. 2006. Document Number: LABS12CINTRO05 /REV 1
Answers provided to the Instructor assume that the reader is using Freescale HCS12DT256 Family Student Learning Kit, and CodeWarrior development software.
This short course has been created using an adapted version of the Process Oriented Guided Inquiry Learning (POGIL) method. For more information visit www.pogil.org
Freescale Semiconductor
LABS12CINTRO05, Rev 1
The Assembler
Lesson Planning: This module encourages the student to learn the correct source code format for an assembly program.
Overview
After you have written a program it must be converted to the binary codes representing the instructions, which are then placed into the microcontroller's program memory. Although many application programs are written in a high level language like C, assembly language allows you to write programs directly using the instruction set of the processor. An assembler converts this assembly language program to the binary codes needed by the microcontroller.
Learning Objectives
In this module you will learn about some of the features of a typical assembler. You will learn how to enter a source file with the proper source code fields and how to use assembler directives to control how the assembler does its job.
Success Criteria
When you have finished this module you will understand how to use the assembler to create a program to download to your microcontroller. You will be able to define constants, allocate memory storage for variables, and initialize memory locations with constant data.
Prerequisites
You must be able to enter an assembly program for your microcontroller and be able to test it with your software development system's simulator.
Common Errors: A typical mistake students make when entering a program is to not put a white space before the operation code mnemonic. This will generate an error message because the assembler treats it as a legal label and does not generate any code. Students should get into the habit of checking their list file before
Freescale Semiconductor
LABS12CINTRO05, Rev 1
running the program. Another common error is putting white space in front of a label without a colon (:) at the end of the label. In this case an error message "Not a [sic] hc12 instruction or directive." is given. Explore 1. 1. 2. What characters may be used for labels in your assembler? Is your assembler case sensitive? Answers: 1) Alphanumeric upper and lower case, underscore (_) and period(.). 2) CodeWarrior is case sensitive as a default but it may be made noncase sensitive with an assembly control switch. Stimulate 1. 1. 2. Why do you think a label in the assembler must start with an alphabetic character? Give two examples where a label might be used in an assembler program. Answers: 1) If the label starts with a number the assembler is going to try to make it into a number. If it finds a character that is not part of the number character set (0-9 for decimal, 0-9, A-F for hexadecimal) it will generate an error message. 2) Labels are used to symbolically reference a memory location and for the target of a jump or branch instruction. Opcodes: Opcodes are instruction mnemonics or a special assembler directive to help the assembler do its job. The operand field must have at least one white space in front of it. We'll discuss assembler directives below. Operands: The operand field must follow the opcode field and be separated from it by at least one white space. Operands can be symbols, constants or expressions. Explore 2. Teach: A whitespace is either a space or tab character.
Answers:
Freescale Semiconductor
LABS12CINTRO05, Rev 1
1. 2.
How are symbols used in your assembler? Constants are numerical values. How is the base for constants specified in your assembler for the following bases? a. b. c. d. Decimal Hexadecimal Binary ASCII
1) To be a readable substitute for a constant. 2a) None 2b) $ 2c) % 2d) ' ' or " " 3) Decimal
3.
Comment Field: The comment field may be anywhere in your assembler program and it must start with a character such as ";" or "*". Note that the assembler quits "looking" for other fields when it finds the semicolon or asterisk character ";".You can also have a blank line in your program to improve the readability and looks. Stimulate 2. 1. Give two reasons for using comments in a program.
Teach: We need to get the students to think about using comments to document the design as well as what an instruction is doing.
Freescale Semiconductor
LABS12CINTRO05, Rev 1
1.
If your assembler is an absolute assembler, give an example of an ORG directive that will locate the code in the correct place for your laboratory student learning kit. If your assembler is a relative assembler, give an example of a SECTION directive you will use when developing code for your student learning kit.
2.
Teach: Students often have trouble understanding that the EQU does not produce any code. Relating it to #define in C may help. Answers: 1a) C1: EQU 18 1b) C2: EQU $12 1c) C3: EQU %00010010
Explore 5. 1. Show how to define a constant whose value is decimal 18 using the following number bases: a. b. c. Decimal Hexadecimal Binary
Teach: If students have had a C programming course it is useful to point out that variable storage allocations must be done in high level programs too. It is just that this allocation is done by the C compiler in the startup code and is invisible to them. Automatic variables are allocated stack space and static variables in other RAM memory sections. The compiler doesn't use DS directives and any initialization is done in the copydown portion of the startup code. Answers: 1) RAM 2) 12K bytes
Freescale Semiconductor
LABS12CINTRO05, Rev 1
2. 3. 4.
How much variable memory is available in your student learning kit? What is the range of addresses for the variable memory? Show how to define a data storage buffer with decimal 26 bytes and whose first address is defined by the label BUF1.
Answers: 1) CR: DC.B $0D 2) CRLF: DC.W $0D0A or CRLF: DC.B $0D, $0A 3) STRING: DC.B "This is a message!" Teach: Students often think an expression can generate code. For example, they may try to load five times the value of a variable into a register expecting the expression to do the multiplication at run time. It is well worth talking about assembly time and run time in the context of assembler expressions and initializing variables for which storage has been allocated. Answers: Chapter 7, HC(S) Assembler Manual Teach: Students should be learning how to write
3.
Assembler Expressions
An assembler expression is a combination of symbols, constants and algebraic operators. The expression is evaluated at assembly time (when the assembler runs) and thus can define only constants. We often write an expression to define a constant in a readable way or to use in other constant definitions. For example, if we were to define the following constant CONST EQU 5
Then, if for some reason we needed to change the value of CONST, say to 6, CONST5X would automatically be changed as well.
Explore 8. 1. What algebraic operators can you use in your assembler expressions?
Freescale Semiconductor
LABS12CINTRO05, Rev 1
assembler. When labels are referred to in one module and then defined in another, you must specify these for the assembler and the linker. Different assemblers may have different names for these directives. The CodeWarrior assembler uses the terms XDEF and XREF. XREF: XREF is used to indicate that the label is referred to in this module and defined in another. XDEF: XDEF is used to indicate that a label is defined in this module and is therefore globally known in the system. It can be linked to any module that has an associated XREF. For every XREF there must be a companion XDEF in some other module.
Problem 1. Two relocatable code modules are shown below. Enter these in your relocatable assembler program and complete the following questions. 1. For the main module: a. Two, one-byte variable data storage locations VAR1 and VAR2 are required. Add these in the MyData variable data section. Two, one-byte constants, CON1 = 1 and CON2 = 2, are to be initialized in the MyConst constant data section. Add these. Complete the XDEF and XREF lines.
b.
c. 2.
For module_1: a. b. c. A constant, CON4 = 4, is used in both modules. Define it. A one-byte constant, CON3 = 3, is to be initialized in the MyConst constant data section. Three, one-byte variable data storage locations, VAR2, VAR3 and VAR5 are required. Add these in the MyData variable data section. One, two-byte variable data storage location, VAR4 is required. Add this in the MyData variable data section. Complete the XDEF and XREF lines.
d.
e. 3.
When the program has been run, what are the contents of the following memory locations
Freescale Semiconductor
LABS12CINTRO05, Rev 1
a. b. 4. 5. 6.
Main Module: VAR1, VAR2? Module_1: VAR2, VAR3, VAR4 and VAR5
Why can you have a variable VAR2 defined in both modules? What would happen if you allocated storage for a variable VAR3 in the main module? In module_1 how is the expression CON4+1 evaluated?
;******************************** ; Main Module XDEF XREF ; Code Section MyCode: SECTION Entry: main: ;******************************** ; Initialize stack pointer register lds main_loop: ; DO movb ldaa staa jsr ; FOREVER bra main_loop MyConst:SECTION ; Place constant data here ;******************************** MyData: SECTION ; Place variable data here #CON4,VAR2 CON2 VAR3 module_2 #__SEG_END_SSTACK ;********************************
;********************************
Freescale Semiconductor
LABS12CINTRO05, Rev 1
; Module 1 ;******************************** XDEF XREF ;******************************** ; Constant Equates ;******************************** module_2: movb ldaa staa ldab stab std rts ;******************************** MyConst:SECTION ;******************************** MyData: SECTION #CON4+1,VAR5 CON1 VAR2 CON3 VAR1 VAR4
Communication Reporting
Demonstrate to your laboratory instructor that you can assemble the two module programs, link them, and run the program using the simulator. Show your instructor the assembler list files for each of the modules.
Reflection on Learning
1. 2. 3. What was the most useful thing you learned from this module? For which part of this module do you have questions? What will be your strategy for learning more about something you still have questions about?
Freescale Semiconductor
LABS12CINTRO05, Rev 1
Instructor Notes
Problem 1 Answers 1a) VAR1: DS.B 1 VAR2: DS.B 1 1b) CON1: DC.B $01 CON2: DC.B $02 1c) XDEF XREF Entry, main, CON1, VAR1 __SEG_END_SSTACK, VAR3, CON4, module_2
2a) CON4: EQU 4 2b) CON3: DC.B 3 2c) VAR2: DS.B 1 VAR3: DS.B 1 VAR5: DS.B 1 2d) VAR4: DS.W 1 or VAR4: DS.B 2 2e) XDEF XREF VAR3, CON3, CON4, module_2 CON1, VAR1
4) The scope of VAR2 is only the module within which it is defined in each case because VAR2 has not been either XDEFed or XREFed. 5) The assembler will give you a duplicate label found error. 6) CON4+1 = 5
Freescale Semiconductor
LABS12CINTRO05, Rev 1
10
Revision History
Revision 0 1 2 Comments Initial Release Miscellaneous edits Updated for use with PBMCUSLK and CSM12DT256B MCU Module (2/8/2008) Author Fred Cady Fred Cady Ken Hsu RIT
Freescale Semiconductor
LABS12CINTRO05, Rev 1
11
Web Support: http://www.freescale.com/support USA/Europe or Locations Not Listed: Freescale Semiconductor, Inc. Technical Information Center, EL516 2100 East Elliot Road Tempe, Arizona 85284 +1-800-521-6274 or +1-480-768-2130 www.freescale.com/support
Europe, Middle East, and Africa: Freescale Halbleiter Deutschland GmbH Technical Information Center Schatzbogen 7 81829 Muenchen, Germany +44 1296 380 456 (English) +46 8 52200080 (English) +49 89 92103 559 (German) +33 1 69 35 48 48 (French) www.freescale.com/support
Japan: Freescale Semiconductor Japan Ltd. Headquarters ARCO Tower 15F 1-8-1, Shimo-Meguro, Meguro-ku, Tokyo 153-0064 Japan 0120 191014 or +81 3 5437 9125 support.japan@freescale.com Asia/Pacific: Freescale Semiconductor Hong Kong Ltd. Technical Information Center 2 Dai King Street Tai Po Industrial Estate Tai Po, N.T., Hong Kong +800 2666 8080 support.asia@freescale.com
Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. ARM is the registered trademark of ARM Limited. ARM9, ARM11, and ARML210 are the trademarks of ARM Limited. Java and all other Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. The PowerPC name is a trademark of IBM Corp. and used under license. Freescale Semiconductor, Inc. 2006. Document Number: LABS12CINTRO05 /REV 1
For Literature Requests Only: Freescale Semiconductor Literature Distribution Center P.O. Box 5405 Denver, Colorado 80217 1-800-441-2447 or 303-675-2140 Fax: 303-675-2150
LDCForFreescaleSemiconductor@hibbertgroup.com
Freescale and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product or service names are the property of their respective owners. ARM is the registered trademark of ARM Limited. ARM9, ARM11, and ARML210 are the trademarks of ARM Limited. Java and all other Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. The PowerPC name is a trademark of IBM Corp. and used under license. Freescale Semiconductor, Inc. 2006. Document Number: LABS12CINTRO05 /REV 1