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

10.2 Indexable Data Structures                                      297

                                            T
           * ZTRANS computes the transpose Z  of a 5 by 5 matrix Z of 1-byte elements,
           *        CALLING SEQUENCE:
           *        PSHX                Address of the transpose matrix ZT
           *        PSH Y               Address of the matrix Z
           *        BSR    ZTRANS
           *        LEAS 4,SP           Balance the stack
           jf:
           *        PARAMETERS
           *
           RA:      EQU    0            ; Return address
           ADDRZ: EQU      RA+2         ; Address of Z
           ADDRZT: EQU     ADDRZ+2      ; Address of ZT
           *
           ZTRANS: LDX     ADDRZ ,SP    ; First row address into X
                    LDY    ADDRZT, SP ; First column address into Y
                    LDAA #5             ; There are 5 columns in the matrix Z
           *
           STR1:    LDAB     #5         ; There are 5 elements in a column of the matrix Z
           *
           STR2 :   MOVB 5, X+, 1, Y+ ; Transfer data and move pointers to next array element
                    DBNE B, STR2        ; Count down number of elements in a column
           *
                    LEAK - 2 4, X       ; Move to next element of row 0 (back up 25-1)
                    DBNE A, STR1        ; Count down number of columns
                    RTS

                                Figure 10.4. Subroutine ZTRANS

              A table is a vector of identically structured lists. For example, one might have a
           table of lists where each list is exactly like the list example just discussed, one for each
           person in the table. In C, a table of 100 telephone number and Social Security number
           lists can be represented by struct{ char name[30 ]; long ss; char address [ 45 ];
           long phone; } t[ 100];. A search for a specific Social Security number theSS,
           putting the matching telephone number in theTel, is accomplished in the program main
           below:

               main() { long theSS, theTel; int i;
                 for(i =0 ; i <1 00; i++)
                      if(theSS — t[i].ss) break;
                      theTel = t[i].phone;
               }
           We 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. In assembly language, index
           addressing can be used to access any particular list in the table and offsets can be used, as
           done earlier, to access any particular element of the list. For instance, the directive
   315   316   317   318   319   320   321   322   323   324   325