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

296                                   Chapter 10 Elementary Data Structures

               A (two-dimensional) array is a vector whose elements are vectors, each of which
          has the same length and precision. For us, it suffices to consider an array as the usual
          two-dimensional matrix pattern of elements of the same precision, where, as before with
           vectors and lists, it is convenient to start indexing the rows and columns from 0, If we
          consider our array to be a vector of rows, the data structure is called a row major array. If
           we consider it to be a vector of columns, the data structure is called a column major
           array. In C, row-major order is used by arrays; a zero origin 1-byte precision two-
           dimensional array is declared as char AR[ 5 ] [ 5 ], and the ith row jth column element is
           designated AR{i][j ], Rows are kept together. For instance, AR[ 0 ] [ 4 ] might be
           located in memory at location $833, AR[ 1 ] [ 0 ] at location $834, and AR[ 1 ] {1 ] at
           location $835. In assembly-language programming, two dimensional n by m arrays are
           declared as a vector of n times m elements, e.g., AR DS n*m. The address of AR(ij) is
           given, by

                          address of AR(ij) = (i * 5) + j + address of AR(0,0)  (8)

          Formula (8) can easily be modified for arrays with higher-precision elements. One uses
          MUL to compute array addresses. For instance, if the precision of each element of AR is
          two bytes, as if declared in C as int AR[ 5 ] [ 5 ], and if AR is the address of the first byte
          of the array, and further if i and j are in accumulators A and B, respectively, the
           following segment puts AR(i,j) into accumulator D.

                      PSHB                   ; Save j
                      LDAB #5                ; Number of columns into B
                      MUL                    ; i * 5 into D
                      ADDB 1, SP+
                      ADCA #0                ; (i * 5) + j into D
                      ASLD                   ; 2 * ((i * 5) +j) into D
                      XGDX                   ; Put combined offset in X
                      LDD    AR,X            ; AR(ij) into D

          In this segment, multiplication by two for the contents of D is done by ASLD.
           Multiplication by powers of two can be done by repeating ASLD.
                                                            T
              Consider a program that writes into ZT the transpose Z  of a 5 by 5 matrix Z of 1-
          byte elements. The C procedure is shown below.

          void ZTRANS( char Z[5][5], char ZT[5][5] ) {char i,j;
               for{i =0 ; i < 5; i++)
                    for(j = 0; j < 5; j++)
                         ZT[i][j] = Z[j][i];
           }



          While this subroutine appears to pass its arguments by value, they are actually passed by
          name, because all vectors and arrays are passed by name. Figure 10.4 shows the
          assembly-language program that performs the same operation, but in an optimized way.
   314   315   316   317   318   319   320   321   322   323   324