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

4.5 A Simplified Two-Pass Assembler                                  103


                                                               L A
                       Location Contents Mnemonics
                                                               A B
                                                               S C
                          0      03       L 3
                          1      44       A 4                A D 12
                          2      85       S 5                B D 34
                                                             C D 00
                           a. Machine code                 b. Source code
                            Figure 4.13. Machine and Source Code

            Finally, we show a pair of programs that will build and search a dictionary (of
        letters). Figure 4.12a's program segment is executed each time a letter in accumulator A
        is inserted into the dictionary. The letter has a numerical "value" associated with the
        letter in B. The program segment in Figure 4.12b searches the dictionary. On entry, a
        letter is put in A. The "value" of the letter is left in B when the segment is completed.
            These simple search programs can be fairly easily expanded for searching for longer
        strings of characters, or counting characters, rather than testing for a NULL, to determine
        when to terminate the search. Variations can handle the case where the comparand is not
        found. However, these are all linear searches in which the time to search for an item in
        the dictionary is linearly related to the number of elements in the dictionary. For large
        dictionaries, linear searches are entirely too slow; linked lists (§10.4) are much faster.
        However, linear searches are adequate for the simple assembler in §4.5.

        4.5 A Simplified Two-Pass Assembler


        An assembler is really a simple program. To illustrate how it works and to gain valuable
        experience in assembly-language techniques, we write parts of a "Simple Assembler"
        SA1 for a simple computer, with the overall specifications shown in Figure 4.14. Figure
        4.13a shows SAl's machine code for a program to add two numbers (like Figure 1.5),
        and Figure 4.13b similarly shows how its source code might appear.
            1. The target computer has only one 8-bit accumulator and 64 bytes of memory.
            2. The target computer's opcodes will be L (binary 00), A (01), and S (10), coded as
               bits 7 and 6 of the opcode byte, which have a 6-bit direct address coded in the
               low-order 6 bits of the opcode byte. The assembler has a directive D, for "define
               constant byte," that has one two-digit hexadecimal number operand.
            3. A source code line can have (1) a label and one space or else (2) two spaces. Then
               it has an opcode or assembler directive. Then it has a space and an operand,
               ending in a carriage return.
            4. The assembler is to be run on the 6812 host. Assume the source code does not
               have errors. The source code will be stored in a constant ASCII string SOURCE,
               which is null terminated; and the object code, stored in 8-byte vector OBJECT, is
               indexed using an 8-bit variable LCNTR. No listing is produced.
            5. All labels will be exactly one character long. The symbol table, stored in the 8-
               byte vector LABELS, consists of four 2-byte rows for each symbol, each row
               comprising a character followed by a one-byte address.
                 Figure 4.14. Simple Computer and Assembler SA1 Specifications
   121   122   123   124   125   126   127   128   129   130   131