Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Introduction to Microcontrollers. Architecture, Programming, and Interfacing of the Motorola 68HC12 (G.J. Lipovski, 1999).pdf
Скачиваний:
191
Добавлен:
12.08.2013
Размер:
29.57 Mб
Скачать

5

Advanced Assemblers,

Linkers, and Loaders

This chapter discusses the advanced assembler and the linker, which are tools needed to assemble large programs; it is written for the reader who intends to write a lot of assembly-language programs or large assembly-language programs. Whereas the last chapter gave sufficient detail for the reader to understand the assembler output of a C compiler and to embed a limited amount of assembly language code in a C procedure, this chapter provides additional tools and greater depth to enable you to write large assembly-language programs using a relocatable, conditional, or macro assembler, and to join together separately assembled programs using a linker program.

This chapter is optional. Current economics leads to writing programs in a highlevel language like C or C++ rather than in assembly language. Most programmers will not need to write a lot of programs in assembly language nor to write large programs in assembly language. Such readers can skip this chapter without losing any background needed for the rest of this book.

The first section introduces the complementary ideas of cross assembler and downloader. The next section describes the pair of ideas of relocatable assembler and linker program. Section 5.3 discusses how conditional assembly is used. The next section shows the power of macros in assembly-language programs. A final section recommends good documentation standards for programs written in assembly language.

Upon completion of this chapter, the reader should understand the tools needed for writing a large number of assembly-language programs or large assembly-language programs. He or she should have little difficulty writing assembly-language programs in the order of a couple of hundred lines long.

5.1 Cross Assemblers and Downloaders

In this section we introduce a close cousin of the assembler, the cross-assembler, which, like the assembler, converts sequences of (ASCII) characters into machine instructions. For the most part, this section's material is descriptive, almost philosophical, rather than precise and practical. It is important general knowledge, and it is included here because the reader should understand what he or she is doing when using a personal computer to assemble a program for a microcontroller.

119

5,4 Macro Assemblers

 

129

ADD:

MACRO

 

 

LDX

\1

 

LDAB

\2

 

CLRA

 

\@:

ADDA

1,X+

 

DBNE

B, \ @

 

ENDM

 

Figure 5.6. Loop Macro to Add Consecutive Values

letters to generate unique labels inside the macro itself. This capability is especially useful if a macro expansion has two or more "goto" labels in it; for without it, unambiguous labels could not be generated. Using the macro invocation number, each macro expansion generates labels that are different from the labels generated from the same macro that are expanded at a different time. For example, the macro in Figure 5.6, when implemented by ADD #M,N, adds the contents of the N bytes beginning in location M, putting the result in accumulator A.

A macro definition can use a macro defined earlier, or even itself (recursively). For macro assemblers that have conditional assembly, conditional directives within their definition can be used to control the expansion. The actual parameters of the macro can then be tested with these directives to determine how the macro is expanded. In particular, the IFC and IFNC directives can be used to compare a macro parameter, considered as a string, against any constant string. If a parameter is missing, it is a null string "\0." We can compare a given parameter, such as the second parameter, considered as a string denoted "\2," to a null string "\0." When the strings are equal, the second parameter is missing, so this condition can terminate the macro expansion.

The example in Figure 5.7 illustrates the use of recursion, conditional assembly, and early exiting of macros using MEXIT. You might want to use the ADDA instruction with a series of arguments, to add several bytes to accumulator A. If you wish, you can use a text editor to make copies of the ADDA instruction with different arguments. However, you can define a macro whose name is ADDQ, in which the body of the macro expands into one or more ADDA directives to implement the same effect. This ADDQ macro uses recursion to add one parameter at a time, up to eight parameters in this simple example, stopping when a parameter is missing (null string). When a null (missing) parameter is encountered, the macro "exits" by executing MEXIT, thereby not generating any more expansion or code.

ADDQ: MACRO

IFNC "\1",""

ADDA \1

ENDC

IFC "\2",""

MEXIT

ENDC

ADDQ \2,\3,\4,\5,\6,\7,\8

ENDM

Figure 5.7. Recursive Macro to Add up to Eight Values