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

10.2 Indexable Data Structures                                      293

            The data structure, as we observed in Chapter 3, affects both static and dynamic
        efficiency. Besides the compactness and speed of the program itself, the compactness of
        the data may be affected by the structure used to store it. In a small microcomputer with
         IK bytes of RAM, a program may not work at all if the data are stored in a structure that
        requires 2K bytes of RAM, but it may work if the correct data structure is used and that
        structure requires only 100 bytes of RAM. Using the right data structure can also
        improve clarity, because the techniques to access the structure for a particular program
        may be more transparent than with a less appropriate structure.


        10.2 Indexable Data Structures

        We have already used the vector data structure, which is the most common example of an
        indexable data structure. A vector is a sequence of elements (or components), each of
        which is labeled by a number called an index. The indexes of successive elements are
        successive integers, and each element is represented by the same number of bytes. The
        number of bytes representing each element of the vector is termed its precision, the
        number of elements in the vector is its length, and the index of the first element is its
        origin (or base). For example, a vector Z with N elements would usually have its
        element labeled with i denoted Z(i), where i runs between the origin and the (origin + N
        - 1). For an origin of 1, the elements are labeled Z(1), . . . , Z(N) while, for an origin of
        0, the elements are labeled Z(Q),. .., Z(N - 1). If the origin is understood, we refer to the
        element Z(i) as the ith element.
            Vectors stored in memory are stored in buffers, putting successive elements in
        successive memory locations. If the elements of the vector have more than one byte of
        precision and represent integers or addresses, we have adopted the Motorola convention
        that the elements are stored most significant byte first.
            In C programs, a vector data structure of any length is obviously handled by C
        vector notation. Zero-origin 8-bit and 16-bit precision vectors are directly handled; for
        instance, a global vector Z of N 16-bit elements is declared as int  Z[N] , and an
        element i is accessed as z [ i] . If the origin is changed, for instance to 1, then an
        element i is accessed as z {i — 1 ]. If the precision is changed, unless memory space
        is critical, the next higher precision, 8-bit or 16-bit precision, would be used.
            In assembly language, a buffer to hold vector Z is established with directive
                                    Z:   DS   20                             (1)
        With this directive, we have a buffer that will hold a vector of up to twenty 1-byte
        elements, a vector of up to ten 2-byte elements, a vector of up to five 4-byte elements,
        and so on. Although the directive (1) establishes the buffer to hold the vector, it does not
        specify the origin, precision, or the length of the vector stored in the buffer. Any element
        of a vector can be accessed and, to access the ith element, the programmer must know the
        precision and origin of the vector. For example, if global vector Z has origin 1 and 1-
        byte precision, then if i is in accumulator B, Z(i) can be loaded into A with

                                    LDX #Z        ; Point X to Z             (2)
                                    DECS          ; i-1 intoB
                                    LDAA B, X     ; Z(i) into A
   311   312   313   314   315   316   317   318   319   320   321