home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / m / mod2tutr.zip / CHAP14.TXT < prev    next >
Text File  |  1989-01-18  |  14KB  |  354 lines

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