Page 169 - ARM 64 Bit Assembly Language
P. 169

156 Chapter 6


                             Listing 6.1 Definition of an Abstract Data Type in a C header file.

                1  #ifndef IMAGE_H
                2  #define IMAGE_H
                3  #include <stdio.h>
                4
                5  typedef unsigned char pval;
                6
                7  struct imageStruct;
                8
                9  typedef struct imageStruct Image;
                10
                11  Image *allocateImage();
                12  void freeImage(Image *image);
                13
                14  int readImage(FILE *f, Image *image);
                15  int writeImage(FILE *f, Image *image);
                16
                17  int setPixelRGB(Image *image, int row, int col, pval r, pval g, pval b);
                18  int setPixelGray(Image *image, int row, int col, pval v);
                19
                20  pixel getPixelRGB(Image *image, int row, int col);
                21  pval getPixelGray(Image *image, int row, int col);
                22
                23  #endif



                  and the set of operations that can be used on the data. Listing 6.1 gives an example of an ADT
                  interface in C. The type Image is not fully defined in the interface. This prevents client soft-
                  ware from accessing the internal structure of the image data type. Therefore, programmers
                  using the ADT can modify images only by using the provided functions. Other structured
                  programming and object-oriented programming languages such as C++, Java, Pascal, and
                  Modula 2 provide similar protection for data structures so that client code can access the data
                  structure only through the provided interface. Note that only the pval definition is exposed,
                  indicating to client programs that the red, green, and blue components of a pixel must be a
                  number between 0 and 255. In C, as with other structured programming languages, the im-
                  plementation of the subroutines can also be hidden by placing them in separate compilation
                  modules. Only the ADT implementation code will have access to the internal structure of the
                  Image data type.

                  Assembly language does not have the ability to define a data structure as such, but it does pro-
                  vide the mechanisms needed to specify the location of each field with respect to the beginning
                  of a data structure, as well as the overall size of the data structure. With a little thought and
                  effort, it is possible to implement ADTs in Assembly language.
   164   165   166   167   168   169   170   171   172   173   174