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

98                                Chapter 4 Assembly Language Programming


            We have also been using the inequality symbols with index addressing to indicate
        whether the constant offset is to be described with eight or sixteen bits. The 6812
        actually has another choice for the offset that we have not discussed before now because
        there is no special symbol for it. This is the 5-bit offset option. In this case, one can
        actually squeeze the offset into the post byte as described in the instruction set summary
        of the CPU12RG/D manual. The assembler chooses between the three offset options in
        exactly the same way that it chooses between page zero and direct addressing. On the first
        pass, if it knows the values in all the labels used in an expression for the offset, it will
        choose the shortest possible offset option or, if the expression is zero, it will take the
        zero offset option, which is pointer addressing. If it does not know some of the labels
        used in the expression for the offset, the assembler will default to the 16-bit offset
        option, determining these bytes on the second pass. From now on, we will drop the use
        of inequality signs in all addressing modes, except where it is needed in the relative mode
        to designate a short forward reference. Generally, it is best to let the assembler choose the
        appropriate option.
            We sometimes observe an error message "phasing error," or "labels changed values
        on second pass." Such an error occurs when an instruction's length is computed on the
        first pass but is computed to have a different value on the second pass. Following such
        an instruction, successive labels will have a different value on the second pass than they
        had on the first pass. To fix such an error, read backward from the first instruction with
        the line that has such an error until you see an instruction whose length changes in. the
        second pass, due to its using a different addressing mode. Put a prefix or suffix on its
        operand to force the instruction's first pass length to its second pass length,
                                   ORG       $800
                            K:     DS       M
                            M:     EQU       2
                             *
                                   LDD      K
                                   ADDD      #3
                                   STD      K
                                   SWI
                       Figure 4.6. Program with Illegal Forward Reference
            As we have discussed earlier, an assembler does several things for us. It allows us to
        use instruction mnemonics, labels for variable locations, and labels for instruction
        locations while still providing the machine code for our program. As Figure 4.6 shows,
        however, we must be careful with forward references when using assembler directives.
            The assembler reads the assembly-language program in Figure 4.6 twice. In pass
        one, the symbol table is generated and, in pass two, the symbol table, the instruction
        set, and assembler directive tables are used to produce the machine code and assembly
        listing. On each pass, each line of assembly language is processed before going to the
        next line so that some undetermined labels may be determined on the second pass. For
        example, in the program in Figure 4.6 the assembler will not determine the length of M
        on the first pass because the DS directive makes a forward reference to M, that is, uses a
        symbol in the expression for K that has not been determined yet. Suppose now that we
        change the program a little bit. See Figure 4.7.
   116   117   118   119   120   121   122   123   124   125   126