Chapter 4 Assembler Rules and Directives
4.4.1 Defining a Block of Data or Code
As you create code, particularly compiled code from C programs, the tools will need to be told how to treat all the different parts of it—data sections, program sections,
blocks of coefficients, etc. These sections, which are indivisible and named, then get manipulated by the linker and ultimately end up in the correct type of memory in a system. For example, data, which could be read-write information, could get stored in RAM, as opposed to the program code which might end up in Flash memory.
Normally you will have separate sections for your program and your data, especially in larger programs. Blocks of coefficients or tables can be placed in a section of their own. Since the two main tool sets that we’ll use throughout the book do things in very different ways, both formats are presented below.
4.4.1.1 Keil Tools
You tell the assembler to begin a new code or data section using the AREA directive, which has the following syntax:
AREA sectionname{,attr}{,attr}…
where sectionname is the name that the section is to be given. Sections can be given almost any name, but if you start a section name with a digit, it must be enclosed in bars, e.g., |1_DataArea|; otherwise, the assembler reports a missing section name error. There are some names you cannot use, such as |.text|, since this is used by the C compiler (but it would be a rather odd name to pick at random).
Your code must have at least one AREA directive in it, which you’ll usually find in the first few lines of a program. Table 4.3 shows some of the attributes that are available, but a full list can be found in the RealView Assembler User Guide in the Keil tools.
EXAMPLE 4.1
The following example defines a read-only code section named Example.
AREA Example,CODE,READONLY ; An example code section.
; code
TABLE 4.2
Frequently Used Directives
Keil Directive CCS Directive Uses
AREA .sect Defines a block of code or data
RN .asg Can be used to associate a register with a name
EQU .equ Equates a symbol to a numeric constant
ENTRY Declares an entry point to your program
DCB, DCW, DCD .byte, .half, .word Allocates memory and specifies initial runtime contents ALIGN .align Aligns data or code to a particular memory boundary SPACE .space Reserves a zeroed block of memory of a particular size
LTORG Assigns the starting point of a literal pool
END .end Designates the end of a source file
4.4.1.2 Code Composer Studio Tools
It’s often helpful to break up large assembly files into sections, e.g., creating a sepa- rate section for large data sets or blocks of coefficients. In fact, the TI assembler has directives to address similar concepts. Table 4.4 shows some of the directives used to create sections.
The .sect directive is similar to the AREA directive in that you use it to create an initialized section, to put either your code or some initialized data there. Sections can be made read-only or read-write, just as with Keil tools. You can make as many sections as you like; however, it is usually best to make only as many as needed. An example of a section of data called Coefficients might look like
.sect “Coefficients”
.float 0.05 .float 2.03 .word 0AAh
The default section is the .text section, which is where your assembly program will normally sit, and in fact, you can create it either by saying
.sect “.text”
TABLE 4.3
Valid Section Attributes (Keil Tools)
ALIGN = expr This aligns a section on a 2expr-byte boundary (note that this is different from the ALIGN directive); e.g., if expr = 10, then the section is aligned to a 1KB boundary.
CODE The section is machine code (READONLY is the default) DATA The section is data (READWRITE is the default)
READONLY The section can be placed in read-only memory (default for sections of CODE) READWRITE The section can be placed in read-write memory (default for sections of DATA)
TABLE 4.4
TI Assembler Section Directives
Directive Use
Uninitialized sections .bss Reserves space in the .bss section
.usect Reserves space in a specified uninitialized named section Initialized sections .text The default section where the compiler places code
.data Normally used for pre-initialized variables or tables .sect Defines a named section similar to the default .text and
.data sections
or by simply typing .text
Anything after this will be placed in the .text section. As we’ll see in Chapter 5 for both the Keil tools and the Code Composer Studio tools, there is a linker com- mand file and a memory map that determines where all of these sections ultimately end up in memory. As with most silicon vendors, TI ships a default linker command file for their MCUs, so you shouldn’t need to modify anything to get up and running.