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

10.2 Indexable Data Structures                                      295


        whose precisions are 30,4,45, and 4, respectively. In C, a list is conveniently handled
        by a struct. The list above can be represented by struct{ char name [ 30 ]; long ss;
        char address [45]; long phone; } s;. The phone element of struct s is indicated
        by the notation s.phone. The assembly-language implementation of lists is simple.
        Assuming that the label L is used for the address of the first byte of the list, we can load
        the jth byte of L2 (the address) into A with the sequence

                                    LDX #L+34     ; Point X to L2            (5)
                                    LDAA A, X     ; jth byte of L2 into A

        where we have also assumed that the value of j is initially in accumulator A. While the
        segment (5) seems simple enough, remember that we have had to compute the proper
        offset to add to L in order to point X to L2. For simple lists, such as this example, this
        is not much of a problem, and the programmer may elect to do it "in his (or her) head."
        But the assembler can help. For our example list, we can create labels for the offsets to
        avoid remembering the sizes of each element.
          NAME:     EQU    0               ; Name of person                  (6)
          SSN:      EQU    NAME+30         ; Social Security number
          ADDRE SS:EQU     SSN+4           ; Address of person
          TN:       EQU ADDRESS+45         ; Telephone number
          NBYTES: EQU      TN+4            ; Number of bytes in the list

        In the following program segment, a telephone number is stored in accumulator D (high
        16 bits) and index register X (low 16 bits). If the list L's telephone number matches this
        D:X, put the Social Security number in D:X, otherwise go to label NoMatch:

              CMPD    L+TN      ; check high 16-bits of telephone number     (7)
              BNE     NoMatch
              CPX     L+TN+2    ; check low 16-bits of telephone number
              BNE     NoMatch
              LDD     L+SSN     ; get high 16-bits of Social Security number
              LDX     L+SSN+2 ; get low 16-bits of Social Security number

        Notice that with the EQU directives of (6) the program segment (7) becomes much more
        self-documenting. What makes this technique work is that it is easy to associate labels
        with attributes, particularly because the order of the list elements is usually unimportant.
        Notice that the EQU statements (6) not only let the programmer use labels for offsets,
        but also let the assembler calculate the offsets for the programmer. This same example
        will be continued for the description of a table, which is a vector of lists. However, we
        will first discuss an array, which is a bit simpler.
                      AR(0,0) ,    AR(0,1),      AR(0,2),.. .
                      AR(1,0),     AR(1,1),      AR(1,2),.. .
                      AR(2,0) ,    AR(2,1),      AR(2,2) r .. .

                                   Figure 103. An Array
   313   314   315   316   317   318   319   320   321   322   323