home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / MISC / TNH_PC.ZIP / NIEHOFF.NL < prev    next >
Encoding:
Text File  |  1987-01-14  |  7.4 KB  |  269 lines

  1. Back to BASICs
  2.  
  3.              Phil Niehoff
  4.     North East Indiana IBM PC Club
  5.  
  6. This article discusses the COMMON
  7. statement and methods of implementing
  8. it with the BASIC COMPILER.
  9.  
  10. To understand the need for the COMMON
  11. statement, we must look at the
  12. internal architecture of the 8088.
  13. The 8088 is the "brains" behind the
  14. PC. It segments the PC's memory into
  15. portions of 64K: therefore, when
  16. BASIC is loaded, it is limited to one
  17. of these segments. Consequently no
  18. given BASIC program can exceed 64K,
  19. which creates the need for the COMMON
  20. statement. This statement allows a
  21. programmer to create two or more
  22. BASIC programs and pass variables
  23. between them. The COMMON statement
  24. must use its companion statement,
  25. CHAIN, which is the command that
  26. allows one BASIC program to call
  27. another and pass variables between
  28. them while leaving files open.
  29.  
  30. The following two programs may be
  31. entered either under BASIC or BASICA.
  32. Enter these two programs and save
  33. them on a diskette.  The first
  34. program should be named TEST1.BAS and
  35. the second TEST2.BAS.
  36.  
  37. 10  'TEST1.BAS
  38. 20  COMMON X,Y
  39. 30  X=1
  40. 40  Y=2
  41. 50  CHAIN "TEST2.BAS"
  42. 60  END
  43. ====================================
  44. 10  'TEST2.BAS
  45. 20  '
  46. 30  PRINT X
  47. 40  PRINT Y
  48. 50  '
  49. 60  END
  50.  
  51. When TEST1.BAS is run, it will set up
  52. X and Y as COMMON variables (the X
  53. and Y are assigned values). The CHAIN
  54. statement passes program control to
  55. the second program, TEST2.BAS.
  56.  
  57. Any variable type may be passed with
  58. the COMMON statement, but it must be
  59. listed in the statement. Arrays are
  60. passed by including the array name
  61. after the COMMON statement and
  62. followed by empty brackets.  Thus,
  63. COMMON A () would pass an array
  64. called A.
  65.  
  66. In interpreted BASIC, the COMMON
  67. statement can be placed anywhere in
  68. the program as long as it is
  69. encountered before the CHAIN
  70. statement is executed. Only the
  71. program being CHAINed FROM requires a
  72. COMMON statement; the program being
  73. CHAINed TO does not need a COMMON
  74. statement unless it CHAINs to another
  75. program.
  76.  
  77. When the BASIC program is to be
  78. compiled, the COMMON statement must
  79. appear in the program before any
  80. executable statements. The only non
  81. executable statements are:
  82.  
  83.       COMMON
  84.       DEFtype
  85.       DIM
  86.       OPTION BASE
  87.       REM
  88.       all compiler metacommands
  89.  
  90. The program will not compile if the
  91. COMMON statement is placed anywhere
  92. else in the program. If there are any
  93. variables in the COMMON statement
  94. whose types are defined by a DEFtype
  95. and DIM statements must precede the
  96. COMMON statements at the beginning of
  97. a program, even if it will not be
  98. compiled immediately. It will save
  99. work afterwards if the program winds
  100. up being compiled.
  101.  
  102. Also, when a program is being
  103. compiled, both the program being
  104. CHAINed TO and the program being
  105. CHAINed FROM require a COMMON
  106. statement. Both statements MUST have
  107. the variables listed in the same
  108. order, and the common variables must
  109. be common to all programs being
  110. CHAINed to! Using COMMON statements
  111. in compiled BASIC also requires use
  112. of the Runtime Module of the BASIC
  113. COMPILER.
  114.  
  115. As I said earlier, the COMMON
  116. statement is meaningless unless it is
  117. used in conjunction with the CHAIN
  118. statement. The CHAIN statement, as
  119. used in the above sample programs, is
  120. fairly straight-forward.  Note that
  121. when the file name is given, the .BAS
  122. extension need not be used. As a
  123. matter of fact, if the program will
  124. chain only to other files with
  125. extension .BAS, it is preferable not
  126. to include the extension.
  127.  
  128. The major reason for this position is
  129. that when the program is being
  130. debugged in BASIC, the default
  131. extension is assumed to be .EXE.
  132. Thus, elimination of the extension
  133. saves programming time later.
  134.  
  135. The CHAIN statement would be a
  136. relatively easy command to use if its
  137. only use was the simple transfer of
  138. control from one program to another,
  139. as shown in the sample programs. By
  140. now, however, most of you probably
  141. have found that programming a
  142. micro-computer is not easy.
  143.  
  144. In the above sample programs, if line
  145. 50 in TEST1.BAS read
  146.  
  147. 50  CHAIN  "TEST2.BAS",40
  148.  
  149. when program control was passed to
  150. TEST2.BAS, statement execution would
  151. have begun at line 40. This is a nice
  152. feature; however, the BASIC COMPILER
  153. does not support this function. How
  154. does one perform a similar function?
  155. The answer is relatively simple. The
  156. following examples should help to
  157. illustrate it.
  158.  
  159. 10  'TEST1.BAS
  160. 20  COMMON X,Y,CHAINING$
  161. 25  CHAINING$="Y"
  162. 30  X=1
  163. 40  Y=2
  164. 50  CHAIN "TEST2.BAS"
  165. 60  END
  166. ====================================
  167. 10  'TEST2.BAS
  168. 20  '
  169. 25  IF CHAINING$="Y" GOTO 40
  170. 30  PRINT X
  171. 40  PRINT Y
  172. 50  '
  173. 60  END
  174.  
  175. By creating another variable called
  176. CHAINING$ and giving it a value of
  177. "Y", we are able to test for this
  178. variable in the second program. If
  179. the value is a "Y", then we are able
  180. to perform the same function. I
  181. usually use the variable CHAINING$,
  182. although any variable will do.
  183.  
  184. Another option on the CHAIN statement
  185. is the ALL function. In the above
  186. example programs, if line 50 in
  187. TEST1.BAS read:
  188.  
  189.       50  CHAIN "TEST2.BAS",ALL
  190.  
  191. all the variable values would pass
  192. from one program to the next. A
  193. COMMON statement would not be
  194. required at all. Again, this is one
  195. of the features that the BASIC
  196. COMPILER does not support. The only
  197. means to pass all variables in a
  198. compiled program is to include them
  199. in a COMMON statement.
  200.  
  201. The final options on the CHAIN
  202. statement are MERGE and DELETE, and
  203. they are used in conjunction with
  204. each other. The MERGE option allows
  205. the original BASIC program to stay
  206. resident while the second BASIC
  207. program merges with the first.  The
  208. DELETE feature is used to delete any
  209. line numbers that may be in that
  210. portion of the program where the
  211. second program is to be merged. These
  212. features of the CHAIN statement were
  213. used in the original version of the
  214. MUSIC.BAS program on the DOS 1.0
  215. diskette. Perhaps an example will
  216. help at this point. We will begin
  217. with our original TEST1.BAS and
  218. TEST2.BAS programs; however, we will
  219. modify them slightly:
  220.  
  221. 10  'TEST1.BAS
  222. 20  COMMON X, Y
  223. 30  X=1
  224. 40  Y=2
  225. 50  CHAIN MERGE "TEST2.BAS", DELETE
  226.      50-60
  227. 60  END
  228. ====================================
  229. 60  'TEST2.BAS
  230. 70  '
  231. 80  PRINT X
  232. 90  PRINT Y
  233. 100 END
  234.  
  235. When TEST1.BAS is run, lines 50 and
  236. 60 will be deleted and TEST2.BAS will
  237. be MERGEd into TEST1.BAS.    The two
  238. programs will effectively become one.
  239. Several points should be made here:
  240.  
  241. 1.  These features will not work with
  242.     the BASIC COMPILER.
  243. 2.  The line numbers specified in the
  244.     DELETE must actually exist in the
  245.     program or an "Illegal Function
  246.     Call" error will result.
  247. 3.  The program to be merged must be
  248.     saved in ASCII (i.e., SAVE
  249.     filename, A.  This means that the
  250.     program will take more room on
  251.     the disk than usual, and it will
  252.     also load more slowly.)
  253. 4.  The MERGE feature preserves the
  254.     current OPTION BASE setting.
  255. 5.  If the DELETE option is omitted,
  256.     any statements in the area where
  257.     the second program, will remain
  258.     after MERGing.  This may be
  259.     desired in some cases but could
  260.     cause problems in other cases.
  261.  
  262. From the above discussion, it should
  263. be apparent that the effective use of
  264. the COMMON and CHAIN statements can
  265. circumvent the 64K program segment
  266. problem in BASIC(A), and with a
  267. little more programming, it also can
  268. be circumvented in compiled BASIC.
  269.