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

4.3 Mechanics of a Two-Pass Assemblers                                97


         *        This program searches the array COLUMN looking for odd, negative,
         *        one-byte numbers which then are stored in array ODD. The length of
         *        COLUMN is N and the length of ODD is M, which the program calculates,
         *
                  ORG $800
        N:        DS    1
        M:        DS    1
        COLUMN: DS      50
        ODD:      DS    50
         *
                  CLR   M                  initialize M
                  LDAB N                   Put N into B
                  LDX   #COLUMN            Point X to COLUMN
                  LDY   #ODD               Point Y to ODD
        LOOP:     LDAA 1, X+              Next number of COLUMN into A
                  BPL   JUMP              Go to next number if positive
                  BITA #1                 Z = 1 if, and only if, A is even
                  BEQ   JUMP              Go to next number if even
                  STAA 1, Y+              Store odd, negative number
                  INC   M                 Increment length of ODD
         JUMP:    DBNE B, LOOP          ; Decrement counter; loop if not done
                  BGND                  ; Halt
                      Figure 4.5. Program to Select Negative Odd Numbers


        which takes two bytes in the program. We do not know the second byte of this
        instruction because we do not know the value of the address JUMP yet. (This is called a
        forward reference, using a label whose value is not yet known.) However, we can leave
        this second byte undetermined and proceed until we see that the machine code for DBNE
        is put into location $87f, thus giving JUMP the value $87f. As we continue our first
        pass downward, we allocate three bytes for DBNE B,LOOP. We do not find this
        instruction's offset yet, even though we already know the value of LOOP.
            Scanning through the program again, which is the second pass, we can fill in all the
        bytes, including those not determined the first time through, for the instructions BPL
        JUMP, BEQ JUMP, and DBNE B,LOOP. At this time, all object code can be
        generated, and the listing can be printed, to show what was generated.
            What we have described is a two-pass assembler. On the first pass it generates the
        symbol table for the program, and on the second pass it generates the machine code and
        listing for the program.
            We have been using the prefix "<" in instructions like LDAA <N or a postfix ". B"
        such as in LDAA N. B to indicate an 8- or 9-bit addressing mode. If the prefix "<" or
        postfix ". B" is omitted, the assembler will still try to use 8-bit or 9-bit addressing when
        possible. Specifically, on the first pass, if the assembler knows the value of N when the
        instruction LDAA N is encountered, it will automatically use page zero addressing if N is
        on page zero. If it does not know the value of N yet, or if N is known but is not on page
        zero, it will then use direct addressing.
   115   116   117   118   119   120   121   122   123   124   125