home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / modula2 / mod2txt.zip / CHAP14.TXT < prev    next >
Text File  |  1987-03-25  |  14KB  |  322 lines

  1.                    Chapter 14 - Machine Dependent Facilities
  2.  
  3.  
  4.                       PREREQUISITES FOR THIS MATERIAL
  5.  
  6.              Before  attempting  to understand  this  material,  you
  7.         should  understand the material presented in Part I of  this
  8.         tutorial  and  a  clear  understanding of  the  material  on
  9.         pointers in Part II.
  10.  
  11.                    THIS IS WHERE YOU CAN GET INTO TROUBLE
  12.  
  13.              Modula-2  does  a good job of insulating you  from  the
  14.         underlying  peculiarities of your computer due to the strong
  15.         TYPE checking which it does.  It can prevent you from making
  16.         many  kinds of rather stupid blunders simply by forcing  you
  17.         to  follow  its predefined conventions.   There  are  times,
  18.         however,  when  you wish to ignore some of its help  and  do
  19.         something that is out of the ordinary.  If you had a need to
  20.         directly interface with some external device, you would need
  21.         to  get down to the nitty gritty of the operating system and
  22.         do  some  things  that are outside of the  realm  of  normal
  23.         programming  practice.   Modula-2 will allow you to do  such
  24.         things but you will pay a price because you take the  chance
  25.         of hopelessly confusing the system.
  26.  
  27.              The  principles  taught in this chapter  can  lead  you
  28.         directly  into the operating system where you will have more
  29.         freedom than you would have thought possible with  Modula-2,
  30.         but it will place more responsibility on you.  This material
  31.         is  only for the advanced programmer because it will require
  32.         a  knowledge of the inner workings of the computer  and  the
  33.         operating system.   Nevertheless,  it would be good for you,
  34.         as  a student of Modula-2,  to at least read this  material,
  35.         examine the example programs, and compile and run them.  You
  36.         will  then have a store of knowledge of these things so that
  37.         you can use them when you need them.
  38.  
  39.                           TYPE RELAXATION EXAMPLE
  40.  
  41.              Load  and display the program named TYPEREL.MOD for  an
  42.         example  of a program with some very unusual  type  transfer
  43.         functions.   Note  first that three TYPES are defined,  each
  44.         being  the same size considering storage requirements.   The
  45.         first TYPE is 10 INTEGERS,  the second is 10 CARDINALS,  and
  46.         the  third  is  20 CHAR variables which  requires  the  same
  47.         amount  of storage as 10 INTEGERS or  CARDINALS.   The  fact
  48.         that all three TYPES are the same size is very important for
  49.         what we will do later in the program.
  50.  
  51.              The first thing we do in the program part of the MODULE
  52.         is to assign a number to "Count",  a CARDINAL type variable.
  53.         In  the next line we assign the value of "Count" to  "Index"
  54.         even though they are of different TYPES because we transform
  55.  
  56.  
  57.                                    Page 89
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                    Chapter 14 - Machine Dependent Facilities
  68.  
  69.  
  70.         the  TYPE in the same manner that we did back in  Chapter  3
  71.         when  we studied TRANSFER.MOD but this time we will go a bit
  72.         farther with the transformations.   Actually,  we don't need
  73.         the  type  transformation here because INTEGER and  CARDINAL
  74.         are assignment compatible.
  75.  
  76.              We  load  up  the  INTEGER array  "IntVars"  with  some
  77.         nonsense  data to work with,  the data being the  series  of
  78.         numbers  from  65 to 74,  which should be easy  for  you  to
  79.         ascertain.   Then in line 23,  we copy one of the array data
  80.         points  to  one  of the other to illustrate  that  the  type
  81.         transformation works even on array elements.
  82.  
  83.                     NOW FOR THE BIG TYPE TRANSFORMATION
  84.  
  85.              In  line 24 of the program we copy the entire field  of
  86.         10 INTEGER type variables into the array of 10 CARDINAL type
  87.         variables.   The only restriction is that both of the fields
  88.         must be exactly the same size which these two are.  In order
  89.         to  do the transformation,  the TYPE of the  resulting  data
  90.         area  is  used  in front of the parentheses  of  the  source
  91.         variables.   Line  25 goes a step farther and copies the new
  92.         CARDINAL  data  into  20  CHAR  type  variables,   which  is
  93.         permissible  because 20 CHAR variables uses the same  amount
  94.         of  storage  as  10  CARDINAL  variables.   You  could  even
  95.         transform  a record made up of several different types  into
  96.         all  CHAR  variables,  or  all  INTEGERS,  or  even  another
  97.         completely different record.   The only requirement is  that
  98.         both of the groups be exactly the same size.
  99.  
  100.              This may appear to be a really neat thing to be able to
  101.         do  but there are problems that you will find with this  new
  102.         transformation.   There  are no data conversions done,  only
  103.         type  conversions,  which means that you may wind up with  a
  104.         real mess trying to decipher just what the transformed  data
  105.         means.   In  addition,  since  each compiler may define  the
  106.         various types of data slightly different,  your program will
  107.         not  be easily transportable to another computer,  or  maybe
  108.         not even to another compiler on the same computer.
  109.  
  110.              Five  of  the  CARDINAL numbers are  displayed  on  the
  111.         monitor,  then 10 of the CHAR numbers are displayed to  show
  112.         you that they really are the same numbers.  The order of the
  113.         numbers are reversed when output as individual bytes because
  114.         of  the way the data is stored in the microprocessor in your
  115.         computer.   This in itself is an indication that there is no
  116.         data conversion, but only a data copying, byte by byte.
  117.  
  118.              One  other  rule must be pointed out,  you cannot do  a
  119.         data transformation within a function call, but it is simple
  120.         enough to do the transformation to a dummy variable and  use
  121.  
  122.  
  123.                                    Page 90
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                    Chapter 14 - Machine Dependent Facilities
  134.  
  135.  
  136.         the   dummy  variable  in  the  function  call  if  that  is
  137.         necessary.   This will be illustrated shortly.   Compile and
  138.         run this program after you study it.
  139.  
  140.                          WORD AND ADDRESS VARIABLES
  141.  
  142.              Load and display WORDADDR.MOD for an example using some
  143.         new data types.  In order to get down to the lowest level of
  144.         the  machine,  we  need these  new  types,  ADR,  WORD,  and
  145.         ADDRESS,  which  must  be  IMPORTED from the  pseudo  module
  146.         SYSTEM.   The  pseudo  module SYSTEM does not  exist  as  an
  147.         external module as the others do because the kinds of things
  148.         it  does  are closely associated with the  compiler  itself.
  149.         The designers of Modula-2 have therefore defined this module
  150.         to make these things available to us.
  151.  
  152.              The  new data type "WORD" is compatible with  all  data
  153.         types that use a single word for storage, but it is somewhat
  154.         limited in what you can do with it. It is most useful as the
  155.         formal  argument to a function which can be called with  any
  156.         data  type that is contained in one word.   In lines 27  and
  157.         28,  the same procedure is called, once with an INTEGER, and
  158.         once  with a CARDINAL.   Since the procedure is designed  to
  159.         handle either,  it will print out both numbers by converting
  160.         them first to CARDINAL using the type transformation in line
  161.         17,  then calling the output procedures.   Once  again,  the
  162.         type  transformation cannot be done in the procedure call so
  163.         a temporary variable is used.
  164.  
  165.                            A NEW KIND OF POINTER
  166.  
  167.              The variable "Peach" is assigned the type ADDRESS which
  168.         is  also  imported from the pseudo  module  SYSTEM,  and  is
  169.         therefore a pointer to any WORD type of variable.  Peach can
  170.         therefore  point  to an INTEGER or a CARDINAL as is done  in
  171.         lines 25 and 26.  The procedure "ADR" returns the address of
  172.         any  WORD type of variable and it too must be IMPORTED  from
  173.         the pseudo module SYSTEM.
  174.  
  175.                              ABSOLUTE ADDRESSES
  176.  
  177.              Notice  the  two strange looking variables in lines  10
  178.         and 11.   The variable "MonoVideo" is an array of 4000  CHAR
  179.         type variables but we have forced it to be located at a very
  180.         specific location in memory, namely at segment=B000(hex) and
  181.         offset=0000(hex).   This  is the method provided for you  by
  182.         Modula-2  by  which  you  can force a variable to  be  at  a
  183.         specific memory location.   In this case we have defined the
  184.         variable  to be stored in the locations in memory where  the
  185.         monochrome  monitor display is stored so we can  store  data
  186.         directly into the monochrome monitor display area.
  187.  
  188.  
  189.                                    Page 91
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                    Chapter 14 - Machine Dependent Facilities
  200.  
  201.  
  202.  
  203.              The  variable "ColorVideo" is the same except that  the
  204.         location  referenced  is  that area where the output  for  a
  205.         color  monitor  is stored.   You can see that you  can  gain
  206.         control over the actual hardware with this capability but it
  207.         does  require  a lot of knowledge of the  hardware  and  the
  208.         operating system.
  209.  
  210.              In the last line of the program the variable "Peach" is
  211.         assigned   the   address  of  a  specific  location  as   an
  212.         illustration only.  This is only possible because "Peach" is
  213.         a variable of type ADDRESS.
  214.  
  215.              It should be clear to you that with these functions, it
  216.         is  possible  to do a lot of data shuffling that  could  not
  217.         otherwise be done.  The next example program will illustrate
  218.         their use further.
  219.  
  220.                           MORE ADDRESSING EXAMPLES
  221.  
  222.              Load  and  display  the  program  ADRSTUFF.MOD.    This
  223.         program  uses  the ADDRESS type and  adds  two  new,  rather
  224.         simple functions,  SIZE and TSIZE.   Actually, these are not
  225.         completly  new  since  we used the  TSIZE  function  in  the
  226.         chapter  on  pointers  and dynamic  allocation.   These  two
  227.         functions  will return the size in bytes of any variable  or
  228.         of any type.   The program on your monitor has several types
  229.         defined, then several variables, and finally initializes all
  230.         of the elements of the array "Stuff" to some nonsense  data.
  231.         The really interesting things begin happening at line 25.
  232.  
  233.              The pointer "NeatPoint" is pointed at the first element
  234.         of  the  array "Stuff",  and its value is dereferenced  into
  235.         "Index".   The  type transformation is required because  the
  236.         result  of  the dereferencing is a CARDINAL.   The  data  is
  237.         written  out.   Next  the  size of the  type  "IntArray"  is
  238.         assigned to the variable "IncreAmt", which should be 8 words
  239.         or  16 bytes.   In line 29 we do some pointer arithmetic  by
  240.         adding the size of the type "IntArray" to the original value
  241.         of  the pointer which should cause it to point to  the  next
  242.         row  of  the  array.   After dereferencing the  pointer  and
  243.         getting its new value,  we print it out to find that it  did
  244.         indeed move to the next row of the array.
  245.  
  246.              Based on the above discussion, it should be apparent to
  247.         you that you can move the pointer all around the array named
  248.         "Stuff"  and get whatever data you wish.   The next  section
  249.         uses  a loop to continue the process through all five  rows.
  250.         The only thing that may be confusing is line number 34 where
  251.         we get the size of the "BigArray" type and divide it by  the
  252.         size  of the "IntArray" type.   The result should be 5,  and
  253.  
  254.  
  255.                                    Page 92
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                    Chapter 14 - Machine Dependent Facilities
  266.  
  267.  
  268.         you will see that it does five iterations through the  loop.
  269.         This  is  really a dumb way to get through  this  particular
  270.         loop  but it is only for purposes of illustration that it is
  271.         done.   Notice all of the type transformations to INTEGER in
  272.         these statements, this is because the functions all return a
  273.         CARDINAL  type  of  data.   Doing all of  this  in  CARDINAL
  274.         numbers would have made it much cleaner,  but this was  more
  275.         illustrative for you.
  276.  
  277.                        TWO MORE LINES OF ILLUSTRATION
  278.  
  279.              Lines 42 and 43 are given as an illustration for you of
  280.         how to use the SIZE function.  It simply returns the size in
  281.         bytes, of any variable used as an argument.
  282.  
  283.         PROGRAMMING EXERCISES
  284.  
  285.         1.   Modify  the "AdrStuff" module to print out some of  the
  286.              type  and  variable sizes such as those  calculated  in
  287.              lines 41 and 42.
  288.  
  289.         2.   Write a program with an array of 100 CARDINAL elements,
  290.              fill the elements with nonsense data, and use a pointer
  291.              to  print out every 12th value starting at the  highest
  292.              element (number 100) and working downward.
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.                                    Page 93
  322.