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

5.4 Macro Assemblers                                                1.27

        5.4 Macro Assemblers

        A macro assembler is able to generate a program segment, which is defined by a
        macro, when the name of the macro appears as an opcode in a program. The macro
        assembler is still capable of regular assembler functioning, generating a machine
        Instruction for each line of assembly language code; but like a compiler, it can generate
        many machine instructions from one line of source code. Its instruction set can be
        expanded to include new mnemonics, which generate these program segments of machine
        code. The following discussion of how a macro works will show how this can be done,
             A frequently used program segment can be written just once, in the macro
        definition at the beginning of a program. For example, the macro
                              AAX:     MACRO
                                       EXG       A,B
                                       ABX
                                       EXG       A,B
                                       ENDM
        allows the programmer to use the single mnemonic AAX to generate the sequence

                                       EXG       A,B
                                       ABX
                                       EXG       A,B
        The assembler will insert this sequence each time the macro AAX is written in the
        assembler source code. If the mnemonic AAX is used ten times in the program, the three
        instructions above will be inserted into the program each time the mnemonic AAX is
        used. The advantage is clear. The programmer almost has a new instruction that adds the
        unsigned contents of A to X, and he or she can use it like the real instruction ABX that
        actually exists in the machine. The general form of a macro is
                              label    MACRO
                                       instructions
                                       ENDM

        Here the symbolic name "label" that appears in the label field of the directive is the name
        of the macro. It must not be the same as an instruction mnemonic or an assembler
        directive. The phrases MACRO and ENDM are assembler directives indicating the start and
        the end of the macro. See Table 5.4. Both appear in the operation field of the instruction.

                                 Table 5.4. Macro Directive
   145   146   147   148   149   150   151   152   153   154   155