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

298                                    Chapter 10 Elementary Data Structures

          HUM:      EQU    100             ; Number of lists in the table    (9)
          TABLE:    DS     NUM*NBYTES      ; Allocation of table

        creates a buffer for 100 of the lists defined by (6). The address of the first byte of the
        buffer is TABLE. The following program segment searches such a table for a certain
        telephone number, which is stored (in binary) in accumulator D (low 16-bits) and index
        register X (high 16 bits). We will assume it finds a matching telephone number, which
        Is left in accumulator D (low 16-bits) and index register X (high 16 bits) when we exit.

         LOOP:    CPX      TN, Y      ; check high 16-bits of telephone number of row Y
                  BNE      NOMTC
                  CPD      TN+2, Y    ; check low 16-bits of telephone number of row Y
                  BEQ      MTCH
         NOMTC:   LEAY     NBYTES,Y ; skip to next list
                  CPY      #TABLE+NUM*NBYTES ; at end of table?
                  BNE      LOOP       ; if not, loop
         MTCH:    LDD      SSN, Y     ; get high 16-bit Social Security number of row Y
                  LDX      SSN+2 , Y ; get low 16-bit Social Security number of row Y

            This discussion has examined indexable data structures. Each element of an
        indexable data structure can be accessed, and, furthermore, some form of indexing can be
        used for the access. The simple, but very useful, vector was easy to access because the
        address of the ith element, assuming a zero origin, is obtained by adding imprecision) to
        the address of the vector. A list is like a vector but has fewer restrictions, in that
        elements can be of any precision. Arrays and tables are just mixtures of these two
        structures. These indexable structures are used often in a microcomputer like the 6812,
        because they are so easy to handle with its index addressing options and multiply
        instructions,



        10.3 Sequential Data Structures

        We now consider sequential data structures. The ubiquitous string, which you met earlier,
        and various deques, including the stack, are sequential structures. The key characteristic of
        sequential structures is that there is a current location, or top or bottom, to the structure,
        and access to the data in this structure is limited to this location.
            Strings can be variable or constant. A buffer is used to hold a variable string,
        which, in particular, can have a variable length. In a program with string manipulations,
        the length of a particular string can change in the program (up to the size of its buffer) in
        contrast to a vector of, say, 2-byte numbers that has a constant length throughout most
        programs. In C, a global constant string is declared char s [ 11 ] = "High there";.
        Most strings in C are terminated by a null character ($0); the number of bytes allocated
        for a string generally must include this extra null character at the end. In assembly
        language, constant strings can be created in memory with assembler directives like
                 s:    DC.B        "This is a string"                        (9)
   316   317   318   319   320   321   322   323   324   325   326