Page 148 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 148
5.3 Conditional Assemblers 125
The assembler begins program segments with either an ORG statement, introduced
in the previous chapter, or a SECTION statement, introduced above. Another way of
looking at this: SECTION is an alternative to ORG. Sections beginning with ORG are
called absolute sections; each section runs until the next ORG, SECTION, or END
directive or the end of the file. The programmer is responsible for ensuring that absolute
sections, and memory segments, declared in the parameter file, do not overlap.
The linking loader, in a manner similar to a two-pass assembler, takes the
parameter file and the list of external and entry symbols declared in XREF and XDEF
declarations; it calculates the addresses needed at load time, and inserts these into the
machine code in a .o file that was generated by the assembler. In this example, the 2-byte
relative address for FUN in the instruction above is determined and then inserts the code
for the instruction. The linker output file, a .abs file, is ready to be downloaded into a
target microcontroller.
5.3 Conditional Assemblers
A near cousin of the assembler, that we examined in the last chapter, is the conditional
assembler. A conditional assembler allows the use of conditional directives such as
IFEQ and ENDC, as listed in Table 5.3. For example, the segment
IFEQ MODE
LDAA #1
STAA LOCI
ENDC
inserted in an assembly language program causes the assembler to include instructions
LDAA #1
STAA LOCI
in the program if the value of MODE is equal to zero. If the value of MODE is not equal to
zero, assembler directives in lines from the IFEQ directive to the ENDC directive, except
ENDC and ELSE, are ignored and do not generate machine code. The label MODE is
usually defined, often at the beginning of the program, through an EQU directive, say
MODE EQU 1. There are often several conditional directives such as IFEQ MODE
throughout the program, for a single directive such as MODE EQU 1. The single EQU
directive uniformly governs all of these conditional directives. This way, a directive at
the beginning of the program can control the assembly of several program segments
throughout the program.
The conditional statement argument can be a more complex expression. There are
other conditional directives — IFNE, IFGE, IFGT, IFLE, IFLT, and IFNE—
that can be used instead of IFEQ, and the ELSE statement can cause code to be
assembled if the condition is false, so code immediately after the conditional is not
generated. And conditional expressions can be nested. For instance, the program segment