home *** CD-ROM | disk | FTP | other *** search
- Back to BASICs
-
- Phil Niehoff
- North East Indiana IBM PC Club
-
- This article discusses the COMMON
- statement and methods of implementing
- it with the BASIC COMPILER.
-
- To understand the need for the COMMON
- statement, we must look at the
- internal architecture of the 8088.
- The 8088 is the "brains" behind the
- PC. It segments the PC's memory into
- portions of 64K: therefore, when
- BASIC is loaded, it is limited to one
- of these segments. Consequently no
- given BASIC program can exceed 64K,
- which creates the need for the COMMON
- statement. This statement allows a
- programmer to create two or more
- BASIC programs and pass variables
- between them. The COMMON statement
- must use its companion statement,
- CHAIN, which is the command that
- allows one BASIC program to call
- another and pass variables between
- them while leaving files open.
-
- The following two programs may be
- entered either under BASIC or BASICA.
- Enter these two programs and save
- them on a diskette. The first
- program should be named TEST1.BAS and
- the second TEST2.BAS.
-
- 10 'TEST1.BAS
- 20 COMMON X,Y
- 30 X=1
- 40 Y=2
- 50 CHAIN "TEST2.BAS"
- 60 END
- ====================================
- 10 'TEST2.BAS
- 20 '
- 30 PRINT X
- 40 PRINT Y
- 50 '
- 60 END
-
- When TEST1.BAS is run, it will set up
- X and Y as COMMON variables (the X
- and Y are assigned values). The CHAIN
- statement passes program control to
- the second program, TEST2.BAS.
-
- Any variable type may be passed with
- the COMMON statement, but it must be
- listed in the statement. Arrays are
- passed by including the array name
- after the COMMON statement and
- followed by empty brackets. Thus,
- COMMON A () would pass an array
- called A.
-
- In interpreted BASIC, the COMMON
- statement can be placed anywhere in
- the program as long as it is
- encountered before the CHAIN
- statement is executed. Only the
- program being CHAINed FROM requires a
- COMMON statement; the program being
- CHAINed TO does not need a COMMON
- statement unless it CHAINs to another
- program.
-
- When the BASIC program is to be
- compiled, the COMMON statement must
- appear in the program before any
- executable statements. The only non
- executable statements are:
-
- COMMON
- DEFtype
- DIM
- OPTION BASE
- REM
- all compiler metacommands
-
- The program will not compile if the
- COMMON statement is placed anywhere
- else in the program. If there are any
- variables in the COMMON statement
- whose types are defined by a DEFtype
- and DIM statements must precede the
- COMMON statements at the beginning of
- a program, even if it will not be
- compiled immediately. It will save
- work afterwards if the program winds
- up being compiled.
-
- Also, when a program is being
- compiled, both the program being
- CHAINed TO and the program being
- CHAINed FROM require a COMMON
- statement. Both statements MUST have
- the variables listed in the same
- order, and the common variables must
- be common to all programs being
- CHAINed to! Using COMMON statements
- in compiled BASIC also requires use
- of the Runtime Module of the BASIC
- COMPILER.
-
- As I said earlier, the COMMON
- statement is meaningless unless it is
- used in conjunction with the CHAIN
- statement. The CHAIN statement, as
- used in the above sample programs, is
- fairly straight-forward. Note that
- when the file name is given, the .BAS
- extension need not be used. As a
- matter of fact, if the program will
- chain only to other files with
- extension .BAS, it is preferable not
- to include the extension.
-
- The major reason for this position is
- that when the program is being
- debugged in BASIC, the default
- extension is assumed to be .EXE.
- Thus, elimination of the extension
- saves programming time later.
-
- The CHAIN statement would be a
- relatively easy command to use if its
- only use was the simple transfer of
- control from one program to another,
- as shown in the sample programs. By
- now, however, most of you probably
- have found that programming a
- micro-computer is not easy.
-
- In the above sample programs, if line
- 50 in TEST1.BAS read
-
- 50 CHAIN "TEST2.BAS",40
-
- when program control was passed to
- TEST2.BAS, statement execution would
- have begun at line 40. This is a nice
- feature; however, the BASIC COMPILER
- does not support this function. How
- does one perform a similar function?
- The answer is relatively simple. The
- following examples should help to
- illustrate it.
-
- 10 'TEST1.BAS
- 20 COMMON X,Y,CHAINING$
- 25 CHAINING$="Y"
- 30 X=1
- 40 Y=2
- 50 CHAIN "TEST2.BAS"
- 60 END
- ====================================
- 10 'TEST2.BAS
- 20 '
- 25 IF CHAINING$="Y" GOTO 40
- 30 PRINT X
- 40 PRINT Y
- 50 '
- 60 END
-
- By creating another variable called
- CHAINING$ and giving it a value of
- "Y", we are able to test for this
- variable in the second program. If
- the value is a "Y", then we are able
- to perform the same function. I
- usually use the variable CHAINING$,
- although any variable will do.
-
- Another option on the CHAIN statement
- is the ALL function. In the above
- example programs, if line 50 in
- TEST1.BAS read:
-
- 50 CHAIN "TEST2.BAS",ALL
-
- all the variable values would pass
- from one program to the next. A
- COMMON statement would not be
- required at all. Again, this is one
- of the features that the BASIC
- COMPILER does not support. The only
- means to pass all variables in a
- compiled program is to include them
- in a COMMON statement.
-
- The final options on the CHAIN
- statement are MERGE and DELETE, and
- they are used in conjunction with
- each other. The MERGE option allows
- the original BASIC program to stay
- resident while the second BASIC
- program merges with the first. The
- DELETE feature is used to delete any
- line numbers that may be in that
- portion of the program where the
- second program is to be merged. These
- features of the CHAIN statement were
- used in the original version of the
- MUSIC.BAS program on the DOS 1.0
- diskette. Perhaps an example will
- help at this point. We will begin
- with our original TEST1.BAS and
- TEST2.BAS programs; however, we will
- modify them slightly:
-
- 10 'TEST1.BAS
- 20 COMMON X, Y
- 30 X=1
- 40 Y=2
- 50 CHAIN MERGE "TEST2.BAS", DELETE
- 50-60
- 60 END
- ====================================
- 60 'TEST2.BAS
- 70 '
- 80 PRINT X
- 90 PRINT Y
- 100 END
-
- When TEST1.BAS is run, lines 50 and
- 60 will be deleted and TEST2.BAS will
- be MERGEd into TEST1.BAS. The two
- programs will effectively become one.
- Several points should be made here:
-
- 1. These features will not work with
- the BASIC COMPILER.
- 2. The line numbers specified in the
- DELETE must actually exist in the
- program or an "Illegal Function
- Call" error will result.
- 3. The program to be merged must be
- saved in ASCII (i.e., SAVE
- filename, A. This means that the
- program will take more room on
- the disk than usual, and it will
- also load more slowly.)
- 4. The MERGE feature preserves the
- current OPTION BASE setting.
- 5. If the DELETE option is omitted,
- any statements in the area where
- the second program, will remain
- after MERGing. This may be
- desired in some cases but could
- cause problems in other cases.
-
- From the above discussion, it should
- be apparent that the effective use of
- the COMMON and CHAIN statements can
- circumvent the 64K program segment
- problem in BASIC(A), and with a
- little more programming, it also can
- be circumvented in compiled BASIC.