Page 152 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 152

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
   147   148   149   150   151   152   153   154   155   156   157