Professional Documents
Culture Documents
Slau131b PDF
Slau131b PDF
User's Guide
Preface.............................................................................................................................. 13
1 Introduction to the Software Development Tools .......................................................... 15
1.1 Software Development Tools Overview ................................................................................. 16
1.2 Tools Descriptions.......................................................................................................... 17
List of Figures
1-1 MSP430 Software Development Flow .................................................................................. 16
2-1 Partitioning Memory Into Logical Blocks ................................................................................ 20
2-2 Using Sections Directives Example ...................................................................................... 25
2-3 Object Code Generated by the File in Figure 2-2 ...................................................................... 26
2-4 Combining Input Sections to Form an Executable Object Module................................................... 28
3-1 The Assembler in the MSP430 Software Development Flow ........................................................ 35
3-2 Example Assembler Listing ............................................................................................... 50
4-1 The .field Directive ......................................................................................................... 59
4-2 Initialization Directives ..................................................................................................... 60
4-3 The .align Directive......................................................................................................... 61
4-4 The .space and .bes Directives .......................................................................................... 61
4-5 Single-Precision Floating-Point Format ................................................................................. 78
4-6 The .field Directive ......................................................................................................... 84
4-7 The .usect Directive ..................................................................................................... 111
6-1 The Archiver in the MSP430 Software Development Flow .......................................................... 133
7-1 The Linker in the MSP430 Software Development Flow ............................................................ 139
7-2 Section Allocation Defined by Example 7-4 ........................................................................... 163
7-3 Run-Time Execution of Example 7-9 ................................................................................... 175
7-4 Memory Allocation Shown in Example 7-11 and Example 7-12 .................................................... 176
7-5 Autoinitialization at Run Time ........................................................................................... 199
7-6 Initialization at Load Time ............................................................................................... 200
8-1 Absolute Lister Development Flow ..................................................................................... 204
9-1 The Cross-Reference Lister in the MSP430 Software Development Flow ........................................ 210
11-1 The Hex Conversion Utility in the MSP430 Software Development Flow ......................................... 220
11-2 Hex Conversion Utility Process Flow................................................................................... 224
11-3 Object File Data and Memory Widths .................................................................................. 225
11-4 Data, Memory, and ROM Widths ....................................................................................... 227
11-5 The infile.out File Partitioned Into Four Output Files ................................................................. 230
11-6 ASCII-Hex Object Format................................................................................................ 236
11-7 Intel Hexadecimal Object Format ....................................................................................... 237
11-8 Motorola-S Format ........................................................................................................ 238
11-9 TI-Tagged Object Format ................................................................................................ 239
11-10 TI-TXT Object Format .................................................................................................... 240
11-11 Extended Tektronix Object Format ..................................................................................... 241
List of Tables
3-1 MSP430 Assembler Options .............................................................................................. 36
3-2 Operators Used in Expressions (Precedence) ........................................................................ 48
3-3 Symbol Attributes........................................................................................................... 52
4-1 Directives That Define Sections .......................................................................................... 54
4-2 Directives That Initialize Constants (Data and Memory) .............................................................. 54
4-3 Directives That Perform Alignment and Reserve Space .............................................................. 55
4-4 Directives That Format the Output Listing .............................................................................. 55
4-5 Directives That Reference Other Files .................................................................................. 55
4-6 Directives That Enable Conditional Assembly .......................................................................... 56
4-7 Directives That Define Structures ........................................................................................ 56
4-8 Directives That Define Symbols at Assembly Time .................................................................... 56
4-9 Directives That Perform Miscellaneous Functions ..................................................................... 56
5-1 Substitution Symbol Functions and Return Values................................................................... 118
5-2 Creating Macros .......................................................................................................... 129
5-3 Manipulating Substitution Symbols ..................................................................................... 129
5-4 Conditional Assembly .................................................................................................... 129
5-5 Producing Assembly-Time Messages .................................................................................. 129
5-6 Formatting the Listing .................................................................................................... 129
7-1 Linker Options Summary ................................................................................................ 141
7-2 Groups of Operators Used in Expressions (Precedence) ........................................................... 183
9-1 Symbol Attributes in Cross-Reference Listing ........................................................................ 212
11-1 Basic Hex Conversion Utility Options .................................................................................. 222
11-2 Options for Specifying Hex Conversion Formats ..................................................................... 236
A-1 Symbolic Debugging Directives ......................................................................................... 253
• In syntax descriptions, the instruction, command, or directive is in a bold typeface and parameters are
in an italic typeface. Portions of a syntax that are in bold should be entered as shown; portions of a
syntax that are in italics describe the type of information that should be entered.
• Square brackets ( [ and ] ) identify an optional parameter. If you use an optional parameter, you specify
the information within the brackets. Unless the square brackets are in the bold typeface, do not enter
the brackets themselves. The following is an example of a command that has an optional parameter:
cl430 [options] [filenames] [--run_linker [link_options] [object files]]
• Braces ( { and } ) indicate that you must choose one of the parameters within the braces; you do not
enter the braces themselves. This is an example of a command with braces that are not included in the
actual syntax but indicate that you must specify either the --rom_model or --ram_model option:
cl430 --run_linker {--rom_model | --ram_model} filenames [--output_file=name.out]
--library= libraryname
• In assembler syntax statements, column 1 is reserved for the first character of a label or symbol. If the
label or symbol is optional, it is usually not shown. If it is a required parameter, it is shown starting
against the left margin of the box, as in the example below. No instruction, command, directive, or
parameter, other than a symbol or label, can begin in column 1.
symbol .usect "section name", size in bytes[, alignment]
• Some directives can have a varying number of parameters. For example, the .byte directive can have
up to 100 parameters. This syntax is shown as [, ..., parameter].
• Following are other symbols and abbreviations used throughout this document:
Symbol Definition
B,b Suffix — binary integer
H, h Suffix — hexadecimal integer
LSB Least significant bit
MSB Most significant bit
0x Prefix — hexadecimal integer
Q, q Suffix — octal integer
The MSP430™ is supported by a set of software development tools, which includes an optimizing C/C++
compiler, an assembler, a linker, and assorted utilities. This chapter provides an overview of these tools.
The MSP430 is supported by the following assembly language development tools:
• Assembler
• Archiver
• Linker
• Absolute lister
• Cross-reference lister
• Object file display utility
• Name utility
• Strip utility
• Hex conversion utility
This chapter shows how these tools fit into the general software tools development flow and gives a brief
description of each tool. For convenience, it also summarizes the C/C++ compiler and debugging tools.
For detailed information on the compiler and debugger, and for complete descriptions of the MSP430,
refer to books listed in Related Documentation From Texas Instruments.
C/C++
source
files
Macro
source C/C++
files compiler
C/C++ name
Assembler
Archiver demangling
source
utility
Macro
library Assembler
Library-build Debugging
Object
Archiver process tools
files
Run-time-
Library of support
object library
files Linker
Executable
object file
Hex-conversion
utility
The assembler and linker create object modules that can be executed by a MSP430™ device.
Object modules make modular programming easier because they encourage you to think in terms of
blocks of code and data when you write an assembly language program. These blocks are known as
sections. Both the assembler and the linker provide directives that allow you to create and manipulate
sections.
This chapter focuses on the concept and use of sections in assembly language programs.
2.1 Sections................................................................................... 20
2.2 How the Assembler Handles Sections ......................................... 21
2.3 How the Linker Handles Sections ............................................... 27
2.4 Relocation ................................................................................ 29
2.5 Run-Time Relocation ................................................................. 30
2.6 Loading a Program .................................................................... 31
2.7 Symbols in an Object File .......................................................... 31
2.1 Sections
The smallest unit of an object file is called a section. A section is a block of code or data that occupies
contiguous space in the memory map with other sections. Each section of an object file is separate and
distinct. Object files usually contain three default sections:
.text section usually contains executable code
.data section usually contains initialized data
.bss section usually reserves space for uninitialized variables
In addition, the assembler and linker allow you to create, name, and link named sections that are used like
the .data, .text, and .bss sections.
There are two basic types of sections:
Initialized sections contain data or code. The .text and .data sections are initialized; named
sections created with the .sect assembler directive are also initialized.
Uninitialized sections reserve space in the memory map for uninitialized data. The .bss section is
uninitialized; named sections created with the .usect assembler directive are
also uninitialized.
Several assembler directives allow you to associate various portions of code and data with the appropriate
sections. The assembler builds these sections during the assembly process, creating an object file
organized as shown in Figure 2-1.
One of the linker's functions is to relocate sections into the target system's memory map; this function is
called allocation. Because most systems contain several types of memory, using sections can help you
use target memory more efficiently. All sections are independently relocatable; you can place any section
into any allocated block of target memory. For example, you can define a section that contains an
initialization routine and then allocate the routine into a portion of the memory map that contains ROM.
Figure 2-1 shows the relationship between sections in an object file and a hypothetical target memory.
.bss RAM
.data EEPROM
.text
ROM
The assembler identifies the portions of an assembly language program that belong in a given section.
The assembler has five directives that support this function:
• .bss
• .usect
• .text
• .data
• .sect
The .bss and .usect directives create uninitialized sections; the .text, .data, and .sect directives create
initialized sections.
You can create subsections of any section to give you tighter control of the memory map. Subsections are
created using the .sect and .usect directives. Subsections are identified with the base section name and a
subsection name separated by a colon; see Section 2.2.4.
symbol points to the first byte reserved by this invocation of the .bss or .usect directive. The
symbol corresponds to the name of the variable that you are reserving space for. It can
be referenced by any other section and can also be declared as a global symbol (with
the .global directive).
size in bytes is an absolute expression.
• The .bss directive reserves size in bytes bytes in the .bss section. The default value
is 1 byte.
• The .usect directive reserves size in bytes bytes in section name. You must specify a
size; there is no default value.
alignment is an optional parameter. It specifies the minimum alignment in bytes required by the
space allocated. The default value is byte aligned. The value must be power of 2.
section name tells the assembler which named section to reserve space in. See Section 2.2.3.
The initialized section directives (.text, .data, and .sect) tell the assembler to stop assembling into the
current section and begin assembling into the indicated section. The .bss and .usect directives, however,
do not end the current section and begin a new one; they simply escape from the current section
temporarily. The .bss and .usect directives can appear anywhere in an initialized section without affecting
its contents. For an example, see Section 2.2.6.
The assembler treats uninitialized subsections (created with the .usect directive) in the same manner as
uninitialized sections. See Section 2.2.4, for more information on creating subsections.
.text
.data
.sect " section name "
When the assembler encounters one of these directives, it stops assembling into the current section
(acting as an implied end of current section command). It then assembles subsequent code into the
designated section until it encounters another .text, .data, or .sect directive.
Sections are built through an iterative process. For example, when the assembler first encounters a .data
directive, the .data section is empty. The statements following this first .data directive are assembled into
the .data section (until the assembler encounters a .text or .sect directive). If the assembler encounters
subsequent .data directives, it adds the statements following these .data directives to the statements
already in the .data section. This creates a single .data section that can be allocated continuously into
memory.
Initialized subsections are created with the .sect directive. The assembler treats initialized subsections in
the same manner as initialized sections. See Section 2.2.4, for more information on creating subsections.
The section name parameter is the name of the section. Section names are significant to 200 characters.
You can create up to 32 767 separate named sections. For the .usect and .sect directives, a section name
can refer to a subsection; see Section 2.2.4 for details.
Each time you invoke one of these directives with a new name, you create a new named section. Each
time you invoke one of these directives with a name that was already used, the assembler assembles
code or data (or reserves space) into the section with that name. You cannot use the same names with
different directives. That is, you cannot create a section with the .usect directive and then try to use the
same section with .sect.
2.2.4 Subsections
Subsections are smaller sections within larger sections. Like sections, subsections can be manipulated by
the linker. Subsections give you tighter control of the memory map. You can create subsections by using
the .sect or .usect directive. The syntaxes for a subsection name are:
A subsection is identified by the base section name followed by a colon and the name of the subsection. A
subsection can be allocated separately or grouped with other sections using the same base name. For
example, you create a subsection called _func within the .text section:
.sect ".text:_func"
Using the linker's SECTIONS directive, you can allocate .text:_func separately, or with all the .text
sections. See Section 7.8.1 for an example using subsections.
You can create two types of subsections:
• Initialized subsections are created using the .sect directive. See Section 2.2.2.
• Uninitialized subsections are created using the .usect directive. See Section 2.2.1.
Subsections are allocated in the same manner as sections. See Section 7.8 for information on the
SECTIONS directive.
See Section 3.10 for more information on interpreting the fields in a source listing.
1 .global _mpyi
5 0000 .data
6 0000 0011 coeff .word 011h,0x22,0x33
0002 0022
0004 0033
18 0000 .text
19 0000 403A add: MOV.W #0x1234,R10
0002 1234
20 0004 521A ADD.W &coeff+1,R10
0006 0001!
24 0008 .data
25 0008 00AA ivals .word 0xAA,0xBB,0xCC
000a 00BB
000c 00CC
34 0008 .text
35 0008 403C mpy: MOV.W #0x3456,R12
000a 3456
36 000c 421D MOV.W &coeff,R13
000e 0000!
37 0010 1290 CALL _mpyi
0012 FFEE!
As Figure 2-3 shows, the file in Figure 2-2 creates five sections:
.text contains six 32-bit words of object code.
.data contains seven words of initialized data.
vectors is a named section created with the .sect directive; it contains one word of initialized data.
.bss reserves 10 bytes in memory.
newvars is a named section created with the .usect directive; it contains eight bytes in memory.
The second column shows the object code that is assembled into these sections; the first column shows
the source statements that generated the object code.
19 403A .text
19 1234
20 521A
20 0001!
35 403C
35 3456
36 421D
36 0000!
37 1290
37 FFEE!
6 0011 .data
6 0022
6 0033
14 0123
25 00AA
25 00BB
25 00CC
No data - .bss
10 ten bytes
reserved
No data - newvars
29
eight bytes
30
reserved
0300 vectors
42
The linker has two main functions related to sections. First, the linker uses the sections in object files as
building blocks; it combines input sections (when more than one file is being linked) to create output
sections in an executable output module. Second, the linker chooses memory addresses for the output
sections.
Two linker directives support these functions:
• The MEMORY directive allows you to define the memory map of a target system. You can name
portions of memory and specify their starting addresses and their lengths.
• The SECTIONS directive tells the linker how to combine input sections into output sections and where
to place these output sections in memory.
Subsections allow you to manipulate sections with greater precision. You can specify subsections with the
linker's SECTIONS directive. If you do not specify a subsection explicitly, then the subsection is combined
with the other sections with the same base section name.
It is not always necessary to use linker directives. If you do not use them, the linker uses the target
processor's default allocation algorithm described in Section 7.12. When you do use linker directives, you
must specify them in a linker command file.
Refer to the following sections for more information about linker command files and linker directives:
• Section 7.5, Linker Command Files
• Section 7.7, The MEMORY Directive
• Section 7.8, The SECTIONS Directive
• Section 7.12, Default Allocation Algorithm
.bss
.text Executable
object module Memory map
.data file1
(.bss) Space for
variables
Init file2 (.bss)
(named section) (.bss)
file1
(.data) Initialized
data
file2 (.data)
(.data)
file1
(.text) Executable
code
file2 (.text)
file2.obj (.text)
.data
Tables
(named section)
In Figure 2-4, file1.obj and file2.obj have been assembled to be used as linker input. Each contains the
.text, .data, and .bss default sections; in addition, each contains a named section. The executable object
module shows the combined sections. The linker combines the .text section from file1.obj with the .text
section from file2.obj to form one .text section, then combines the two .data sections and the two .bss
sections, and finally places the named sections at the end. The memory map shows how the sections are
put into memory; by default, the linker begins at address 0h and places the sections one after the other as
shown.
By default, the linker begins at 0h and places the sections one after the other in the following order: .text,
.const, .data, .bss, .cinit, and then any named sections in the order they are encountered in the input files.
The C/C++ compiler uses the .const section to store string constants, and variables or arrays that are
declared as . The C/C++ compiler produces tables of data for autoinitializing global variables; these
variables are stored in a named section called .cinit (see Example 7-7). For more information on the .const
and .cinit sections, see the MSP430 Optimizing C/C++ Compiler User's Guide.
2.4 Relocation
The assembler treats each section as if it began at address 0. All relocatable symbols (labels) are relative
to address 0 in their sections. Of course, all sections cannot actually begin at address 0 in memory, so the
linker relocates sections by:
• Allocating them into the memory map so that they begin at the appropriate address as defined with the
linker's MEMORY directive
• Adjusting symbol values to correspond to the new section addresses
• Adjusting references to relocated symbols to reflect the adjusted symbol values
In Example 2-1, both symbols X and Y are relocatable. Y is defined in the .text section of this module; X is
defined in another module. When the code is assembled, X has a value of 0 (the assembler assumes all
undefined external symbols have values of 0), and Y has a value of 8 (relative to address 0 in the .text
section). The assembler generates two relocation entries: one for X and one for Y. The reference to X is
an external reference and the reference to Y is an internally defined relocatable symbol (both are indicated
by the ! character in the listing).
After the code is linked, suppose that X is relocated to address 0x0800. Suppose also that the .text
section is relocated to begin at address 0x0600; Y now has a relocated value of 0x0608. The linker uses
the relocation entry for the reference to X to patch the branch instruction in the object code:
4B820000! becomes 4B820800
Each section in an object module has a table of relocation entries. The table contains one relocation entry
for each relocatable reference in the section. The linker usually removes relocation entries after it uses
them. This prevents the output file from being relocated again (if it is relinked or when it is loaded). A file
that contains no relocation entries is an absolute file (all its addresses are absolute addresses). If you
want the linker to retain relocation entries, invoke the linker with the --relocatable option (see
Section 7.4.2.2).
The .global directive for x declares that it is an external symbol defined in this module and that other
modules can reference x. The .global directive for y declares that it is an undefined symbol that is defined
in another module. The assembler determines that y is defined in another module because it is not defined
in the current module.
The assembler places both x and y in the object file’s symbol table. When the file is linked with other
object files, the entry for x resolves references to x in other files. The entry for y causes the linker to look
through the symbol tables of other files for y’s definition.
The linker must match all references with corresponding definitions. If the linker cannot find a symbol's
definition, it prints an error message about the unresolved reference. This type of error prevents the linker
from creating an executable object module.
Assembler Description
The MSP430™ assembler translates assembly language source files into machine language object files.
These files are in object modules, which are discussed in Chapter 2. Source files can contain the following
assembly language elements:
Assembler directives described in Chapter 4
Macro directives described in Chapter 5
Assembly language instructions described in the MSP430x1xx Family User's Guide,
MSP430x3xx Family User's Guide, or MSP430x4xx
Family User's Guide
C/C++
source
files
Macro
source C/C++
files compiler
C/C++ name
Assembler
Archiver demangling
source
utility
Macro
library Assembler
Library-build Debugging
Object
Archiver process tools
files
Run-time-
Library of support
object library
files Linker
Executable
object file
Hex-conversion
utility
cl430 is the command that invokes the assembler through the compiler. The compiler considers
any file with an .asm extension to be an assembly file and calls the assembler.
input file names the assembly language source file.
options identify the assembler options that you want to use. Options are not case sensitive and can
appear anywhere on the command line following the command. Precede each option with a
hyphen.
.copy ["]filename["]
.include ["]filename["]
.mlib ["]filename["]
The filename names a copy/include file that the assembler reads statements from or a macro library that
contains macro definitions. If filename begins with a number the double quotes are required. The filename
may be a complete pathname, a partial pathname, or a filename with no path information. The assembler
searches for the file in the following locations in the order given:
1. The directory that contains the current source file. The current source file is the file being assembled
when the .copy, .include, or .mlib directive is encountered.
2. Any directories named with the --include_path option
3. Any directories named with the MSP430_A_DIR environment variable
4. Any directories named with the MSP430_C_DIR environment variable
Because of this search hierarchy, you can augment the assembler's directory search algorithm by using
the --include_path option (described in Section 3.4.1) or the MSP430_A_DIR environment variable
(described in Section 3.4.2). MSP430_C_DIR is discussed in the MSP430 Optimizing C/C++ Compiler
User's Guide.
There is no limit to the number of --include_path options per invocation; each --include_path option names
one pathname. In assembly source, you can use the .copy, .include, or .mlib directive without specifying
path information. If the assembler does not find the file in the directory that contains the current source
file, it searches the paths designated by the --include_path options.
For example, assume that a file called source.asm is in the current directory; source.asm contains the
following directive statement:
.copy "copy.asm"
UNIX: /tools/files/copy.asm
Windows: c:\tools\files\copy.asm
You could set up the search path with the commands shown below:
The assembler first searches for copy.asm in the current directory because source.asm is in the current
directory. Then the assembler searches in the directory named with the --include_path option.
The pathnames are directories that contain copy/include files or macro libraries. The pathnames must
follow these constraints:
• Pathnames must be separated with a semicolon.
• Spaces or tabs at the beginning or end of a path are ignored. For example the space before and after
the semicolon in the following is ignored:
set MSP430_A_DIR= c:\path\one\to\tools ; c:\path\two\to\tools
• Spaces and tabs are allowed within paths to accommodate Windows directories that contain spaces.
For example, the pathnames in the following are valid:
set MSP430_A_DIR=c:\first path\to\tools;d:\second path\to\tools
In assembly source, you can use the .copy, .include, or .mlib directive without specifying path information.
If the assembler does not find the file in the directory that contains the current source file or in directories
named by the --include_path option, it searches the paths named by the environment variable.
For example, assume that a file called source.asm contains these statements:
.copy "copy1.asm"
.copy "copy2.asm"
The assembler first searches for copy1.asm and copy2.asm in the current directory because source.asm
is in the current directory. Then the assembler searches in the directory named with the --include_path
option and finds copy1.asm. Finally, the assembler searches the directory named with MSP430_A_DIR
and finds copy2.asm.
The environment variable remains set until you reboot the system or reset the variable by entering one of
these commands:
The MSP430 assembler reads up to 200 characters per line. Any characters beyond 200 are truncated.
Keep the operational part of your source statements (that is, everything other than comments) less than
200 characters in length for correct assembly. Your comments can extend beyond the 200-character limit,
but the truncated portion is not included in the listing file.
Follow these guidelines:
• All statements must begin with a label, a blank, an asterisk, or a semicolon.
• Labels are optional; if used, they must begin in column 1.
• One or more blanks must separate each field. Tab and space characters are blanks. You must
separate the operand list from the preceding field with a blank.
• Comments are optional. Comments that begin in column 1 can begin with an asterisk or a semicolon (*
or ;), but comments that begin in any other column must begin with a semicolon.
• A mnemonic cannot begin in column 1 or it will be interpreted as a label.
The following sections describe each of the fields.
A label on a line by itself is a valid statement. The label assigns the current value of the section program
counter to the label; this is equivalent to the following directive statement:
label .equ $ ; $ provides the current value of the SPC
When a label appears on a line by itself, it points to the instruction on the next line (the SPC is not
incremented):
3 0050 Here:
4 0050 0003 .word 3
If you do not use a label, the character in column 1 must be a blank, an asterisk, or a semicolon.
You use immediate values as operands primarily with instructions. In some cases, you can use immediate
values with the operands of directives. For instance, you can use immediate values with the .byte directive
to load values into the current section. It is not usually necessary to use the # prefix for directives.
Compare the following statements:
ADD.W #10, R11
.byte 10
3.6 Constants
The assembler supports several types of constants:
• Binary integer
• Octal integer
• Decimal integer
• Hexadecimal integer
• Character
• Assembly time
The assembler maintains each constant internally as a 32-bit quantity. Constants are not sign extended.
For example, the constant 00FFh is equal to 00FF (base 16) or 255 (base 10); it does not equal -1.
However, when used with the .byte directive, -1 is equivalent to 00FFh.
You can also use the .set directive to assign symbolic constants for register names. In this case, the
symbol becomes a synonym for the register:
OP1 .set R11
MOV OP1, 2(SP)
3.8 Symbols
Symbols are used as labels, constants, and substitution symbols. A symbol name is a string of up to 200
alphanumeric characters (A-Z, a-z, 0-9, $, and _). The first character in a symbol cannot be a number, and
symbols cannot contain embedded blanks. The symbols you define are case sensitive; for example, the
assembler recognizes ABC, Abc, and abc as three unique symbols. You can override case sensitivity with
the --syms_ignore_case assembler option (see Section 3.3). A symbol is valid only during the assembly in
which it is defined, unless you use the .global directive or the .def directive to declare it as an external
symbol (see Identify Global Symbols ).
3.8.1 Labels
Symbols used as labels become symbolic addresses that are associated with locations in the program.
Labels used locally within a file must be unique. Mnemonic opcodes and assembler directive names
without the . prefix are valid label names.
Labels can also be used as the operands of .global, .ref, .def, or .bss directives; for example:
.global func
This is an example of code that declares and uses a local label legally:
.global ADDRA, ADDRB, ADDRC
Label1: MOV #ADDRA, R11 ; Load Address A to R11.
SUB #ADDRB, R11 ; Subtract Address B.
JL $1 ; If < 0, branch to $1
MOV #ADDRB, R11 ; otherwise, load ADDRB to R11
JMP $2 ; and branch to $2.
$1 MOV #ADDRA, R11 ; $1: load ADDRA to AC0.
$2 ADD #ADDRC, R11 ; $2: add ADDRC.
.newblock ; Undefine $1 so it can be used again.
JMP $1 ; If less than zero, branch to $1.
MOV R11, &ADDRC ; Store AC0 low in ADDRC.
$1 NOP
The $1 label is not undefined before being reused by the second branch instruction. Therefore, $1 is
redefined, which is illegal.
Local labels are especially useful in macros. If a macro contains a normal label and is called more than
once, the assembler issues a multiple-definition error. If you use a local label and .newblock within a
macro, however, the local label is used and reset each time the macro is expanded.
Up to ten local labels of the $n form can be in effect at one time. Local labels of the form name? are not
limited. After you undefine a local label, you can define it and use it again. Local labels do not appear in
the object code symbol table.
Because local labels are intended to be used only locally, branches to local labels are not expanded in
case the branch's offset is out of range.
The assembler also has several predefined symbolic constants; these are discussed in Section 3.8.5.
The name is the name of the symbol you want to define. The value is the constant or string value you
want to assign to the symbol. If the value is omitted, the symbol is set to 1.
Once you have defined the name with the --asm_define option, the symbol can be used in place of a
constant value, a well-defined expression, or an otherwise undefined symbol used with assembly
directives and instructions. For example, on the command line you enter:
cl430 --asm_define=SYM1=1 --asm_define=SYM2=2 --asm_define=SYM3=3 --asm_define=SYM4=4 value.asm
Since you have assigned values to SYM1, SYM2, SYM3, and SYM4, you can use them in source code.
Example 3-2 shows how the value.asm file uses these symbols without defining them explicitly.
Within assembler source, you can test the symbol defined with the --asm_define option with the following
directives:
Type of Test Directive Usage
Existence .if $isdefed(" name ")
Nonexistence .if $isdefed(" name ") = 0
Equal to value .if name = value
Not equal to value .if name != value
The argument to the $isdefed built-in function must be enclosed in quotes. The quotes cause the
argument to be interpreted literally rather than as a substitution symbol.
Register symbols and aliases can be entered as all uppercase or all lowercase characters; that is, R1
could also be entered as r1, SP, or sp.
• Processor symbols, as follow:
– The .MSP430 symbol is always set to 1. The processor symbol can be entered as all uppercase or
all lowercase characters; that is, .MSP430 could also be entered as .msp430.
– The .MSP4619 symbol is set to 1 when the --silicon_version=mspx option is specified. The
processor symbol can be entered as all uppercase or all lowercase characters; that is, .MSP4619
could also be entered as .msp4619.
– The __LARGE_CODE_MODEL symbol is set to 1 when the --version=mspx option is specified.
– The __LARGE_DATA_MODEL symbol is set to 1 when the --version=mspx and
--large_memory_model options are used.
When you are using macros, substitution symbols are important because macro parameters are actually
substitution symbols that are assigned a macro argument. The following code shows how substitution
symbols are used in macros:
myadd .macro src, dest
; addl macro definition
ADD src, dest
; Add the value in register dest to the value in
; register src.
.endm
*myadd invocation
myadd R4, R5
; Calls the macro addl and substitutes R4 for src
; and R5 for dest. The macro adds the value of R4
; and the value of R5.
3.9 Expressions
An expression is a constant, a symbol, or a series of constants and symbols separated by arithmetic
operators. The 32-bit ranges of valid expression values are -2147 483 648 to 2147 483 647 for signed
values, and 0 to 4 294 967 295 for unsigned values. Three main factors influence the order of expression
evaluation:
Parentheses Expressions enclosed in parentheses are always evaluated first.
8 / (4 / 2) = 4, but 8 / 4 / 2 = 1
You cannot substitute braces ( { } ) or brackets ( [ ] ) for parentheses.
Precedence groups Operators, listed in Table 3-2, are divided into nine precedence groups.
When parentheses do not determine the order of expression evaluation,
the highest precedence operation is evaluated first.
8 + 4 / 2 = 10 (4 / 2 is evaluated first)
Left-to-right evaluation When parentheses and precedence groups do not determine the order of
expression evaluation, the expressions are evaluated from left to right,
except for Group 1, which is evaluated from right to left.
8 / 4*2 = 4, but 8 / (4*2) = 1
3.9.1 Operators
Table 3-2 lists the operators that can be used in expressions, according to precedence group.
(1)
Group 1 operators are evaluated right to left. All other operators are evaluated left to right.
(2)
Unary + and - have higher precedence than the binary forms.
1 .copy ”mac1.inc”
A 1 addfive .macro dst
A 2 ADD.W #5,dst
A 3 .endm
2
3 .global var1
4
5
6 0000 430B MOV.W #0,R11
7
8 .loop 5
9 addfive R11
10 .endloop
1 0002 addfive R11
2 0002 503B ADD.W #5,R11
0004 0005
1 0006 addfive R11
2 0006 503B ADD.W #5,R11
0008 0005
1 000a addfive R11
2 000a 503B ADD.W #5,R11
000c 0005
1 000e addfive R11
2 000e 503B ADD.W #5,R11
0010 0005
1 0012 addfive R11
2 0012 503B ADD.W #5,R11
0014 0005
11
12 0016 4B82 MOV.W R11,&var1
0018 0000!
When you invoke cl430 with --symdebug:dwarf (or -g) when compiling an assembly file, the assembler
provides symbolic debugging information that allows you to step through your assembly code in a
debugger rather than using the Disassembly window in Code Composer Studio. This enables you to view
source comments and other source-code annotations while debugging.
The .asmfunc and .endasmfunc (see Mark Function Boundaries ) directives enable you to use C
characteristics in assembly code that makes the process of debugging an assembly file more closely
resemble debugging a C/C++ source file.
The .asmfunc and .endasmfunc directives allow you to name certain areas of your code, and make these
areas appear in the debugger as C functions. Contiguous sections of assembly code that are not enclosed
by the .asmfunc and .endasmfunc directives are automatically placed in assembler-defined functions
named with this syntax:
$ filename : starting source line : ending source line $
If you want to view your variables as a user-defined type in C code, the types must be declared and the
variables must be defined in a C file. This C file can then be referenced in assembly code using the .ref
directive (see Identify Global Symbols ).
Example 3-3. Viewing Assembly Variables as C Types C Program
typedef struct
{
int m1;
int m2;
} X;
X svar = { 1, 2 };
.ref svar
.global addfiv
addfive: .asmfunc
MOV #5,R12
ADD R12,&svar
ADD R12,&svar + 2
RET
.endasmfunc
Example 3-3 shows the cvar.c C program that defines a variable, svar, as the structure type X. The svar
variable is then referenced in the addfive.asm assembly program in Example 3-4 and 5 is added to svar's
second data member.
Compile both source files with the --symdebug:dwarf option (-g) and link them as follows:
cl430 -symdebug:dwarf cvars.c addfive.asm --run_linker --library=lnk.cmd --library=rts430.lib
--output_file=addfive.out
When you load this program into a symbolic debugger, addfive appears as a C function. You can monitor
the values in svar while stepping through main just as you would any regular C variable.
.MSP430 0001 0
.msp430 0001 0
addfive 0000' 6 3
svar REF 1 7 8
Label column contains each symbol that was defined or referenced during the assembly.
Value column contains an 8-digit hexadecimal number (which is the value assigned to the
symbol) or a name that describes the symbol's attributes. A value may also be
preceded by a character that describes the symbol's attributes. Table 3-3 lists these
characters and names.
Definition (DEFN) column contains the statement number that defines the symbol. This
column is blank for undefined symbols.
Reference (REF) column lists the line numbers of statements that reference the symbol. A
blank in this column indicates that the symbol was never used.
Assembler Directives
Assembler directives supply data to the program and control the assembly process. Assembler directives
enable you to do the following:
• Assemble code and data into specified sections
• Reserve space in memory for uninitialized variables
• Control the appearance of listings
• Initialize memory
• Assemble conditional blocks
• Define global variables
• Specify libraries from which the assembler can obtain macros
• Examine symbolic debugging information
This chapter is divided into two parts: the first part (Section 4.1 through Section 4.10) describes the
directives according to function, and the second part (Section 4.11) is an alphabetical reference.
The .bss and .usect directives do not end the current section or begin new sections; they reserve the
specified amount of space, and then the assembler resumes assembling code or data into the current
section.
3 bits
15 8 7 6 5 4 3
0 0 1 0 0 0 0 1 1 .field 8, 6
6 bits
15 13 12 11 10 9
1 0 0 0 0 0 0 1 0 0 0 0 1 1 .field 16, 5
5 bits
15 14 13 12 11 10 9
0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 .field 16, 5
16 bits
• The .int and .word directives place one or more 16-bit values into consecutive words in the current
section.
• The .string directive places 8-bit characters from one or more character strings into the current
section. This directive is similar to .byte, placing an 8-bit character in each consecutive byte of the
current section.
• The .long directive places one or more 32-bit values into consecutive words in the current section.
Figure 4-2 compares the .byte, .char, .int, .long, .float, .word, and .string directives, using the following
assembled code:
1 0000 00AA .byte 0AAh,0BBh
0001 00BB
2 0002 00CC .char 0xCC
3 0004 DDDD .word 0xDDDD
4 0006 FFFF .long 0xEEEEFFFF
0008 EEEE
5 000a DDDD .int 0xDDDD
6 000c FCB9 .float 1.9999
000e 3FFF
7 0010 0048 .string "Hello"
0011 0065
0012 006C
0013 0070
7 0
1 B B .byte OBBh
7 0
2 C C .char OCCh
15 0
4 D D D D .word DDDDh
15 0
6 F F F F .long EEEEFFFFh
15 0
8 E E E E
15 0
a D D D D .int 0DDDDh
31 0
c F C B D .float 1.9999
31 0
e 3 F F F
7 0
10 4 8 .string “Help”
H
11 6 5
e
12 6 C
l
13 7 0
p
• The .bes and .space directives reserve a specified number of bytes in the current section. The
assembler fills these reserved bytes with 0s.
– When you use a label with .space, it points to the first byte that contains reserved bits.
– When you use a label with .bes, it points to the last byte that contains reserved bits.
Figure 4-4 shows how the .space and .bes directives work for the following assembled code:
1 0000 0100 .word 0x100,0x200
0002 0200
2 0004 Res_1 .space 17
3 0016 000F .word 15
4 002b Res_2 .bes 20
5 002c 00BA .byte 0xBA
Res_1 points to the first byte in the space reserved by .space. Res_2 points to the last byte in the
space reserved by .bes.
Res_1 = 04h
17 bytes
reserved
20 bytes
reserved
Res_2 = 2Bh
• The source code listing includes false conditional blocks that do not generate code. The .fclist and
.fcnolist directives turn this listing on and off. You can use the .fclist directive to list false conditional
blocks exactly as they appear in the source code. You can use the .fcnolist directive to list only the
conditional blocks that are actually assembled.
• The .length directive controls the page length of the listing file. You can use this directive to adjust
listings for various output devices.
• The .list and .nolist directives turn the output listing on and off. You can use the .nolist directive to
prevent the assembler from printing selected source statements in the listing file. Use the .list directive
to turn the listing on again.
• The source code listing includes macro expansions and loop blocks. The .mlist and .mnolist directives
turn this listing on and off. You can use the .mlist directive to print all macro expansions and loop
blocks to the listing, and the .mnolist directive to suppress this listing.
• The .option directive controls certain features in the listing file. This directive has the following
operands:
A turns on listing of all directives and data, and subsequent expansions, macros, and blocks.
B limits the listing of .byte and .char directives to one line.
H limits the listing of .half and .short directives to one line.
M turns off macro expansions in the listing.
N turns off listing (performs .nolist).
O turns on listing (performs .list).
R resets the B, H, M, T, and W directives (turns off the limits of B, H, M, T, and W).
T limits the listing of .string directives to one line.
W limits the listing of .word and .int directives to one line.
X produces a cross-reference listing of symbols. You can also obtain a cross-reference listing
by invoking the assembler with the --cross_reference option (see Section 3.3).
• The .if/.elseif/.else/.endif directives tell the assembler to conditionally assemble a block of code
according to the evaluation of an expression.
.if well-defined expression marks the beginning of a conditional block and assembles code
if the .if well-defined expression is true.
[.elseif well-defined expression] marks a block of code to be assembled if the .if well-defined
expression is false and the .elseif condition is true.
.else marks a block of code to be assembled if the .if well-defined
expression is false and any .elseif conditions are false.
.endif marks the end of a conditional block and terminates the block.
• The .loop/.break/.endloop directives tell the assembler to repeatedly assemble a block of code
according to the evaluation of an expression.
.loop [well-defined expression] marks the beginning of a repeatable block of code. The optional
expression evaluates to the loop count.
.break [well-defined expression] tells the assembler to assemble repeatedly when the .break
well-defined expression is false and to go to the code
immediately after .endloop when the expression is true or
omitted.
.endloop marks the end of a repeatable block.
The assembler supports several relational operators that are useful for conditional expressions. For more
information about relational operators, see Section 3.9.4.
In the single-line format, the options are followed by one or more filenames to include.
The filenames and options are separated by commas. Each file listed acts as if #include
"filename" was specified in the multiple-line format.
In the multiple-line format, the line following .cdecls must contain the opening .cdecls
block indicator %{. Everything after the %{, up to the closing block indicator %}, is
treated as C/C++ source and processed. Ordinary assembler processing then resumes
on the line following the closing %}.
The text within %{ and %} is passed to the C/C++ compiler to be converted into
assembly language. Much of C language syntax, including function and variable
definitions as well as function-like macros, is not supported and is ignored during the
conversion. However, all of what traditionally appears in C header files is supported,
including function and variable prototypes; structure and union declarations;
non-function-like macros; enumerations; and #define's.
The resulting assembly language is included in the assembly file at the point of the
.cdecls directive. If the LIST option is used, the converted assembly statements are
printed in the listing file.
The assembly resulting from the .cdecls directive is treated similarly to a .include file.
Therefore the .cdecls directive can be nested within a file being copied or included. The
assembler limits nesting to ten levels; the host operating system may set additional
restrictions. The assembler precedes the line numbers of copied files with a letter code
to identify the level of copying. An A indicates the first copied file, B indicates a second
copied file, etc.
The .cdecls directive can appear anywhere in an assembly source file, and can occur
multiple times within a file. However, the C/C++ environment created by one .cdecls is
not inherited by a later .cdecls; the C/C++ environment starts new for each .cdecls.
See Chapter 12 for more information on setting up and using the .cdecls directive with C
header files.
Example In this example, the .cdecls directive is used call the C header.h file.
C header file:
#define WANT_ID 10
#define NAME "John\n"
Source file:
.cdecls C,LIST,"myheader.h"
Listing File:
1 .cdecls C,LIST,"myheader.h"
A 1 ; ------------------------------------------
A 2 ; Assembly Generated from C/C++ Source Code
A 3 ; ------------------------------------------
A 4
A 5 ; =========== MACRO DEFINITIONS ===========
A 6 .define "1",WANT_ID
A 7 .define """John\n""",NAME
A 8 .define "1",_OPTIMIZE_FOR_SPACE
A 9
A 10 ; =========== TYPE DEFINITIONS ===========
A 11 status_enum .enum
A 12 0001 OK .emember 1
A 13 0100 FAILED .emember 256
A 14 0000 RUNNING .emember 0
A 15 .endenum
A 16
A 17 myCstruct .struct 0,2 ; struct size=(6 bytes|48 bits), alignment=2
A 18 0000 member_a .field 16 ; int member_a - offset 0 bytes, size (2
bytes|16 bits)
A 19 0002 member_b .field 32 ; float member_b - offset 2 bytes, size (4
bytes|32 bits)
Listing file:
1 0000 .space 29
2 .copy "byte.asm"
A 1 ** In byte.asm
A 2 001d 0020 .byte 32,1+ 'A'
001e 0042
A 3 .copy "word.asm"
B 1 ** In word.asm
B 2 0020 ABCD .word 0ABCDh, 56q
0022 002E
A 4 ** Back in byte.asm
A 5 0024 006A .byte 67h + 3q
3
4 ** Back in original file
5 0025 0064 .string "done"
0026 006F
0027 006E
0028 0065
Example 2 In this example, the .include directive is used to read and assemble source statements
from other files; then, the assembler resumes assembling into the current file. The
mechanism is similar to the .copy directive, except that statements are not printed in the
listing file.
include.asm byte2.asm word2.asm
(source file) (first copy file) (second copy file)
.space 29 ** In byte2.asm ** In word2.asm
.include "byte2.asm" .byte 32,1+ 'A' .word 0ABCDh, 56q
** Back in original file .include "word2.asm"
.string "done" ** Back in byte2.asm
.byte 67h + 3q
Listing file:
1 0000 .space 29
2 .include "byte2.asm"
3
4 **Back in original file
5 0025 0064 .string "done"
0026 006F
0027 006E
0028 0065
Syntax .data
Description The .data directive tells the assembler to begin assembling source code into the .data
section; .data becomes the current section. The .data section is normally used to contain
tables of data or preinitialized variables.
For more information about sections, see Chapter 2.
Example In this example, code is assembled into the .data and .text sections.
1 ; Comments here
2 0000 .data
3 0000 .space 0xCC
4
5
6 ; Comments here
7 0000 .text
8 0000 INDEX .set 0
9 0000 430B MOV #INDEX,R11
10
11
12
13 ; Comments here
14 00cc Table: .data
15 00cc FFFF .word -1
16 00ce 00FF .byte 0xFF
17
18
19
20 ; Comments here
21 0002 .text
22 0002 00CC! con .field Table,16
23 0004 421B MOV &con,R11
0006 0002!
24 0008 5B1C ADD 0(R11),R12
000a 0000
25
26 00cf .data
S E E E E E E E E MMMMMMMMMMMMM MMMMMMMMMM
31 23 0
When you use .double or .float in a .struct/.endstruct sequence, they define a member's
size; they do not initialize memory. For more information, see the .struct/.endstruct/.tag
topic .
Example Following are examples of the .float and .double directives:
1 0000 5951 .double -2.0e25
0002 E984
2 0004 5951 .float -1.0e25
0006 E904
3 0008 0000 .double 6
000a 40C0
4 000c 0000 .float 3
000e 4040
Syntax .drlist
.drnolist
Description Two directives enable you to control the printing of assembler directives to the listing file:
The .drlist directive enables the printing of all directives to the listing file.
The .drnolist directive suppresses the printing of the following directives to the listing
file. The .drnolist directive has no affect within macros.
Example This example shows how .drnolist inhibits the listing of the specified directives.
Source file:
.asg 0, x
.loop 2
.eval x+1, x
.endloop
.drnolist
.asg 1, x
.loop 3
.eval x+1, x
.endloop
Listing file:
1 .asg 0, x
2 .loop 2
3 .eval x+1, x
4 .endloop
1 .eval 0+1, x
1 .eval 1+1, x
5
6 .drnolist
7
9 .loop 3
10 .eval x+1, x
11 .endloop
Listing file:
1 MSG_EX .macro parm1
2 .if $symlen(parm1)=0
3 .emsg "ERROR -- MISSING PARAMETER"
4 .else
5 add parm1, r7
6 .endif
7 .endm
8
9 0000 MSG_EX R11
1 .if $symlen(parm1)=0
1 .emsg "ERROR -- MISSING PARAMETER"
1 .else
1 0000 5B07 add R11, r7
1 .endif
10
11 0002 MSG_EX
1 .if $symlen(parm1)=0
1 .emsg "ERROR -- MISSING PARAMETER"
"emsg.asm", ERROR! at line 11: [ ***** USER ERROR ***** - ] ERROR --
MISSING PARAMETER
1 .else
1 add parm1, r7
1 .endif
In addition, the following messages are sent to standard output by the assembler:
*** ERROR! line 11: ***** USER ERROR ***** - : ERROR -- MISSING PARAMETER
.emsg "ERROR -- MISSING PARAMETER"
Syntax .end
Description The .end directive is optional and terminates assembly. The assembler ignores any
source statements that follow a .end directive. If you use the .end directive, it must be
the last source statement of a program.
This directive has the same effect as an end-of-file character. You can use .end when
you are debugging and you want to stop assembling at a specific point in your code.
Ending a Macro
Note: Do not use the .end directive to terminate a macro; use the .endm macro
directive instead.
Example This example shows how the .end directive terminates assembly. If any source
statements follow the .end directive, the assembler ignores them.
Source file:
START: .space 300
TEMP .set 15
.bss LOC1,0x48
LOCL_n .word LOC1
MOV #TEMP,R11
MOV &LOCL_n,R12
MOV 0(R12),R13
.end
.byte 4
.word 0xCCC
Listing file:
1 0000 START: .space 300
2 000F TEMP .set 15
3 0000 .bss LOC1,0x48
4 012c 0000! LOCL_n .word LOC1
5 012e 403B MOV #TEMP,R11
0130 000F
6 0132 421C MOV &LOCL_n,R12
0134 012C!
7 0136 4C1D MOV 0(R12),R13
0138 0000
8 .end
Syntax .fclist
.fcnolist
Description Two directives enable you to control the listing of false conditional blocks:
The .fclist directive allows the listing of false conditional blocks (conditional blocks that
do not produce code).
The .fcnolist directive suppresses the listing of false conditional blocks until a .fclist
directive is encountered. With .fcnolist, only code in conditional blocks that are actually
assembled appears in the listing. The .if, .elseif, .else, and .endif directives do not
appear.
By default, all conditional blocks are listed; the assembler acts as if the .fclist directive
had been used.
Example This example shows the assembly language and listing files for code with and without
the conditional blocks listed.
Source file:
AAA .set 1
BB .set 0
.fclist
.if AAA
ADD #1024,R11
.else
ADD #1024*10,R11
.endif
.fcnolist
.if AAA
ADD #1024,R11
.else
ADD #1024*10,R11
.endif
Listing file:
1 0001 AAA .set 1
2 0000 BB .set 0
3 .fclist
4 .if AAA
5 0000 503B ADD #1024,R11
0002 0400
6 .else
7 ADD #1024*10,R11
8 .endif
9 .fcnolist
11 0004 503B ADD #1024,R11
0006 0400
You can use the .align directive to force the next .field directive to begin packing into a
new word.
If you use a label, it points to the byte that contains the specified field.
When you use .field in a .struct/.endstruct sequence, .field defines a member's size; it
does not initialize memory. For more information, see the .struct/.endstruct/.tag topic .
Example This example shows how fields are packed into a word. The SPC does not change until
a word is filled and the next word is begun. Figure 4-6 shows how the directives in this
example affect memory.
1 ************************************
2 ** Initialize a 14-bit field. **
3 ************************************
4 0000 0ABC .field 0ABCh, 14
5
6 ************************************
7 ** Initialize a 5-bit field **
8 ** in the same word. **
9 ************************************
10 0002 000A L_F: .field 0Ah, 5
11
12 ************************************
13 ** Write out the word. **
14 ************************************
15 .align 4
16
17 ************************************
18 ** Initialize a 4-bit field. **
19 ** This fields starts a new word. **
20 ************************************
21 0004 000C x: .field 0Ch, 4
22
23 ************************************
24 ** 16-bit relocatable field **
25 ** in the next word. **
26 ************************************
27 0006 0004! .field x
28
29 ************************************
30 ** Initialize a 16-bit field. **
31 ************************************
32 0008 4321 .field 04321h, 16
14-bit field
15 0
0 0 0 0 0 1 0 1 0 1 0 1 1 1 1 0 0 .field 00Ah, 5
1 0 1 0 1 0
5-bit field
15 0 .align 4
2 1 1 0 0 .field 0Ch, 4
4-bit field
15 0
3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 .field x
15 0
4 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 .field 04321, 16
file2.lst
1 ; Global symbols defined in this file
2 .global X, Y, Z
3 ; Global symbol defined in file1.lst
4 .global INIT
5 0001 X: .set 1
6 0002 Y: .set 2
7 0003 Z: .set 3
8 0000 0000! .word INIT
9 ; .
10 ; .
11 ; .
12 .end
file3.lst
1 ; Global symbol defined in this file
2 .def INIT
3 ; Global symbols defined in file2.lst
4 .ref X, Y, Z
5 0000 INIT:
6 0000 503B ADD #56h, R11
0002 0056
7 0004 0000! .word X
8 ; .
9 ; .
10 ; .
11 .end
file4.lst
1 ; Global symbols defined in this file
2 .def X, Y, Z
3 ; Global symbol defined in file3.lst
4 .ref INIT
5 0001 X: .set 1
6 0002 Y: .set 2
7 0003 Z: .set 3
8 0000 0000! .word INIT
9 ; .
10 ; .
11 ; .
12 .end
See Section 7.9 for more information about assigning run-time and load-time addresses
in the linker.
********************************************
** Page length = 55 lines **
** Page width = 100 characters **
********************************************
.length 55
.width 100
Syntax .list
.nolist
Description Two directives enable you to control the printing of the source listing:
The .list directive allows the printing of the source listing.
The .nolist directive suppresses the source listing output until a .list directive is
encountered. The .nolist directive can be used to reduce assembly time and the source
listing size. It can be used in macro definitions to suppress the listing of the macro
expansion.
The assembler does not print the .list or .nolist directives or the source statements that
appear after a .nolist directive. However, it continues to increment the line counter. You
can nest the .list/.nolist directives; each .nolist needs a matching .list to restore the
listing.
By default, the source listing is printed to the listing file; the assembler acts as if the .list
directive had been used. However, if you do not request a listing file when you invoke
the assembler by including the --asm_listing option on the command line (see
Section 3.3), the assembler ignores the .list directive.
Example This example shows how the .copy directive inserts source statements from another file.
The first time this directive is encountered, the assembler lists the copied source lines in
the listing file. The second time this directive is encountered, the assembler does not list
the copied source lines, because a .nolist directive was assembled. Note that the .nolist,
the second .copy, and the .list directives do not appear in the listing file. Also, the line
counter is incremented, even when source statements are not listed.
Source file:
.copy "copy2.asm"
* Back in original file
NOP
.nolist
.copy "copy2.asm"
.list
* Back in original file
.string "Done"
Listing file:
1 .copy "copy2.asm"
A 1 * In copy2.asm (copy file)
A 2 0000 0020 .word 32, 1 + 'A'
0002 0042
2 * Back in original file
3 0004 4303 NOP
7 * Back in original file
8 000a 0044 .string "Done"
000b 006F
000c 006E
000d 0065
Now you can use the .mlib directive to reference the macro library and define the inc4
and dec4 macros:
1 .mlist
2 .mlib "mac.lib"
3 ; Macro call
4 0000 inc4 R11, R12, R13, R14
1 0000 531B ADD.W #1,R11
1 0002 531C ADD.W #1,R12
1 0004 531D ADD.W #1,R13
1 0006 531E ADD.W #1,R14
5
6 ; Macro call
7 0008 dec4 R11, R12, R13, R14
1 0008 831B SUB.W #1,R11
1 000a 831C SUB.W #1,R12
1 000c 831D SUB.W #1,R13
1 000e 831E SUB.W #1,R14
Syntax .mlist
.mnolist
Description Two directives enable you to control the listing of macro and repeatable block
expansions in the listing file:
The .mlist directive allows macro and .loop/.endloop block expansions in the listing file.
The .mnolist directive suppresses macro and .loop/.endloop block expansions in the
listing file.
By default, the assembler behaves as if the .mlist directive had been specified.
See Chapter 5 for more information on macros and macro libraries. See the
.loop/.break/.endloop topic for information on conditional blocks.
Example This example defines a macro named STR_3. The first time the macro is called, the
macro expansion is listed (by default). The second time the macro is called, the macro
expansion is not listed, because a .mnolist directive was assembled. The third time the
macro is called, the macro expansion is again listed because a .mlist directive was
assembled.
1 STR_3 .macro P1, P2, P3
2 .string ":p1:", ":p2:", ":p3:"
3 .endm
4
5 0000 STR_3 "as", "I", "am" ; Invoke STR_3 macro.
1 0000 003A .string ":p1:", ":p2:", ":p3:"
0001 0070
0002 0031
0003 003A
0004 003A
0005 0070
0006 0032
0007 003A
0008 003A
0009 0070
000a 0033
000b 003A
6 .mnolist ; Suppress expansion.
7 000c STR_3 "as", "I", "am" ; Invoke STR_3 macro.
8 .mlist ; Show macro expansion.
9 0018 STR_3 "as", "I", "am" ; Invoke STR_3 macro.
1 0018 003A .string ":p1:", ":p2:", ":p3:"
0019 0070
001a 0031
001b 003A
001c 003A
001d 0070
001e 0032
001f 003A
0020 003A
0021 0070
0022 0033
0023 003A
Syntax .newblock
Description The .newblock directive undefines any local labels currently defined. Local labels, by
nature, are temporary; the .newblock directive resets them and terminates their scope.
A local label is a label in the form $n, where n is a single decimal digit, or name?, where
name is a legal symbol name. Unlike other labels, local labels are intended to be used
locally, cannot be used in expressions, and do not qualify for branch expansion if used
with a branch. They can be used only as operands in 8-bit jump instructions. Local labels
are not included in the symbol table.
After a local label has been defined and (perhaps) used, you should use the .newblock
directive to reset it. The .text, .data, and .sect directives also reset local labels. Local
labels that are defined within an include file are not valid outside of the include file.
See Section 3.8.2 for more information on the use of local labels.
Example This example shows how the local label $1 is declared, reset, and then declared again.
1 .global ADDRA,ADDRB,ADDRC
2
3 0000 403B Label1: MOV #ADDRA, R11 ; Load Address A to R11
0002 0000!
4 0004 803B SUB #ADDRB, R11 ; Subtract Address B.
0006 0000!
5 0008 3803 JL $1 ; If < 0, branch to $1
6 000a 403B MOV #ADDRB, R11 ; otherwise, load ADDRB to R11
000c 0000!
7 000e 3C02 JMP $2 ; and branch to $2
8 0010 403B $1 MOV #ADDRA, R11 ; $1: load ADDRA to AC0.
0012 0000!
9 0014 503B $2 ADD #ADDRC, R11 ; $2: add ADDRC.
0016 0000!
10 .newblock ; Undefine $1 so can be used again.
11 0018 3C02 JMP $1 ; If less than zero,branch to $1.
12 001a 4B82 MOV R11,&ADDRC ; Store AC0 low in ADDRC.
001c 0000!
13 001e 4303 $1 NOP
Syntax .page
Description The .page directive produces a page eject in the listing file. The .page directive is not
printed in the source listing, but the assembler increments the line counter when it
encounters the .page directive. Using the .page directive to divide the source listing into
logical divisions improves program readability.
Example This example shows how the .page directive causes the assembler to begin a new page
of the source listing.
Source file:
.title "**** Page Directive Example ****"
; .
; .
; .
.page
Listing file:
MSP430 COFF Assembler PC vx.x.x Mon Jul 26 10:58:19 2004
2 ; .
3 ; .
4 ; .
MSP430 COFF Assembler PC vx.x.x Mon Jul 26 10:58:19 2004
Syntax .sslist
.ssnolist
Description Two directives allow you to control substitution symbol expansion in the listing file:
The .sslist directive allows substitution symbol expansion in the listing file. The
expanded line appears below the actual source line.
The .ssnolist directive suppresses substitution symbol expansion in the listing file.
By default, all substitution symbol expansion in the listing file is suppressed; the
assembler acts as if the .ssnolist directive had been used.
Lines with the pound (#) character denote expanded substitution symbols.
Example This example shows code that, by default, suppresses the listing of substitution symbol
expansion, and it shows the .sslist directive assembled, instructing the assembler to list
substitution symbol code expansion.
1 SHIFT .macro dst,amount
2 .loop amount
3 RLA dst
4 .endloop
5 .endm
6
7 .global value
8
9 0000 SHIFT R5,3
1 .loop 3
1 RLA dst
1 .endloop
2 0000 5505 RLA R5
2 0002 5505 RLA R5
2 0004 5505 RLA R5
10 0006 5582 ADD R5,&value
0008 0000!
11
12 .sslist
13
14 000a SHIFT R5,3
1 .loop amount
# .loop 3
1 RLA dst
1 .endloop
2 000a 5505 RLA dst
# RLA R5
2 000c 5505 RLA dst
# RLA R5
2 000e 5505 RLA dst
# RLA R5
The following examples show various uses of the .struct, .tag, and .endstruct directives.
Example 1 1 REAL_REC .struct ;stag
2 0000 NOM .int ;member1 = 0
3 0002 DEN .int ;member2 = 2
4 0004 REAL_LEN .endstruct ;real_len = 4
5
6 0000 .bss REAL, REAL_LEN
7
8 0000 .text
9 0000 521B ADD.W &REAL + REAL_REC.DEN,R11
0002 0002!
10
Listing file:
1 ; default tab size
2 0000 4303 NOP
3 0002 4303 NOP
4 0004 4303 NOP
5
7 0006 4303 NOP
8 0008 4303 NOP
9 000a 4303 NOP
10
12 000c 4303 NOP
13 000e 4303 NOP
14 0010 4303 NOP
Syntax .text
Description The .text directive tells the assembler to begin assembling into the .text section, which
usually contains executable code. The section program counter is set to 0 if nothing has
yet been assembled into the .text section. If code has already been assembled into the
.text section, the section program counter is restored to its previous value in the section.
The .text section is the default section. Therefore, at the beginning of an assembly, the
assembler assembles code into the .text section unless you use a .data or .sect directive
to specify a different section.
For more information about sections, see Chapter 2.
Example This example assembles code into the .text and .data sections.
1 ********************************************
2 * Begin assembling into the .data section. *
3 ********************************************
4 0000 .data
5 0000 000A .byte 0Ah, 0Bh
0001 000B
6 0002 0011 coeff .word 011h,0x22,0x33
0004 0022
0006 0033
7
8 ********************************************
9 * Begin assembling into the .text section. *
10 ********************************************
11 0000 .text
12 0000 0041 START: .string "A", "B", "C"
0001 0042
0002 0043
13 0003 0058 $END: .string "X", "Y", "Z"
0004 0059
0005 005A
14 0006 403A MOV.W #0x1234,R10
0008 1234
15 000a 521A ADD.W &coeff+1,R10
000c 0003!
16
17 ********************************************
18 * Resume assembling into .data section. *
19 ********************************************
20 0008 .data
21 0008 000C .byte 0Ch, 0Dh
0009 000D
22
23 ********************************************
24 * Resume assembling into .text section. *
25 ********************************************
26 000e .text
27 000e 0051 .string "QUIT"
000f 0055
0010 0049
0011 0054
The assembler prints the title on the page that follows the directive and on subsequent
pages until another .title directive is processed. If you want a title on the first page, the
first source statement must contain a .title directive.
Example In this example, one title is printed on the first page and a different title is printed on
succeeding pages.
Source file:
.title "**** Fast Fourier Transforms ****"
; .
; .
; .
.title "**** Floating-Point Routines ****"
.page
Listing file:
MSP430 COFF Assembler PC vx.x.x Mon Jul 26 12:43:54 2004
2 ; .
3 ; .
4 ; .
MSP430 COFF Assembler PC vx.x.x Mon Jul 26 12:43:54 2004
array
100 bytes
100 bytes
Macro Description
The MSP430™ assembler supports a macro language that enables you to create your own instructions.
This is especially useful when a program executes a particular task several times. The macro language
lets you:
• Define your own macros and redefine existing macros
• Simplify long or complicated assembly code
• Access macro libraries created with the archiver
• Define conditional and repeatable blocks within a macro
• Manipulate strings within a macro
• Control expansion listing
You can define a macro anywhere in your program, but you must define the macro before you can use it.
Macros can be defined at the beginning of a source file or in a .copy/.include file (see Copy Source File );
they can also be defined in a macro library. For more information about macro libraries, see Section 5.4.
Macro definitions can be nested, and they can call other macros, but all elements of the macro must be
defined in the same file. Nested macros are discussed in Section 5.9.
A macro definition is a series of source statements in the following format:
macname .macro [parameter1] [, ... ,parametern]
model statements or macro directives
[.mexit]
.endm
macname names the macro. You must place the name in the source statement's label field.
Only the first 128 characters of a macro name are significant. The assembler
places the macro name in the internal opcode table, replacing any instruction or
previous macro definition with the same name.
.macro is the directive that identifies the source statement as the first line of a macro
definition. You must place .macro in the opcode field.
parameter1, are optional substitution symbols that appear as operands for the .macro directive.
parametern Parameters are discussed in Section 5.3.
model statements are instructions or assembler directives that are executed each time the macro is
called.
macro directives are used to control macro expansion.
.mexit is a directive that functions as a goto .endm. The .mexit directive is useful when
error testing confirms that macro expansion fails and completing the rest of the
macro is unnecessary.
.endm is the directive that terminates the macro definition.
If you want to include comments with your macro definition but do not want those comments to appear in
the macro expansion, use an exclamation point to precede your comments. If you do want your comments
to appear in the macro expansion, use an asterisk or semicolon. See Section 5.7 for more information
about macro comments.
Macro definition: The following code defines a macro, sadd4, with four parameters:
1 add3 .macro P1,P2,P3,SUM
2 MOV #0,SUM
3 ADD P1,SUM
4 ADD P2,SUM
5 ADD P3,SUM
6 .endm
Macro call: The following code calls the sadd4 macro with four arguments:
7
8 0000 add3 R11,R12,R13,R14
Macro expansion: The following code shows the substitution of the macro definition for the macro call.
The assembler substitutes A0, A1, A2, and A3 for the r1, r2, r3, and r4 parameters of sadd4.
1 0000 430E MOV #0,R14
1 0002 5B0E ADD R11,R14
1 0004 5C0E ADD R12,R14
1 0006 5D0E ADD R13,R14
If you want to call a macro several times with different data each time, you can assign parameters within
the macro. The macro language supports a special symbol, called a substitution symbol, which is used for
macro parameters.
Macro parameters are substitution symbols that represent a character string. These symbols can also be
used outside of macros to equate a character string to a symbol name (see Section 3.8.6).
Valid substitution symbols can be up to 128 characters long and must begin with a letter. The remainder
of the symbol can be a combination of alphanumeric characters, underscores, and dollar signs.
Substitution symbols used as macro parameters are local to the macro they are defined in. You can define
up to 32 local substitution symbols (including substitution symbols defined with the .var directive) per
macro. For more information about the .var directive, see Section 5.3.6.
During macro expansion, the assembler passes arguments by variable to the macro parameters. The
character-string equivalent of each argument is assigned to the corresponding parameter. Parameters
without corresponding arguments are set to the null string. If the number of arguments exceeds the
number of parameters, the last parameter is assigned the character-string equivalent of all remaining
arguments.
If you pass a list of arguments to one parameter or if you pass a comma or semicolon to a parameter, you
must surround these terms with quotation marks .
At assembly time, the assembler replaces the macro parameter/substitution symbol with its corresponding
character string, then translates the source code into object code.
Example 5-2 shows the expansion of a macro with varying numbers of arguments.
Example 5-2. Calling a Macro With Varying Numbers of Arguments
Macro definition:
Parms .macro a,b,c
; a = :a:
; b = :b:
; c = :c:
.endm
Parms """string""",x,y
; a = "string"
; b = x
; c = y
.asg 1,counter
.loop 100
.word counter
.eval counter + 1,counter
.endloop
In Example 5-4, the .asg directive could be replaced with the .eval directive (.eval 1, counter) without
changing the output. In simple cases like this, you can use .eval and .asg interchangeably. However, you
must use .eval if you want to calculate a value from an expression. While .asg only assigns a character
string to a substitution symbol, .eval evaluates an expression and then assigns the character string
equivalent to a substitution symbol.
See Assign a Substitution Symbol for more information about the .asg and .eval assembler directives.
(1)
For more information about predefined register names, see Section 3.8.5.
The assembler expands substitution symbols surrounded by colons before expanding other substitution
symbols.
You can use the forced substitution operator only inside macros, and you cannot nest a forced substitution
operator within another forced substitution operator.
Example 5-7 shows how the forced substitution operator is used.
1 force .macro
2 .asg 0,x
3 .loop 8
4 AUX:x: .set x
5 .eval x+1,x
6 .endloop
7 .endm
8
9 0000 force
1 .asg 0,x
1 .loop 8
1 AUX:x: .set x
1 .eval x+1,x
1 .endloop
2 0000 AUX0 .set 0
2 .eval 0+1,x
2 0001 AUX1 .set 1
2 .eval 1+1,x
2 0002 AUX2 .set 2
2 .eval 2+1,x
2 0003 AUX3 .set 3
2 .eval 3+1,x
2 0004 AUX4 .set 4
2 .eval 4+1,x
2 0005 AUX5 .set 5
2 .eval 5+1,x
2 0006 AUX6 .set 6
2 .eval 6+1,x
2 0007 AUX7 .set 7
2 .eval 7+1,x
.asg 0,pos
.asg "ar1 ar2 ar3 ar4",regs
substr 1,"ar2",regs,pos
.word pos
You can access the macro library by using the .mlib assembler directive (described in Define Macro
Library ). The syntax is:
.mlib filename
When the assembler encounters the .mlib directive, it opens the library named by filename and creates a
table of the library's contents. The assembler enters the names of the individual members within the library
into the opcode tables as library entries; this redefines any existing opcodes or macros that have the same
name. If one of these macros is called, the assembler extracts the entry from the library and loads it into
the macro table.
The assembler expands the library entry in the same way it expands other macros. See Section 5.1 for
how the assembler expands macros. You can control the listing of library entry expansions with the .mlist
directive. For more information about the .mlist directive, see Section 5.8 and Start/Stop Macro Expansion
Listing . Only macros that are actually called from the library are extracted, and they are extracted only
once.
You can use the archiver to create a macro library by including the desired files in an archive. A macro
library is no different from any other archive, except that the assembler expects the macro library to
contain macro definitions. The assembler expects only macro definitions in a macro library; putting object
code or miscellaneous source files into the library may produce undesirable results. For information about
creating a macro library archive, see Chapter 6.
The .elseif and .else directives are optional in conditional assembly. The .elseif directive can be used
more than once within a conditional assembly code block. When .elseif and .else are omitted and when
the .if expression is false (0), the assembler continues to the code following the .endif directive. See
Assemble Conditional Blocks for more information on the .if/ .elseif/.else/.endif directives.
The .loop/.break/.endloop directives enable you to assemble a code block repeatedly. The format of a
repeatable block is:
The .loop directive's optional well-defined expression evaluates to the loop count (the number of loops to
be performed). If the expression is omitted, the loop count defaults to 1024 unless the assembler
encounters a .break directive with an expression that is true (nonzero). See Assemble Conditional Blocks
Repeatedly for more information on the .loop/.break/.endloop directives.
The .break directive and its expression are optional in repetitive assembly. If the expression evaluates to
false, the loop continues. The assembler breaks the loop when the .break expression evaluates to true or
when the .break expression is omitted. When the loop is broken, the assembler continues with the code
after the .endloop directive.
For more information, see Section 4.7.
Example 5-10, Example 5-11, and Example 5-12 show the .loop/.break/ .endloop directives, properly
nested conditional assembly directives, and built-in substitution symbol functions used in a conditional
assembly code block.
Example 5-10. The .loop/.break/.endloop Directives
.asg 1,x
.loop
.eval x+1,x
.endloop
.asg 1,x
.loop
.eval x+1,x
.endloop
Example 5-12. Built-In Substitution Symbol Functions in a Conditional Assembly Code Block
.fcnolist
* Increment or decrement
* Macro Call
INCDEC +,R11
Example 5-13 shows unique label generation in a macro. The maximum label length is shortened to allow
for the unique suffix. For example, if the macro is expanded fewer than 10 times, the maximum label
length is 126 characters. If the macro is expanded from 10 to 99 times, the maximum label length is 125.
The label with its unique suffix is shown in the cross-listing file. To obtain a cross-listing file, invoke the
assembler with the --cross_reference option (see Section 3.3).
Macro comments are comments that appear in the definition of the macro but do not show up in the
expansion of the macro. An exclamation point in column 1 identifies a macro comment. If you want your
comments to appear in the macro expansion, precede your comment with an asterisk or semicolon.
Example 5-14 shows user messages in macros and macro comments that do not appear in the macro
expansion.
For more information about the .emsg, .mmsg, and .wmsg assembler directives, see Define Messages .
The macro language supports recursive and nested macro calls. This means that you can call other
macros in a macro definition. You can nest macros up to 32 levels deep. When you use recursive macros,
you call a macro from its own definition (the macro calls itself).
When you create recursive or nested macros, you should pay close attention to the arguments that you
pass to macro parameters because the assembler uses dynamic scoping for parameters. This means that
the called macro uses the environment of the macro from which it was called.
Example 5-15 shows nested macros. The y in the in_block macro hides the y in the out_block macro. The
x and z from the out_block macro, however, are accessible to the in_block macro.
Example 5-15. Using Nested Macros
Example 5-16 shows recursive and fact macros. The fact macro produces assembly code necessary to
calculate the factorial of n, where n is an immediate value. The result is placed in data memory address
loc. The fact macro accomplishes this by calling fact1, which calls itself recursively.
fact1 .macro
.if N > 1
MOV #N,R12 ; Assume MPY requires args to be in R12,R13
MOV &loc,R13
CALL MPY
MOV R12,&loc ; Assume MPY returns product in R12
.eval N - 1,N
fact1
.endif
.endm
.global fact_result
.global MPY
fact 5,fact_result
Archiver Description
The MSP430™ archiver lets you combine several individual files into a single archive file. For example,
you can collect several macros into a macro library. The assembler searches the library and uses the
members that are called as macros by the source file. You can also use the archiver to collect a group of
object files into an object library. The linker includes in the library the members that resolve external
references during the link. The archiver allows you to modify a library by deleting, replacing, extracting, or
adding members.
C/C++
source
files
Macro
source C/C++
files compiler
C/C++ name
Assembler
Archiver demangling
source
utility
Macro
library Assembler
Library-build Debugging
Object
Archiver process tools
files
Run-time-
Library of support
object library
files Linker
Executable
object file
Hex-conversion
utility
ar430 -t function
The archiver makes a copy of push.asm and places it in the current directory; it does not remove
push.asm from the library. Now you can edit the extracted file. To replace the copy of push.asm in the
library with the edited copy, enter:
ar430 -r macros push.asm
• If you want to use a command file, specify the command filename after the -@ command. For
example:
ar430 -@modules.cmd
Linker Description
The MSP430™ linker creates executable modules by combining object modules. This chapter describes
the linker options, directives, and statements used to create executable modules. Object libraries,
command files, and other key concepts are discussed as well.
The concept of sections is basic to linker operation; Chapter 2 discusses the object module sections in
detail.
C/C++
source
files
Macro
source C/C++
files compiler
C/C++ name
Assembler
Archiver demangling
source
utility
Macro
library Assembler
Library-build Debugging
Object
Archiver process tools
files
Run-time-
Library of support
object library
files Linker
Executable
object file
Hex-conversion
utility
cl430 --run_linker is the command that invokes the linker. The --run_linker option's short form is
-z.
options can appear anywhere on the command line or in a link command file. (Options
are discussed in Section 7.4.)
filename1, filenamen can be object files, link command files, or archive libraries. The default
extension for all input files is .obj; any other extension must be explicitly
specified. The linker can determine whether the input file is an object or ASCII
file that contains linker commands. The default output filename is a.out, unless
you use the --output_file option to name the output file.
When you use a command file, you can also specify other options and files on the command line. For
example, you could enter:
The linker reads and processes a command file as soon as it encounters the filename on the
command line, so it links the files in this order: file1.obj, file2.obj, and file3.obj. This example creates an
output file called link.out and a map file called link.map.
For information on invoking the linker for C/C++ files, see Section 7.17.
SECTIONS
{
.fast_code: { *.obj(*fast*) } > FAST_MEM
.vectors : { vectors.obj(.vector:part1:*) > 0xFFFFFF00
.str_code : { rts*.lib<str*.obj>(.text) } > S1ROM
}
The output file a.out can be relinked with other object files or relocated at load time. (Linking a file that will
be relinked with other files is called partial linking. For more information, see Section 7.16.)
7.4.3 Allocate Memory for Use by the Loader to Pass Arguments (--arg_size Option)
The --arg_size option instructs the linker to allocate memory to be used by the loader to pass arguments
from the command line of the loader to the program. The syntax of the --arg_size option is:
--arg_size= size
The size is a number representing the number of bytes to be allocated in target memory for command-line
arguments.
By default, the linker creates the __c_args__ symbol and sets it to -1. When you specify --arg_size=size,
the following occur:
• The linker creates an uninitialized section named .args of size bytes.
• The __c_args__ symbol contains the address of the .args section.
The loader and the target boot code use the .args section and the __c_args__ symbol to determine
whether and how to pass arguments from the host to the target program. See the MSP430 Optimizing
C/C++ Compiler User's Guide for information about the loader.
--display_error_number Displays a diagnostic's numeric identifier along with its text. Use this option in
determining which arguments you need to supply to the diagnostic
suppression options (--diag_suppress, --diag_error, --diag_remark, and
--diag_warning). This option also indicates whether a diagnostic is
discretionary. A discretionary diagnostic is one whose severity can be
overridden. A discretionary diagnostic includes the suffix -D; otherwise, no
suffix is present. See the MSP430 Optimizing C/C++ Compiler User's Guide
for more information on understanding diagnostic messages.
--issue_remarks Issues remarks (nonserious warnings), which are suppressed by default.
--no_warnings Suppresses warning diagnostics (errors are still issued).
--set_error_limit=num Sets the error limit to num, which can be any decimal value. The linker
abandons linking after this number of errors. (The default is 100.)
--verbose_diagnostics Provides verbose diagnostics that display the original source with line-wrap
and indicate the position of the error in the source line
7.4.8 Link Command File Preprocessing (--disable_pp, --define and --undefine Options)
The linker preprocesses link command files using a standard C preprocessor. Therefore, the command
files can contain well-known preprocessing directives such as #define, #include, and #if / #endif.
Three linker options control the preprocessor:
--disable_pp Disables preprocessing for command files
--define=name[=val] Predefines name as a preprocessor macro
--undefine=name Removes the macro name
The compiler has --define and --undefine options with the same meanings. However, the linker options are
distinct; only --define and --undefine options specified after --run_linker are passed to the linker. For
example:
cl430 --define=FOO=1 main.c --run_linker --define=BAR=2 lnk.cmd
The linker sees only the --define for BAR; the compiler only sees the --define for FOO.
When one command file #includes another, preprocessing context is carried from parent to child in the
usual way (that is, macros defined in the parent are visible in the child). However, when a command file is
invoked other than through #include, either on the command line or by the typical way of being named in
another command file, preprocessing context is not carried into the nested file. The exception to this is
--define and --undefine options, which apply globally from the point they are encountered. For example:
--define GLOBAL
#define LOCAL
Two cautions apply to the use of --define and --undefine in command files. First, they have global effect as
mentioned above. Second, since they are not actually preprocessing directives themselves, they are
subject to macro substitution, probably with unintended consequences. This effect can be defeated by
quoting the symbol name. For example:
--define MYSYM=123
--undefine MYSYM /* expands to --undefine 123 (!) */
--undefine "MYSYM" /* ahh, that's better */
The linker uses the same search paths to find #include files as it does to find libraries. That is, #include
files are searched in the following places:
1. If the #include file name is in quotes (rather than <brackets>), in the directory of the current file
2. In the list of directories specified with --Iibrary options or environment variables (see Section 7.4.14)
There are two exceptions: relative pathnames (such as "../name") always search the current directory; and
absolute pathnames (such as "/usr/tools/name") bypass search paths entirely.
The linker has the standard built-in definitions for the macros __FILE__, __DATE__, and __TIME__. It
does not, however, have the compiler-specific options for the target (____MSP430____), version
(__TI_COMPILER_VERSION__), run-time model, and so on.
The linker creates the .sysmem section only if there is a .sysmem section in an input file.
The linker also creates a global symbol __SYSMEM_SIZE and assigns it a value equal to the size of the
heap. The default size is 128 bytes.
For more information about C/C++ linking, see Section 7.17.
7.4.14 Alter the Library Search Algorithm (--library Option, --search_path Option, and
MSP430_C_DIR Environment Variable)
Usually, when you want to specify a file as linker input, you simply enter the filename; the linker looks for
the file in the current directory. For example, suppose the current directory contains the library object.lib.
Assume that this library defines symbols that are referenced in the file file1.obj. This is how you link the
files:
cl430 --run_linker file1.obj object.lib
If you want to use a file that is not in the current directory, use the --library linker option. The --library
option's short form is -l. The syntax for this option is:
--library=[pathname] filename
The filename is the name of an archive, an object file, or link command file. You can specify up to 128
search paths.
The --library option is not required when one or more members of an object library are specified for input
to an output section. For more information about allocating archive members, see Section 7.8.7.
You can augment the linker's directory search algorithm by using the --search_path linker option or the
MSP430_C_DIR environment variable. The linker searches for object libraries and command files in the
following order:
1. It searches directories named with the --search_path linker option. The --search_path option must
appear before the --Iibrary option on the command line or in a command file.
2. It searches directories named with MSP430_C_DIR.
3. If MSP430_C_DIR is not set, it searches directories named with the assembler's MSP430_A_DIR
environment variable.
4. It searches the current directory.
The pathnames are directories that contain input files. Use the --library linker option on the command line
or in a command file to tell the linker which library or link command file to search for. The pathnames must
follow these constraints:
• Pathnames must be separated with a semicolon.
• Spaces or tabs at the beginning or end of a path are ignored. For example the space before and after
the semicolon in the following is ignored:
set MSP430_C_DIR= c:\path\one\to\tools ; c:\path\two\to\tools
• Spaces and tabs are allowed within paths to accommodate Windows directories that contain spaces.
For example, the pathnames in the following are valid:
set MSP430_C_DIR=c:\first path\to\tools;d:\second path\to\tools
The environment variable remains set until you reboot the system or reset the variable by entering:
Operating System Enter
UNIX (Bourne shell) unset MSP430_C_DIR
Windows set MSP430_C_DIR=
The assembler uses an environment variable named MSP430_A_DIR to name alternate directories that
contain copy/include files or macro libraries. If MSP430_C_DIR is not set, the linker searches for object
libraries in the directories named with MSP430_A_DIR. For information about MSP430_A_DIR, see
Section 3.4.2. For more information about object libraries, see Section 7.6.
The map file contains the name of the output module and the entry point; it can also contain up to three
tables:
• A table showing the new memory configuration if any nondefault memory is specified (memory
configuration). The table has the following columns; this information is generated on the basis of the
information in the MEMORY directive in the link command file:
– Name. This is the name of the memory range specified with the MEMORY directive.
– Origin. This specifies the starting address of a memory range.
– Length. This specifies the length of a memory range.
– Unused. This specifies the total amount of unused (available) memory in that memory area.
– Attributes. This specifies one to four attributes associated with the named range:
R specifies that the memory can be read.
W specifies that the memory can be written to.
X specifies that the memory can contain executable code.
I specifies that the memory can be initialized.
For more information about the MEMORY directive, see Section 7.7.
• A table showing the linked addresses of each output section and the input sections that make up the
output sections (section allocation map). This table has the following columns; this information is
generated on the basis of the information in the SECTIONS directive in the link command file:
– Output section. This is the name of the output section specified with the SECTIONS directive.
– Origin. The first origin listed for each output section is the starting address of that output section.
The indented origin value is the starting address of that portion of the output section.
– Length. The first length listed for each output section is the length of that output section. The
indented length value is the length of that portion of the output section.
– Attributes/input sections. This lists the input file or value associated with an output section. If the
input section could not be allocated, the map file will indicate this with "FAILED TO ALLOCATE".
For more information about the SECTIONS directive, see Section 7.8.
• A table showing each external symbol and its address sorted by symbol name.
• A table showing each external symbol and its address sorted by symbol address.
This following example links file1.obj and file2.obj and creates a map file called map.out:
cl430 --run_linker file1.obj file2.obj --map_file=map.out
The --mapfile_contents option controls display filter settings by specifying a comma-delimited list of display
attributes. When prefixed with the word no, an attribute is disabled instead of enabled. For example:
--mapfile_contents=copytables,noentry
--mapfile_contents=all,nocopytables
--mapfile_contents=none,entry
By default, those sections that are currently included in the map file when the --map_file option is specified
are included. The filters specified in the --mapfile_contents options are processed in the order that they
appear in the command line. In the third example above, the first filter, none, clears all map file content.
The second filter, entry, then enables information about entry points to be included in the generated map
file. That is, when --mapfile_contents=none,entry is specified, the map file contains only information about
entry points.
There are two new filters included with the --mapfile_contents option, load_addr and sym_defs. These are
both disabled by default. If you turn on the load_addr filter, the map file includes the load address of
symbols that are included in the symbol list in addition to the run address (if the load address is different
from the run address).
The sym_defs filter can be used to include information about all static and global symbols defined in an
application on a file by file basis. You may find it useful to replace the sym_name and sym_runaddr
sections of the map file with the sym_defs section by specifying the following --mapfile_contents option:
--mapfile_contents=nosym_name,nosym_runaddr,sym_defs
The --no_demangle option disables the demangling of symbol names in diagnostics. For example:
undefined symbol first referenced in file
_ZN9ANewClass8getValueEv test.obj
-[ f1.c ]-
#include "header.h"
...
-[ f2.c ]-
#include "header.h"
...
When these files are compiled for debugging, both f1.obj and f2.obj have symbolic debugging entries to
describe type XYZ. For the final output file, only one set of these entries is necessary. The linker
eliminates the duplicate entries automatically.
Use the COFF only --no_sym_merge option if you want the linker to keep such duplicate entries in COFF
object files. Using the --no_sym_merge option has the effect of the linker running faster and using less
machine memory.
7.4.24 Exhaustively Read and Search Libraries (--reread_libs and --priority Options)
There are two ways to exhaustively search for unresolved symbols:
• Reread libraries if you cannot resolve a symbol reference (--reread_libs).
• Search libraries in the order that they are specified (--priority).
The linker normally reads input files, including archive libraries, only once when they are encountered on
the command line or in the command file. When an archive is read, any members that resolve references
to undefined symbols are included in the link. If an input file later references a symbol defined in a
previously read archive library, the reference is not resolved.
With the --reread_libs option, you can force the linker to reread all libraries. The linker rereads libraries
until no more references can be resolved. Linking using --reread_libs may be slower, so you should use it
only as needed. For example, if a.lib contains a reference to a symbol defined in b.lib, and b.lib contains a
reference to a symbol defined in a.lib, you can resolve the mutual dependencies by listing one of the
libraries twice, as in:
cl430 --run_linker --library=a.lib --library=b.lib --library=a.lib
The --priority option provides an alternate search mechanism for libraries. Using --priority causes each
unresolved reference to be satisfied by the first library that contains a definition for that symbol. For
example:
objfile references A
lib1 defines B
lib2 defines A, B; obj defining A references B
Under the existing model, objfile resolves its reference to A in lib2, pulling in a reference to B, which
resolves to the B in lib2.
Under --priority, objfile resolves its reference to A in lib2, pulling in a reference to B, but now B is resolved
by searching the libraries in order and resolves B to the first definition it finds, namely the one in lib1.
The --priority option is useful for libraries that provide overriding definitions for related sets of functions in
other libraries without having to provide a complete version of the whole library.
For example, suppose you want to override versions of malloc and free defined in the rts430.lib without
providing a full replacement for rts430.lib. Using --priority and linking your new library before rts430.lib
guarantees that all references to malloc and free resolve to the new library.
The --priority option is intended to support linking programs with DSP/BIOS where situations like the one
illustrated above occur.
If you specified a different stack size in an input section, the input section stack size is ignored. Any
symbols defined in the input section remain valid; only the stack size is different.
When the linker defines the .stack section, it also defines a global symbol, __STACK_SIZE, and assigns it
a value equal to the size of the section. The default software stack size is 128 bytes.
Linker command files allow you to put linking information in a file; this is useful when you invoke the linker
often with the same information. Linker command files are also useful because they allow you to use the
MEMORY and SECTIONS directives to customize your application. You must use these directives in a
command file; you cannot use them on the command line.
Linker command files are ASCII files that contain one or more of the following:
• Input filenames, which specify object files, archive libraries, or other command files. (If a command file
calls another command file as input, this statement must be the last statement in the calling command
file. The linker does not return from called command files.)
• Linker options, which can be used in the command file in the same manner that they are used on the
command line
• The MEMORY and SECTIONS linker directives. The MEMORY directive defines the target memory
configuration (see Section 7.7). The SECTIONS directive controls how sections are built and allocated
(see Section 7.8.)
• Assignment statements, which define and assign values to global symbols
To invoke the linker with a command file, enter the cl430 --run_linker command and follow it with the
name of the command file:
cl430 --run_linker command_filename
The linker processes input files in the order that it encounters them. If the linker recognizes a file as an
object file, it links the file. Otherwise, it assumes that a file is a command file and begins reading and
processing commands from it. Command filenames are case sensitive, regardless of the system used.
Example 7-1 shows a sample link command file called link.cmd.
Example 7-1. Linker Command File
The sample file in Example 7-1 contains only filenames and options. (You can place comments in a
command file by delimiting them with /* and */.) To invoke the linker with this command file, enter:
You can place other parameters on the command line when you use a command file:
The linker processes the command file as soon as it encounters the filename, so a.obj and b.obj are
linked into the output module before c.obj and d.obj.
You can specify multiple command files. If, for example, you have a file called names.lst that contains
filenames and another file called dir.cmd that contains linker directives, you could enter:
One command file can call another command file; this type of nesting is limited to 16 levels. If a command
file calls another command file as input, this statement must be the last statement in the calling command
file.
For more information, see Section 7.7 for the MEMORY directive, and Section 7.8 for the SECTIONS
directive.
then:
• Member 1 of liba.lib satisfies the f1.obj and f2.obj references to clrscr because the library is searched
and the definition of clrscr is found.
• Member 0 of libc.lib satisfies the reference to origin.
• Member 3 of liba.lib satisfies the reference to fillclr.
If, however, you enter:
If any member of libc.lib defines rout1, the linker includes that member.
Library members are allocated according to the SECTIONS directive default allocation algorithm; see
Section 7.8.
Section 7.4.14 describes methods for specifying directories that contain object libraries.
/*******************************************************/
/* Sample command file with MEMORY directive */
/*******************************************************/
file1.obj file2.obj /* Input files */
--output_file=prog.out /* Options */
MEMORY
{
FLASH (RX): origin = 0x1100 length = 0xEEE0
RAM (RX): origin = 0x0200 length = 0x0800
}
name names a memory range. A memory name can be one to 64 characters; valid characters
include A-Z, a-z, $, ., and _. The names have no special significance to the linker; they
simply identify memory ranges. Memory range names are internal to the linker and are not
retained in the output file or in the symbol table. All memory ranges must have unique
names and must not overlap.
attr specifies one to four attributes associated with the named range. Attributes are optional;
when used, they must be enclosed in parentheses. Attributes restrict the allocation of
output sections into certain memory ranges. If you do not use any attributes, you can
allocate any output section into any range with no restrictions. Any memory for which no
attributes are specified (including all memory in the default model) has all four attributes.
Valid attributes are:
R specifies that the memory can be read.
W specifies that the memory can be written to.
X specifies that the memory can contain executable code.
I specifies that the memory can be initialized.
origin specifies the starting address of a memory range; enter as origin, org, or o. The value,
specified in bytes, is a 16-bit constant and can be decimal, octal, or hexadecimal.
length specifies the length of a memory range; enter as length, len, or l. The value, specified in
bytes, is a 32-bit constant and can be decimal, octal, or hexadecimal.
fill specifies a fill character for the memory range; enter as fill or f. Fills are optional. The value
is a integer constant and can be decimal, octal, or hexadecimal. The fill value is used to fill
areas of the memory range that are not allocated to a section.
The following example specifies a memory range with the R and W attributes and a fill constant of
0FFFFh:
MEMORY
{
RFILE (RW) : o = 0x0020h, l = 0x1000, f = 0x0FFFF
}
You normally use the MEMORY directive in conjunction with the SECTIONS directive to control allocation
of output sections. After you use MEMORY to specify the target system's memory model, you can use
SECTIONS to allocate output sections into specific named memory ranges or into memory that has
specific attributes. For example, you could allocate the .text and .data sections into the area named
FLASH and allocate the .bss section into the area named RAM.
Each section specification, beginning with name, defines an output section. (An output section is a section
in the output file.) A section name can be a subsection specification. (See Section 7.8.4 for information on
multi-level subsections.) After the section name is a list of properties that define the section's contents and
how the section is allocated. The properties can be separated by optional commas. Possible properties for
a section are as follows:
type = NOLOAD
See Section 7.11.
• Fill value defines the value used to fill uninitialized holes.
Syntax: fill = value or
name : [properties =
value]
See Section 7.14.
/*******************************************************/
/* Sample command file with SECTIONS directive */
/*******************************************************/
file1.obj file2.obj /* Input files */
--output_file=progr.out /* Options */
SECTIONS
{
.bss : load = RAM
Figure 7-2 shows the six output sections defined by the SECTIONS directive in Example 7-4 (.vectors,
.text, .const, .bss, .data:alpha, and .data:beta) and shows how these sections are allocated in memory.
0x0200
RAM
.bss – Allocated in The .bss section combines the .bss sections from
RAM file1.obj and file2.obj.
0x1110
FLASH
.text – Allocated in The .text section combines the .text sections from
FLASH file1.obj and file2.obj. The link step combines all
sections named .text into this section.
0xFFE0
.vectors – Bound at The .vectors section is composed of the .intvec1
0xFFE0 section from t1.obj and the .intvec2 section from
t2.obj.
0xFFFF
7.8.2 Allocation
The linker assigns each output section two locations in target memory: the location where the section will
be loaded and the location where it will be run. Usually, these are the same, and you can think of each
section as having only a single address. The process of locating the output section in the target's memory
and assigning its address(es) is called allocation. For more information about using separate load and run
allocation, see Section 7.9.
If you do not tell the linker how a section is to be allocated, it uses a default algorithm to allocate the
section. Generally, the linker puts sections wherever they fit into configured memory. You can override this
default allocation for a section by defining it within a SECTIONS directive and providing instructions on
how to allocate it.
You control allocation by specifying one or more allocation parameters. Each parameter consists of a
keyword, an optional equal sign or greater-than sign, and a value optionally enclosed in parentheses. If
load and run allocation are separate, all parameters following the keyword LOAD apply to load allocation,
and those following the keyword RUN apply to run allocation. The allocation parameters are:
Binding allocates a section at a specific address.
.text: load = 0x1000
Named memory allocates the section into a range defined in the MEMORY directive with the specified
name (like SLOW_MEM) or attributes.
.text: load > SLOW_MEM
Alignment uses the align or palign keyword to specify that the section must start on an address
boundary.
.text: align = 0x100
Blocking uses the block keyword to specify that the section must fit between two address
boundaries: if the section is too big, it starts on an address boundary.
.text: block(0x100)
For the load (usually the only) allocation, you can simply use a greater-than sign and omit the load
keyword:
text: > SLOW_MEM .text: {...} > SLOW_MEM
.text: > 0x4000
If more than one parameter is used, you can string them together as follows:
.text: > SLOW_MEM align 16
You can also use an input section specification to identify the sections from input files that are combined
to form an output section. See Section 7.8.3.
7.8.2.1 Binding
You can supply a specific starting address for an output section by following the section name with an
address:
.text: 0x00001000
This example specifies that the .text section must begin at location 0x1000. The binding address must be
a 16-bit constant.
Output sections can be bound anywhere in configured memory (assuming there is enough space), but
they cannot overlap. If there is not enough space to bind a section to a specified address, the linker issues
an error message.
SECTIONS
{
.text : > SLOW_MEM
.data : > FAST_MEM ALIGN(128)
.bss : > FAST_MEM
}
In this example, the linker places .text into the area called SLOW_MEM. The .data and .bss output
sections are allocated into FAST_MEM. You can align a section within a named memory range; the .data
section is aligned on a 128-byte boundary within the FAST_MEM range.
In this example, the .text output section can be linked into either the SLOW_MEM or FAST_MEM area
because both areas have the X attribute. The .data section can also go into either SLOW_MEM or
FAST_MEM because both areas have the R and I attributes. The .bss output section, however, must go
into the FAST_MEM area because only FAST_MEM is declared with the W attribute.
You cannot control where in a named memory range a section is allocated, although the linker uses lower
memory addresses first and avoids fragmentation when possible. In the preceding examples, assuming no
conflicting assignments exist, the .text section starts at address 0. If a section must start on a specific
address, use binding instead of named memory.
The HIGH specifier used on the .stack section allocation causes the linker to attempt to allocate .stack into
the higher addresses within the RAM memory range. The .bss and .sysmem sections are allocated into
the lower addresses within RAM. Example 7-5 illustrates a portion of a map file that shows where the
given sections are allocated within RAM for a typical program.
Example 7-5. Linker Allocation With the HIGH Specifier
As shown in Example 7-5 , the .bss and .sysmem sections are allocated at the lower addresses of RAM
(0x0200 - 0x0590) and the .stack section is allocated at address 0x08c0, even though lower addresses
are available.
Without using the HIGH specifier, the linker allocation would have resulted in the code shown in Example
7-6
The HIGH specifier is ignored if it is used with specific address binding or automatic section splitting (>>
operator).
Example 7-6. Linker Allocation Without HIGH Specifier
You can specify the same alignment with the palign keyword. In addition, palign ensures the section's size
is a multiple of its placement alignment restrictions, padding the section size up to such a boundary, as
needed.
Blocking is a weaker form of alignment that allocates a section anywhere within a block of size n. The
specified block size must be a power of 2. For example, the following code allocates .bss so that the entire
section is contained in a single 128-byte page or begins on that boundary.:
bss: load = block(0x0080)
You can use alignment or blocking alone or in conjunction with a memory area, but alignment and
blocking cannot be used together.
If the linker adds padding to an initialized output section then the padding space is also initialiized. By
default, padding space is filled with a value of 0 (zero). However, if a fill value is specified for the output
section then any padding for the section is also filled with that fill value.
In this example, the length of the .mytext section is 6 bytes before the palign operator is applied. The
contents of .mytext are as follows:
addr content
---- -------
0000 0x1234
0002 0x1234
0004 0x1234
After the palign operator is applied, the length of .mytext is 8 bytes, and its contents are as follows:
addr content
---- -------
0000 0x1234
0002 0x1234
0004 0x1234
0006 0xffff
The size of .mytext has been bumped to a multiple of 8 bytes and the padding created by the linker has
been filled with 0xff.
The fill value specified in the linker command file is interpreted as a 16-bit constant, so if you specify this
code:
.mytext: palign(8), fill = 0xff {} > PMEM
The fill value assumed by the linker is 0x00ff, and .mytext will then have the following contents:
addr content
---- -------
0000 0x1234
0002 0x1234
0004 0x1234
0006 0xffff
0008 0x00ff
000a 0x00ff
If the palign operator is applied to an uninitialized section, then the size of the section is bumped to the
appropriate boundary, as needed, but any padding created is not initialized.
SECTIONS
{
.text:
.data:
.bss:
}
In Example 7-7, the linker takes all the .text sections from the input files and combines them into the .text
output section. The linker concatenates the .text input sections in the order that it encounters them in the
input files. The linker performs similar operations with the .data and .bss sections. You can use this type of
specification for any output section.
You can explicitly specify the input sections that form an output section. Each input section is identified by
its filename and section name:
SECTIONS
{
.text : /* Build .text output section */
{
f1.obj(.text) /* Link .text section from f1.obj */
f2.obj(sec1) /* Link sec1 section from f2.obj */
f3.obj /* Link ALL sections from f3.obj */
f4.obj(.text,sec2) /* Link .text and sec2 from f4.obj */
}
}
It is not necessary for input sections to have the same name as each other or as the output section they
become part of. If a file is listed with no sections,all of its sections are included in the output section. If any
additional input sections have the same name as an output section but are not explicitly specified by the
SECTIONS directive, they are automatically linked in at the end of the output section. For example, if the
linker found more .text sections in the preceding example and these .text sections were not specified
anywhere in the SECTIONS directive, the linker would concatenate these extra sections after f4.obj(sec2).
The specifications in Example 7-7 are actually a shorthand method for the following:
SECTIONS
{
.text: { *(.text) }
.data: { *(.data) }
.bss: { *(.bss) }
}
The specification *(.text) means the unallocated .text sections from all the input files. This format is useful
when:
• You want the output section to contain all input sections that have a specified name, but the output
section name is different from the input sections' name.
• You want the linker to allocate the input sections before it processes additional input sections or
commands within the braces.
The following example illustrates the two purposes above:
SECTIONS
{
.text : {
abc.obj(xqt)
*(.text)
}
.data : {
*(.data)
fil.obj(table)
}
}
In this example, the .text output section contains a named section xqt from file abc.obj, which is followed
by all the .text input sections. The .data section contains all the .data input sections, followed by a named
section table from the file fil.obj. This method includes all the unallocated sections. For example, if one of
the .text input sections was already included in another output section when the linker encountered
*(.text), the linker could not include that first .text input section in the second output section.
This SECTIONS specification allocates the input sections as indicated in the comments:
SECTIONS {
nordic: {*(europe:north)
*(europe:central:denmark)} /* the nordic countries */
central: {*(europe:central)} /* france, germany */
therest: {*(europe)} /* spain, italy, malta */
}
This SECTIONS specification allocates the input sections as indicated in the comments:
SECTIONS {
islands: {*(europe:south:malta)
*(europe:north:iceland)} /* malta, iceland */
europe:north:finland : {} /* finland */
europe:north : {} /* norway, sweden */
europe:central : {} /* germany, denmark */
europe:central:france: {} /* france */
SECTIONS
{
.text : { } > P_MEM1 | P_MEM2 | P_MEM4
}
The | operator is used to specify the multiple memory ranges. The .text output section is allocated as a
whole into the first memory range in which it fits. The memory ranges are accessed in the order specified.
In this example, the linker first tries to allocate the section in P_MEM1. If that attempt fails, the linker tries
to place the section into P_MEM2, and so on. If the output section is not successfully allocated in any of
the named memory ranges, the linker issues an error message.
With this type of SECTIONS directive specification, the linker can seamlessly handle an output section
that grows beyond the available space of the memory range in which it is originally allocated. Instead of
modifying the link command file, you can let the linker move the section into one of the other areas.
The linker can split output sections among multiple memory ranges to achieve an efficient allocation. Use
the >> operator to indicate that an output section can be split, if necessary, into the specified memory
ranges. For example:
MEMORY
{
P_MEM1 : origin = 0x2000, length = 0x1000
P_MEM2 : origin = 0x4000, length = 0x1000
P_MEM3 : origin = 0x6000, length = 0x1000
P_MEM4 : origin = 0x8000, length = 0x1000
}
SECTIONS
{
.text: { *(.text) } >> P_MEM1 | P_MEM2 | P_MEM3 | P_MEM4
}
In this example, the >> operator indicates that the .text output section can be split among any of the listed
memory areas. If the .text section grows beyond the available memory in P_MEM1, it is split on an input
section boundary, and the remainder of the output section is allocated to P_MEM2 | P_MEM3 | P_MEM4.
The | operator is used to specify the list of multiple memory ranges.
You can also use the >> operator to indicate that an output section can be split within a single memory
range. This functionality is useful when several output sections must be allocated into the same memory
range, but the restrictions of one output section cause the memory range to be partitioned. Consider the
following example:
MEMORY
{
RAM : origin = 0x1000, length = 0x8000
}
SECTIONS
{
.special: { f1.obj(.text) } = 0x4000
.text: { *(.text) } >> RAM
}
SECTIONS
{
.text: { *(.text) } >> (RW)
}
The linker attempts to allocate all or part of the output section into any memory range whose attributes
match the attributes specified in the SECTIONS directive.
This SECTIONS directive has the same effect as:
SECTIONS
{
.text: { *(.text) } >> P_MEM1 | P_MEM2}
}
Example 7-8 specifies that the text sections of boot.obj, exit.obj, and strcpy.obj from the run-time-support
library should be placed in section .boot. The remainder of the .text sections from the run-time-support
library are to be placed in section .rts. Finally, the remainder of all other .text sections are to be placed in
section .text.
SECTIONS
{
boot > BOOT1
{
--library=rtsXX.lib<boot.obj> (.text)
--library=rtsXX.lib<exit.obj strcpy.obj> (.text)
}
The --library option (which normally implies a library path search be made for the named file following the
option) listed before each library in Example 7-8 is optional when listing specific archive members inside <
>. Using < > implies that you are referring to a library.
To collect a set of the input sections from a library in one place, use the --library option within the
SECTIONS directive. For example, the following collects all the .text sections from rts430.lib into the
.rtstest section:
SECTIONS
{
.rtstest { ---library=rts430.lib(.text) } > RAM
}
At times, you may want to load code into one area of memory and run it in another. For example, you may
have performance-critical code in slow external memory. The code must be loaded into slow external
memory, but it would run faster in fast external memory.
The linker provides a simple way to accomplish this. You can use the SECTIONS directive to direct the
linker to allocate a section twice: once to set its load address and again to set its run address. For
example:
.fir: load = SLOW_MEM, run = FAST_MEM
Use the load keyword for the load address and the run keyword for the run address.
See Section 2.5 for an overview on run-time relocation.
A warning is issued, load is ignored, and space is allocated in FAST_MEM. All of the following examples
have the same effect. The .bss section is allocated in FAST_MEM.
.bss: load = FAST_MEM
.bss: run = FAST_MEM
.bss: > FAST_MEM
.label fir_end
MOV &fir_s,R11
MOV &fir_e,R12
MOV #fir,R13
LOOP: CMP R11,R12
JL Copy_Done
MOV @R11+,0(R13)
INC R13
JMP LOOP
Copy_Done:
JMP fir
/*****************************************************/
/* PARTIAL LINKER COMMAND FILE FOR FIR EXAMPLE */
/*****************************************************/
MEMORY
{
MEM1: origin = 0x2000, length 0x0500
MEM2: origin = 0x3000, length 0x0500
}
SECTIONS
{
.text: load = MEM1
.fir: load = MEM2, run = MEM1
}
0x2000
MEM1
.text
fir (relocated
to run here)
0x2500
0x3000
MEM2
0x3500
0xFFFF
SECTIONS
{
.text: load = SLOW_MEM
UNION: run = FAST_MEM
{
.bss:part1: { file1.obj(.bss) }
.bss:part2: { file2.obj(.bss) }
}
.bss:part3: run = FAST_MEM { globals.obj(.bss) }
}
Allocation of a section as part of a union affects only its run address. Under no circumstances can
sections be overlaid for loading. If an initialized section is a union member (an initialized section, such as
.text, has raw data), its load allocation must be separately specified. See Example 7-12.
Example 7-12. Separate Load Addresses for UNION Sections
Figure 7-4. Memory Allocation Shown in Example 7-11 and Example 7-12
.bss:part3 .bss:part3
SLOW_MEM SLOW_MEM
.text 2 (load)
Since the .text sections contain data, they cannot load as a union, although they can be run as a union.
Therefore, each requires its own load address. If you fail to provide a load allocation for an initialized
section within a UNION, the linker issues a warning and allocates load space anywhere it can in
configured memory.
Uninitialized sections are not loaded and do not require load addresses.
The UNION statement applies only to allocation of run addresses, so it is meaningless to specify a load
address for the union itself. For purposes of allocation, the union is treated as an uninitialized section: any
one allocation specified is considered a run address, and if both run and load addresses are specified, the
linker issues a warning and ignores the load address.
SECTIONS
{
.text /* Normal output section */
.bss /* Normal output section */
GROUP 0x00001000 : /* Specify a group of sections */
{
.data /* First section in the group */
term_rec /* Allocated immediately after .data */
}
}
You can use binding, alignment, or named memory to allocate a GROUP in the same manner as a single
output section. In the preceding example, the GROUP is bound to address 0x1000. This means that .data
is allocated at 0x1000, and term_rec follows it in memory.
SECTIONS
{
GROUP 0x1000 : run = FAST_MEM
{
UNION:
{
mysect1: load = SLOW_MEM
mysect2: load = SLOW_MEM
}
UNION:
{
mysect3: load = SLOW_MEM
mysect4: load = SLOW_MEM
}
}
}
• The run addresses of mysect1/mysect2 and mysect3/mysect4 are allocated contiguously, as directed
by the GROUP statement (subject to alignment and blocking restrictions).
To refer to groups and unions, linker diagnostic messages use the notation:
GROUP_n UNION_n
In this notation, n is a sequential number (beginning at 1) that represents the lexical ordering of the group
or union in the linker control file, without regard to nesting. Groups and unions each have their own
counter.
The name you defined is used in diagnostics for easy identification of the problem LCF area. For example:
warning: LOAD placement ignored for "BSS_SYSMEM_STACK_GROUP": object is uninitialized
UNION(TEXT_CINIT_UNION)
{
.const :{}load=D_MEM, table(table1)
.pinit :{}load=D_MEM, table(table1)
}run=P_MEM
MEMORY
{
MEM : origin = 0x0200, length = 0xFDFD
RESET : origin = 0xFFFE, length = 0x0002
}
SECTIONS
{
.text : {} > MEM
.const : {} > MEM
.data : {} > MEM
.bss : {} > MEM
All .text input sections are concatenated to form a .text output section in the executable output file, and all
.data input sections are combined to form a .data output section.
If you use a SECTIONS directive, the linker performs no part of the default allocation. Allocation is
performed according to the rules specified by the SECTIONS directive and the general algorithm
described next in Section 7.12.1.
If an output section is formed as a result of a SECTIONS directive, this definition completely determines
the section's contents. (See Section 7.8 for examples of how to define an output section's content.)
If an output section is formed by combining input sections not specified by a SECTIONS directive, the
linker combines all such input sections that have the same name into an output section with that name.
For example, suppose the files f1.obj and f2.obj both contain named sections called Vectors and that the
SECTIONS directive does not define an output section for them. The linker combines the two Vectors
sections from the input files into a single output section named Vectors, allocates it into memory, and
includes it in the output file.
By default, the linker does not display a message when it creates an output section that is not defined in
the SECTIONS directive. You can use the --warn_sections linker option (see Section 7.4.32) to cause the
linker to display a message when it creates a new output section.
The symbol should be defined externally. If it is not, the linker defines a new symbol and enters it into the
symbol table. The expression must follow the rules defined in Section 7.13.3. Assignment statements must
terminate with a semicolon.
The linker processes assignment statements after it allocates all the output sections. Therefore, if an
expression contains a symbol, the address used for that symbol reflects the symbol's address in the
executable output file.
For example, suppose a program reads data from one of two tables identified by two external symbols,
Table1 and Table2. The program uses the symbol cur_tab as the address of the current table. The
cur_tab symbol must point to either Table1 or Table2. You could accomplish this in the assembly code,
but you would need to reassemble the program to change tables. Instead, you can use a linker
assignment statement to assign cur_tab at link time:
prog.obj /* Input file */
cur_tab = Table1; /* Assign cur_tab to one of the tables */
This defines Dstart to be the first linked address of the .data section. (Dstart is assigned before .data is
allocated.) The linker relocates all references to Dstart.
A special type of assignment assigns a value to the . symbol. This adjusts the SPC within an output
section and creates a hole between two input sections. Any value assigned to . to create a hole is relative
to the beginning of the section, not to the address actually represented by the . symbol. Holes and
assignments to . are described in Section 7.14.
The following symbols are defined only for C/C++ support when the --ram_model or --rom_model option is
used.
__STACK_SIZE is assigned the size of the .stack section.
__SYSMEM_SIZE is assigned the size of the .sysmem section.
7.13.5 Assigning Exact Start, End, and Size Values of a Section to a Symbol
The code generation tools currently support the ability to load program code in one area of (slow) memory
and run it in another (faster) area. This is done by specifying separate load and run addresses for an
output section or group in the link command file. Then execute a sequence of instructions (the copying
code in Example 7-9) that moves the program code from its load area to its run area before it is needed.
There are several responsibilities that a programmer must take on when setting up a system with this
feature. One of these responsibilities is to determine the size and run-time address of the program code to
be moved. The current mechanisms to do this involve use of the .label directives in the copying code. A
simple example is illustrated Example 7-9.
This method of specifying the size and load address of the program code has limitations. While it works
fine for an individual input section that is contained entirely within one source file, this method becomes
more complicated if the program code is spread over several source files or if the programmer wants to
copy an entire output section from load space to run space.
Another problem with this method is that it does not account for the possibility that the section being
moved may have an associated far call trampoline section that needs to be moved with it.
The new address and dimension operators can be associated with several different kinds of allocation
units, including input items, output sections, GROUPs, and UNIONs. The following sections provide some
examples of how the operators can be used in each case.
This can be rewritten using the START and END operators as follows:
outsect:
{
s1.obj(.text) { END(end_of_s1) }
s2.obj(.text) { START(start_of_s2), END(end_of_s2) }
}
The values of end_of_s1 and end_of_s2 will be the same as if you had used the dot operator in the
original example, but start_of_s2 would be defined after any necessary padding that needs to be added
between the two .text sections. Remember that the dot operator would cause start_of_s2 to be defined
before any necessary padding is inserted between the two input sections.
The syntax for using these operators in association with input sections calls for braces { } to enclose the
operator list. The operators in the list are applied to the input item that occurs immediately before the list.
In this case, the SIZE operator defines size_of_outsect to incorporate any padding that is required in the
output section to conform to any alignment requirements that are imposed.
The syntax for specifying the operators with an output section do not require braces to enclose the
operator list. The operator list is simply included as part of the allocation specification for an output
section.
7.13.7.3 GROUPs
Here is another use of the START and SIZE operators in the context of a GROUP specification:
GROUP
{
outsect1: { ... }
outsect2: { ... }
} load = ROM, run = RAM, START(group_start), SIZE(group_size);
This can be useful if the whole GROUP is to be loaded in one location and run in another. The copying
code can use group_start and group_size as parameters for where to copy from and how much is to be
copied. This makes the use of .label in the source code unnecessary.
7.13.7.4 UNIONs
The RUN_SIZE and LOAD_SIZE operators provide a mechanism to distinguish between the size of a
UNION's load space and the size of the space where its constituents are going to be copied before they
are run. Here is an example:
UNION: run = RAM, LOAD_START(union_load_addr),
LOAD_SIZE(union_ld_sz), RUN_SIZE(union_run_sz)
{
.text1: load = ROM, SIZE(text1_size) { f1.obj(.text) }
.text2: load = ROM, SIZE(text2_size) { f2.obj(.text) }
}
Here union_ld_sz is going to be equal to the sum of the sizes of all output sections placed in the union.
The union_run_sz value is equivalent to the largest output section in the union. Both of these symbols
incorporate any padding due to blocking or alignment requirements.
Another way to create a hole in an output section is to combine an uninitialized section with an initialized
section to form a single output section. In this case, the linker treats the uninitialized section as a hole and
supplies data for it. The following example illustrates this method:
SECTIONS
{
outsect:
{
file1.obj(.text)
file1.obj(.bss) /* This becomes a hole */
}
}
Because the .text section has raw data, all of outsect must also contain raw data. Therefore, the
uninitialized .bss section becomes a hole.
Uninitialized sections become holes only when they are combined with initialized sections. If several
uninitialized sections are linked together, the resulting output section is also uninitialized.
Filling Sections
Note: Because filling a section (even with 0s) causes raw data to be generated for the entire
section in the output file, your output file will be very large if you specify fill values for large
sections or holes.
...
}
In this example, the LOAD_START(), RUN_START(), and SIZE() operators instruct the linker to create
three symbols:
Symbol Description
_flash_code_ld_start Load address of .flashcode section
_flash_code_rn_start Run address of .flashcode section
_flash_code_size Size of .flashcode section
These symbols can then be referenced from the copy table. The actual data in the copy table will be
updated automatically each time the application is linked. This approach removes step 1 of the process
described in Section 7.15.1.
While maintenance of the copy table is reduced markedly, you must still carry the burden of keeping the
copy table contents in sync with the symbols that are defined in the link command file. Ideally, the linker
would generate the boot copy table automatically. This would avoid having to build the application twice
and free you from having to explicitly manage the contents of the boot copy table.
For more information on the LOAD_START(), RUN_START(), and SIZE() operators, see Section 7.13.7.
SECTIONS
{
...
UNION
{
GROUP
{
.task1: { task1.obj(.text) }
.task2: { task2.obj(.text) }
GROUP
{
.task3: { task3.obj(.text) }
.task4: { task4.obj(.text) }
...
}
The application must manage the contents of the memory overlay at run time. That is, whenever any
services from .task1 or .task2 are needed, the application must first ensure that .task1 and .task2 are
resident in the memory overlay. Similarly for .task3 and .task4.
To affect a copy of .task1 and .task2 from ROM to RAM at run time, the application must first gain access
to the load address of the tasks (_task12_load_start), the run address (_task_run_start), and the size
(_task12_size). Then this information is used to perform the actual code copy.
SECTIONS
{
...
UNION
{
GROUP
{
.task1: { task1.obj(.text) }
.task2: { task2.obj(.text) }
GROUP
{
.task3: { task3.obj(.text) }
.task4: { task4.obj(.text) }
} run = RAM
...
}
Using the SECTIONS directive from Example 7-17 in the link command file, the linker generates two copy
tables named: _task12_copy_table and _task34_copy_table. Each copy table provides the load address,
run address, and size of the GROUP that is associated with the copy table. This information is accessible
from application source code using the linker-generated symbols, _task12_copy_table and
_task34_copy_table, which provide the addresses of the two copy tables, respectively.
Using this method, you do not have to worry about the creation or maintenance of a copy table. You can
reference the address of any copy table generated by the linker in C/C++ or assembly source code,
passing that value to a general purpose copy routine which will process the copy table and affect the
actual copy.
For this example, the linker creates a copy table that can be accessed through a special linker-generated
symbol, ___binit__, which contains the list of all object components that need to be copied from their load
location to their run location at boot-time. If a link command file does not contain any uses of table(BINIT),
then the ___binit__ symbol is given a value of -1 to indicate that a boot-time copy table does not exist for
a particular application.
You can apply the table(BINIT) specification to an output section, GROUP, or UNION member. If used in
the context of a UNION, only one member of the UNION can be designated with table(BINIT). If applied to
a GROUP, then none of that GROUP's members may be marked with table(BINIT).The linker detects
violations of these rules and reports them as warnings, ignoring each offending use of the table(BINIT)
specification.
SECTIONS
{
UNION
{
.first: { a1.obj(.text), b1.obj(.text), c1.obj(.text) }
load = EMEM, run = PMEM, table(BINIT), table(_first_ctbl)
...
}
In this example, the output sections .first and .extra are copied from external memory (EMEM) into
program memory (PMEM) at boot time while processing the BINIT copy table. After the application has
started executing its main thread, it can then manage the contents of the overlay using the two overlay
copy tables named: _first_ctbl and _second_ctbl.
/*****************************************************************************/
/* cpy_tbl.h v3.0.0 */
/* Copyright (c) 2003 Texas Instruments Incorporated */
/* */
/* Specification of copy table data structures which can be automatically */
/* generated by the linker (using the table() operator in the LCF). */
/* */
/*****************************************************************************/
#ifndef _CPY_TBL
#define _CPY_TBL
#ifdef __cplusplus
extern "C" namespace std {
#endif /* __cplusplus */
/*****************************************************************************/
/* Copy Record Data Structure */
/*****************************************************************************/
typedef struct copy_record
{
unsigned long load_addr;
unsigned long run_addr;
unsigned long size;
} COPY_RECORD;
/*****************************************************************************/
/* Copy Table Data Structure */
/*****************************************************************************/
typedef struct copy_table
{
unsigned short rec_size;
unsigned short num_recs;
COPY_RECORD recs[1];
} COPY_TABLE;
/*****************************************************************************/
/* Prototype for general purpose copy routine. */
/*****************************************************************************/
extern void copy_in(COPY_TABLE *tp);
#ifdef __cplusplus
} /* extern "C" namespace std */
#ifndef _CPP_STYLE_HEADER
using std::COPY_RECORD;
using std::COPY_TABLE;
using std::copy_in;
#endif /* _CPP_STYLE_HEADER */
#endif /* __cplusplus */
#endif /* !_CPY_TBL */
For each object component that is marked for a copy, the linker creates a COPY_RECORD object for it.
Each COPY_RECORD contains at least the following information for the object component:
• The load address
• The run address
• The size
The linker collects all COPY_RECORDs that are associated with the same copy table into a
COPY_TABLE object. The COPY_TABLE object contains the size of a given COPY_RECORD, the
number of COPY_RECORDs in the table, and the array of COPY_RECORDs in the table. For instance, in
the BINIT example in Section 7.15.6, the .first and .extra output sections will each have their own
COPY_RECORD entries in the BINIT copy table. The BINIT copy table will then look like this:
COPY_TABLE __binit__ = { 12, 2,
{ <load address of .first>,
<run address of .first>,
<size of .first> },
{ <load address of .extra>,
<run address of .extra>,
<size of .extra> } };
/*****************************************************************************/
/* cpy_tbl.c v3.0.0 */
/* Copyright (c) 2003-2008 Texas Instruments Incorporated */
/* */
/* General purpose copy routine. Given the address of a linker-generated */
/* COPY_TABLE data structure, effect the copy of all object components */
/* that are designated for copy via the corresponding LCF table() operator. */
/* */
/*****************************************************************************/
#include <cpy_tbl.h>
#include <string.h>
/*****************************************************************************/
/* COPY_IN() */
/*****************************************************************************/
void copy_in(COPY_TABLE *tp)
{
unsigned short i;
SECTIONS
{
UNION
{
.first: { a1.obj(.text), b1.obj(.text), c1.obj(.text) }
load = EMEM, run = PMEM, table(BINIT), table(_first_ctbl)
...
For the link command file in Example 7-21, the boot-time copy table is generated into a .binit input section,
which is collected into the .binit output section, which is mapped to an address in the BMEM memory
area. The _first_ctbl is generated into the .ovly:_first_ctbl input section and the _second_ctbl is generated
into the .ovly:_second_ctbl input section. Since the base names of these input sections match the name of
the .ovly output section, the input sections are collected into the .ovly output section, which is then
mapped to an address in the BMEM memory area.
If you do not provide explicit placement instructions for the linker-generated copy table sections, they are
allocated according to the linker's default placement algorithm.
The linker does not allow other types of input sections to be combined with a copy table input section in
the same output section. The linker does not allow a copy table section that was created from a partial link
session to be used as input to a succeeding link session.
For example, consider an application which has seven tasks. Tasks 1 through 3 are overlaid with tasks 4
through 7 (using a UNION directive). The load placement of all of the tasks is split among four different
memory areas (LMEM1, LMEM2, LMEM3, and LMEM4). The overlay is defined as part of memory area
PMEM. You must move each set of tasks into the overlay at run time before any services from the set are
used.
You can use table() operators in combination with splitting operators, >>, to create copy tables that have
all the information needed to move either group of tasks into the memory overlay as shown in Example
7-22. Example 7-23 illustrates a possible driver for such an application.
Example 7-22. Creating a Copy Table to Access a Split Object Component
SECTIONS
{
UNION
{
.task1to3: { *(.task1), *(.task2), *(.task3) }
load >> LMEM1 | LMEM2 | LMEM4, table(_task13_ctbl)
GROUP
{
.task4: { *(.task4) }
.task5: { *(.task5) }
.task6: { *(.task6) }
.task7: { *(.task7) }
} run = PMEM
...
#include <cpy_tbl.h>
main()
{
...
copy_in(&task13_ctbl);
task1();
task2();
task3();
...
copy_in(&task47_ctbl);
task4();
task5();
task6();
task7();
...
}
You must declare a COPY_TABLE object as far to allow the overlay copy table section placement to be
independent from the other sections containing data objects (such as .bss).
Step 2: Link the file file2.com; use the --relocatable option to retain relocation information in the
output file tempout2.out.
cl430 --run_linker --relocatable --output_file=tempout2 file2.com
file2.com contains:
SECTIONS { ss2: { g1.obj g2.obj . . . gn.obj } }
The C/C++ compiler produces assembly language source code that can be assembled and linked. For
example, a C program consisting of modules prog1, prog2, etc., can be assembled and then linked to
produce an executable file called prog.out:
cl430 --run_linker --rom_model --output_file prog.out prog1.obj prog2.obj ... rts430.lib
The --rom_model option tells the linker to use special conventions that are defined by the C/C++
environment.
The archive libraries shipped by TI contain C/C++ run-time-support functions.
C, C++, and mixed C and C++ programs can use the same run-time-support library. Run-time-support
functions and variables that can be called and referenced from both C and C++ will have the same
linkage.
For more information about the MSP430 C/C++ language, including the run-time environment and
run-time-support functions, see the MSP430 Optimizing C/C++ Compiler User's Guide
cint Initialization
.cinit
Loader tables
section
(EXT_MEM)
Boot
routine
.bss
section
(D_MEM)
.cinit Loader
.bss
The output sections are constructed from the following input sections:
• Executable code, contained in the .text sections of demo.obj, ctrl.obj, and tables.obj, must be linked
into FLASH.
• A set of interrupt vectors, contained in the .intvecs section of tables.obj, must be linked at address
0xFFE0.
• A table of coefficients, contained in the .data section of tables.obj, must be linked into FLASH. The
remainder of block FLASH must be initialized to the value 0xFF00.
/****************************************************************************/
/* Specify Linker Options */
/****************************************************************************/
--entry_point=SETUP /* Define the program entry point */
--output_file=demo.out /* Name the output file */
--map_file=demo.map /* Create an output map file */
/****************************************************************************/
/* Specify Input Files */
/****************************************************************************/
demo.obj
ctrl.obj
tables.obj
/****************************************************************************/
/* Specify System Memory Map */
/****************************************************************************/
MEMORY
{
SFR(R) : origin = 0x0000, length = 0x0010
PERIPHERALS_8BIT : origin = 0x0010, length = 0x00F0
PERIPHERALS_16BIT: origin = 0x0100, length = 0x0100
RAM(RW) : origin = 0x0200, length = 0x0800
INFOA : origin = 0x1080, length = 0x0080
INFOB : origin = 0x1000, length = 0x0080
FLASH : origin = 0x1100, length = 0xEEE0
VECTORS(R) : origin = 0xFFE0, length = 0x001E
RESET : origin = 0xFFFE, length = 0x0002
}
/****************************************************************************/
/* Specify Output Sections */
/****************************************************************************/
SECTIONS
{
.text : {} > FLASH /* Link all .text section into flash */
.intvecs : {} > 0xFFE0 /* Link interrupt vectors. at 0xFFE0 */
.data : /* Link .data sections */
{
tables.obj(.data)
. = 0x400; /* Create hole at end of block */
} = 0xFF00 > FLASH /* Fill and link into FLASH */
} = 0x0100 > RAM /* Fill with 0x0100 and link into RAM */
/****************************************************************************/
/* End of Command File */
/****************************************************************************/
This creates the map file shown in Example 7-25 and an output file called demo.out that can be run on a
MSP430.
Example 7-25. Output Map File, demo.map
MEMORY CONFIGURATION
output attributes/
section page origin length input sections
-------- ---- ---------- ---------- ----------------
.text 0 00000020 00000138
00000020 000000a0 ctrl.obj (.text)
000000c0 00000000 tables.obj (.text)
000000c0 00000098 demo.obj (.text)
GLOBAL SYMBOLS
address name address name
-------- ---- -------- ----
00001500 .bss 00000020 clear
08000000 .data 00000020 .text
00000020 .text 000000b8 set
000000d4 SETUP 000000c0 x42
00000020 clear 000000d4 SETUP
08000400 edata 00000158 etext
00001600 end 00001500 .bss
00000158 etext 00001600 end
000000b8 set 08000000 .data
000000c0 x42 08000400 edata
[10 symbols]
The MSP430™ absolute lister is a debugging tool that accepts linked object files as input and creates .abs
files as output. These .abs files can be assembled to produce a listing that shows the absolute addresses
of object code. Manually, this could be a tedious process requiring many operations; however, the
absolute lister utility performs these operations automatically.
ÍÍ
Figure 8-1. Absolute Lister Development Flow
ÍÍ
ÍÍ
Step 1: Assembler First, assemble a source file.
source file
ÍÍ
ÍÍ
ÍÍ
Assembler
ÍÍ
ÍÍ
ÍÍ
Object
file
ÍÍ
Step 2: Link the resulting object file.
ÍÍ
ÍÍ
Linker
ÍÍ
ÍÍ
Linked object
ÍÍ
file
ÍÍ
Step 3: Invoke the absolute lister; use the linked ob-
ÍÍ
ject file as input. This creates a file with an .abs
Absolute extension.
ÍÍ
lister
ÍÍ
ÍÍ.abs
ÍÍ
file
ÍÍ
Step 4: Finally, assemble the .abs file; you must in-
ÍÍ
voke the assembler with the −a option. This
Assembler produces a listing file that contains absolute
ÍÍ addresses.
ÍÍ
Absolute
listing
The absolute lister produces an output file for each file that was linked. These files are named with the
input filenames and an extension of .abs. Header files, however, do not generate a corresponding .abs
file.
Assemble these files with the --absolute_listing assembler option as follows to create the absolute listing:
cl430 --absolute_listing filename.abs
The -e options affect both the interpretation of filenames on the command line and the names of the
output files. They should always precede any filename on the command line.
The -e options are useful when the linked object file was created from C files compiled with the debugging
option (--symdebug:dwarf compiler option). When the debugging option is set, the resulting linked object
file contains the name of the source files used to build it. In this case, the absolute lister does not generate
a corresponding .abs file for the C header files. Also, the .abs file corresponding to a C source file uses
the assembly file generated from the C source file rather than the C source file itself.
For example, suppose the C source file hello.csr is compiled with the debugging option set; the debugging
option generates the assembly file hello.s. The hello.csr file includes hello.hsr. Assuming the executable
file created is called hello.out, the following command generates the proper .abs file:
An .abs file is not created for hello.hsr (the header file), and hello.abs includes the assembly file hello.s,
not the C source file hello.csr.
module2.asm
.global dflag
.global array
.global offset
.bss offset,1
.text
MOV &offset,R14
globals.def
.global dflag
.global array
.global offset
The following steps create absolute listings for the files module1.asm and module2.asm:
1. First, assemble module1.asm and module2.asm:
cl430 module1
cl430 module2
This creates two object files called module1.obj and module2.obj.
2. Next, link module1.obj and module2.obj using the following linker command file, called bttest.cmd:
--output_file=bttest.out
--map_file=bttest.map
module1.obj
module2.obj
MEMORY
{
RAM: origin=0x0200, length=0x0800
FLASH: origin=0x1100, length=0xEEE0
}
SECTIONS
{
.bss: > RAM
.text: > FLASH
}
Invoke the linker:
This command creates an executable object file called bttest.out; use this new file as input for the
absolute lister.
abs430 bttest.out
module1.abs:
.nolist
.text .setsym 000001100h
__text__ .setsym 000001100h
etext .setsym 000001112h
__etext__ .setsym 000001112h
.bss .setsym 000000200h
__bss__ .setsym 000000200h
end .setsym 000000266h
__end__ .setsym 000000266h
array .setsym 000000201h
dflag .setsym 000000200h
offset .setsym 000000265h
.setsect ".text",000001100h
.setsect ".bss",000000200h
.setsect ".debug_line",000000000h
.list
.text
.copy "module1.asm"
module2.abs:
.nolist
.text .setsym 000001100h
__text__ .setsym 000001100h
etext .setsym 000001112h
__etext__ .setsym 000001112h
.bss .setsym 000000200h
__bss__ .setsym 000000200h
end .setsym 000000266h
__end__ .setsym 000000266h
array .setsym 000000201h
dflag .setsym 000000200h
offset .setsym 000000265h
.setsect ".text",00000110eh
.setsect ".bss",000000265h
.setsect ".debug_line",00000003ah
.setsect ".debug_info",00000014fh
.list
.text
.copy "module2.asm"
These files contain the following information that the assembler needs for step 4:
• They contain .setsym directives, which equate values to global symbols. Both files contain global
equates for the symbols dflag and offset. The symbols dflag and array were defined in the file
module1.asm. The symbol offset was defined in the file module2.asm.
• They contain .setsect directives, which define the absolute addresses for sections.
• They contain .copy directives, which defines the assembly language source file to include.
The .setsym and .setsect directives are useful only for creating absolute listings, not normal assembly.
4. Finally, assemble the .abs files created by the absolute lister (remember that you must use the
--absolute_listing option when you invoke the assembler):
cl430 --absolute_listing module1.abs
17 001100 .text
18 .copy "module1.asm"
A 1 .global dflag
A 2 .global array
A 3 .global offset
A 4 000200 .bss dflag,1
A 5 000201 .bss array,100
A 6 001100 .text
A 7 001100 421B MOV &array,R11
001102 0201!
A 8 001104 421C MOV &offset,R12
001106 0265!
A 9 001108 5C0B ADD R12,R11
A 10 00110a 4BA2 MOV @R11,&dflag
00110c 0200!
18 00110e .text
19 .copy "module2.asm"
A 1 .global dflag
A 2 .global array
A 3 .global offset
A 4 000265 .bss offset,1
A 5 00110e .text
A 6 00110e 421E MOV &offset,R14
001110 0265!
The MSP430™ cross-reference lister is a debugging tool. This utility accepts linked object files as input
and produces a cross-reference listing as output. This listing shows symbols, their definitions, and their
references in the linked source files.
Figure 9-1. The Cross-Reference Lister in the MSP430 Software Development Flow
Step 1: Assembler First, invoke the assembler with the -ax option.
source file This option produces a cross-reference table
in the listing file and adds to the object file
cross-reference information. By default, the
Assembler assembler cross-references only global
symbols. If you use the -as option when
invoking the assembler, it cross-references
local symbols as well.
Object
file
Step 2: Link the object file (.obj) to obtain an
executable object file (.out).
Link step
Linked object
file
Step 3: Invoke the cross-reference lister. The following
section provides the command syntax for
Cross-reference invoking the cross-reference lister utility.
lister
Cross-reference
listing
output filename is the name of the cross-reference listing file. If you omit the output filename, the default
filename is the input filename with an .xrf extension.
================================================================================
================================================================================
Symbol: array
================================================================================
================================================================================
Symbol: dflag
================================================================================
================================================================================
Symbol: offset
ofd430 is the command that invokes the object file display utility.
input filename names the object file (.obj), executable file (.out), or archive library (.lib) source file.
The filename must contain an extension.
options identify the object file display utility options that you want to use. Options are not case
sensitive and can appear anywhere on the command line following the command.
Precede each option with a hyphen.
-g appends DWARF debug information to program output.
-o=filename sends program output to filename rather than to the
screen.
-x displays output in XML format.
If an archive file is given as input to the object file display utility, each object file member of the archive is
processed as if it was passed on the command line. The object file members are processed in the order in
which they appear in the archive file.
If the object file display utility is invoked without any options, it displays information about the contents of
the input files on the console screen.
The MSP430™ assembler and linker create object files which are in binary formats that encourage
modular programming and provide powerful and flexible methods for managing code segments and target
system memory.
Most EPROM programmers do not accept object files as input. The hex conversion utility converts an
object file into one of several standard ASCII hexadecimal formats, suitable for loading into an EPROM
programmer. The utility is also useful in other applications requiring hexadecimal conversion of an object
file (for example, when using debuggers and loaders).
The hex conversion utility can produce these output file formats:
• ASCII-Hex, supporting 16-bit addresses
• Extended Tektronix (Tektronix)
• Intel MCS-86 (Intel)
• Motorola Exorciser (Motorola-S), supporting 16-bit addresses
• Texas Instruments SDSMAC (TI-Tagged), supporting 16-bit addresses
11.1 The Hex Conversion Utility's Role in the Software Development Flow
Figure 11-1 highlights the role of the hex conversion utility in the software development process.
Figure 11-1. The Hex Conversion Utility in the MSP430 Software Development Flow
C/C++
source
files
Macro
source C/C++
files compiler
C/C++ name
Assembler
Archiver demangling
source
utility
Macro
library Assembler
Library-build Debugging
Object
Archiver process tools
files
Run-time-
Library of support
object library
files Linker
Executable
object file
Hex-conversion
utility
There are two basic methods for invoking the hex conversion utility:
• Specify the options and filenames on the command line. The following example converts the file
firmware.out into TI-Tagged format, producing two output files, firm.lsb and firm.msb.
hex430 -t firmware -o firm.lsb -o firm.msb
• Specify the options and filenames in a command file. You can create a batch file that stores
command line options and filenames for invoking the hex conversion utility. The following example
invokes the utility using a command file called hexutil.cmd:
hex430 hexutil.cmd
In addition to regular command line information, you can use the hex conversion utility ROMS and
SECTIONS directives in a command file.
11.2.1 Invoking the Hex Conversion Utility From the Command Line
To invoke the utility and use the options you defined in a command file, enter:
hex430 command_filename
You can also specify other options and files on the command line. For example, you could invoke the
utility by using both a command file and command line options:
hex430 firmware.cmd -map firmware.mxp
The order in which these options and filenames appear is not important. The utility reads all input from the
command line and all information from the command file before starting the conversion process. However,
if you are using the -q option, it must appear as the first option on the command line or in a command file.
The -q option suppresses the hex conversion utility's normal banner and progress information.
• Assume that a command file named firmware.cmd contains these lines:
firmware.out /* input file */
-t /* TI-Tagged */
-o firm.lsb /* output file */
-o firm.msb /* output file */
You can invoke the hex conversion utility by entering:
hex430 firmware.cmd
• This example shows how to convert a file called appl.out into eight hex files in Intel format. Each output
file is one byte wide and 4K bytes long.
appl.out /* input file */
-I /* Intel format */
-map appl.mxp /* map file */
ROMS
{
ROW1: origin=0x00000000 len=0x4000 romwidth=8
files={ appl.u0 appl.u1 app1.u2 appl.u3 }
ROW2: origin=0x00004000 len=0x4000 romwidth=8
files={ app1.u4 appl.u5 appl.u6 appl.u7 }
}
SECTIONS
{ .text, .data, .cinit, .sect1, .vectors, .const:
}
The hex conversion utility makes your memory architecture more flexible by allowing you to specify
memory and ROM widths. To use the hex conversion utility, you must understand how the utility treats
word widths. Three widths are important in the conversion process:
• Target width
• Memory width
• ROM width
The terms target word, memory word, and ROM word refer to a word of such a width.
Figure 11-2 illustrates the two separate and distinct phases of the hex conversion utility's process flow.
Output file(s)
Figure 11-3 demonstrates how the memory width is related to object file data.
Source file
.word 0AABBh
.word 01122h
. . .
0AABBh
01122h
. . .
You can change ROM width (except for TI-Tagged format) by:
• Using the -romwidth option. This option changes the ROM width value for the entire object file.
• Setting the romwidth parameter of the ROMS directive. This parameter changes the ROM width value
for a specific ROM address range and overrides the -romwidth option for that range. See Section 11.4.
For both methods, use a value that is a power of 2 greater than or equal to 8.
If you select a ROM width that is wider than the natural size of the output format (16 bits for TI-Tagged or
8 bits for all others), the utility simply writes multibyte fields into the file.
Figure 11-4 illustrates how the object file data, memory, and ROM widths are related to one another.
Source file
.word 0AABBCDDh
.word 01122344h
. . .
-memwidth 16 -memwidth 8
Data after AABB BB
phase I
of hex 430 1122 AA
. . .
22
11
. . .
Output files
-romwidth 16
-o file.wrd AABB1122 . . .
-romwidth 8
-o file.byt BBAA2211 . . .
The ROMS directive specifies the physical memory configuration of your system as a list of address-range
parameters.
Each address range produces one set of files containing the hex conversion utility output data that
corresponds to that address range. Each file can be used to program one single ROM device.
The ROMS directive is similar to the MEMORY directive of the MSP430 linker: both define the memory
map of the target address space. Each line entry in the ROMS directive defines a specific address range.
The general syntax is:
ROMS
{
romname : [origin=value,] [length=value,] [romwidth=value,]
[ memwidth=value,] [fill=value]
[files={filename1, filename2, ...}]
romname : [origin=value,] [length=value,] [romwidth=value,]
[ memwidth=value,] [fill=value]
[files={filename1, filename2, ...}]
...
}
length specifies the length of a memory range as the physical length of the ROM device. It
can be entered as length, len, or l. The value must be a decimal, octal, or hexadecimal
constant. If you omit the length value, it defaults to the length of the entire address
space.
romwidth specifies the physical ROM width of the range in bits (see Section 11.3.3). Any value
you specify here overrides the -romwidth option. The value must be a decimal, octal,
or hexadecimal constant that is a power of 2 greater than or equal to 8.
memwidth specifies the memory width of the range in bits (see Section 11.3.2). Any value you
specify here overrides the -memwidth option. The value must be a decimal, octal, or
hexadecimal constant that is a power of 2 greater than or equal to 8. When using the
memwidth parameter, you must also specify the paddr parameter for each section in
the SECTIONS directive. (See Section 11.5.)
fill specifies a fill value to use for the range. In image mode, the hex conversion utility
uses this value to fill any holes between sections in a range. A hole is an area between
the input sections that comprises an output section that contains no actual code or
data. The fill value must be a decimal, octal, or hexadecimal constant with a width
equal to the target width. Any value you specify here overrides the -fill option. When
using fill, you must also use the -image command line option. (See Section 11.8.2.)
files identifies the names of the output files that correspond to this range. Enclose the list of
names in curly braces and order them from least significant to most significant output
file, where the bits of the memory word are numbered from right to left. The number of
file names must equal the number of output files that the range generates. To calculate
the number of output files, see Section 11.3.3. The utility warns you if you list too many
or too few filenames.
Unless you are using the -image option, all of the parameters that define a range are optional; the
commas and equal signs are also optional. A range with no origin or length defines the entire address
space. In image mode, an origin and length are required for all ranges.
Ranges must not overlap and must be listed in order of ascending address.
infile.out
-image
-memwidth 16
ROMS
}
EPROM1: org = 0x4000, len = 0x2000, romwidth = 8
files = { rom4000.b0, rom4000.b1}
Figure 11-5. The infile.out File Partitioned Into Four Output Files
COFF file: Output files:
infile.out EPROM1
rom4000.b0 rom4000.b1
0x4000 0x4000
.text (org) .text .text
0x487F 0x4880
0x5B80 0h 0h
.data 0x5B80
0x633F .data .data
0x6700 0x5FFF
.table
Width = 8 bits
0x7C7F len = 2000h (8K)
EPROM2
rom6000.b0 rom6000.b1
0x6000
.data .data
0x6340
FFh 00h
0x6700
.table .table
0x7C80
FFh 00h
0x7FFF
The map file (specified with the -map option) is advantageous when you use the ROMS directive with
multiple ranges. The map file shows each range, its parameters, names of associated output files, and a
list of contents (section names and fill values) broken down by address. Example 11-2 is a segment of the
map file resulting from the example in Example 11-1.
Example 11-2. Map File Output From Example 11-1 Showing Memory Ranges
-----------------------------------------------------
4000..5fff Page=0 Width=8 "EPROM1"
-----------------------------------------------------
OUTPUT FILES: rom4000.b0 [b0..b7]
rom4000.b1 [b8..b15]
CONTENTS: 4000..487f .text
4880..5b7f FILL = 0000
5b80..5fff .data
-----------------------------------------------------
6000..7fff Page=0 Width=8 "EPROM2"
-----------------------------------------------------
OUTPUT FILES: rom6000.b0 [b0..b7]
rom6000.b1 [b8..b15]
CONTENTS: 6000..633f .data
6340..66ff FILL = ff00
6700..7c7f .table
7c80..7fff FILL = ff00
EPROM1 defines the address range from 0x4000 through 0x5FFF with the following sections:
This section ... Has this range ...
.text 0x4000 through 0x487F
.data 0x5B80 through 0x5FFF
The rest of the range is filled with 0h (the default fill value), converted into two output files:
• rom4000.b0 contains bits 0 through 7
• rom4000.b1 contains bits 8 through 15
EPROM2 defines the address range from 0x6000 through 0x7FFF with the following sections:
This section ... Has this range ...
.data 0x6000 through 0x633F
.table 0x6700 through 0x7C7F
The rest of the range is filled with 0xFF00 (from the specified fill value). The data from this range is
converted into two output files:
• rom6000.b0 contains bits 0 through 7
• rom6000.b1 contains bits 8 through 15
Use the SECTIONS directive in a command file. (See Section 11.2.2.) The general syntax for the
SECTIONS directive is:
SECTIONS
{
sname[:] [paddr=value][,]
sname[:] [paddr=value][,]
...
}
For more similarity with the linker's SECTIONS directive, you can use colons after the section names. For
example, the data in your application (section partB) must be loaded on the EPROM at address 0x0. Use
the paddr option with the SECTIONS directive to specify this:
SECTIONS
{
partB: paddr = 0x0
}
The commas separating section names are optional. For example, the COFF file contains six initialized
sections: .text, .data, .const, .vectors, .coeff, and .tables. Suppose you want only .text and .data to be
converted. Use a SECTIONS directive to specify this:
SECTIONS { .text, .data }
b. The range number in the ROMS directive. Ranges are numbered starting with 0. If there is no
ROMS directive, or only one range, the utility omits this character.
c. The file number in the set of files for the range, starting with 0 for the least significant file.
For example, assume a.out is for a 16-bit target processor and you are creating Intel format output.
With no output filenames specified, the utility produces two output files named a.i0, a.i1.
If you include the following ROMS directive when you invoke the hex conversion utility, you would have
four output files:
ROMS
{
range1: o = 0x1000 l = 0x1000
range2: o = 0x2000 l = 0x1000
}
These output files ... Contain data in these locations ...
a.i00 and a.i01 0x1000 through 0x1FFF
a.i10 and a.i11 0x2000 through 0x2FFF
Step 1: Define the ranges of target memory with a ROMS directive. See Section 11.4.
Step 2: Invoke the hex conversion utility with the -image option. You can optionally use the -zero
option to reset the address origin to 0 for each output file. If you do not specify a fill value
with the ROMS directive and you want a value other than the default of 0, use the -fill option.
Address bits determine how many bits of the address information the format supports. Formats with
16-bit addresses support addresses up to 64K only. The utility truncates target addresses to fit in the
number of available bits.
The default width determines the default output width of the format. You can change the default width by
using the -romwidth option or by using the romwidth parameter in the ROMS directive. You cannot change
the default width of the TI-Tagged format, which supports a 16-bit width only.
^B $AXXXXXXXX,
XX XX XX XX XX XX XX XX XX XX. . .^C
Data byte
The file begins with an ASCII STX character (ctrl-B, 02h) and ends with an ASCII ETX character (ctrl-C,
03h). Address records are indicated with $AXXXXXXX, in which XXXXXXXX is a 8-digit (16-bit)
hexadecimal address. The address records are present only in the following situations:
• When discontinuities occur
• When the byte stream does not begin at address 0
You can avoid all discontinuities and any address records by using the -image and -zero options. This
creates output that is simply a list of byte values.
Record type00, the data record, begins with a colon ( : ) and is followed by the byte count, the address of
the first data byte, the record type (00), and the checksum. The address is the least significant 16 bits of a
32-bit address; this value is concatenated with the value from the most recent 04 (extended linear
address) record to create a full 32-bit address. The checksum is the 2s complement (in binary form) of the
preceding bytes in the record, including byte count, address, and data bytes.
Record type 01, the end-of-file record, also begins with a colon ( : ), followed by the byte count, the
address, the record type (01), and the checksum.
Record type 04, the extended linear address record, specifies the upper 16 address bits. It begins with a
colon ( : ), followed by the byte count, a dummy address of 0h, the record type (04), the most significant
16 bits of the address, and the checksum. The subsequent address fields in the data records contain the
least significant bytes of the address.
Figure 11-7 illustrates the Intel hexadecimal object format.
:2000000000000100020003000400050006000700080009000A000B000C000D000E000F0068
:2000200010001100120013001400150016001700180019001A001B001C001D001E001F0048 Data
:2000400000000100020003000400050006000700080009000A000B000C000D000E000F0028 records
:2000600010001100120013001400150016001700180019001A001B001C001D001E001F0008
:00000001FF
Checksum
Byte Record End-of-file
count type record
The byte count is the character pair count in the record, excluding the type and byte count itself.
The checksum is the least significant byte of the 1s complement of the sum of the values represented by
the pairs of characters making up the byte count, address, and the code/data fields.
Figure 11-8 illustrates the Motorola-S object format.
Figure 11-9 illustrates the tag characters and fields in TI-Tagged object format.
K000COFFTOTI90000BFFFFBFFFFBFFFFBFFFFBFFFFBFFFFBFFFFBFFFFBFFFF7EF3DF
BFFFFBFFFFBFFFFBFFFFBFFFFBFFFFBFFFFBFFFFBFFFFBFFFFBFFFFBFFFFBFFFF7EE37F Data
BFFFFBFFFFBFFFFBFFFFBFFFFBFFFFBFFFFBFFFFBFFFFBFFFF7F245F records
:
End-of-file Data
record words Checksum
If any data fields appear before the first address, the first field is assigned address 0000h. Address fields
may be expressed but not required for any data byte. The checksum field, preceded by the tag character
7, is the 2s complement of the sum of the 8-bit ASCII values of characters, beginning with the first tag
character and ending with the checksum tag character (7 or 8). The end-of-file record is a colon ( : ).
@ADDR1
Data
DATA01 DATA02 ........ DATA16
bytes DATA17 DATA32 ........ DATA32
DATAm ........ DATAn
Section @ADDR2
start DATA01 .................... DATAn Data
q bytes
End-of-line
character
@F000
31 40 00 03 B2 40 80 5A 20 01 D2 D3 22 00 D2 E3
21 00 3F 40 E8 FD 1F 83 FE 23 F9 3F
@FFFE
00 F0
Q
The header field in the data record contains the following information:
Number of ASCII
Item Characters Description
% 1 Data type is Tektronix format
Block length 2 Number of characters in the record, minus the %
Block type 1 6 = data record
8 = termination record
Checksum 2 A 2-digit hex sum modulo 256 of all values in the record except the % and the
checksum itself.
The load address in the data record specifies where the object code will be located. The first digit
specifies the address length; this is always 8. The remaining characters of the data record contain
the object code, two characters per byte.
Figure 11-11 illustrates the Tektronix object format.
Header %15621810000000202020202020
character
Load address: 10000000h
Block type: 6 Length of
(data) load address
You can use the .cdecls assembler directive to share C headers containing declarations and prototypes
between C and assembly code. Any legal C/C++ can be used in a .cdecls block and the C/C++
declarations will cause suitable assembly to be generated automatically, allowing you to reference the
C/C++ constructs in assembly code.
SLAU131B – March 2008 Sharing C/C++ Header Files With Assembly Source 243
Submit Documentation Feedback
Overview of the .cdecls Directive www.ti.com
.cdecls C,NOLIST
%{
#ifndef ASMTEST
#warn "ASMTEST not defined!" /* will be issued */
#endif
%}
Therefore, a typical use of the .cdecls block is expected to be a single usage near the beginning of the
assembly source file, in which all necessary C/C++ header files are included.
Use the compiler --include_path=path options to specify additional include file paths needed for the header
files used in assembly, as you would when compiling C files.
Any C/C++ errors or warnings generated by the code of the .cdecls are emitted as they normally would for
the C/C++ source code. C/C++ errors cause the directive to fail, and any resulting converted assembly is
not included.
C/C++ constructs that cannot be converted, such as function-like macros or variable definitions, cause a
comment to be output to the converted assembly file. For example:
; ASM HEADER WARNING - variable definition 'ABCD' ignored
The prefix ASM HEADER WARNING appears at the beginning of each message. To see the warnings,
either the WARN parameter needs to be specified so the messages are displayed on STDERR, or else
the LIST parameter needs to be specified so the warnings appear in the listing file, if any.
Finally, note that the converted assembly code does not appear in the same order as the original C/C++
source code and C/C++ constructs may be simplified to a normalized form during the conversion process,
but this should not affect their final usage.
12.2.1 Comments
Comments are consumed entirely at the C level, and do not appear in the resulting converted assembly
file.
244 Sharing C/C++ Header Files With Assembly Source SLAU131B – March 2008
Submit Documentation Feedback
www.ti.com Notes on C/C++ Conversions
12.2.2 Conditional Compilation (#if/#else/#ifdef/etc.)
Conditional compilation is handled entirely at the C level during the conversion step. Define any necessary
macros either on the command line (using the compiler --define=name=value option) or within a .cdecls
block using #define. The #if, #ifdef, etc. C/C++ directives are not converted to assembly .if, .else, .elseif,
and .endif directives.
12.2.3 Pragmas
Pragmas found in the C/C++ source code cause a warning to be generated as they are not converted.
They have no other effect on the resulting assembly file. See the .cdecls topic for the WARN and
NOWARN parameter discussion for where these warnings are created.
Some macros, while they are converted, have no functional use in the containing assembly file. For
example, the following results in the assembly substitution symbol FOREVER being set to the value
while(1), although this has no useful use in assembly because while(1) is not legal assembly code.
#define FOREVER while(1)
SLAU131B – March 2008 Sharing C/C++ Header Files With Assembly Source 245
Submit Documentation Feedback
Notes on C/C++ Conversions www.ti.com
Macro values are not interpreted as they are converted. For example, the following results in the
assembler substitution symbol OFFSET being set to the literal string value 5+12 and not the value 17.
This happens because the semantics of the C/C++ language require that macros are evaluated in context
and not when they are parsed.
#define OFFSET 5+12
Because macros in C/C++ are evaluated in their usage context, C/C++ printf escape sequences such as
\n are not converted to a single character in the converted assembly macro. See Section 12.2.11 for
suggestions on how to use C/C++ macro strings.
Macros are converted using the new .define directive (see Section 12.4.2), which functions similarly to the
.asg assembler directive. The exception is that .define disallows redefinitions of register symbols and
mnemonics to prevent the conversion from corrupting the basic assembly environment. To remove a
macro from the assembly scope, .undef can be used following the .cdecls that defines it (see
Section 12.4.3).
The macro functionality of # (stringize operator) is only useful within functional macros. Since functional
macros are not supported by this process, # is not supported either. The concatenation operator ## is only
useful in a functional context, but can be used degenerately to concatenate two strings and so it is
supported in that context.
12.2.10 Enumerations
Enumeration members are converted to .enum elements in assembly. For example:
enum state { ACTIVE=0x10, SLEEPING=0x01, INTERRUPT=0x100, POWEROFF, LAST};
The members are used via the pseudo-scoping created by the .enum directive.
The usage is similar to that for accessing structure members, enum_name.member.
This pseudo-scoping is used to prevent enumeration member names from corrupting other symbols within
the assembly environment.
12.2.11 C Strings
Because C string escapes such as \n and \t are not converted to hex characters 0x0A and 0x09 until their
use in a string constant in a C/C++ program, C macros whose values are strings cannot be represented
as expected in assembly substitution symbols. For example:
#define MSG "\tHI\n"
becomes, in assembly:
.define """\tHI\n""",MSG ; 6 quoted characters! not 5!
When used in a C string context, you expect this statement to be converted to 5 characters (tab, H, I,
newline, NULL), but the .string assembler directive does not know how to perform the C escape
conversions.
You can use the .cstring directive to cause the escape sequences and NULL termination to be properly
handled as they would in C/C++. Using the above symbol MSG with a .cstring directive results in 5
characters of memory being allocated, the same characters as would result if used in a C/C++ strong
context. (See Section 12.4.7 for the .cstring directive syntax.)
246 Sharing C/C++ Header Files With Assembly Source SLAU131B – March 2008
Submit Documentation Feedback
www.ti.com Notes on C/C++ Conversions
12.2.12 C/C++ Built-In Functions
The C/C++ built-in functions, such as sizeof( ), are not translated to their assembly counterparts, if any, if
they are used in macros. Also, their C expression values are not inserted into the resulting assembly
macro because macros are evaluated in context and there is no active context when converting the
macros to assembly.
Suitable functions such as $sizeof( ) are available in assembly expressions. However, as the basic types
such as int/char/float have no type representation in assembly, there is no way to ask for $sizeof(int), for
example, in assembly.
The conversion processes the above statements in the same manner: generating a temporary name for
the structure and then using .define to output a typedef from the temporary name to the user name. You
should use your mystrname in assembly the same as you would in C/C++, but do not be confused by the
assembly structure definition in the list, which contains the temporary name. You can avoid the temporary
name by specifying a name for the structure, as in:
typedef struct a_st_name { ... } mystrname;
If a shorthand method is used in C to declare a variable with a particular structure, for example:
extern struct a_name { int a_member; } a_variable;
Then after the structure is converted to assembly, a .tag directive is generated to declare the structure of
the external variable, such as:
_a_variable .tag a_st_name
SLAU131B – March 2008 Sharing C/C++ Header Files With Assembly Source 247
Submit Documentation Feedback
Notes on C++ Specific Conversions www.ti.com
The above format is the short method for declaring a single function. To use this method for multiple
functions, you can also use the following syntax:
extern "C"
{
void somefunc(int arg);
int anotherfunc(int arg);
...
}
----------------------------------------------------------
In C++ code, the class derived would contain both integers b1 and d1. In the converted assembly
structure "derived", the members of the base class must be accessed using the name of the base class,
such as derived.__b_base.b1 rather than the expected derived.b1.
A non-virtual, non-empty base class will have __b_ prepended to its name within the derived class to
signify it is a base class name. That is why the example above is derived.__b_base.b1 and not simply
derived.base.b1.
248 Sharing C/C++ Header Files With Assembly Source SLAU131B – March 2008
Submit Documentation Feedback
www.ti.com New Assembler Support
12.3.3 Templates
No support exists for templates.
The .enum directive begins the enumeration definition and .endenum terminates it.
The enumeration name (ENUM_NAME) cannot be used to allocate space; its size is reported as zero.
The format to use the value of a member is ENUM_NAME.MEMBER, similar to a structure member
usage.
The .emember directive optionally accepts the value to set the member to, just as in C/C++. If not
specified, the member takes a value one more than the previous member. As in C/C++, member names
cannot be duplicated, although values can be. Unless specified with .emember, the first enumeration
member will be given the value 0 (zero), as in C/C++.
The .endenum directive cannot be used with a label, as structure .endstruct directives can, because the
.endenum directive has no value like the .endstruct does (containing the size of the structure).
Conditional compilation directives (.if/.else/.elseif/.endif) are the only other non-enumeration code allowed
within the .enum/.endenum sequence.
SLAU131B – March 2008 Sharing C/C++ Header Files With Assembly Source 249
Submit Documentation Feedback
New Assembler Support www.ti.com
See Section 12.2.11 for more information on the new .cstring directive.
250 Sharing C/C++ Header Files With Assembly Source SLAU131B – March 2008
Submit Documentation Feedback
Appendix A
SLAU131B – March 2008
The assembler supports several directives that the MSP430 C/C++ compiler uses for symbolic debugging.
These directives differ for the two debugging formats, DWARF and COFF.
These directives are not meant for use by assembly-language programmers. They require arguments that
can be difficult to calculate manually, and their usage must conform to a predetermined agreement
between the compiler, the assembler, and the debugger. This appendix documents these directives for
informational purposes only.
The --keep_asm option instructs the compiler to retain the generated assembly file.
To disable the generation of all symbolic debug directives, invoke the compiler with the -symdebug:none
option:
cl430 --symdebug:none --keep_asm input_file
The linker supports the generation of an XML link information file via the --xml_link_info file option. This
option causes the linker to generate a well-formed XML file containing detailed information about the result
of a link. The information included in this file includes all of the information that is currently produced in a
linker-generated map file.
As the linker evolves, the XML link information file may be extended to include additional information that
could be useful for static analysis of linker results.
This appendix enumerates all of the elements that are generated by the linker into the XML link
information file.
<input_file_list>
<input_file id="fl-1">
<kind>object</kind>
<file>hi.obj</file>
<name>hi.obj</name>
</input_file>
<input_file id="fl-2">
<path>/tools/lib/</path>
<kind>archive</kind>
<file>rtsxxx.lib</file>
<name>boot.obj</name>
</input_file>
<input_file id="fl-3">
<path>/tools/lib/</path>
<kind>archive</kind>
<file>rtsxxx.lib</file>
<name>exit.obj</name>
</input_file>
<input_file id="fl-4">
<path>/tools/lib/</path>
<kind>archive</kind>
<file>rtsxxx.lib</file>
<name>printf.obj</name>
</input_file>
...
</input_file_list>
<object_component id="oc-20">
<name>.text</name>
<load_address>0xac00</load_address>
<run_address>0xac00</run_address>
<size>0xc0</size>
<input_file_ref idref="fl-4"/>
</object_component>
<object_component id="oc-21">
<name>.data</name>
<load_address>0x80000000</load_address>
<run_address>0x80000000</run_address>
<size>0x0</size>
<input_file_ref idref="fl-4"/>
</object_component>
<object_component id="oc-22">
<name>.bss</name>
<load_address>0x80000000</load_address>
<run_address>0x80000000</run_address>
<size>0x0</size>
<input_file_ref idref="fl-4"/>
</object_component>
Example B-4. Logical Group List for the fl-4 Input File
<logical_group_list>
...
<logical_group id="lg-7">
<name>.text</name>
<load_address>0x20</load_address>
<run_address>0x20</run_address>
<size>0xb240</size>
<contents>
<object_component_ref idref="oc-34"/>
<object_component_ref idref="oc-108"/>
<object_component_ref idref="oc-e2"/>
...
</contents>
</logical_group>
...
<overlay id="lg-b">
<name>UNION_1</name>
<run_address>0xb600</run_address>
<size>0xc0</size>
<contents>
<object_component_ref idref="oc-45"/>
<logical_group_ref idref="lg-8"/>
</contents>
</overlay>
...
<split_section id="lg-12">
<name>.task_scn</name>
<size>0x120</size>
<contents>
<logical_group_ref idref="lg-10"/>
<logical_group_ref idref="lg-11"/>
</contents>
...
</logical_group_list>
<placement_map>
<memory_area>
<name>PMEM</name>
<page_id>0x0</page_id>
<origin>0x20</origin>
<length>0x100000</length>
<used_space>0xb240</used_space>
<unused_space>0xf4dc0</unused_space>
<attributes>RWXI</attributes>
<usage_details>
<allocated_space>
<start_address>0x20</start_address>
<size>0xb240</size>
<logical_group_ref idref="lg-7"/>
</allocated_space>
<available_space>
<start_address>0xb260</start_address>
<size>0xf4dc0</size>
</available_space>
</usage_details>
</memory_area>
...
</placement_map>
<symbol_table>
<symbol>
<name>_c_int00</name>
<value>0xaf80</value>
</symbol>
<symbol>
<name>_main</name>
<value>0xb1e0</value>
</symbol>
<symbol>
<name>_printf</name>
<value>0xac00</value>
</symbol>
...
</symbol_table>
Glossary
command file— A file that contains options, filenames, directives, or commands for the linker or hex
conversion utility.
comment— A source statement (or portion of a source statement) that documents or improves readability
of a source file. Comments are not compiled, assembled, or linked; they have no effect on the
object file.
conditional processing— A method of processing one block of source code or an alternate block of
source code, according to the evaluation of a specified expression.
configured memory— Memory that the linker has specified for allocation.
constant— A type whose value cannot change.
cross-reference lister— A utility that produces an output file that lists the symbols that were defined,
what file they were defined in, what reference type they are, what line they were defined on, which
lines referenced them, and their assembler and linker final values. The cross-reference lister uses
linked object files as input.
cross-reference listing— An output file created by the assembler that lists the symbols that were defined,
what line they were defined on, which lines referenced them, and their final values.
.data section— One of the default object file sections. The .data section is an initialized section that
contains initialized data. You can use the .data directive to assemble code into the .data section.
directives— Special-purpose commands that control the actions and functions of a software tool (as
opposed to assembly language instructions, which control the actions of a device).
emulator— A hardware development system that duplicates the MSP430 operation.
entry point— A point in target memory where execution starts.
environment variable— A system symbol that you define and assign to a string. Environmental variables
are often included in Windows batch files or UNIX shell scripts such as .cshrc or .profile.
executable module— A linked object file that can be executed in a target system.
expression— A constant, a symbol, or a series of constants and symbols separated by arithmetic
operators.
external symbol— A symbol that is used in the current program module but defined or declared in a
different program module.
field— For the MSP430, a software-configurable data type whose length can be programmed to be any
value in the range of 1-16 bits.
global symbol— A symbol that is either defined in the current module and accessed in another, or
accessed in the current module but defined in another.
GROUP— An option of the SECTIONS directive that forces specified output sections to be allocated
contiguously (as a group).
hex conversion utility— A utility that converts object files into one of several standard ASCII hexadecimal
formats, suitable for loading into an EPROM programmer.
high-level language debugging— The ability of a compiler to retain symbolic and high-level language
information (such as type and function definitions) so that a debugging tool can use this
information.
hole— An area between the input sections that compose an output section that contains no code.
incremental linking— Linking files in several passes. Incremental linking is useful for large applications,
because you can partition the application, link the parts separately, and then link all of the parts
together.
partial linking— Linking files in several passes. Incremental linking is useful for large applications
because you can partition the application, link the parts separately, and then link all of the parts
together.
quiet run— An option that suppresses the normal banner and the progress information.
raw data— Executable code or initialized data in an output section.
relocation— A process in which the linker adjusts all the references to a symbol when the symbol's
address changes.
ROM width— The width (in bits) of each output file, or, more specifically, the width of a single data value
in the hex conversion utility file. The ROM width determines how the utility partitions the data into
output files. After the target words are mapped to memory words, the memory words are broken
into one or more output files. The number of output files is determined by the ROM width.
run address— The address where a section runs.
run-time-support library— A library file, rts.src, that contains the source for the run time-support
functions.
section— A relocatable block of code or data that ultimately will be contiguous with other sections in the
memory map.
section program counter (SPC)— An element that keeps track of the current location within a section;
each section has its own SPC.
sign extend— A process that fills the unused MSBs of a value with the value's sign bit.
simulator— A software development system that simulates MSP430 operation.
source file— A file that contains C/C++ code or assembly language code that is compiled or assembled to
form an object file.
static variable— A variable whose scope is confined to a function or a program. The values of static
variables are not discarded when the function or program is exited; their previous value is resumed
when the function or program is reentered.
storage class— An entry in the symbol table that indicates how to access a symbol.
string table— A table that stores symbol names that are longer than eight characters (symbol names of
eight characters or longer cannot be stored in the symbol table; instead they are stored in the string
table). The name portion of the symbol's entry points to the location of the string in the string table.
structure— A collection of one or more variables grouped together under a single name.
subsection— A relocatable block of code or data that ultimately will occupy continuous space in the
memory map. Subsections are smaller sections within larger sections. Subsections give you tighter
control of the memory map.
symbol— A string of alphanumeric characters that represents an address or a value.
symbolic debugging— The ability of a software tool to retain symbolic information that can be used by a
debugging tool such as a simulator or an emulator.
tag— An optional type name that can be assigned to a structure, union, or enumeration.
target memory— Physical memory in a system into which executable object code is loaded.
.text section— One of the default object file sections. The .text section is initialized and contains
executable code. You can use the .text directive to assemble code into the .text section.
unconfigured memory— Memory that is not defined as part of the memory map and cannot be loaded
with code or data.