Tandy 1000 MS-DOS Basic Reference Manual
تبلیغات
تبلیغات
TERNS A\C rO\D T O \ S OF SA-! A+D . OF TAhDV COMP-TER EO, PNEhT A 4 0
SOF-;\AQF J.RCnASE3 F33V 7AC 0 SnACI( :3VPA\'-OhhED COVP-TER CEhTERS RETA j - ? R E ? A \ ? >A0 S SrAC< F9A\Cn SEES 'IR 2EA-E;IS AT TnE R A-TnOR ZEC -0CAT OhS
LIMITED WARRANTY
CUSTOMER OBLIGATIONS
A CUSTOMER assumes full responsibility that this computer hardware purchased (the Equipment and any copies of software included with the Equipment or licensed se arately (the Software ) meets the specifications capacity capabilities versatility and other requirements of CUETOMER
6 CUSTOMER assumes full responsibility lor the condAon and effectiveness of the operating environment in whlch toe Equipment and Software are to function and lor its installation
LIMITED WARRANTIES AND CONDITIONS OF SALE
A For a period of ninety (901 calendar days lrom the date of the Radio Shack sales document received upon purchase of the Equipment RADIO SHACK warrants t o the original CUSTOMER that the Equipment and the medium upon which the Software is stored is free from manufacturing defects This warranty is only applicable lo purchases of Tandy Equipment by the original customer from Radlo Shack company-owned computer cenlers retail stores, and Radlo Shack franchisees and dealers at their aulhorlzed locations The warrantv is
111.
IV.
V.
VI.
6 9ADIO SPACK sha I not be liable for any damages caused by delay in delivering or furnishing Equipment and or
Softviare
C No actio1 ar sin out of any claimed breach of this Warranty or transactions under this Warranty may be brought rr'ore than two 92) years after the cause of action has accrued or more than four (4) years after the date of the
Radio Shack sales document for the Equlpment or Software whichever first occurs
D S o r e states do not a1 ow the imitat on or exclusion of incidental or coiseouential damages so the above imitationis) or exclusionls) may l o t apply to CUSTOMER
SOFTWARE LICENSE
RADIO SHACK grants to CUSTOMER a non exclusive paid up license to use the TANDY Software on subject to the following provisions one computer
A Except as otherwise provided in this Software License applicable copyright laws shall apply to the Software
B Title to the medium on Nhich the Software is recorded (cassette and or diskette) or stored IROMi is transferred to
CLSTOMER but not t'tle t o the Software
C CLSTOMER may use Software on a multiuser or network system only if eit'ler the Software is expressly labeled to be for use on a multiuser or network system or one copy of th s software is purchased for each node or t e r w i i a l on wh ch Software s to be used simultaneously
D CUSTOMER shal, not use make manufacture or reproduce copies of Software except for use on one computer and as is specifically provided in this Software License Customer is expressly prohibited from disassembling the
Software
E CUSTOKER s perwitted to make additional copies of the Software only for backup or archival purposes or If additional copies are required in the operation of one computer with the Software but only to the extent the
Software allows a backup copy to be made However for TRSDOS Software CUSTOMER is permitted to make a lim ted number of additionas copies for CUSTOMER S own use
F CLSTOMER may resell or distribute unmodified copies of the Software provided CUSTOMER has purchased one copy of the Software for each one soid or distributed The provisions of this Software License shall also applicable to third parties receving copies of the Software from CUSTOMER be
C All copyright notices s+al' be retained on all copies of the Software
APPLICABILITY OF WARRANTY
A T9e t e r m and conditions of this Warranty are applicable as between RADIO SHACK and CLSTOMER to either a sale of the Equipment and or Software License to CUSTOMER or to a transaction whereby Radio Slack sells or conveys such Equipment to a third party lor lease t o CUSTOMER
6 The imitations of liabil ty and Warraity provisions herein sha I nure to the benefit of RADIO SHACK the author owner and or licensor of the Software and any manufacturer of the Equipment sold by Radio Shack
STATE LAW RIGHTS
The warranties granted herein give the original CUSTOMER specific legal rlghts and 1% original CUSTOMER may have other r i g l t s which vary from state to state
8 85
GW '"-BASIC Software: Copyright 1983, 1984 Microsoft Corpora- tion. Licensed to Tandy Corporation. All Rights Reserved.
MS"'-DOS Software: Copyright 1981, 1982, 1983 Microsoft Cor- poration. Licensed to Tandy Corporation. All Rights Reserved.
Tandym 1000 BIOS Software: Copyright 1984 Tandy Corporation and Phoenix Compatibility Corporation. All Rights Reserved.
BASIC
Reference Manual: Copyright 1985 Tandy Corporation. All
Rights Reserved.
MS and GW are trademarks of Microsoft Corporation.
Tandy is a registered trademark of Tandy Corporation.
Reproduction or use without express written permission from
Tandy Corporation, of any portion of this manual is prohibited.
While reasonable efforts have been taken in the preparation of this manual to assure its accuracy, Tandy Corporation assumes no liability resulting from any errors in or omissions from this manual, or from the use of the information contained herein.
10 9 8 7 6
5
4 3 2
Contents
Introduction to BASIC
Chapter
About BASIC for
MS-DOS
Chapter 2
Loading
MS-DOS
and BASIC
Redirection of Input and Output
Chapter 3
Sample Session
Loading the Program into Memory
Chapter 4
General Information
11
11
12
12
13
13
14
7
17
17
17
18
18
19
19
20
23
27
27
27
28
28
33
33
34
36
38
39
3
Chapter 5
Basic Concepts
Declaring Numeric Constants and Variables
Chapter
Arrays
Chapter 7
Disk Files
Creating a Sequential Access File
Updating a Sequential Access File
Accessing a Direct Access File
Chapter
Displaying Color and Graphics
Chapter 9
Introduction to BASIC Keywords 85
10
10
85
86
87
92
Chapter
BASIC Keywords 97
77
77
78
79
82
67
67
68
69
70
71
73
43
43
44
46
47
47
48
48
49
51
51
54
56
59
62
63
4
Chapter 11
Technical Information
Interfacing With Assembly-Language Routines
Memory Allocation Outside BASIC’s Work Area
Memory Allocation Inside BASIC’s Work Area
USR
Information for Creating Child Processes
Chapter 12
BASIC Error Codes and
Messages
Appendix
A
BASIC Reserved and Derived Functions
Words
Appendix B
Keyboard and Character Code
Charts
Appendix
C
I Video Display Worksheet
Appendix
D
Extended Codes
Index
365
375
379
379
383
389
391
393
351
351
351
351
353
355
355
357
358
358
361
361
5
INTRODUCTION TO BASIC
About This Manual
This manual describes BASIC for
MS-DOS.
It is a reference manual, not a tutorial. We assume you already know BASIC and are using this manual to locate information quickly. If you do not know BASIC, see your Radio Shack dealer for the following book:
Learning BASZC for the Tandy 200011 000 by David Lien, Cat. No. 25-1500
Your local bookstore has many books about BASIC available that are written in tutorial fashion.
Notations
The following notations are used throughout this manual:
CAPITALS Material that you must enter exactly as i t appears. italics Words, letters, characters, or values within command lines you must supply from a set of acceptable entries. Elsewhere, italics are used for emphasis.
Items preceding the ellipsis may be repeated. . . . (ellipsis)
[ I
&Hnnnn
&Onnnnn
Items enclosed in brackets are optional. nnnn is a hexadecimal number. nnnnn is a n octal number.
[keynameJ
16
A key on your keyboard.
A blank character (ASCII code 32). For exam- ple, in
BASICMMPROG two spaces are between BASIC and PROG.
7
Introduction to BASIC
Terms
The following terms are used in this manual: buffer parameters arguments syntax
An area in memory that BASIC uses to create and access a disk file. A buffer is represented by a number in the range 1
to
15. Once you use a buffer t o create a file, you cannot use it to create or access any other files; you must first close the file. You may only access a n open file with the buffer used to open it.
Information you supply t o specify how a com- mand is t o operate.
Expressions you supply for a function evaluate. t o
A command with its parameterb), or a func- tion with its argument(s). This shows the for- m a t t o u s e for e n t e r i n g a keyword i n a program line.
8
Chapter
1
ABOUT BASIC FOR MS-DOS
BASIC for MS”’-DOS is an interpreter. This means that, when you run a program, BASIC looks a t one statement a t a time and executes it before going t o the next statement.
BASIC also lets you take advantage of many MS-DOS features, such as:
0
0
Multilevel directories
Faster running programs
Expanded graphics capabilities
Disk
Files
BASIC uses the MS-DOS multilevel directory structure on disk.
A formatted disk has a main directory called the “root” direc- tory which is represented by the backslash ( \ ) . The root direc- tory can contain both files and “second-level’’ directories.
This illustration demonstrates how a typical system for a sales and service company might be set up. The root directory con- tains two second-level directories: SALES and root directory also contains a file called letters.
SERVICE. The
11
Chapter 1 I About BASIC for MS-DOS
Pathnames
BASIC lets you specify pathnames t o access disk files just as MS-
DOS does. A pathname is enclosed in quotation marks and may be a maximum of 63 characters. It contains the following information:
The drive is specified by d : and may be either A, B, C, or D. If you omit d : ,
Path gives the directory path for filename. The directory names are separated by a backslash ( \
1.
If you omit path, BASIC uses the current directory.
Filename specifies the name of the file being accessed. Filenames are 1 to 8 characters long. If a filename is more than 8 character,
BASIC truncates it t o 8 characters.
Ext is a n optional extension t o the filename. Use extensions t o help distinguish types of files. Extensions are always preceded by a pe- riod (.I and are l to 3 characters long. If you omit the extension,
BASIC assumes .bas. Here are some common extensions:
.bas
.txt
.dat
.obj
.re1
.src for for for for for for
BASIC programs
ASCII text
Data files
Object code
Relocatable code
Source code
Directory Paths
Some commands, such as CHDIR, MKDIR and RMDIR use a direc- tory path (dirpath) instead of a pathname. The only difference be- tween the two is that a dirpath does not include a filename:
The drive identifier ( d : )
12
Chapter 1 I About BASIC for MS-DOS
Names
Directory names and filenames must conform to MS-DOS con- ventions. They may contain any of the following characters:
0
0
0 uppercase letters (A-Z) lowercase letters ( a 4 decimal digits (0-9)
In addition, they may contain the following special characters:
$ & # 5% ( ) - (6 { } - ' ! ' * -
An entire pathname (or directory path) may contain a maximum of
63 characters.
Some sample filenames are: prog.bas report telephon. sls
Some sample directory names are:
MEMOS LETTERS EMPLOYEE
In this manual, directory names are in uppercase letters, and filenames are in lowercase letters. This lets you easily distin- guish a directory name from a filename.
Wildcards
BASIC follows MS-DOS usage of wildcard notations when search- ing for directories or filenames. The wildcard notations are:
? indicates that any character can occupy that position.
* indicates that any character can occupy that position or the remaining positions in the filename or extension.
For example, if you specify this filename: data?tst.txt
BASIC might find these files: dataltst.txt data3tst.txt
If you specify this filename: data*.txt
13
Chapter 1 I About BASIC for MS-DOS
BASIC finds those files and might also find these: data.txt datatst.txt datal.txt
Device Names
BASIC uses device identifiers (dev:) to indicate a physical device to be used for communication. These names, which must be enclosed in quotation marks, are:
KYBD:
SCRN:
LPT1:
COMn: keyboard. Use for input only. screen. Use for output only. printer. Use for output only.
RS232
communications channel 1 or 2. Use for input or output.
You can open any of these devices just as you would a disk file.
14
Chapter 2
LOADING MS-DOS AND BASIC
The diskette that came with your computer contains MS-DOS and the BASIC Interpreter. Before you begin using BASIC, make backups of this diskette. First, load MS-DOS into the computer.
Loading MS-DOS
To start up MS-DOS, turn on your computer and insert the MS-
DOSIBASIC diskette into Drive A. See your computer’s introduc- tion book for information on turning on your computer and Drive
A.
Shortly, MS-DOS prompts you t o enter the date and time. We suggest that you enter this information since some BASIC state- ments use the system date and time. MS-DOS displays the proper format.
Next, MS-DOS loads and displays its prompt:
A >
This means that you are a t the MS-DOS command level where you can execute a program or command. You should now make backups of your MS-DOSIBASIC diskette.
Making Backups
Just as you always keep a spare key for a car, you should keep backup copies of all your diskettes. After you make the backup of your MS-DOSIBASIC diskette, store the original in a cool, dust- free place where it can’t get bent or warped. Also be sure it is not near any magnetic source. If something should happen t o any of your backup MS-DOSIBASIC diskettes, be sure to make another backup. Do not use the original diskette except t o make backups.
You can also use the following procedures to make backups diskettes you create. of
17
Chapter 21 Loading MS-DOS and BASIC
One Drive System
1. At the MS-DOS system prompt (A>), type:
FORMAT
[ENTER]
2. When prompted, remove the MS-DOSIBASIC diskette and insert a n unformatted diskette into Drive A. Press
[SPACEBAR] to begin formatting. MS-DOS displays a line of dashes. The dashes change formatted. t o dots as each track is
3. When formatting is complete, MS-DOS prompts:
FORMAT a n o t h e r ( Y / N ) 7
Format as m a n y d i s k e t t e s a s you w a n t . When you a r e through, answer m.
4. Place the MS-DOSIBASIC diskette into Drive A. At the MS-
DOS prompt, type:
D I S K C O P Y
[ENTER]
5.
DISKCOPY prompts you to insert the “target” diskette into
Drive A. Remove the MS-DOSIBASIC diskette from Drive A and insert the newly formatted diskette. Press (SPACEBAR] to be- gin copying. Shortly, you are prompted to insert the “ s o ~ r c e ’ ~ diskette. The source diskette is the MS-DOSIBASIC diskette.
MS-DOS will prompt you to swap the diskettes several times.
6. When DISKCOPY is complete, it prompts:
C o p y c o m p l e t e . C o p y a n o t h e r ( Y / N ) 7
Make a few more backups. When through, type (
Two Drive System
1. At the MS-DOS system prompt (A>), type:
FORMAT E: [ENTER]
2. When prompted, insert a n unformatted diskette into Drive B.
Press [SPACEBAR] to begin formatting. MS-DOS displays a line of dashes. The dashes change to dots as each track is formatted.
3. When formatting is complete, MS-DOS prompts:
FORMAT a n o t h e r ( Y / N ) ?
18
Chapter 21 Loading MS-DOS and BASIC!
Format as many d i s k e t t e s as you w a n t . When you a r e through, answer m.
D I S K C O P Y A : E: [ENTER)
5 . DISKCOPY prompts you to insert the “source” diskette into
Drive A and the “target” diskette into Drive B. Insert the newly formatted diskette into Drive B. Since the MS-DOS/
BASIC diskette is already in Drive A, press .
6. When DISKCOPY is complete, it prompts:
C o p y c o m p l e t e . C o p y a n o t h e r ( Y / N ) 7
Make a few more backups. When through, type (iJ.
Loading BASIC
To load BASIC, first start up MS-DOS as described earlier.
When MS-DOS displays its system prompt, A>, you load BASIC by typing 1 of 2 commands.
B A S I C [ENTER) immediately loads BASIC into the computer’s memory.
B A S I C A [ENTER] loads the small loader program BASICA.COM, which in turn loads BASIC. This alternate method of loading BASIC is dis- cussed in detail in the section “Loading BASIC via BASICA.”
Once you load BASIC, a paragraph of copyright information ap- pears on your screen, followed by BASIC’s prompt: Ok.
At this point, you can begin using BASIC. The next chapter pro- vides a sample session on loading BASIC and using some of its capabilities. If you don’t want to use any of BASIC’s other op- tions for loading, go ahead to the next chapter.
Loading BASIC
via
BASICA
Some computers require you to type B A S I [ENTER) to load
BASIC. To increase compatibility with such computers, your computer also accepts the BASICA command. When you enter the command, the computer executes the program BASICA.COM, which in turn loads BASIC.
19
Chapter 21 Loading MS-DOS and BASIC
In addition to compatibility, other advantages of loading BASIC via BASICA are as follows:
If your computer has more than 128K bytes of memory, BASIC is loaded at a different memory location than it would be oth- erwise. This feature lets you run a few BASIC programs that make use of certain memory locations that BASIC would oth- erwise have occupied.
You can gain space on your program or system diskette be- cause you can store the BASIC.EXE file on a separate disk.
The only limitations imposed by BASICA are:
The /I option switch (discussed below) is always on.
The communications buffer size is limited to 40K bytes if the system has 1 RS232 card or 20K bytes if it has 2 cards.
After you type [ENTER), BASICA.COM searches the cur- rent directory for the file BASIC.EXE. If it finds BASIC.EXE,
BASICA.COM loads it and passes control to it.
If BASICA.COM does not find BASIC.EXE, it asks you to replace your program disk with a disk that contains the file. Place a disk containing BASIC.EXE in any drive, and press m. program searches all drives, beginning with the current drive, until it finds BASIC.EXE or until you press [CTRL]
After finding BASIC.EXE, the program asks you to re-insert your program disk if you removed it. Put the disk back in the drive, and press m.
Options for
Loading BASIC
When you load BASIC, you can also specify a set of options, which includes:
BASIC
[pathnamel[<input-file] [>[>loutput-fiZel [IF:# of files1
[/M:highest memory locatwn,maximum block sizel[/C:buffer size]
[/S:record lengthl[/Dl[/Il
If you load BASIC by typing B A S I C A [ENTER], the /I switch is al- ways invoked. Other than that, gardless of how you load BASIC. you have the same options, re-
Pathname specifies a program to run immediately after BASIC is started.
20
Chapter 21 L o a d i m MS-DOS and BASIC
<Input-file tells BASIC to receive input from input-file instead of the usual standard input (the keyboard). This option must fol- low pathname and precede all other options in the command line.
Redirection of i n p u t a n d output is discussed l a t e r i n t h i s chapter.
>[>lOutput-file redirects BASIC’s output to output-file instead of the standard output (video display). If you specify 1 greater- than sign, output file is overwritten. If you use 2 greater-than signs, it is appended. This option must follow input file (if given) and precede all other options in the command line. Redirection of input and output is discussed later in this chapter.
IF: specifies the maximum number of data files that may be open a t any one time. If you specify the IF: option, you must also specify the /I option. If you omit this option, the number of files defaults t o three. The number of open files that MS-DOS sup- ports depends on the value given for the FILES= command in the CONFIG.SYS file. We recommend that you set FILES
=
10 for BASIC. BASIC automatically reserves 4 files for internal use.
This leaves 6 for BASIC file IIO; thus lF:6 is the maximum sup- ported by MS-DOS when FILES= command is set to 10 in the
CONFIGSYS file.
Each file you specify may use a maximum 190 bytes of memory.
Sequential access files always use 190 bytes of memory. The amount of memory a direct access file uses depends on the record size set with the IS: 62 bytes of memory for the file control block, plus the record size.
For example, if you specify a record size of 50 with the IS: switch, the file uses 112 bytes.
1s: specifies the maximum record size for direct access files. If you use the omit the
IS: option, you also must specify the /I option. If you is: option, BASIC assumes 128 bytes.
IC: specifies the size of the receive buffer for each RS232 commu- nications channel present in the system. The maximum amount you can specify depends on the number of
RS232
cards in the system and on the method used t o load BASIC. present
21
Chapter 21
Loading
MS-DOS and BASIC
Loading Method
BASIC
BASIC
BASICA
BASICA
Number of Ports
1
2
1
2
Buffer Size
64K bytes
32K bytes
40K bytes
20K bytes
Chapter 21 Loading MS-DOS and BASIC
Examples
B A S I C DEBITS initializes BASIC to 3 data files with all memory available.
BASIC then loads and runs the program DEBITS.
B A S I C P A Y R O L L / F : 5 initializes BASIC to 5 data files with all memory available.
BASIC then loads and runs the program PAYROLL.
B A S I C / M : 2 1 0 0 0 initializes BASIC to 3 data files and sets the highest memory lo- cation to be used by BASIC a t 21000, the first 21000 bytes of
BASIC’s data segment.
B A S I C B U D G E T / D initializes BASIC to
3
data files with all memory available.
BASIC loads and uses the Double Precision Transcendental math package.
Redirection of Input and Output
BASIC lets you redirect input and output. The syntax to redirec- tion is:
BASIC
[pathname] [<input-file] [>[>loutput-file1
You can redirect standard input, normally from the keyboard, to the file input-file.
Standard output, normally rected to the file t o the video display, can be redi- output-file. If output-file already exists, it is ov- e r w r i t t e n . You c a n , however, append t h e existing file by using the append notation: output-file t o t h e
>>output-file. If out- put-file does not exist, it is created.
The following BASIC statements use standard input:
INPUT
INPUT$
LINE INPUT
INKEY$
The BASIC statements PRINT and WRITE access standard output.
Error messages are sent both to the standard output and t o the redirected output.
23
Chapter 21 Loading MS-DOS and BASIC
Examples
BASIC DAILY >DAILY.OUT initializes BASIC and runs the program DAILY. Redirects all output, normally sent to the screen, to the file DAILY.OUT.
BASIC DAILY <DAILY.IN initializes BASIC and executes the program DAILY. DAILY re- ceives all input, normally entered through the keyboard, from the file DAILYJN.
BASIC S A M P L E <TSTDATA.IN >TSTDATA.OUT initializes BASIC and executes the program SAMPLE. SAMPLE r e c e i v e s i n p u t f r o m TSTDATA.IN a n d s e n d s o u t p u t t o
TSTDATA. OUT.
BASIC P A Y R O L L < W E E K 2 5 > > Y T D T O T A L initializes BASIC and executes the program PAYROLL. PAY-
ROLL receives input from the file WEEK25. The output is ap- pended to the file YTDTOTAL.
Hints
for
redirection
of
input and output:
0
File i n p u t from t h e KYBD: device s t i l l r e a d s from t h e keyboard.
0
File output to the SCRN: device still outputs to the screen.
0
BASIC s t i l l t r a p s k e y s when you u s e t h e O N K E Y 0 statement .
[ C s R L j m ] tells BASIC to close all open files, then issue the message “Break in line xxxx” to standard output. Control re- turns to MS-DOS.
0
Redirected input continues until BASIC receives a [CTRLJ[T].
This condition can be tested by the EOFO function. If the in- put file is not terminated by a [CTRL][T] a BASIC file input statement tries to read past the end-of-file, BASIC closes any open files and issues the message “Read past end” to standard output. Control returns to MS-DOS. e The printer echo key combination ([CTRL][PRlNT)), which nor- mally causes all output on the display to be echoed on LPTl:, will not work if you redirect standard output.
24
Chapter
3
SAMPLE SESSION
The easiest way to learn how BASIC operates is t o write and run a program. This chapter provides sample statements and instruc- tions t o help familiarize you with the way BASIC works.
The main steps in running a program are:
1. Loading BASIC
2. Typing the program
3. Running the program
4. Saving the program
5. Loading the program into memory
Loading BASIC
For this sample, load BASIC by typing:
B A S I C [ENTER)
Typing the Program
Type in the sample program below. After typing each line, check it for any mistakes. If there are no mistakes, press
I make a mistake, use the [tl take and retype the rest of the line t o correct the mistake.
1 0 A $ = ” W I L L I A M S H A K E S P E A R E WROTE
1 5 B $ = ” T H E MERCHANT OF V E N I C E ”
2 0 P R I N T A $ ; B$ [ENTERJ
*
Check your program again. If you find a mistake, enter the line number and type the line again. The newly typed line replaces the old line.
It does not matter if you enter Line 15 after Line 20; BASIC still reads and executes Line 15 before “looking” a t Line 20. BASIC always reads program lines in numerical order.
Tell BASIC t o execute this program by typing:
RUN
[ENTER]
Your screen should display:
W I L L I A M S H A K E S P E A R E WROTE T H E MERCHANT OF V E N I C E
27
Chapter 31 Sample Session
BASIC has powerful special keys that let you correct mistakes without retyping the entire line. These commands are discussed in Chapter
Saving the Program on Disk
You can save any BASIC program on disk by assigning it a path- name. The pathname tells BASIC on which disk and directory you want to save the file and the name of the file. The pathname must be enclosed in quotation marks. Pathnames must conform t o the conventions discussed in Chapter 1, "About BASIC for
MS-DOS.
"
For example, to save the program we just wrote on Drive B, in the directory BOOKS with the filename author.bas, use the fol- lowing command:
S A V E " E : \ B D O K S \ a u t h o r . b a s "
[ENTER]
Notice that BOOKS is located in the root directory since preceded by the root symbol ( \ ). it is
You can also save the file with this command:
SCIVE " a u t h o r . w i l " which saves the program a s author.wi1 in the current directory on the MS-DOS current drive.
It takes a few seconds for the computer to find a place on the disk t o store a program and to copy the program from memory to the disk. When the program is saved on the disk, BASIC dis- plays its prompt (Ok).
Loading the Program Into
Memory
If, after writing or running other programs, you want to use this program again, you must load it back into memory from disk.
For example, to load the program author.bas from the directory
BOOKS, type:
LOAD " B : \ B O O K S \ a u t h o r . b a 5 " , R
28
Chapter 31 Sample Session
The R option tells BASIC to run the program after loading it.
LOAD " a u t h o r . w i 1 " loads the file rent drive. author.wi1 from the current directory on the cur-
Another way to load and run a program is to type:
RUN "pathname"
RUN automatically loads and runs the program specified by pathname.
The SAVE, LOAD, and RUN commands are discussed in more detail in Chapter 10.
29
ChaDter 4
GENERAL INFORMATION
When BASIC displays the Ok prompt, you can type in program lines or commands. If you want BASIC t o read what you type in, you must press [ENTER] at the end of the line.
A single line can be a maximum of 255 visible characters. Visi- ble characters are those that take up a space on the display.
Since 255 characters cannot fit on one line of the display, BASIC moves the extra characters t o the next line. This is called wrap- around.
BASIC looks a t the first character of a line. If it is a digit,
BASIC stores the line in memory as a program line. For exam- ple, if you type:
1 0 P R I N T " T H E TINE I S
"
T I M E $ (ENTER]
BASIC takes this as a program line and stores it in memory. It does not execute the line until you type RUN and press m.
If the first character is not a digit, BASIC tries to execute the line as a command. For example, if you type:
M I L E S = 1 3 3 : G A L L O N = 1 1 : M P G = M I L E S / G ~ L L O N
BASIC immediately executes this line as a command. After it is executed, the statement no longer exists in memory, but the val- ues of the variables MILES, GALLON, and MPG are stored in memory.
This BASIC capability lets you use the computer as a calculator for quick computations that do not require an entire program.
Editing
BASIC lets you correct errors in program and command lines quickly and efficiently without retyping entire lines.
You can use the special keys defined at the end of this chapter t o make corrections or changes a t any time. To correct a line, sim- ply use the arrow keys to position the cursor on the line you want to alter. After you make changes to the line, press (ENTER) t o store the changes.
33
Chapter 41 General Information
When modifying program lines, you can edit specific lines by typing:
EDIT line number [ENTER]
If the line number you specify does not exist, BASIC returns an
"Undefined line number" error.
You can also specify the current program line by using a period
(.I instead of a line number:
E D I T
. [ENTER)
The current line is the last line entered, the last line altered, or a line in which an error has occurred. Notice that you must type a space before the period; otherwise, BASIC displays a "Syntax error" message.
BASIC automatically enters EDIT when a syntax error occurs when executing a program. error and waits for you t o
It
displays the line that contains the make corrections.
Sample Editing Session
This sample session shows how you can easily edit lines in
BASIC. Even though the sample is a BASIC program, you can use the same procedure for command lines. All special keys used in this session are described a t the end of this chapter.
To begin the sample session, type the following line and press m:
100 PRINT " T h i s
1 5 o u r e x a m p l e l i n e . "
Now use the m to position the cursor on Line 100.
Use the to move across the line t o the T in This. Type lower- case and then [ENTERJ. Remember, none of the changes you make t o a program line are recorded until you press m.
Type LIST 100 and press [ENTERJ t o see that BASIC has stored your change in memory. BASIC displays:
100 PRINT " t h i s
1 5 o u r example line."
Notice that you can make simple changes by typing over the old material.
34
Chapter 41 General Information
Now, position the cursor over Line 100 again. Press (ENDJ and then use t o position the cursor on the second set of quotation marks. Press
W e i n s e r t e d t h e s e c o n d 5 e n t e n c e . [ENTER]
Use the LIST command again t o see the new statement that is stored in memory.
Now use the EDIT command t o edit Line 100. Type:
E D I T
.
[ENTER]
Remember, the period (.) tells BASIC t o edit the current line.
Don’t forget to type a space before the period.
Using and
W,
position the cursor on the
i
in inserted. Hold down [CTRL) and press [ENDI. BASIC deletes all the characters
YOU have inserted except We and the blank space.
Press [BACKSPACE) t o delete the space.
Hold down [CTRL) and press t o position the cursor on the pre- ceding word. Press then J
[DELETE) twice t o delete We. Press and t o put the quotation mark a t the end of the statement.
Press mand
[ENTtRl t o record the changes. You can use the LIST com- t o see the new line.
Use the EDIT command again, this time with the line number.
Type:
E D I T 1 0 0 (ENTER)
Using m, position the cursor on the P in PRINT. Press (SPACEBAR] to change the P t o a blank.
Press (CTRL] while pressing this. Press t o position the cursor on the t o change the lowercase t to a capital T . t in
Instead of pressing [ENTER] after you make the changes, press
@. Use the LIST command. Notice that BASIC did not record your changes because you pressed JESC) instead of m.
IESCJ key tells BASIC t o erase the line and not to make any changes t o the line.
Now you have used most of the special keys in the editor. If you still do not feel comfortable with them, go through the sample session again.
35
Chapter 41 General Information
If you feel confident that you understand the editor, read on t o learn about some special keys that make it easier and faster to change lines anywhere on the screen.
Special Keys
The following keys perform special functions in BASIC for enter- ing and editing lines. To use some of these keys you must press and hold down the [CTRLJ key while pressing the second key. For example, when you use [CTRL][T] backspace, hold down the key and press i at the same time.
Key
[CAPS]
Description
switches to all uppercase or uppercase1 lowercase mode.
(SPACEBAR]
changes the current character to a blank and advances the cursor 1 position t o the right.
[ESCI or [TK][T]
[DELETE] backspaces the cursor, erasing the first character t o the left. All characters t o the right move left 1 position. Use this to correct typing errors before you press m. interrupts line entry and starts over with a new line. Any changes previously made t o the line are not saved. ends the current line. BASIC reads the line. erases the entire line from the screen, but not from memory. clears the screen and positions the cursor a t the first position in Row 1. clears the screen from the current cursor position t o the end of the screen. deletes the character a t the cursor posi- tion and moves all remaining characters to the left 1 position.
36
Chapter 41 General Information
[INSERT] or (CTRL][T]
(HOME] or [CTRL][F]
[TAB] or [CTRL][T]
@ or [CTRL][T] or [CTRLJIT] or
[CTRLJ[:]
turns the insert mode on if it is off; or off if it is on. The insert mode lets you add new characters t o the line at the cursor position. moves the cursor to the first position in
Row 1. moves the cursor t o the end of the line. deletes all characters from the current cursor position t o the end of the line. advances the cursor t o the next tab posi- tion. Tab positions are set at every 8 characters . moves the cursor 1 position t o the left. moves the cursor 1 position to the right. moves the cursor up 1 row t o the charac- ter above the current cursor position. moves the cursor down 1 row t o the char- acter below the current cursor position. moves the cursor to its left and t o the first character in the preceding word, which is the first character preceded by a blank. moves the cursor t o its right and to the first character in the next word, which is the first character preceded by a blank. rings the bell a t the terminal. issues a linefeed. This moves the cursor to the next line of the display without ex- ecuting or storing the line.
37
Chapter 41 General Information
The following special keys act differently while BASIC is execut- ing programs or commands:
IHOLD] pauses execution. Press [HOLD] again t o continue.
[rnJrnJ
terminates execution and returns you
t o
BASIC’s prompt.
IENTER] or [CTRL][T] signifies the end of data entry. When a
BASIC program or command prompts you to enter data, press [ENTERI response. t o end the
The
(ALT] Key
The [ALTI key provides a quick and easy way to type certain
BASIC keywords. These keywords are associated with alphabetic characters (A-Z).
To enter these keywords, press and hold down the [ALTJ key while pressing the desired letter. BASIC inserts the keyword a t the current cursor position. The keywords and their associated let- ters are listed below.
A AUTO
B BSAVE
C COLOR
D DELETE
E ELSE
F FOR
G GOT0
H HEX$
I INPUT
J (none)
K KEY
L LOCATE
M MOTOR*
N NEXT
0 OPEN
P PRINT
Q (none)
R RUN
S SCREEN
T THEN
U USING
V VAL
W WIDTH
X XOR
Y (none)
Z (none)
*MOTOR is a reserved word, but is not recognized in this imple- mentation of BASIC.
38
Chapter 41 General Information
The
Kev
Pressing [sHlFT][PRlNT] dumps the current text content screen t o the line printer LPT1:. of the switch. If the echo is characters sent off, pressing [ C T R L ] [ m ] once causes all t o the screen t o also be sent t o the line printer
LPT1:. Pressing [ C T R L ] [ m ] a second time turns off this echo feature.
39
Chapter
5
BASIC CONCEPTS
This chapter describes the different ways BASIC handles and manipulates data. By understanding how BASIC does this, you can build more efficient programs.
Elements
of
a Program
A program is a group of instructions that performs a certain task. It is made up of 1 or more numbered lines.
Each line can contain a maximum the
255
of 255 visible characters. Of characters, BASIC automatically reserves 1 space for each digit in the line number and another space for the space fol- lowing the line number. If you enter more than 255 visible char- acters, BASIC truncates the line.
Here is a sample program line:
10 PRINT " o n e "
A line number is always the first element of a program line. In
BASIC line numbers must be in the range 0 t o 65529. In the sample program line, the line number is 10.
A BASIC statement follows the line number. A statement tells
BASIC to perform a specific operation. In the sample program line, the statement is PRZNT "om". This statement tells BASIC to print, or display, the word om on the screen.
You can have more than 1 statement on a program line by plac- ing a colon between each statement. For example:
20 FOR X = 1 TO 5:PRINT " o n e " : N E X T X
This program line has 3 statements. They are:
1. F O R X = 1 T O 5
2. PRINT "one"
3 . NEXTX
You can add explanations, or remarks, t o your program lines. A remark is preceded by a single quotation mark to separate it from the statements. Here is a program line with a remark:
20 F O R X =
1 TO 5:PRINT "one":NEXT X ' l o o p
43
Chapter 51 Basic Concepts
Data
Data is information on which BASIC performs its operations.
Data can be numbers, characters, or symbols. BASIC classifies data into two groups: string and numeric.
String data is a sequence of ASCII characters, graphics or non-
ASCII symbols. A string can be a maximum of 255 characters.
If the string is entered on a program or command line, it must be enclosed in quotation marks (see “Constants” later in this section). If the string is entered in response t o a prompt, it is not enclosed in quotation marks. BASIC does not evaluate string data; it simply stores it for the program t o use or manipulate.
Hint:
ASCII stands for American Standard Code for
Information Interchange. In ASCII, each character has a unique number that represents it. This is necessary since computers understand and process only numbers.
Here are some sample strings:
“ ~ 1 ~ 3 7
“25 dollars”
“MAIN STREET” “255 CENTRAL AVE”
“$250” “2
+
4”
Notice that numbers can be in a string. Remember, BASIC does not evaluate strings. Type the following line a t BASIC’s prompt:
P R I N T “ 2
+
4 ”
BASIC does not add 2 and displays 2
4.
It
obeys the command PRINT and
+
4 on your screen.
Strings use 3 bytes of memory plus the number of characters in the string. For example, the string “CATS” takes up 7 bytes of memory: 4
Numeric data consists of positive and negative numbers. BASIC divides numeric data into 5 groups: integer, single precision, double precision, hexadecimal, and octal.
Integers are whole numbers in the range -32768 t o +32767 that do not contain a decimal point. For example:
1 3200 -2 500 -12345
Integers use the least amount of memory (2 bytes). Because they use less memory, BASIC can access them fastest.
44
Chapter 51 Basic Concepts
Single precision numbers can be a maximum of 7 digits and may have a decimal point. Single precision numbers must be in the range to Sample single precision numbers are:
10.001 -200034 123.4567
If a single precision number is more than 7 digits, BASIC dis- plays the number in scientific notation, or exponential format, in the E form. For example:
1.74E 6.9838 1043-7
BASIC stores a single precision number in 4 bytes of memory.
Double precision numbers can include a maximum of 16 digits and may have a decimal point. Double precision numbers have the same range as single precision numbers. Sample double pre- cision numbers are:
1010234567 -8.7777651010
If a double precision number is more than 16 digits, BASIC dis- plays the number in scientific notation, or exponential format, in the D form. For example:
8.00100708D12 -6.7765499824D16
BASIC stores double precision numbers in 8 bytes of memory. Al- though double precision numbers consume more memory, they are the most exact.
Hexadecimal numbers are the hexadecimal representation of dec- imal numbers. They contain 1 to 4 digits and are preceded by
&H. The hexadecimal numbers are 0, 1, 2, 3, 8, 9, A,
B, C, D, E, and F. Here are some hexadecimal numbers and their decimal equivalents:
Hex Decimal
&H76
&H02FF
&HFF
118
767
255
BASIC stores hexadecimal numbers as integers.
45
Chapter 51 Basic Concepts
Octal numbers are the octal representation of decimal numbers.
They contain 1 to
6
digits and are preceded by &O or &. Al- though only the & is required, we recommend that you use &O for clarity in your programs. The octal numbers are
0, 1, 2,
3, 4,
5, 6,
and 7. Here are some octal numbers and their decimal equivalents:
Octal Decimal
&7
&0123
&0000456
7
83
302
BASIC stores octal numbers a s integers.
Constants
Constants are values input to a program that are not subject to change. Constants can be either string or numeric data (integer, single or double precision, hexadecimal, or octal).
Numeric data that will not change can be represented as either a string or numeric constant. If you use punctuation in the num- ber, i t must be a string constant. For example:
PR I N T " $ 2 5 0 0 0
"
When BASIC encounters a data constant in a statement, BASIC must determine how to store it:
If the value is enclosed in quotation marks, BASIC stores it as a string.
If the value is not enclosed in quotation marks, BASIC stores it as a n integer or a single precision or a double pre- cision number, according to the requirements described in the previous section. The section, "Declaring Numeric Con- stants and Variables," describes ways to override BASIC's classification of constants.
BASIC evaluates numeric constants in program lines as soon as you enter the line. It does not wait until you run the program. If any numbers are out of range for their type, BASIC returns a n error message immediately.
Here are some examples of constants:
P R I N T - N A M E * ' , " A D D R E s si' ,
"c
I T Y
9 1
, " S T A T E "
46
ChaDter 51 Basic ConceBts
This line contains 4 string constants: NAME, ADDRESS, CITY, and STATE. These values will not change. Every time BASIC ex- ecutes this statement, the same 4 words are printed.
P R I N T “ 1 0 0 0 P L U S ” ; 2 0 0 0 ; “ E Q U A L S ” ; 3 0 0 0
The 1000 is a string constant, the 2000 and the 3000 are nu- meric constants.
Variables
Variables are symbolic names for a value in a BASIC program. A variable name can be a maximum of 40 characters and must be- gin with a letter (A-Z).
Note:
You cannot use any of the reserved words listed in Appendix A a s variable names. However, reserved words may be imbedded in a variable name.
The following are examples of variable names:
A
L
A1 ADDRESS
L2 LEN2
ADDRESS. OLD
LENGTH
The 2 types of variables are string and numeric. BASIC initially classifies all variables as single precision with a value of zero
(0). (The next section describes how to declare variables a s string, integer, or double precision variables.)
The following examples assign a value t o a variable.
L E T A = 1 2 3 4 5
A = 6 0 1 . 4 3 2
B A L A N C E = 3 3 8 . 9 2
BASIC automatically stores all the above examples as single pre- cision numbers. Chapter 10, “BASIC Keywords,” describes more ways to assign values to variables.
Declaring Numeric Constants and Variables
BASIC lets you override its automatic classification of numeric constants and variables.
47
Chapter 51 Basic Concepts
Numeric Constants
To change the way BASIC stores a numeric constant, add one of the following symbols to the end of the number. If BASIC must shorten a number to meet the new requirements, it rounds the number.
! declares a single precision number. For example, BASIC stores the number 12.345678901234! as a single precision number: 12.34568.
E
# declares the number a single precision exponential number.
For example, BASIC stores the number 1.2E5 as a single precision number: 120000. declares a double precision number. For example, BASIC stores the number 1.5# as a double precision number: 1.5.
BASIC does not expand constants when declaring them dou- ble precision.
D declares the number a double precision exponential number.
For example, BASIC stores the number 1.2D2 as a double precision number: 120.
See the next section on converting numbers for important infor- mation on converting from numbers to another precision.
-
Numeric Variables
BASIC initially classifies all numeric variables as single preci- sion. You can declare variables as other than single precision in
2 ways:
Append a symbol to the variable name:
% declares a n integer variable. BASIC stores the value of the variable as a n integer. I%, FT%, and COUNTER% are samples of integer-declared variables.
! declares a single precision variable. BASIC stores the value of the variable a s a single precision number. F!,
NM!, and BALANCE! are samples as single precision. of variables declared
48
Chapter 51 Basic Concepts
# declares a double precision variable. BASIC stores the value of the variable as a double precision number. S#,
AD#, and YTDTOTAL# are samples of variables de- clared as double precision.
$ declares a string variable. The value of the variable must be enclosed in double quotes. A$, WRD$, and CITY$ are samples of variables declared as string variables.
Note:
Any variable name can represent 4 different variables. For example, A5%, A5!, AS#, and A5$ are all valid and distinct variable names.
0
Use the following BASIC statements:
DEFINT
DEFSNG
DEFDBL
DEFSTR
Defines specified variableh) as integer.
Defines specified variable(s) as single precision.
(Since BASIC initially classifies all variables as single precision, you need t o use DEFSNG only if one of the other DEF statements is used.)
Defines specified variableb) as double precision.
Defines specified variableb) a s string.
Chapter 10 describes these BASIC statements fully.
Numeric Precision Conversion
Your program may ask BASIC to convert numeric data from one precision to another. The following section describes t h i s procedure.
When converting singleldouble precision to integers, BASIC rounds the fractional portion of the number, if any. For example:
A% = 32766.7
A%
=
-123.4567
BASIC stores as 32767
BASIC stores as -123
When converting pends integers to singleldouble precision, BASIC ap- a decimal point and zeroes to the right of the original value. For example:
A# = 32767 BASIC stores as 32767.00000000000
A!
=
-1234 BASIC stores as -1234.000
49
Chapter 51 Basic Concepts
When converting double t o single precision, BASIC rounds the number t o
7
significant digits. For example:
A! =
A!
=
1.2345678901234567
BASIC stores as
1.234568
1.3333333333333333
BASIC stores as
1.333333
When converting single
to
double precision,
BASIC adds
trailing zeroes t o the right of the original value. If the original value has an exact binary representation in single precision format, the re- sulting value is accurate. For example:
A# =
1.5
BASIC stores as
1.50000000000000
However, if a number does not have an exact binary representa- tion, the conversion creates a n erroneous value. For example:
A#
=
1.3
BASIC stores as
1.299999952316284
You should not use such conversions in your program because most fractional numbers do not have exact binary representa- tions. You can avoid this by forcing the constant to be double pre- cision, such as:
A# =
1.3#
or A#
=
1.3D
which BASIC stores as
1.3.
If you must convert from single t o double precision, the following programs show a special technique.
Type and run the following program:
1 0 A ! = 1 . 3
2 0 A # = A !
3 0 PRINT A #
BASIC prints
1.299999952316284
Now type and run this program:
1 0 A ! = 1 . 3
2 0 A # = V A L ( S T R S ( A ! ) )
3 0 PRINT A #
BASIC prints
1.3.
Converting the single precision number t o a string before converting it t o a double precision value causes
BASIC to store the value accurately.
Note:
BASIC cannot automatically convert numeric data t o string data or vice versa. This results in a
“Type mismatch” error. Use the tions
VAL and STR$ func- t o accomplish this kind of conversion.
50
Chapter 51 Basic Concepts
Manipulating Data
BASIC uses expressions as a way to manipulate data. An expres- sion is 2 or more pieces of data connected by operators.
An operator is a symbol or a word that signifies some action t o be performed on the specified data. Each data item is called an operand.
An expression might look like this: operand1 operator operand2
6
+
2
A few operators allow only one operand, for example operator
- operand
5
Expressions must be used in a BASIC statement, such as:
A = 6 + 2
P R I N T - 5
BASIC has four types of operators:
Arithmetic
String
Relational
Logical used for numeric data only. used for string data only. used for both numeric and string data. used for numeric data only.
Arithmetic Operators
Arithmetic operators perform operations on numeric data. Both operands must be numeric. When BASIC evaluates the expres- sion, all operands are converted t o the same degree of precision, that of the most precise operand. The result of the arithmetic op- eration is also returned t o this degree of precision.
The arithmetic operators are listed below. They are in order of precedence, that is, the order in which BASIC executes them if 1 or more operators are in the same statement.
A Exponentiation. Calculates t h e power of a number. For example, 2^3 is 8 (2 to the power of 3 is the same as 2*2*2).
Negation or Unary Minus. Makes a number negative. For example, -10 is “negative ten.”
51
Chapter 51 Basic Concepts
*,
\
I
MOD
Multiplication, Division. For example, 3*3 is 9, and 1015 is 2.
Integer Division. BASIC rounds both operands t o integers and truncates the result to an in- teger. Integer division is faster than standard
division. For
example, 10\4
2.
Modulus Arithmetic. BASIC performs integer division as described above and returns the remainder as an integer value. For example, 10
MOD
3
results in 1.
+,
-
Addition, Subtraction. For example, 2 + 9 is 11, and 15-8 is 7.
String Operator
The string operator is the plus sign (
+
1. It appends one string t o another. All operands must be strings, and the resulting value is
1 string. For example:
P R I N T " A P R I L SHOWERS
'I
+
" B R I N G "
+
F L O W E R S .
"
'' MAY prints APRIL SHOWERS BRING MAY FLOWERS.
Relational Operators
Relational operators compare 2 pieces of numeric data or 2 pieces of string data. The result of the comparison is either true or false. If the relationship is true, BASIC returns -1. If the rela- tionship is false, BASIC returns 0 (zero).
The relational operators are, in order of precedence:
Equal. Both operands are equal.
< Less Than. The than first operand is less or precedes the second operand.
> G r e a t e r T h a n . The f i r s t operand i s g r e a t e r t h a n o r follows t h e second operand.
>< or <> I n e q u a l i t y . The operands a r e not equal.
52
Chawter 51 Basic ConceDts
< = or = < Less Than or Equal To. The first oper- and is less than (precedes) or is equal t o the second operand.
> = or = > Greater Than or Equal To. The first operand is greater than (follows) or is equal t o the second operand.
Relational operators are usually used within an IFiTHEN state- ment. For example:
I F CI = 1 THEN P R I N T "CORRECT"
BASIC looks at the value in variable A. If the value is equal to
1, BASIC prints the word CORRECT.
If arithmetic and relational operators are combined in the same expression, BASIC evaluates the arithmetic operations first. For example :
I F X * Y / 2 < = 1 5 P R I N T " 4 V E R A G E SCORE"
BASIC performs the arithmetic operation X*Y/2 and then com- pares the result with 15.
When relational operators are used with strings,
BASIC compares the strings character by character. When it finds 2 characters that do not match, it checks t o see which character has the lower value ASCII code. The character with the lower
ASCII code comes before the word with the higher ASCII value in an alphabetical listing, just as one word comes before another in a dictionary.
Consider these examples:
"fl" < llgll
BASIC compares the ASCII value value for of the 2 strings. The ASCII
A is 65, and the ASCII value for B is 66. Since 65 is less than 66, BASIC returns a -1. BASIC displays the result if you type PRINT and t h e expression. For example, PRINT
"A">"B".
"CODE" > " C 00 L "
This is false. The first 2 characters of the strings match. How- ever, the third character does not. BASIC then compares the AS-
CII codes. The ASCII code for D is 68 and the code for 0 is 79.
Since 79 is not less than 68, BASIC returns a 0.
" T R A I L " < "TRA I L E R "
53
Chapter 51 Basic Concepts
This is true. If BASIC reaches the end of one string before find- ing 2 characters that don’t match, the shorter string is consid- ered the less of the two strings (lower in precedence). Therefore,
TRAIL
is the lesser of the two strings.
Also note that leading blanks are significant in string compari- sons. Therefore, ‘‘
A”
comes before
“A”
because the ASCII code for blank is 32 and the ASCII code for A is 65.
Logical
Operators
Logical operators, or Boolean operators, make logical compari- sons of numeric values. The logical operators are NOT, AND,
OR, XOR,
EQV, and IMP. They take a set of truelfalse values, usually from relational expressions, and return a true or false result.
The following table describes the result for given the described truelfalse values. each logical operator
Meaning of
Operator Operation
First
O p e r a n d
Second
O p e r a n d Result
NOT 1
Q
0
1
AND
OR
The result is the oppo- site of the operand.
When both values are true, the result is true. Otherwise, the result is false.
When both values are false, the result is false.
Otherwise, the result is true.
1
1
0
0
1
1
0
0
1
1
1
0
1
0
Q
0
XOR
EQV
IMP
When one of the values is true, the result is true.
Otherwise, the result is false.
When both values are true or both values are false, the result is true.
The result is true unless the first value is true and the second value is false.
1
1
0
0
1
1
0
0
1
1
0
0
1
0
1
0
1
8
1
0
1
0
1
0
0
1
1
0
1
0
1
1
1
0
0
1
54
Chapter 51 Basic Concepts
Normally, logical operators are used in IFiTHEN statements. For ex ample :
I F A = 1 O R C = 2 THEN P R I N T X
BASIC prints the variable X if 1 or both of the relational expres- sions are true. If both are false, BASIC does not print the vari- able x.
I F S $ = " T E X A S " CIND C $ = " A U S T I N " THEN P R I N T Z $
BASIC prints the value and of Z$ if
S$
contains the word TEXAS
C$ contains the word AUSTIN.
You may also use logical operators t o make bit comparisons of 2 numeric expressions. In this case, BASIC does a bit-by-bit com- parison of the 2 values, according t o predefined rules for the spe- cific operator. Note that the operands are converted t o integer type, stored internally as 16-bit, two's complement numbers.
This information is important when doing bit comparisons.
Hierarchy
of
Operators
BASIC uses a predefined hierarchy when performing operations on expressions with multiple operators. This list shows the oper- ators in the order that BASIC would perform the operations in a statement. Remember, BASIC evaluates statements from left t o right. Operators with the same level of hierarchy are shown on the same line.
A unary
-
* /
\
MOD
+ -
< >
=
< = > = <>
NOT
AND
OR, XOR
EQV
IMP
Consider this expression:
X
*
X
+
5^2.8
55
Chapter 51
Basic
Concepts
BASIC evaluates 5 to the 2.8 power first, then multiplies X*X, and finally adds the 2 values.
You can change the order of the hierarchy by adding parentheses to a n expression. BASIC always evaluates the expressions inside the parentheses before evaluating the rest of the expression.
Look a t
this
expression:
X
*
(X
+
5Y2.8
BASIC evaluates the expression value to the
(X
+
5) first and raises that
2.8 power before performing the multiplication.
If a n expression contains multiple parentheses, BASIC evaluates the innermost parentheses first.
Functions
A function is a built-in sequence of operations that BASIC per- forms on data. BASIC always performs functions first when eval- uating a statement.
Numeric functions, such as ABS, SQR, and COS, perform prede- fined operations on numeric data.
String functions, such as MID$, VAL$, and LEN$, perform oper- ations on string data.
Functions are described in Chapter 10.
56
Chapter
6
ARRAYS
An array is a group of related data values stored consecutively in memory. The entire group of data values is referred to by one variable name. Each data value is called an element of the array.
A subscript is a n integer used to refer t o each element of the ar- ray. For example, a n array named A may contain 3 elements re- ferred to as:
A(1) A(2) A(3)
Note: Normally array elements start with Element 0; however, t o simplify this example, it starts with Ele- ment 1. You can modify the initial array element base- number with the OPTION BASE statement.
You can use each of these elements t o store a separate data value, such as:
A(1)
=
.10
A(2)
=
.20
A(3) = .30
You can imagine a n array as a row of boxes, with the numbers on them t o identify them. Each box can hold a different value.
For example, Array A may hold your expenses.
Grocery
Expense
Gas
Expense
Clothes
Expense
This is a 1-dimensional array, because elements are arranged in a single row and only one subscript is used t o an element. For example, A(1) holds your grocery expense.
59
Chapter 61 Arrays
This program creates a 1-dimensional array:
5 C L S : O P T I O N B A S E 1
1 0 D A T A GROCERY,GAS,CLOTHES
2 0 D I M A ( 3 )
3 0 FOR C = 1 TO 3
4 0 READ NAMES$
5 0 P R I N T "ENTER THE "NAMES$" EXPENSE I N D O L L A R S "
6 0 I N P U T A ( C )
7 0 N E X T C
The DIM statement in Line 10 reserves space in memory for an array named
A
with 3 elements. As you enter the expenses, the grocery expense is stored in A(l), the gas expense in A(2) and the clothes expense in A(3).
Add these lines to the program to print the contents of Array A:
1 0 0 RESTORE
1 1 0 FOR C = 1 TO 3
1 2 0 READ NAMES$
1 3 0 P R 1 N T : P R I N T NAMES$
"
=
"
A ( C )
1 4 0 N E X T C
Use RUN to see the results of this program.
You can add more dimensions t o the array such as storing the expenses by weeks.
Col 1
Grocery
Col 2
Gas
Col 3
Clothes
Row 1
Week 1
Row 2
Week 2
Row 3
Week 3
A(3,2) =
Gas expense for Week 3
Row 4
Week 4
60
Chmter 61 Arrays
This is a 2-dimensional array. Each element is referred subscripts: t o by 2
A(row,column)
For example, A(3,2) points t o the third week's gas expense.
To make a 2-dimensional array from the earlier program, add the following lines:
2 5 FOR R = 1 TO 4 : R E S T O R E
7 5 N E X T R
1 0 5 FOR R = 1 TO 4 : R E S T O R E
1 5 0 N E X T R and change these lines:
2 0 D I M A ( 4 , 3 ) : W = 1
5 0 P R I N T "ENTER T H E " ; N A M E S $ ; " EXPENSES
L A R S FOR WEEK NO: "W
6 0 I N P U T A ( R , C )
7 0 N E X T C : W
=
W
+
1
1 0 0 R E S T O R E : W = 1
1 3 0 P R 1 N T : P R I N T N A M E S $ ; " EXPENSE FOR WEEK
NO: ";W;" = ";ACR,C)
1 4 0 N E X T C : W = W
+
1
I N D O L -
Run this program and see how it works. We simply added an- other subscript to the original array. Now instead of referring t o an element by a row number only, we refer to it by both a row and column number.
You can add yet another dimension, or subscript, to the array by adding these lines:
2 2 FOR P = 1 TO 2 : RESTORE
7 8 W = 1 : N E X T P
1 0 2 FOR P = 1 TO 2 : RESTORE
1 6 0 W = 1 : N E X T P and changing these lines:
20 DIM A < 2 , 4 , 3 ) : W
=
1 : M
=
1
5 0 P R I N T "ENTER T H E " ; N A M E S $ ; " EXPENSE I N D O L -
L A R S FOR WEEK NO:
6 0 I N P U T A ( P , R , C )
" ; W ; " OF MONTH NO: " ; M
7 5 N E X T R : M = M
+
1
1 0 0 R E S T O R E : W = 1 : M = 1
1 3 0 P R 1 N T : P R I N T N A M E S $ ; " EXPENSE FOR WEEK
NO: " ; W ; " OF MONTH N O : ";M;"
1 5 0 N E X T R : M = M
+
1
= " ; A C P , R , C )
Run the program t o see how it works.
61
Chapter 61 Arrays
Page 2
Month 2
Page 1
Month 1
Row 1
Week 1
Row 2
Week 2
Row 3
Week 3
Row 4
Week 4
Col 1
Grocery
Col 2
Gas
Col 3
Clothes
Imagine the third dimension as an entirely new page. Here, you refer to a n element in the array by using 3 subscripts:
A(page, row, column)
For example, in A(1,2,1), the first subscript (1) stands for the month. The second subscript (2) stands for the week and the third subscript (1) stands for the Grocery category. So A(1,2,1) contains the Grocery expense for the second week of the first month.
Types of Arrays
Arrays may be of any type: string, integer, single precision, or double precision. You can have a maximum of 255 dimensions in your array and a maximum of 32,767 elements in each dimen- sion.
The amount of memory that an array occupies is equal t o the number number of bytes it takes t o store that type of variable times the of elements. For example, if you have a double precision array of 30 elements, it occupies 240 bytes of memory. Remem- ber, double precision numbers are stored in 8 bytes of memory.
6 2
Chawter 61 Arravs
Defining Arrays
You can define arrays in your BASIC program by placing a DIM statement a t the beginning of your program or by setting the value of a n element in the program. For example: cIC5) = 3 0 0 automatically creates an array named A containing 6 elements and assigns element
A(5)
the value 388. Use this method only if your array contains fewer than 11 elements (8-10). If your array c o n t a i n s more t h a n 11 elements, you m u s t u s e t h e DIM statement.
Use a DIM statement t o reserve space in memory for each ele- ment of the array. For example:
D I M C#(99) creates Array C and reserves memory space for 100 double preci- sion elements.
See the DIM statement in Chapter 10 for more information on creating arrays.
63
ChaDter 7
DISK FILES
You may want t o store data on disk for future use. To do this, you need to store the data in a file. A file is a n organized collec- tion of related data. It may contain a mailing list, a personnel record, or almost any kind of information.
You access this information in records. A record is a small por- tion of data from the disk file such as a name and address in a mailing list file. A record is the largest block of information that you can address with a single command.
With BASIC you can create and access 2 types of files: sequen- tial access or direct access.
Sequential Access Files
With sequential access files, you can access data only in the same order as it was originally stored. To read from or write to a particular section in the file, you must first read through all the records in the file from the beginning until you get to the de- sired record.
Data is stored in a sequential access file as ASCII characters.
Therefore, it is ideal for storing free-form data without wasting space between data items. However, it is limited in flexibility and speed.
The statements and functions used with sequential files are:
WRITE #
PRINT#
LOC
INPUT#
PRINT USING # LINE INPUT#
EOF
LOF
OPEN
CLOSE
These statements and functions are discussed in more detail in
Chapter 10.
67
Chapter 71 Disk Files
Creating
a
Sequential Access File
1. To create the file, open it in Output mode (with the letter 0 ) and assign it a buffer number in the range 1 t o 15. For example:
OPEN "O", 1 , " l i 5 t . d a t "
OPEN " l i s t . d a t " FOR OUTPUT A S 1
Either form of the syntax for the OPEN statement opens a se- quential output file named list.&t and gives Buffer 1 access to this file.
2. To input data from the keyboard into 1 or more program vari- ables, use either INPUT or LINE INPUT. For example:
L I N E I N P U T , "NAME? " ; N $ inputs data from the keyboard and stores it in variable N$.
3. To write data to the file, use the WRITE# statement (you also can use PRINT#, but be sure you delimit the data). For example:
W R I T E # 1 , N $ writes variable N$ to the file, using Buffer 1 (the buffer used to open the file). Remember that data must go through a buffer before i t can be written t o a file.
4. To ensure that all the data has been written t o the file, use the CLOSE statement. For example:
CLOSE 1 closes access t o the file that uses Buffer 1 (the same buffer used to open the file).
Sample Program
1 0 OPEN " O " , 1 , " 1 1 5 t . d a t "
2 0 L I N E I N P U T "ENTER A NAME O R 'DONE' TO END
" ; N $
3 0 I F N $
=
"DONE" THEN 6 0
4 0 W R I T E # 1 , N $
5 0 P R I N T : G O T 0 2 0
6 0 CLOSE 1
>
68
Chapter 71 Disk Files
The file list.dat stores the data you input through the aid of the program, not the program itself. To save the program above, you must assign it a name. Use the SAVE command as described in Chapter 3. For example, SAVE "payroll.bas".
Every time you modify a program, you must save it again
(you can use the same name); otherwise, the original pro- gram remains on disk, without your latest corrections.
5. To access data in the file, reopen it, this time in the Input mode with the letter I. For example:
OPEN " 1 1 5 t . d a t " FOR I N P U T A S 1 opens the file named
Buffer 1.
1ist.dut for sequential input, using
6. To read data from the file and assign i t t o program variables, use either INPUT# or LINE INPUT#. For example:
I N P U T # 1 , N $ reads a string item into N$, using Buffer 1 (the buffer used when the file was opened).
L I N E I N P U T # 1 , N $ reads a n entire line of data into N$, using Buffer 1.
Sample Program
1 0 OPEN "I", 1 , " 1 i s t . d a t "
2 0 I F E O F ( 1 1 , THEN 1 0 0
3 0 I N P U T # l , N $
4 0 P R I N T N $
5 0 G O T 0 2 0
1 0 0 CLOSE 1
Updating a Sequential Access File
1. To add data to the file, open it in Append mode with the let- ter A. For example:
OPEN " A " , 1 , "list . d a t " opens the file 1ist.dat so that it can be extended. The data you enter is appended to the file 1ist.dat.
69
Chapter 71 Disk Files
2. To enter new data to the file, follow the same procedure as for entering data in the Output mode.
The following program illustrates this technique. It builds upon the file previously created.
Note:
Read through the entire program first.
If
you encounter BASIC keywords that are unfamiliar to you, refer to Chapter 10 for their definitions.
Sample Program
1 0 OPEN " A " , 1 ,
" 1 1 5 t . d a t "
2 0 L I N E I N P U T " T Y P E A NEW NAME O R PRESS < N > " ;
N $
3 0 I F N $
=
"N" THEN 6 0
4 0 W R I T E # 1 , N $
5 0 GOTO 2 0
6 0 CLOSE 1
If you want the program t o print on your display the informa- tion stored in the updated file, add the following lines:
7 0 OPEN " l i 5 t . d a t " FOR I N P U T AS 1
8 0 I F E O F C I ) T H E N 2 0 0
9 0 I N P U T # 1 , N $
1 0 0 P R I N T N $
1 1 0 GOTO 8 0
2 0 0 CLOSE 1
After you have run this program, save it. For example, type
SAVE "payroll2.bas" to save the program under a different name than the previous program.
Direct Access Files
With a direct access file, you can access data anywhere within the file. It is not necessary t o read through all the information, as with a sequential access file, because in a direct access file you can access each record of information individually by its number.
More program steps are required to create and access direct ac- cess files, but they are more flexible and easier to update than sequential access files.
70
Chapter 71 Disk Files
BASIC allocates space for records in numeric order. That is, if the first record you write to the file is number 200, BASIC allo- cates space for records 0 through 199 before storing record 200 in the file.
The maximum number of logical records is 16,777,215. Each record may contain a minimum of 1 and and a maximum of
3 2 7 68 bytes.
The statements and functions used with direct access files are:
OPEN
CLOSE
MKD$
CVD
LOC
FIELD
GET
MKI$
CVI
LOF
LSETiRSET
PUT
MKS$ cvs
These statements and functions are discussed in more detail in
Chapter 10.
Creating a Direct Access File
1. To create the file, open it for random access in Random mode
(''R'). For example:
OPEN " R " , 1 , " l i s t i n g . d a t " , 3 2 opens the file named listing.dat, gives Buffer 1 direct access t o the file, and sets the record length t o 32 bytes. (If you omit the record length, the default is 128 bytes.) Remember that data is passed t o and from the disk in records.
2. Use the FIELD statement t o allocate space in the buffer for the variables that will be written to the file. This is neces- sary because you must place the entire record into the buffer before putting it into the disk file. For example:
FIELD 1 , 20 A S N $ , 4 A S A $ , 8 A S P$ allocates the first 20 positions in Buffer 1 t o string variable
N$,
the next 4 8 positions to P$.
The variables N$, A$, and P$ are now "field names."
71
Chapter 71 Disk Files
3. To move data into the buffer, use the LSET statement. Nu- meric values must be converted to strings when placed in the buffer. To do this, use the make functions: MKI$ t o make a n integer value into a string, MKS$ for a single precision value, and MKD$ for a double precision value. For example:
L S E T N S = X S
L S E T A $ = M K S S C A M T )
4. To write data from the buffer to a record (within a direct ac- cess disk file), use the PUT statement. For example:
PUT 1 , CODE% writes the data from Buffer 1 to a record with the number
CODE%. (The percentage sign a t the end of a variable speci- fies that it is a n integer variable.)
The following program writes information to a direct access file:
1 0 OPEN " R " , 1 , " l i 5 t i n g . d a t " , 3 2
2 0 F I E L D 1 , 2 0 A S N S , 4 A S A S , 8 A S PS
30 I N P U T " 2 - D I G I T CODE, 0 TO END"; CODE%
4 0 I F CODE% = 0 THEN 1 3 0
5 0 I N P U T "NAME"; X S
6 0 I N P U T "AMOUNT"; AMT
7 0 I N P U T "PHONE"; T E L S
8 0 L S E T N S = X S
90 L S E T A S = M K S $ ( A M T )
1 0 0 L S E T P S = T E L S
1 1 0 PUT 1 , CODE%
1 2 0 GOT0 3 0
1 3 0 CLOSE 1
The 2-digit code that you enter in Line 30 becomes a record number. That record number stores the name(s1, amount(s1, and phone number(s) you enter when Lines 50, 60, and 70 are executed. The record is written to the file when BASIC exe- cutes the PUT statement in Line 110.
72
Chapter 71 Disk Files
After typing this program, save it and run it. Then, enter the following data:
2-DIGIT C O D E , 0 TO END? 20 IENTER]
2-DIGIT C O D E , 0 TO END? 0 [ENIER)
BASIC stores SMITH, file 1isting.bas.
34.55, and 567-9000 in Record 20 of
Accessing
a
Direct Access File
1. Open the file in Random mode:
OPEN " R " , 1 ,"listing.dat",32
2. Use the FIELD statement to allocate space in the buffer for the variables that will be read from the file. For example:
F I E L D 1 , 20 A S N $ , 4 A S A $ , 8 A S P $
3. Before you use the GET statement to read the record, you can check to see if the record is in your file. Set a variable in your program equal t o the record size you used in the OPEN statement. LOF returns the length of the file in bytes. The to- tal number of bytes in the file divided by the record size is equal to the largest record number in the file. An attempt t o access a record number greater than the largest record num- ber in the file results in a n "Input past end" error.
For example:
RECSIZE = 32
IF CODE% > ( L O F ( 1 ) / RECSIZE%) THEN 1000 rect disk file into a buffer. For example:
G E T 1 , CODE% gets the record numbered CODE% and reads it into Buffer 1.
73
Chapter 71 Disk Files
5. Convert string values back to numbers using the "convert" functions: CVI for integers, CVS for single precision values, and CVD for double precision values. For example:
PRINT N $
PRINT CVS(A$)
The program may now access the data in the buffer.
The following program accesses the direct access file list- ing.dat (created with the previous program). When BASIC ex- e c u t e s L i n e 3 0 , e n t e r a n y valid r e c o r d n u m b e r from
10 OPEN "R", 1 ,
" l l 5 t l n g . d a t " ,
32
20 FIELD 1 , 2 0 A S N$,4 A S A $ , 8 A S P $
30 RECSIZE% = 3 2
40 INPUT "2-DIGIT C O D E , 0 TO END"; CODE%
50 IF CODE% = 0 O R CODE% > (LOF(l)/RECSIZE%) THEN
1000
6 0 GET # I , CODE%
70 PRINT N $
8 0 PRINT USING
' I $ $ # . # # " ;
CVSCA$)
9 0 PRINT P$: PRINT
100 GOT0 40
1000 CLOSE 1
After typing this program, save it and run it. When BASIC asks you t o enter a 2-digit code, enter 20 (the record created through the previous program). Your display should show:
2-DIGIT C O D E , 0 TO END? 20
S M I TH
$34.55
567-9000
To update listing.dat, simply use LOAD to load the previous program (the one that created listing.dat)
74
Chapter
8
DISPLAYING COLOR AND GRAPHICS
Interpreter BASIC includes many commands to display text and graphic images in black and white and in color.
You first select a screen mode, which tells BASIC the size of the screen in points (resolution) and how many colors are available.
It also tells BASIC whether you are displaying text only or graphics.
Resolution
The number of points on the screen is called the resolution. The greater the number of points, the sharper the image. The 3 reso- lutions are:
Low resolution
Medium resolution
High resolution
160 x 200 points
320 x 200 points
640 x 200 points
The horizontal length (x axis) is given first followed by the verti- cal length (y axis).
Notice that there are more horizontal points than vertical points.
This means that the horizontal points are closer together than the vertical points. The comparison of the number of points per inch vertically t o horizontally is called the aspect ratio.
The aspect ratio is computed using the following formula: number of vertical points number of horizontal points aspect ratio
= - viewing area height viewing area width
The viewing area is the portion of your screen on which images are displayed. It may be smaller than the screen itself.
77
Chapter 8 I
Displaying
Color and Graphics
The aspect ratio is important when drawing objects on the screen. Keep in mind that the number of horizontal points per inch is greater than the same number of vertical points per inch.
Therefore, for example, if you try t o draw a square, the perime- ter of the square must contain more horizontal points than verti- cal points.
Also, because there is a difference in points per inch among the different screen modes, images that specify the same coordinates do not look the same in different modes.
Colors
Colors are selected by numbers that vary, depending on whether you are in a screen mode that supports 2, modes start with the background as black and the foreground as white. Each color set has a defined list of colors called a palette.
The characteristics of each mode are:
2
Color Set
is the black and white mode; the background is black and the foreground is white. You cannot change these colors.
4
Color Set
has 1 palette of
Screen Modes
4 colors available at all times in
4 and 6. The colors are numbered 0-3. Color 0 is the current background color. The other colors are:
Number Color
1
2
3 cyan magenta white
BASIC uses Color 3 as the foreground color if you do not specify a color parameter with graphic statements. Background can be any one of the 16 colors described in the 16 Color Set. See the
PALETTE statement for information on changing colors in the palette.
78
Chapter 8 i Displaying Color and Graphics
Screen Mode 1 has 2 palettes of 4 colors. Only 1 palette is avail- able a t a time. Color 0 is the current background color. The other colors are:
Number
1
2
3
Palette 0 green
, red hrown
Palette 1 cyan magenta white
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Color Set
has 1 palette of 16 colors available a t all times.
The colors are numbered 0-15. The colors available are:
Number Color black blue green cyan red magenta brown gray dark gray light blue light green light cyan light red light magenta yellow white
The foreground color is initially set to white and the background t o b l a c k . You c a n c h a n g e t h e s e colors w i t h t h e COLOR statement.
Video
Pages
BASIC sets aside memory t o be used for the video display. The amount of memory necessary depends on the screen mode you choose. BASIC initially sets aside 16K for video memory. You can set aside a different amount using the CLEAR statement.
79
Chapter 8 I Displaying Color and Graphics
If you set aside more video memory than is required for the se- lected screen mode, BASIC divides the video memory into pages like pages of a book. You can store information t o one page while displaying another. The page being displayed is called the play page and the page being written dis-
t o
is called the active page.
BASIC initially sets the display page and the active page to the same page.
The amount of memory required for each mode is detailed under
“Selecting Screen Modes.”
Selecting Screen Modes
You select the screen mode with the SCREEN command. BAS-
IC’s graphic screen modes are numbered 1-6.
Screen Mode
0
(Text Mode)
Color Set: 16
Graphics Resolution: not available
Text Width: 40 or 80
Video Page Size: If WIDTH=40, 2048 bytes, 8 pages max.
If WIDTH = 80, 4096 bytes, 4 pages max.
You can select different background, foreground and border colors using the COLORlText command. (Border is the last group of pixels around the perimeter of the screen display.) Foreground can be any of the 16 colors in either solid (Colors 8-15] or blink- ing mode (Colors 16-31). Background can be any of Colors 0-7.
Border is initially set the same as the background, but can be changed t o any of the 16 colors. You can change the text width with the WIDTH command.
Screen Mode
1
Color Set:
Graphics Resolution:
Aspect Ratio:
Text Width:
Video Page Size:
Max. No. of Pages:
4 (2 palettes) medium resolution
320 x 200
516
40
16384 bytes
8
80
Chapter 8 I Displaying Color and Graphics
Screen Mode
2
Color Set:
Graphics Resolution:
Aspect Ratio:
Text Width:
Video Page Size:
Max. No. of Pages:
Screen Mode
3
Color Set:
Graphics Resolution:
Aspect Ratio:
Text Width:
Video Page Size:
Max. No. of Pages:
Screen Mode
4
Color Set:
Graphics Resolution:
Aspect Ratio:
Text Width:
Video Page Size:
Max. No. of Pages:
Screen Mode
5
Color Set:
Graphics Resolution:
Aspect Ratio:
Text Width:
Video Page Size:
Max. No. of Pages:
2 high resolution
648 x 288
5112
88
16384 bytes
8
16 low resolution
168 x 208
513
28
16384 bytes
8
4 medium resolution
328 x 288
516
48
16384 bytes
8
16 medium resolution
328 x 288
516
48
32768 bytes
4
81
Chapter 8 I Displaying Color and Graphics
Screen Mode 6
Color Set:
Graphics Resolution:
Aspect Ratio:
Text Width:
Video Page Size:
Max. No. of Pages:
4 high resolution
640 x 200
5112
80
32768 bytes
4
Specifying Coordinates
To draw your graphics images on the display, you must tell
BASIC where to put the image on the screen. To do this, you must specify horizontal and vertical points.
The horizontal and vertical point numbers are known as the co- ordinates. Coordinates are expressed as x-coordinate, y-coordi- nate or simply x and y . (x is the horizontal point number, and y is the vertical point number.)
When you specify specific points as coordinates, they are re- ferred t o as absolute coordinates.
You may also, in some commands, specify coordinates relative to the current point on the screen. In this case, you specify the number of points from the last graphics point referenced. For ex- ample, if you use the CIRCLE statement t o draw a circle, the last point referenced is the center of the circle. If you then exe- cute a LINE statement, using relative coordinates of (0,0) t o specify the beginning point, BASIC begins drawing the line a t the center of the circle.
You may specify positive or negative values as relative coordi- nates. If you specify a negative value, BASIC subtracts it from the coordinate of the last point referenced. If you specify positive values, BASIC adds i t to t h e coordinate of t h e l a s t point referenced.
82
Chapter 9
INTRODUCTION TO BASIC KEYWORDS
BASIC is made up of keywords. These keywords instruct the computer to perform certain operations.
Chapter 10 describes all of BASIC’s keywords. This chapter ex- plains the format used in Chapter 10. It also gives a quick sum- mary of all of BASIC’s keywords.
Format for Chapter
10
Keyword
Statement
Function
Syntax
Brief definition of keyword.
Detailed definition of keyword and any parameters or argu- ments for that keyword.
Example(s1
Sample ProgramW
This format varies slightly, depending on the complexity of each keyword. For instance, some keywords require certain parame- ters or arguments and others do not.
Some keywords are followed by defining words that explain how t o use the command. The defining words are:
Communications used with RS-232 Communications
Graphics
Trap must be in Screen Modes 1-6 used for event trapping
There are more, but they should be self-explanatory.
85
Chapter 9 I Introduction to BASIC Keywords
Some keywords have sample programs use that further explain their or illustrate useful applications that may not be readily ap- parent.
Important Note:
BASIC for MS-DOS requires that keywords be delimited by spaces. This means that you must leave a space between a keyword and any varia- bles, constants, or other keywords. The only exceptions t o this rule are characters that are shown as part of the syntax of the keyword.
For example, if you type:
DELETE.=
BASIC returns a “Syntax error.” You must leave a blank space between t h e word DELETE and t h e period.
Terms
Used in Chapter
10 line A numeric expression t h a t identifies a
BASIC program line. Each line number in the range 0 to
65529.
has a integer Any integer expression. It may consist of a n integer or of several integers joined by operators. Integers a r e whole numbers and may be in the range
-32768
t o
32767
unless otherwise specified. string number dummy number or dummy string
Any string expression. It may consist of a string, several strings joined by opera- tors, or a string variable.
A
string is a sequence of characters that is to be taken verbatim.
Any numeric expression. It may consist of a number, several numbers joined by op- erators, or a numeric variable.
A number (or string) used in a n expres- sion t o meet syntactic requirements, but the value of which is insignificant.
86
Chaster 9 I Introduction to BASIC Keywords
Statements
A statement tells the computer to perform some operation. The following is a brief description of all BASIC statements:
Statement Description
AUTO
BEEP
BLOAD
BSAVE
CALL
CALLS
CHAIN
CHDIR
CIRCLEiGraphics
CLEAR
CLOSE
CLS
COLORiGraphics
COLORiTex
t
COMiTrap
COMMON
CONT
DATA
DEFDBL
DEF FN
DEFINT
DEF SEG
DEFSNG
DEFSTR
DEF USR
DELETE
DIM
DRAWiGraphics automatically generates line numbers. produces a sound from t h e computer speaker. loads a memory image file from disk. saves a memory image file to disk. calls a n assembly-language subroutine. calls a n MS-FORTRAN subroutine. loads another program and passes varia- bles to that program. changes the current directory. draws a n ellipse on the screen. frees memory for data without erasing the program in memory. closes access to a disk file. clears the screen. selects background and either foreground or palette depending on the screen mode. selects foreground, background, and bor- der display colors for Screen Mode 0. enables communications event trapping. passes variables to a chained program. continues program execution. stores data in your program so that you can access it with a READ statement. defines variables as double precision. defines a function according t o your specifications. defines variables as integers. defines the current segment address. defines variables as single precision. defines variables as strings. defines the offset
USR routines. of the entry point for removes program lines from memory. defines the dimensions of a n array. draws images on the screen.
87
Statement
EDIT
END
ENVIRON
ERASE
ERL
ERR
ERROR
FIELD
FILES
FOR/NEXT
GET
GET/Graphics
GOSUB
GOT0
IFITHENIELSE
INPUT
INPUT#
INPUT$
IOCTL
KEY
KEYiTrap
KILL
LCOPY
LET
LINEiGraphics
LINE INPUT
LINE INPUT#
Chapter 9 I Introduction to BASIC Keywords
Description
edits program lines. ends a program. modifies BASIC’s Environment String
Table. erases an array. returns the number of the line in which a n error occurred. returns a n error code after an error. simulates the specified error. organizes a direct access buffer. displays names of files and directories on a disk. establishes a program loop. gets a record from a direct access file or transfers a specific number of bytes from a communications file. transfers graphic images from the screen to memory. t r a n s f e r s p r o g r a m c o n t r o l t o a subroutine. transfers program control t o the specified line. evaluates a n expression and performs an operation if conditions are met. accepts data from the keyboard. accepts data from a sequential access de- vice or file. accepts data from the keyboard or a se- quential access file. sends control data t o a device driver. assigns or displays the current function- key soft values. enables key-event trapping. deletes a disk file. copies all text data on the screen t o the printer. assigns a value to a variable. (The key- word LET may be omitted.) draws a line on the display. accepts an entire line from the keyboard. accepts a n entire line from a sequential access file.
88
Chapter 9 I Introduction to BASIC Keywords
Statement Description
LIST
LLIST
LOAD
LOCATE
LPRINT
LPRINT USING
LSET
MERGE
MID$
MKDIR
NAME
NEW
NOISE
ON COM GOSUB
ONERRORGOTO
ON/GOSUB
ONiGOTO
ON KEY0 GOSUB
ON PEN GOSUB
ON PLAY0 GOSUB
ON STRIGO GOSUB
ON TIMER0 GOSUB
OPEN
OPEN “COM
OPTION BASE
OUT lists a program t o the display or printer. prints a program on the printer. loads a program from disk. positions the cursor on the screen. prints data a t the printer. prints data a t the printer in a specified format. moves data (and left-justifies it) to a field in a direct access file buffer. merges a disk program with a resident program. replaces a portion of a string. creates a directory. renames a disk file. erases a program from RAM. generates noise through a TV monitor’s speaker. branches t o a subroutine when activity occurs on the communication channel. sets up an error-trapping routine. evaluates a n expression and branches a subroutine. evaluates an expression and branches another program line. to t o branches t o a subroutine when a specific key is pressed. branches pen to a subroutine when the light is activated. branches t o a subroutine when music buffer contains fewer than the specified number of notes. branches t o a subroutine when a joystick button is pressed. branches t o a subroutine when timer equals the specified number. opens a disk file. opens a communications file. declares the minimum value subscripts. for array sends a byte t o a machine output port.
89
Chapter 9 I Introduction to BASIC Keywords
Statement Description
PAINT/Graphics
PALETTEiGraphics
PALETTE USING/
Graphics
PCOPY
PEN/Trap
PLAY
PLAYiTrap
POKE
PRESET/Graphics
PRINT USING
PRINT#
PRINT# USING
PSETiGraphics
PUT/Communications
PUTiGraphics
RANDOMIZE
READ
REM
RENUM
RESET
RESTORE
RESUME
RETURN
RMDIR
RSET
RUN fills in an area of the screen with a se- lected color. changes the color associated with a color number. changes the colors associated with more than one color. copies one video page to another. controls light pen event trapping plays musical notes. c o n t r o l s b a c k g r o u n d music e v e n t trapping. writes a byte into a memory location. draws a point in color a t a specified posi- tion on the screen. lists data t o the display. l i s t s d a t a t o t h e display in a specific format. writes data to a sequential access file. writes data
t o
a sequential access file us- ing the specified format. draws a point on the screen a t a specified position. puts a record into a direct access file or transfers a number of bytes t o a commu- nications file. transfers graphic images from the mem- ory to the screen. reseeds the random number generator. reads data stored in the DATA statement and assigns it t o a variable. inserts a remark line in a program. renumbers a program. closes all open files on all drives. restores the DATA pointer. resumes program execution after an er- ror-handling routine. returns from a subroutine program. t o the calling removes a directory. moves data (and right-justifies it) field in a direct access file buffer. executes a program.
t o
a
90
Chapter 9 I Introduction to BASIC Keywords
Statement
SAVE
SCREEN
SHELL
SOUND
STOP
STRIG
STRIG/Trap
SWAP
SYSTEM
TIMER/Trap
TROFF
TRON
VIE WIGraphics
VIEW PRINT
WAIT
WHILE
...
WIDTH
WINDOW
WRITE
WRITE#
Description
saves a program on disk. sets the screen attributes (text, medium- or high-resolution)
to
be used by subse- quent statements. loads and executes another program as a child process. generates a specific tone for a specified length of time. stops program execution. enables the STRIG function. controls joystick event trapping. exchanges the values of variables. returns to MS-DOS. controls timer event trapping. . turns off the tracer. turns on the tracer. redefines the screen parameters. creates a text viewport
to
redefine screen parameters. suspends program execution while moni- toring t h e s t a t u s of a machine i n p u t port. executes statements in a loop as long as a given condition is true. sets the number of characters per line for the screen or printer. changes the physicial coordinates screen. of the prints data on the display. writes data t o a sequential file.
91
Chawter 9 I Introduction to BASIC Keywords
Functions
A function is a built-in subroutine. You may only use it as part of a statement. Most BASIC functions return numeric or string data.
Function Description
ABS
ASC
ATN
CDBL
CHR$
CINT cos
CSNG
CSRLIN
CVD
CVI cvs
DATE$
ENVIRON$
EOF
ERDEV
ERDEV$
EXP
FIX
FRE returns the absolute value of a number returns the ASCII code of a character. returns the arctangent of a number. converts a number t o double precision. returns the character of an ASCII code. converts a number to an integer. returns the cosine of a number. converts a number to single precision returns the current row position of the cursor. restores data from a direct access disk file t o double precision. restores data from a direct access disk file t o integer. restores data from a direct access disk file t o single precision. sets the date or returns the current date. returns a string from BASIC’s Environ- ment String Table. checks for end-of-file or an empty commu- nications input queue. returns the value of a device error. returns the name error. of a device for device r e t u r n s t h e n a t u r a l exponent of a number. truncates to a whole number. returns the number not being used. of bytes in memory
92
Function
HEX$
INKEY$
INP
INSTR
INT
IOCTL$
LEFT$
LEN
LOC
LOF
LOG
LPOS
MID$
MKD$
MKI$
MKS$
OCT$
Chapter 9 I Introduction to BASIC Keywords
Description
converts a decimal value t o a hexadeci- mal string. returns the keyboard character. returns the byte read from a port. searches for a specified string. returns the integer value of a number. returns control data from a device driver. returns the left portion of a string. returns the length of the string. returns the current disk file record num- ber or the number of characters in a com- munications input queue. returns the total number of bytes in a disk file or the amount of free space in a communication file input queue. r e t u r n s t h e n a t u r a l l o g a r i t h m of a number. returns the position of the print head in the printer buffer. returns the midportion of a string. converts a double precision value t o a string for writing it t o a direct access file. converts an integer value t o a string for writing it to a direct access disk file. converts a single precision number t o a string for writing it to a direct access file. converts a decimal value string. t o a n octal
93
Function
PEEK
PEN
PLAY
PMAP
POINT
POS
RIGHT$
RND
SCREEN
SGN
SIN
SPACE$
SPC
SQR
STICK
STR$
STRIG
STRING$
TAB
TAN
TIME$
TIMER
USR
VAL
VARPTR
VARPTR$
Chapter 9 I Introduction to BASIC Keywords
Description
returns a byte from a memory location. returns the coordinates of the light pen. returns the number of notes in the music buffer. returns the physical or world coordinates. returns either the color rent coordinates. of a point or cur- returns the cursor column position on the display. returns the right portion of a string. returns a random number. returns the ASCII code for the character s t o r e d a t a specific p o s i t i o n on t h e screen. determines the sign of a number returns the sine of a number. returns a string of spaces. prints spaces t o the display. returns the square root of a number. returns the coordinates of the joysticks. converts a number to a string. returns the status of the joystick buttons. returns a string of characters. positions the video cursor or the print head a t a specified position. returns the tangent of a number. sets the time or returns the current time. r e t u r n s t h e number of seconds since midnight. calls a n assembly-language subroutine. returns the numeric value of a string. returns a n offset for a variable or buffer. r e t u r n s character form of memory ad- dress of a variable.
94
ChaDter 10
BASIC KEYWORDS
ABS
ABS (number)
Function
Returns the absolute value of number.
The absolute value of a number is the value without regard t o its sign. Absolute values are always positive or zero.
Example
P R I N T ABS(-66) prints the absolute value of -66 which is 66.
X = A B S ( Y ) computes the absolute value of Y and assigns it t o X.
Sample Program
1 0 0 I N P U T "WHAT'S THE TEMPERATURE O U T S I D E 7
(DEGREES F ) " ; T E M P
1 1 0 I F TEMP < 0 THEN P R I N T " T H A T ' S " A B S ( T E M P 1
"BELOW Z E R O ! B R R ! " : END
1 2 0 I F TEMP = 0 THEN P R I N T "ZERO DEGREES! M I T E
C O L D ! " : END
1 3 0 P R I N T TEMP "DEGREES ABOVE ZERO! B A L M Y ! " : END
97
Chapter 10 I BASIC Keywords
ASC Function
ASC(strZng)
Returns the ASCII code for the first character of string.
ASC returns the value as a decimal number. If
"Illegal function call" error occurs. string is null, an
Example
P R I N T A S C ( " A " 1 prints 65, the ASCII code for A.
Sample Program
You can use ASC t o be sure a program is receiving proper input.
Suppose you want t o write a program that requires the user to input hexadecimal digits (0-9,
A-F).
To be sure that only those characters are input, and all other characters are excluded, you can insert the following routine.
1 0 0 I N P U T "ENTER A H E X A D E C I M A L V A L U E " ; N $
1 1 0 A = A S C C N $ ) ' g e t A S C I I c o d e
1 2 0 I F A > 4 7 AND A < 5 8 O R A > 6 4 AND A < 7 1 THEN P R I N T
" O K . " : GOTO 1 0 0
1 3 0 P R I N T " V A L U E NOT O K . " : GOTO 1 0 0
98
Chawter 10 I BASIC Kevwords
ATN
ATN(number)
Function
Returns the arctangent of number.
ATN returns the angle (in radians) whose tangent is number.
Number must be given in radians.
BASIC always returns the result as a single precision number unless you specified the /D switch when starting up BASIC.
To convert this value t o degrees, use ATN(number*PI), where PI equals
3.141593.
Example
P R I N T A T N ( 7 ) prints the arctangent of 7 which is
1.428899
X = A T N ( Y / 3 ) * 5 7 . 2 9 5 7 8 computes the arctangent of t o x.
Y13
in degrees and assigns the value
99
Chapter 10 I BASIC Ke.ywords
AUTO
AUTO
[line] increment]
Statement
Automatically generates a line number when you are entering a program. AUTO displays a new line number every time you press m.
Line is the line number with which you want BASIC t o start numbering. To start numbering with the current line number, specify a period (.I as line. If you omit line, BASIC starts with
Line 10.
Increment is the increment for BASIC t o use when generating line numbers. You must precede increment with a comma (,). If you want BASIC to use the increment of the last AUTO state- ment, type the comma but omit increment. If you omit increment and the comma, BASIC uses 10. If you omit line but include in- crement, BASIC begins numbering with Line 0.
If BASIC generates a line number that already exists in mem- ing line, press
= generates the next line number.
To turn off AUTO, press [BREAK]. The current line is canceled and
BASIC returns to command level.
Examples
AUTO generates line numbers beginning with Line 10 using incre- ments of 10. For example, 10, 20, 30 ....
AUTO 1 0 0 , 5 0 generates line numbers beginning with Line 100 using incre- ments of 50. For example, 100, 150, 200 ....
AUTO 7 0 0 , generates line numbers beginning with Line 700 using the in- crement of the last AUTO statement, in this case 50. For exam- ple, 700, 750, 800 ....
100
Chapter 10 I BASIC Keywords
Statement
BEEP
BEEP ON
BEEP OFF
Produces a sound from the computer’s speaker.
BEEP sounds the speaker a t 800
Hz
for 114 second.
You can use BEEP with the SOUND statement t o direct sound t o the computer’s internal speaker or an external speaker (such as a television’s speaker) or both.
BEEP ON : SOUND ON d i r e c t s s o u n d speakers .
BEEP OFF : SOUND OFF t u r n s off s o u n d speakers. t o b o t h t o b o t h
BEEP ON : SOUND OFF
BEEP
OFF
: SOUND ON directs sound t o the internal speaker only. directs sound t o the exter- nal speaker only.
BASIC starts up with BEEP ON and SOUND OFF
The BEEP statement is the same as typing PRINT CHR$(7).
Example
I F X > 2 0 T H E N BEEP warns the operator with a beep if the variable that is, greater than 20.
X is out of range,
101
Chapter 10 I BASIC Keywords
BLOAD
BLOAD
pathname[,offsetl
Statement
Loads a memory image file into memory. See BSAVE
A memory image file is a byte-for-byte copy of what was origi- nally in memory. See BSAVE for information about saving mem- ory image files.
Pathname is a standard file specification as defined in Chapter
1.
Offset is a n integer in the range 0 to 65535. Offset is the num- ber of bytes into the current segment where BASIC loads the im- age. If you omit offset, BASIC uses the offset specified when the file was saved with BSAVE.
If you specify offset, BASIC assumes you want to BLOAD a t a n address other than the one given when the program was saved and uses the current segment address as set by the last DEF
SEG statement. Unless you want to load the file into BASIC’s data segment, you must execute the DEF SEG statement before the BLOAD statement.
If you used the
/M:
switch when you loaded BASIC, specify that address as the offset.
If you specify a n offset without using a DEF SEG statement or the /M: switch, BASIC loads the file a t that offset from BASIC’s data segment, destroying BASIC’s workspace.
Note:
BLOAD does not perform a n address r a n g e check.
It
is possible to load a file anywhere in mem- ory. Therefore, you must be careful not to load over
BASIC or over the operating system.
See the section “Interfacing With Assembly-Language Subrou- tines” in Chapter 11 for more information on loading assembly- language programs.
102
Chapter 10 i BASIC Keywords
You may specify any segment as the target or source for BLOAD or BSAVE. This is a useful way to save and redisplay screen im- ages by saving from or loading to the screen buffer.
Sample Programs
Program 1
1 0 ' S A V E A 5 0 b y t e i m a g e of m e m o r y
2 0 D E F SEG = b H l 0
3 0 FOR I = 2 5 6 t o 3 0 6
4 0 V L U E = P E E K ( 1 )
5 0 L P R I N T " A T ADDRESS " ; I ; "WE H A V E A V A L U E
OF " ; V L U E
6 0 N E X T I
7 0 B S A V E "prog1",0,50
8 0 P R I N T " N o w R u n P r o g r a m 2 t o v e r i f y t h a t t h e c o n t e n t 5 s a v e d in t h e f i l e P R O G l m a t c h t h o s e in t h e p r i n t o u t p r o d u c e d b y t h i s p r o g r a m . "
Program 2
1 0 'Load a 5 0 b y t e f i l e i n t o m e m o r y a n d v e r i f y i t
2 0 D E F SEG = & H 1 0
3 0 B L O A D "progl .bas",
4 0 F O R I = 2 5 6 t o 3 0 6
0
5 0 V A L U E = P E E K C I )
6 0 L P R I N T " A T ADDRESS " ; I ; " t h e l o a d e d v a l u e i
5 "
V A L U E
7 0 N E X T I
Program 1 saves a memory image file, and Program 2 reloads that file and prints it.
103
Chapter 10 I BASIC Keywords
BSAVE
%SAVE
pathname,offset,length
Statement
Saves the contents of an area of memory as a disk file.
Pathname is a standard file specification as defined in Chapter
1.
Offset is an integer in the range 0 to
65535.
Offset is the num- ber of bytes into the current segment where
BASIC
starts sav- ing.
Length is an integer in the range 1 t o
65535.
This is the length in bytes of the memory image file t o be saved.
You must specify pathname, offset, and length. If you omit any of them,
BASIC
returns an error and terminates the save.
A
memory image file is a byte-for-byte copy of what is in mem- ory. The
BSAVE
statement lets you save data or programs as memory image files on disk.
BSAVE
is often used for saving as- sembly language programs, but you can also use it t o save data, programs written in other languages, or screen images.
When you load the address
BASIC,
the data segment
(DS)
register is set to of
BASIC’s
workspace. You must execute a
DEF
SEG statement before executing switch when you loaded
BSAVE,
unless you used the /M:
BASIC.
Without the
DEF SEG
state- ment or the /M: switch,
BASIC’s
workspace could be destroyed.
Sample Program
See
BLOAD.
104
Chapter 10 I BASIC Keywords
CALL
CALL variable [(parameter list)]
Statement
Transfers program control to an assembly-language subroutine stored at variable.
Variable contains the offset into the current segment where the subroutine starts in memory. Variable may not be an array vari- able. The offset must be on a 16-byte boundary.
Parameter list contains the variables that are passed to the ex- ternal subroutine. The number, type, and length of the parame- ters being passed must match with the parameters expected by the assembly-language subroutine.
If you omit parameter list, BASIC executes an 8086 CALL in- struction. Your assembly-language subroutine should return with a simple RET instruction.
When you execute a CALL statement, BASIC transfers control to the subroutine through the address given in the last DEF statement and the segment offset specified by
SEG variable. See the section “Interfacing With Assembly-Language Subroutines” in
Chapter 11 for more details.
Example
1 0 0 I = 4 5 : J = 1 0 0 : K = 5 5
1 1 8 MYROUT = A H 0 8 8 8
1 2 0 DEF S E G = A H 1 7 0 0
1 2 0 C A L L M Y R O U T C I , J , K )
The subroutine, MYROUT, begins at offset 0 in the segment that begins at 1700. The values of I, J, and K are passed to the routine.
105
ChaDter 10 I BASIC Kevwords
CALLS
CALLS variables [(parameter list)]
Statement
Transfers program control t o a routine written in MS’” -FOR-
TRAN. CALLS works just like the CALL statement, except that
CALLS passes arguments as segmented addresses.
CALLS uses the address given in most recently executed
SEG statement t o locate the routine being called.
DEF
106
Chawter 10 I BASIC Kevwords
CDBL
CDBL(
num ber)
Function
Converts number to double precision.
This function may be useful if you want to force a n operation to be performed in double precision, even though the operands are single precision or integers.
Sample
Program
2 1 0 A = 4 5 4 . 6 7
2 2 0 P R I N T A , CDBLCA)
When run, this program prints the following:
4 5 4 . 6 7 4 5 4 . 6 7 0 0 1 3 4 2 7 7 3 4 4
107
Chawter 10 I BASIC Kevwords
CHAIN Statement
CHAIN [MERGE]
pathnume [,[,line]
[,DELETE
line-line]]
Lets the current program load and execute another program named pathname.
Pathname is a standard file specification as defined in Chapter
1. It specifies the program you want t o chain. The program must have been previously saved in ASCII format. See SAVE.
Line is the line number or variable containing a line number where BASIC begins execution in the chained program. Line is always preceded by a comma (J. If you plan to use the ALL or
DELETE options and do not specify a line number, you must specify a comma for line. This keeps BASIC from evaluating
ALL and DELETE as variables. If you omit line, BASIC begins execution a t the first program line of the chained program.
The ALL option tells BASIC t o pass every variable in the cur- rent program t o the chained program. If you omit ALL, the cur- rent program m u s t contain a COMMON statement t o pass variables t o the chained program. If chained programs chain subsequent programs and pass variables, each new program must contain either the ALL option or the COMMON statement.
The MERGE option overlays the lines of the chained program with the current program. See the MERGE statement t o under- stand how BASIC overlays (merges) program lines.
The DELETE option deletes lines in the overlay so that you can merge in a new overlay.
Ex s
C H A I N " p r 0 9 2 " loads prog2, chains it to the program currently in memory, and begins executing it.
C H A I N " s u b p r o g . b a 5 " , , A L L loads, chains and executes subprog.bas. The values of all the variables in the current program are passed t o subprog.bas.
108
Chapter 10 I BASIC Keywords
Sample Program
1
1 0 REM T H I S PROGRAM DEMONSTRATES C H A I N I N G U S I N G
COMMON TO P A S S V A R I A B L E S .
2 0 REM S A V E T H I S MODULE ON D I S K A S "PROGI .BAS"
U S I N G T H E A O P T I O N .
30 D I M A S C 2 ) , B S ( 2 )
4 0 COMMON A S 0 , B S O
5 0 A S < l ) = " V A R I A B L E S I N COMMON MUST B E A S S I G N E D
"
6 0 A $ ( 2 ) = " V A L U E S B E F O R E C H A I N I N G "
7 0 B S 1 1
= " I '
B S
= " I '
8 0 C H A I N " p r o g 2 . b a 5 "
9 0 P R I N T : P R I N T B S C I ) : P R I N T : P R I N T B S ( 2 ) :
P R I N T
1 0 0 E N D
Save this program as progl.bas,
A
option (Enter:
SAVE "progl.bas",
A).
Enter NEW, and then enter the following program.
1 0 REM T H E S T A T E M E N T " D I M A S ( 2 ) , B S ( 2 ) " MAY ONLY
B E E X E C U T E D O N C E .
20 REM H E N C E , I T DOES NOT APPEAR I N T H I S M O D U L E .
3 0 REM S A V E T H I S MODULE ON T H E D I S K A S
" P R O G 2 . B A S " U S I N G T H E A O P T I O N .
4 0 COMMON A S 0 , B S O
5 0 P R ' I N T : P R I N T A S ( l ) ; A S ( 2 )
6 0 B $ ( I ) = " N O T E HOW T H E O P T I O N OF S P E C I F Y I N G A
S T A R T I N G L I N E NUMBER"
7 0 B $ < 2 ) = " W H E N C H A I N I N G A V O I D S T H E D I M E N S I O N
S T A T E M E N T I N ' P R O G I
' .
"
8 0 C H A I N " p r o g l . b a 5 " , 9 0
9 0 E N D
Save t h i s program a s prog2.bas, A option. Load
V A R I A B L E S I N COMMON MUST B E A S S I G N E D V A L U E S
B E F O R E C H A I N I N G .
N O T E HOW T H E O P T I O N OF S P E C I F Y I N G A S T A R T I N G
L I N E NUMBER
WHEN C H A I N I N G A V O I D S T H E D I M E N S I O N S T A T E M E N T I N
' P R O G I '
.
109
Chapter 10 I BASIC Keywords
Sample Program
2
Enter NEW and this program:
1 0 REM T H I S PROGRAM DEMONSTRATES C H A I N I N G U S I N G
T H E MERGE AND ALL O P T I O N S .
2 0 A $ = " M A I N P R O G . B A S "
3 0 C H A I N MERGE " o v e r l a y l " , 1 0 0 0 , ALL
4 0 E N D
Save this program a s mainprog.bas,
NEW, and then type:
1 0 0 0 P R I N T A $ ; " H A S C H A I N E D TO O V E R L A Y 1 . B A S . "
1 0 1 0 A $ = " o v e r l a y l . b a s "
1 0 2 0 B $
= " o v e r l a y 2 . b a 5 "
1 0 3 0 C H A I N MERGE " o v e r l a y 2 . b a 5 " , 1 0 0 0 , A L L ,
D E L E T E 1 0 2 0 - 1 0 4 0
1 0 4 0 E N D
Save this program as overlayl.bas, using the A option. Enter
NEW, and then these lines:
1 0 0 0 P R I N T A $ ;
"
H A S C H A I N E D TO
1 0 1 0 E N D
";E$;"."
Save this program as overlay2.bas, using the A option. Load mainprog.bas and r u n it. Your screen should display:
M A I N P R O G . B A S H A S C H A I N E D TO O V E R L A Y 1 . B A S .
O V E R L A Y 1 . B A S H A S C H A I N E D TO O V E R L A Y 2 . B A S .
Hints:
The CHAIN statement with the MERGE option leaves the files open and preserves the current OPTION BASE setting.
0
The CHAIN statement without the MERGE option does not preserve variable types or user-defined functions for use by the chained program. That is, any DEFINT, DEFSNG, DEFDBL,
DEFSTR, or DEF FN statements containing shared variables must be restated in the chained program.
0
When using the MERGE option, place user-defined functions before any CHAIN MERGE statements in the program. Oth- erwise, the user-defined functions will be undefined after the merge is complete.
0
CHAIN automatically executes a RESTORE before running the chained program. The next READ statement starts a t the first item of the first DATA statement.
110
Chapter 10 I BASIC Keywords
CHDIR
CHDIR
dirpath
Statement
Changes the current directory.
Dirpath is a standard directory specification as defined in Chap- ter 1.
Examples
C H D I R "B: \ f ? C C T S \ R E C V B L E " changes the currect directory on Drive B to ACCTS\RECVBLE.
C H D I R "\RECORDS" changes the directory on the current drive to RECORDS.
C H D I R
" . ." changes the current directory to the parent directory of the cur- rent directory.
111
Chapter 10 I BASIC Keywords
CHR$
CHR$(code)
Function
Returns the character corresponding to a n ASCII or control code.
Code is any ASCII or control code.
CHR$ is the inverse of the ASC function and is commonly used to send a special character to the display.
See Appendix B for a list of ASCII codes.
Example
P R I N T C H R $ < 3 5 ) prints the character corresponding t o ASCII code
35,
which is #.
Sample Program
The following program lets you investigate the effect of printing
ASCII codes on the display.
1 0 0 C L S
1 1 0 I N P U T " T Y P E I N THE CODE"; C
1 2 0 P R I N T " C H R $ < C O D E ) = " ; C H R $ ( C )
1 3 0 G O T 0 1 1 0
112
Chapter 10 i BASIC Keywords
CINT
C
INT( num ber)
Function
Converts number t o integer.
Number must be in the range
-32768
t o
32767.
CINT rounds the fractional portion integer. of number t o make it an
See also FIX and INT, which also return integer values.
Examples
P R I N T C I N T ( 1 . 5 6 ) prints 2.
P R I N T C I N T C - 1 . 6 7 ) prints -2.
113
Chapter 10 I BASIC Keywords
CIRCLE/Graphics Statement
Draws an ellipse on the screen with the specified center and radius. x is the horizontal coordinate and y is the vertical coordinate.
Color indicates the color of the ellipse and must be a valid num- ber in the current color set.
The STEP option tells BASIC that the tive to the last point referenced.
The possible ranges for x, and color depend upon the current screen mode as defined in Chapter 8, “Displaying Color and
Graphics.
”
Radius is the major axis of the ellipse.
Start and end are the beginning and ending angles in radians and must be in the range -6.283186 to 6.283186, or -2
*
PI to
2
*
PI, where PI equals 3.141593. If you specify a negative start or end angle, the ellipse is connected t o the center point with a line, and the angles are treated as if they were positive.
Aspect is the ratio of the x-radius to the y-radius in terms of co- ordinates. If aspect is less than 1, radius is the x-radius and is measured i n points i n t h e horizontal direction. If greater than aspect is
1, radius is the y-radius and is measured in points in the vertical direction. If you omit aspect, BASIC uses the de- faults for the current screen mode as defined in Chapter 8. When you use the default, BASIC draws a circle.
114
Chapter 10 I BASIC Ke.ywords
To draw an ellipse that is wider than it is high, use an aspect ratio that is less than the default value for that screen mode.
The smaller the aspect ratio you specify, the wider and shorter the ellipse. For example, in Screen Mode 1, an aspect ratio of 1/2 gives you an ellipse like this:
To draw an ellipse that is higher than it is wide, use an aspect ratio that is larger than the default value for that screen mode.
The larger the aspect ratio that you use, the taller and thinner the ellipse. For example, in Screen Mode 1, an aspect ratio of 716 draws an ellipse like this:
115
Chapter 10 I BASIC Keywords
See Chapter 8 for more information on aspect ratio and specify- ing coordinates.
Examples
1 0 SCREEN 1
2 0 C I R C L E ( 1 5 0 , 1 0 0 ) , 5 0 draws a circle with the center at point 150,100 and a radius of
50.
Sample Program
1 0 SCREEN 1
2 0 FOR 1 = 0 TO 3
3 0 C L S
4 0 C I R C L E ( 1 5 0 , 1 0 0 ) , 5 0 , 1
5 0 P A I N T ( 1 5 0 , 1 0 0 1 , I
6 0 FOR Q = l
7 0 N E X T I
TO 3 0 0 : N E X T
8 0 SCREEN 0
Q
116
Chapter 10 I BASIC Ke-ywords
CLEAR Statement
CLEAR
[ , m e m o r y location.] [,stack space][,uideo memory1
Frees memory for data without erasing the program currently in memory. CLEAR erases all arrays, sets numeric variables t o zero and string variables to null, and erases any information set u s i n g a DEF s t a t e m e n t , such a s DEF SEG and DEF FN.
CLEAR also turns off the SOUND, PEN, and STRIG functions and resets the music background.
Since CLEAR initializes all variables, place it near the begin- ning of your program, before any variables have been defined and before any DEF statements.
Memory location must be an integer. It specifies the highest memory location available for BASIC. The default is the current top of memory as specified with the
/M:
switch when BASIC was loaded. This option is useful if you will be loading an assembly- language subroutine, because it prevents BASIC from using that memory area.
Stack space also must be an integer. This sets aside memory for temporarily storing internal data and addresses during subrou- tine calls and during FOR/NEXT loops. If you omit
stack space,
BASIC sets aside
768
bytes or one-eighth of the memory avail- able, whichever is smaller. BASIC displays an “Out of memory” error if stack space for program execution is insufficient.
Video
memory is an integer that specifies the amount of memory to be set aside as video memory. For graphics modes, if the v d e o memory you specify is not a multiple of
16K
bytes
(16384
bytes),
BASIC rounds it down to the nearest multiple omit of
16K.
If you video memory, BASIC sets aside
16K.
The video memory page size for each screen mode
is
described in Chapter
8,
“Dis- playing Color and Graphics.”
117
Chapter 10 I BASIC Keywords
If you set video memory larger than the required page size,
BASIC divides the assigned memory into pages based on the page size requirement. For example, if you set aside 32K for video memory for Screen Mode 1, BASIC divides the video mem- ory into 2 pages ( 3 2 / 1 6 = 2 ) . See Chapter 8, SCREEN, and
PCOPY for more on video pages.
Note:
BASIC allocates s t r i n g space dynamically.
BASIC displays a n “Out of string” space error if no free memory is left for BASIC.
Ex s
CLEAR clears all variables and closes all files.
C L E A R , 4 5 0 0 0 clears all variables and closes all files; then makes 45000 the highest address BASIC may use to run your programs.
C L E A R , 6 1 0 0 0 , 3 0 0 clears all variables and closes all files; then makes 61000 the highest address BASIC may use to run your programs, and allo- cates 300 bytes for stack space.
CLEAR , , , 3 2 7 6 8 sets aside 32K for video memory.
118
Chapter 10 I BASIC Keywords
Statement
CLOSE
...I
Closes access t o a disk file.
Buffer is the number assigned t o the file when you opened it. If you omit buffer, BASIC closes all open files.
This command terminates access to a file through the specified buffer. If buffer has not been assigned by an OPEN statement, then CLOSE buffer has no effect.
Do not remove a disk that contains an open file. Close the file first, because the last records may not have been written yet.
Closing the file writes the data currently in the buffer if it has not been written already.
Note that CLEAR, END, NEW, RESET, and SYSTEM automat- ically close all files when executed.
See also OPEN and Chapter 7, “Disk Files.”
Examples
C L O S E 1 , 2 , 8 terminates the file assignments t o Buffers 1, 2, and 8. You can now assign these buffers t o other files with OPEN statements.
C L O S E F I R S T %
+
C O U N T % terminates the file assignment t o the buffer specified by the sum
FIRST%
+
COUNT%.
119
Chapter 10 I BASIC Keywords
CLS
CLS
Statement
Clears the screen and returns the cursor t o the home position.
Home is Row 0, Column 0, or in other words, the upper left cor- ner of the screen.
If a viewport is active, CLS clears only the active viewport. To clear the entire screen, you must use VIEW to redefine the en- tire screen before using CLS.
Changing the screen mode with SCREEN or changing the width with WIDTH automatically clears the screen. You can also clear the screen by typing [CTRL][T] [CTRL)[HOME].
Sample Program
5 4 0 C L S
5 5 0 F O R I
=
1 T O 24
5 6 0 P R I N T S T R I N G $ ( 7 9 , 3 3 )
5 7 0 N E X T I
5 8 0 G O T O 5 4 0
~
120
Chapter 10 I BASIC Keywords
COLOR/Graphics
Screen Mode 1:
COLOR
[background] [,[palette11
Screen Modes
COLOR
3-6:
[foreground]
,
background11
Statement
Selects the background color and either the palette or foreground colors, depending on the current screen mode.
Background specifies the background and border colors. It may be 0-15 a s described i n C h a p t e r 8, “Displaying Color a n d
Graphics.
”
Foreground specifies the foreground color. In Screen Modes
3
and
5, foreground may be Colors 1-15. In Screen Modes 4
6 ,
fore- ground may be Colors 1-3. Specifying 0 as the foreground color causes a n “Illegal Function Call” error. Colors are described in
Chapter 8, “Displaying Color and Graphics.”
Note:
If you set foreground the same as background, the characters are invisible.
Palette specifies which palette to use and may be 0 or 1 t o select
Palette 0 or Palette 1. After executing the COLORiGraphics statement, you may use the PALETTE and PALETTE USING statements to change any or all these values. See PALETTE
AND PALETTE USING.
If you omit any parameters, BASIC assumes the default values or previous values.
Examples
1 0 C O L O R 9 , 0 sets background to light blue and selects Palette
Mode 1.
0 in Screen
2 0 C O L O R , I background retains its previous value. Palette 1 is selected in
Screen Mode 1.
121
ChaDter
10
I BASIC Keywords
Sample
Programs
5 S C R E E N 5
1 0 C O L O R 12,l
20 L I N E ( 0 , 0 ) - ( 3 1 9 , 1 9 9 )
Line 10 selects a light red foreground with a blue background.
Line 20 draws a light red diagonal line on the display.
5 S C R E E N 1
1 0 C O L O R 3 , 0
20 L I N E ( 0 , 0 )
-
( 3 1 9 , 1 9 9 )
Line 10 selects a cyan background and Palette 0. Line 20 draws a brown diagonal line on the video display. If you select Palette 1 in Line 10, Line 20 draws a white diagonal line.
122
Chapter 10 I BASIC Ke.ywords
C
OLOR/Text
COLOR [foreground]
,
background]
Statement
Selects the display colors border for the foreground, background, and for displaying in text mode. To be in text mode, you must have selected Screen Mode 0 with the SCREEN statement.
Foreground is an integer in the range 0 to 31, specifying the foreground color. Colors ure the color, add
16-31
cause blinking foreground. To fig-
16
to the color numbers defined in the
16
Color
Set in Chapter 8.
Background is an integer in the range 0 to 7, specifying the background color.
COLORiText can use any of the colors in the
16
Color Set as fore- ground. See Chapter 8 for more information.
Note:
If you set foreground the same as background, the characters are invisible.
Border is an integer in the range 0 to 15, specifying the border color.
If you omit any parameter, BASIC assumes the previous values or the default values.
Examples
COLOR 0 , 7 selects black characters on a white background.
COLOR 1 , 0 selects blue characters on a black background.
COLOR 4 , 0 selects red characters on a black background.
123
Chapter 10 I BASIC Ke.ywords
COM
COM(channe1) action
Statement
Turns on, turns off, or temporarily halts the trapping of activity on the specified communications channel.
Channel selects communications channel 1 or
2.
Action may be any of the following:
ON
OFF
STOP enables communications trapping. disables communications trapping. temporarily suspends communications trapping.
Use the COM statement in a communications trap routine with the ON COM() GOSUB statement t o detect when characters have come into the communications channel.
The COM( ) ON statement turns on the trap. BASIC checks after every program statement t o see if a character has come into the communications channel. If so, BASIC transfers program control t o t h e line n u m b e r specified i n t h e ON C O M ( ) GOSUB statement .
The COM( ) STOP statement temporarily halts communications trapping. If activity occurs on the communications channel,
BASIC does not transfer program control to the ON COM( 1 GO-
SUB statement until you t u r n on communications trapping again by executing a COM() ON statement. BASIC remembers that activity took place and branches t o the subroutine immedi- ately after communications trapping is turned on again.
The COM( ) OFF statement turns off communications activity trapping. BASIC does not remember if activity took place when communications trapping is turned on again.
We recommend that your trap routine read the entire message from the communications port. Do not use a COM trap t o trap a single character message because the amount of time required t o trap and read every character can cause the communications buffer t o overflow.
124
Chapter 10 I BASIC Keywords
See ON COM( ) GOSUB for more information about communica- tions trapping.
Example
1 0 COM(1) O N
2 0 P R I N T "NO A C T I V I T Y "
3 0 ON COM(1 1 GOSUB 1 0 0
4 0 G O T 0 2 0
1 0 0 P R I N T "YOU ARE R E C E I V I N G D A T A "
2 0 0 R E T U R N
Line 10 turns on a communications trar, on Channel 1. If char- acters are received on the communications channel, program control transfers to the subroutine beginning at Line 100. If there is no activity on the communications channel, Line 20 prints a message, and Line 40 keeps the program in a loop until there is activity on the communications channel.
125
Chapter
10
I BASIC Keywords
COMMON
COMMON
. .I
Statement
Passes Variables to a chained program.
Both programs in the chain should contain a COMMON state- ment. COMMON may appear anywhere in a program, but we recommend using it at the beginning.
The same variable cannot appear in more than one COMMON statement in a single program. The size and order of the vari- ables must be the same in the programs being chained. To spec- ify array variables, append “0” to the variable name. If you are passing all variables, use CHAIN with the ALL option and omit the COMMON statement.
Note:
Array variables used in a COMMON statement must have been declared in a DIM statement.
See t h e CHAIN statement for more information on passing variables.
Example
9 0
1 0 0
D I M D ( 5 0 )
C O M M O N A , E, C , D O , G $
CHFI I N ” P R O G 3 ” , 1 0
Line 100 passes variables A, B, C, D, and command in Line 110.
G$ to the CHAIN
126
Chawter 10 I BASIC Kevwords
CONT
CONT
Statement
Resumes program execution.
You may only use CONT if the program has been stopped by
[CTRL] [BREAK] or the execution of a STOP or a n END statement.
CONT is primarily a debugging tool. During a break or stop in execution, you may examine variable values (using PRINT) or change these values. Then type CONT (ENTER] to continue execu- tion with the new variable values.
You cannot use CONT after editing your program lines or other- wise changing your program. CONT is also invalid after execu- tion has ended normally.
See the STOP statement to terminate execution and the GOT0 statement to begin execution a t a specific line number.
Example
1 0 I N P U T "ENTER 3 NUMBERS a , b , c " ; A ,
2 0 K = A " 2
3 0 L = B A 3 / . 2 6
4 0 S T O P
5 0 M = C + 4 0 * K + 1 0 0 : P R I N T M
B , C
Run this program. BASIC prompts for 3 numbers. Type:
1 , 2 , 3 (ENTERJ
The computer displays "Break in 40." You can now enter a BASIC statement as a command. For example:
P R I N T L
[ENTER] displays 30.76923. You can also change the value of A,
For example, to change the value of C, type:
B,
or C. c = 4
Now type:
CONT [ENTER] and BASIC displays 144.
127
Chapter 10 I BASIC Ke.ywords cos
COS( num ber)
Function
Returns the cosine of number.
COS returns the angle (in radians) whose sine is number.
Number must be given in radians. If number is in degrees, you can convert it t o radians by using COS (number
PI equals 3.141593.
*
PI/180), where
BASIC always returns the result as a single precision number unless you specified the /D switch when starting up BASIC.
Examples
P R I N T C O S ( 5 . 8 ) - COS(85 * . 4 2 ) prints the arithmetic (not trigonometric) difference cosines. of the 2
Y = C O S C X . 0 1 7 4 5 3 3 ) stores in Y the cosine of X, if X is an angle in degrees.
128
Chapter 10 I BASIC Keywords
CSNG
C SNG(
num ber)
Function
Converts number to single precision.
.BASIC
rounds the number when converting it to single precision.
Example
P R I N T C S N G C . 1 4 5 3 8 8 5 5 0 9 ) prints .1453885
Sample Program
2 8 0 V U = 8 7 6 . 2 3 4 5 6 7 8 U
2 9 0 P R I N T V # , C S N G C V # )
When run, this program prints:
8 7 6 . 2 3 4 5 6 7 8 8 7 6 . 2 3 4 6
129
Chapter 10 I BASIC Keywords
CSRLIN
CSRLIN
Function
Returns the current row position of the cursor.
See the POS function to return the current column position and the LOCATE statement t o set the row and column positions.
Example
1 0 P R I N T "This i s Line":
20 P R I N T C S R L I N
130
Chapter
10 I BASIC
Keywords
CVD, CVI, CVS
C V D ( 8 - byte string)
CVI(2-byte string)
CVS(4-byte string)
Function
Converts string values to numeric values.
These functions restore data to numeric form after it is read from the disk. Typically, the data has been read by a GET state- ment and is stored in a direct access file buffer.
CVD converts an 8-byte string t o a double precision number.
CVS converts a 4-byte string to a single precision number.
CVI converts a 2-byte string t o an integer.
CVD, CVI, and CVS are the inverse
MKS$, respectively. of MKD$, MKI$, and
Examples
A # = C V D < G R O S S P A Y $ ) assigns the numeric value variable A#. of GROSSPAY$ t o the double precision
Sample Program
This program reads from the file test.dat, which is created in the sample program for the MKD$, MKI$, and MKS$ functions.
1 4 2 0 O P E N "R", 1 , " t e ~ t . d a t " , 1 4
1 4 3 0 F I E L D 1 , 2 A S I 1 $ , 4 A S I 2 $ , 8 A S 13s
1 4 4 0 G E T 1
1 4 5 0 P R I N T C V I < I l $ ) , CVS(I2$), CVD(I39)
1 4 6 0 C L O S E
BASIC prints 3000, 3000.1 and 3000.00001.
Note:
GET without a record number tells BASIC to get the first record from the file or the record follow- ing the last record accessed.
131
Chapter 10 I BASIC Keywords
DATA
DATA
constant [,constant,.
. .]
Statement
Stores numeric and string constants t o be accessed by a READ statement.
This statement may contain as many commas) as can constants (separated by
fit
on a line (a maximum of 255 characters in- cluding the word DATA, commas, and spaces).
DATA statements may appear anywhere it is convenient in a pro- gram. BASIC reads DATA statements sequentially, starting with the first constant in the first DATA statement and ending with the last item in the last DATA statement.
String constants containing delimiters, such as leading or trail- ing blanks, colons, or commas, must be enclosed in double quota- tion marks when used in DATA statements.
The data types in a DATA statement must match with the vari- able types in the corresponding READ statement, otherwise,
BASIC displays a “Syntax error.”
Note that numeric expressions are not allowed in a DATA state- ment.
To reread DATA statements from t h e beginning, use a RE-
STORE statement before the next READ statement.
132
Chapter 10 I BASIC Keywords
Examples
DATA NEW Y O R K , C H I C A G O , LOS A N G E L E S ,
P H I L A D E L P H I A , D E T R O I T stores 5 string data items. Quotation marks are not needed since the strings contain no delimiters and the leading blanks are not significant.
DATA 2 . 7 2 , 3 . 1 4 , 0 . 0 1 7 4 5 , 5 7 . 2 9 5 7 8 stores 4
DATA " S M I T H , T . H . " , 3 8 , "THORN, J . R . " , 4 1 stores both types
of
constants. Quotation marks are required around the first and third items because they contain commas.
Sample Program
1 0 PR I NT "C I T Y "
,
" S T A T E " , " 2
2 0 READ C S , S $ , Z
3 0 DATA "DENVER
,",
COLORADO, 8 0 2 1 1
4 0 P R I N T C S , S S , Z
This program reads string and numeric data from the DATA statement in Line
30.
133
Chapter 10 I BASIC Ke-ywords
DATE$
DATE$[
=
string]
Function
Sets the date or retrieves the current date.
String is a literal, enclosed in quotation marks, that sets the current date by assigning a value to DATE$. If you omit string,
BASIC retrieves the current date.
Setting the Date
BASIC uses the same dates as MS-DOS, January 1, 1980 t o De- cember 31, 2099. You may use either a slash or a hyphen t o sep- arate the month, day, and year. You may use any of the following forms to set the current date: mmlddlyy mm-dd-yy mmlddlyyyy mm -dd-YYYY
The
month (mm)
The day
The year (yy or yyyy) may be 01-99 or 1980-2099.
You may omit leading zeroes for the month and day. If you only supply 2 digits for the year, BASIC precedes these digits with
19.
Retrieving the Date
Regardless of the form you use t o set the date, BASIC retrieves the date in the following form: mm-dd-y y y y
The month and day are always returned as 2 digits, BASIC in- serts zeroes as necessary.
134
Chapter 10 I BASIC Ke.ywords
Examples
D A T E S = " 9 / 6 / 8 4 "
D A T E $ = " 9 / 6 / 1 9 8 4 "
D A T E S = " 9 - 6 - 8 4 "
D A T E S = " 9 - 6 - 1 9 8 4 "
All
the above set the current date as 09-06-1984.
P R I N T D A T E S prints the current system date.
CURDATES = D A T E S a s s i g n s t h e v a l u e
CURDATE$.
of t h e c u r r e n t d a t e t o t h e v a r i a b l e
135
Chapter 10 I BASIC Keywords
DE FDBL/INT/SNG/STR
DEFDBL
DEFINT
...I
. .I
DEFSNG letter[,letter,
DEFSTR
...I
... 1
Statement
Defines any variables beginning with letteris) as: double preci- sion (DBL), integer (INT), single precision (SNG), or string
(STR).
You may specify letter as a range of letters. For example,
A-J.
Remember, a type declaration tag always takes precedence over a
DEF
statement.
Examples
D E F D B L L - P classifies all variables beginning with the letters L through P as double precision variables.
D E F S T R A classifies all variables beginning with the letter variables.
A
as string
D E F I N T I - N , W,Z classifies all variables beginning with the letters
W, and Z as integer variables.
I through N,
D E F S N G I , Q - T classifies all variables beginning with the letters I or Q through
T as single precision variables.
136
Chawter 10 I BASIC Kevwords
DEF FN
Statement
DEF FNname [(argument
list)]
Defines mme a s a function according to the expression.
Name must be a valid variable name. The type of variable you use determines the type of value the function returns. For exam- ple, if you use a single precision variable, the function returns single precision values. This name, preceded by FN, is the name of the function when you call it.
Argument list is a list of dummy variables used in expression.
They are replaced on a one-to-one basis with the variables or values given when the function is called. If you enter several variables, separate them with commas. These variables do not affect variables in your program with the same name.
Expression defines the operation to be performed.
A
variable used in a function definition may or may not appear in argument list. If i t does,
BASIC
uses the value given when the function is called to perform the function. Otherwise, it uses the current value of the variable.
Once you define and name a function (by using this statement), you can use it as you would any
BASIC
function.
Examples
D E F FNR = RND ( 1 ) * 8 9 + 1 0 defines a function FNR to return a random value between 10 a n d 99. Notice t h a t t h e function c a n be defined w i t h no arguments.
2 1 0 D E F FNW#
2 2 0 I # =
< A # , B # ) = ( A # - B # ) * < A # - B # )
3 4 5 . 9 9 8
230 J # = 1 5 0 . 6 6 7
2 4 0 T = F N W # ( I # , J # )
2 5 0 P R I N T T defines function FNW# in Line 210 using dummy variables and
A#
B#. Line 240 calls the function and replaces variables
A#
a n d
B #
w i t h v a r i a b l e s I# a n d J # which a r e used i n t h e program.
137
Chapter 10 I BASIC Keywords
DEF SEG
DEF SEG[
=
address]
Statement
Assigns the current segment address. The segment address is used by BLOAD, BSAVE, CALL, PEEK, POKE, and USR.
Address is a number in the range 0 to 65535, and may be speci- fied a s a n integer or a hexadecimal value. If you specify a num- ber outside this range, BASIC returns a n “Illegal function call” error and uses the previously set address.
BASIC sets the current segment address
(DS).
If you omit address, t o its data segment
If you specify address, do so on a 16-byte boundary. BASIC shifts the value to the left 4 bits, which is the same as multiply- ing it by 16 decimal (10 hexadecimal).
Note:
BASIC does not check the validity of the result- ant segment
+ offset address.
When you load BASIC, the data segment (DS) register is set t o the address of BASIC’s workspace. You must, therefore, execute a
DEF SEG statement before executing BLOAD, BSAVE, PEEK,
POKE, USR, or CALL (unless you used the /M: switch when you loaded BASIC). Without the DEF SEG statement or the i M : switch, BASIC’s workspace could be destroyed.
If you execute a DEF SEG to change the DS register, you must execute another DEF SEG to restore the DS register t o BASIC’s data segment (DS).
Separate DEF and SEG with a space. Otherwise, BASIC inter- prets it as the variable DEFSEG.
138
Chapter 10 I BASIC Ke.ywords
See the section “Interfacing With Assembly-Language Subrou- tines” in Chapter 11 for more information.
1 0 DEF S E G = & H B 8 0 0
20 D E F S E G
’Set s e g m e n t
‘ R e s t o r e t o t o & 8 0 0 H e x
B A S I C d a t a s e g m e n t sets the DS register to B8000 hexadecimal (B800H which is its default value.
*
10H),
139
Chapter 10 I BASIC Keywords
DEF USR
Statement
=
offset
Defines the user number and segment offset of a subroutine to be called by the USR function.
Number may be an integer in the range 0 to 9. If you omit num- ber, BASIC assumes USR0.
Offset is an integer in the range 0 t o 65535. BASIC computes where the subroutine begins in memory by adding the offset to the current segment address as set by DEF SEG. BASIC trans- fers control to this address when you execute the USR function.
If the subroutine is not in BASIC’s data segment, you must exe- cute a DEF SEG statement before the USR function.
A program may contain any number of DEF USR statements, al- lowing access to as many subroutines as necessary. However, only 10 definitions may be in effect at one time.
See the section “Interfacing With Assembly-Language Subrou- tines” in Chapter 11 and USR in this chapter for more details.
Examples
DEF U S R 3 = 6 H 0 0 2 0
D E F SEG = & H I 7 0 0
USR3 begins at 20H bytes into the current data segment which is set at 1700 hexadecimal. When your program calls USR3, con- trol branches to your subroutine beginning
17020. (1700*10
+
20). at absolute address
140
Chapter 10 I BASIC Ke.ywords
DELETE
DELETE linel -line2
Statement
Deletes linel through line2 of the program in memory.
If you omit gram. If linel, BASIC deletes from the beginning of the pro- omit ZineZ, t o the end of the program.
If you specify a line number that does not exist, BASIC displays a n “Illegal function call” error.
You can substitute a period (.) for either linel or line2 to indicate the current line number.
Examples
D E L E T E 7 0 deletes Line
70
from memory.
D E L E T E . - I 1 0 deletes from the current line to Line
110,
inclusive.
D E L E T E - 4 0 deletes all program lines up t o and including Line
40
D E L E T E 1 5 0 - deletes program lines starting at and including
150
to the end of the program.
141
Chapter 10 I BASIC Keywords
DIM
Statement
DIM
. . .I
Sets aside storage for arrays with the dimensions you specify.
Array
is the variable name of the array.
It
may be a string, inte- ger, single precision, or double precision variable.
Dimension is 1 or more integer numbers separated by commas that define the dimensions of the array. The lowest element in a dimen- sion is always zero, unless a n OPTION BASE 1 statement is executed.
When you execute the DIM statement, BASIC reserves space in memory for each element of the array. Each element is initially set to zero for numeric arrays or null for string arrays.
If you do not dimension a n array, the maximum number of ele- ments it can have is 11 (0-10).
Remember that arrays are completely independent of variables that have the same name; that is MN and MNO are unique.
For more information on arrays, see Chapter 6.
Examples
DIM A R ( 1 0 0 ) sets up a 1-dimensional array ARO, containing 101 elements:
AR(0), AR(l), AR(2), ..., through AR(100).
D I M L l % ( 8 , 2 5 ) sets up a 2-dimensional array Ll%(), containing 9 x 26 integer elements.
142
C h w t e r 10 I BASIC Keywords
DRAW/Graphics
DRAW
string
Statement
Draws an image on the screen
String specifies 1 or more of the movement commands listed be- low. String must be enclosed in quotation marks.
Movement commands
Each of the following movement commands begins movement from the current graphics position, which is the coordinate of the last graphics point plotted with another graphics command, such as LINE or PSET. The current position defaults to the center of the screen if no previous graphics command has been executed.
Moves up n points.
Moves down n points.
Moves left n points.
Moves right n points.
Moves diagonally up and right n points.
Moves diagonally down and right n points.
Moves diagonally down and left n points.
Moves diagonally up and left n points.
Moves minus t o point x,y. If you precede x with a plus (
+
) or
(-1 sign, DRAW assumes it is a relative posi- tion. Otherwise, it is an absolute position.
143
Chapter
10
I BASIC Keywords
Prefix
Commands
The following prefix commands can precede the movement com- mands. Prefix commands must be enclosed in quotation marks.
B
N
Aangle
Ccolor
Pcdor, border
Sfactor plots no points after move. returns t o original position when move is complete. sets angle of move. Angle may be in the range 0 t o 3
(0
=
0 degrees, 1
=
90 degrees, 2
=
180 degrees, and 3 = 270 degrees). sets color as described in Chapter 8, “Displaying Color and Graphics.” sets the color t o paint and border color at which t o stop painting. Possible colors are described in Chapter
8, “Displaying Color and Graphics.” sets scale factor. Factor is an integer in the range 1 to
255. The scale factor is factor divided by ple, if factor is 2, the scale factor is 214. To determine the actual travel distance, multiply the scale factor by the number in the movement commands. If you do not specify a factor, BASIC uses 4, which sets the scale to
1.
TAangle moves at the specified angle. Angle is in the range
-360 to +360. If angle is positive, movement is coun- terclockwise. If angle i s negative, movement i s clockwise.
Xuariable;executes a substring. The X command lets you execute a second substring from the first string, much like the
GOSUB statement. Variable is a string variable in your program that contains the substring you want execute. Variable may contain an to
X command to exe- cute another substring. The semicolon after variable is required.
In the prefix commands, the numeric arguments can be con- stants or variables. If you use a variable name as a numeric ar- gument, you must follow it with a semicolon.
144
Chapter 10 I BASIC Keywords
Sample Programs
5 SCREEN 3
1 0 U $ = " U 3 0 ; " :
" R 4 0
.
II
D$= " D 3 0 ; " : L $ = " L 4 0 ; " : R $ =
2 0 S i x $ = U $
+
R $
+
30 DRAW " X B O X $ ; "
D $
+
L $
4 0 A $ = I N K E Y $ : I F A $ = " " T H E N 4 0
5 0 SCREEN 0 draws a rectangle on the screen.
5 SCREEN 3
1 0 U $ = " U 3 0 . " : D $ = " D 3 0 . " : L $ = ' a L 4 0 . ' 1 : R $ =
" R 4 0
; I '
2 0 DRAW " X U $ ; X R B ; XDS; X L $ ; "
3 0 A $ = I N K E Y $ : I F A $ = " " T H E N 30
4 0 SCREEN 0 draws the same rectangle as the previous example.
1 0 SCREEN 1
2 0 DRAW " L 4 0 E 2 0 F 2 0 "
3 0 A $ = I N K E Y $ : I F A $ = " " T H E N 3 0
4 0 SCREEN 0 draws a triangle on the screen.
145
Chapter 10 I BASIC Keywords
EDIT
EDIT
line
Statement
Enters the Edit mode. BASIC displays line for editing.
You can substitute a period (.) for line number. line to indicate the current
See Chapter editing and special keys.
Examples
E D I T 1 0 0 enters Edit mode a t Line 100.
E D l T enters Edit mode at current line.
146
Chapter 10 I BASIC Ke.ywords
END
END
Statement
Ends program execution and closes all files.
You may place this statement anywhere in the program. It forces execution t o end at some point other than the last sequential line.
An END statement a t the end of a program is optional.
Sample Program
4 0 I N P U T S 1 , S 2
5 0 GOSUB 1 0 0
5 5 P R I N T H
6 0 END
1 0 0 H = S Q R < S l * S l
+
S 2 * S 2 )
1 1 0 R E T U R N
Line 60 prevents program control from continuing through the subroutine. Line 100 may be accessed only by a branching state- ment, such as GOSUB in Line 50.
147
Chapter
10 I BASIC Ke,ywords
ENVIRON Advanced Statement
ENVIRON “parameter id
=
text” [;“parameter id
=
text”,
. . .I
Lets you modify BASIC’s Environment String Table, such as changing the PATH parameter for a child process rameters or passing pa- t o a child process. BASIC’s Environment String Table is initially empty.
Parameter Id is the name of the parameter.
Text is the new parameter text. It must be separated from pa- rameter
id
by an equal sign ( = ) or a space. BASIC reads the first nonblank, nonequal sign character after the parameter
id
as the
( ; ), text. If you omit text, or specify a null string or a semicolon
BASIC removes the parameter from the Environment String
Table and compresses the table.
Parameter
id
= text must be enclosed in quotation marks and be typed in all uppercase characters.
When you change a parameter in the Environment String Table,
BASIC deletes the old parameter and adds the new one to the end of the table.
If the parameter does not exist in the Environment String Table,
BASIC adds it to the end of the table.
For more information on Environment String Tables, see the Pro- grammer’s Reference manual for your computer. It is available at your Radio Shack Computer Store.
Examples
E N V I R O N “ P A T H = A : \ ” sets the default path to the root directory on Drive A.
E N V I R 0 N “ S A L E S = MY S A L E S ” sets the name SALES equal to MYSALES. The Environment
String Table now looks like this:
P A T H = A : \ ; S A L E S = M Y S A L E S
148
Chapter 10 I BASIC Keywords
ENVIRON$
Advanced
Function
ENVIRON$ [('>parameter
id")]
[(number>l
Returns the specified environment string from BASIC's Environ- ment String Table.
Parameter
id
specifies the parameter for which t o search. ENVI-
RON$ returns the text string for parameter
id.
If the parameter does not exist or does not contain a text string, ENVIRON$ re- turns a n empty string. Parameter tion marks. If you omit parameter
id
must be enclosed in quota- number.
Number specifies which parameter t o r e t u r n by its position within the table. ENVIRON$ returns the text string for the number parameter. If there is not a parameter in that position,
ENVIRON$ returns an empty string. If you omit number, you must specify parameter
id.
Parameter
id
and number are mutually exclusive, only one may be specified on the command line.
For more information on the Environment String Tables, see the
Programmer's Reference manual for your computer. It is available a t your Radio Shack Computer Store.
Example
If you execute the following ENVIRON statements:
E N V I R O N " P A T H = A :
\ I ' the Environment String Table looks like this:
P A T H = A : \ ; S A L E S = M Y S A L E S
The command PRINT ENVIRON$("PATH") prints A: \ .
T h e c o m m a n d P R I N T E N V I R O N $ ( B ) p r i n t s S A L E S =
MYSALES.
149
Chapter
10
I BASIC Keywords
EOF
E OF(
buffer)
Function
Detects the end of a file.
Buffer is the number assigned must access a n open file. to the file when you opened it. It
This function checks t o see whether all characters up t o the end- of-file marker have been accessed so that you can avoid "Input past end" errors during sequential input.
When used with sequential access files, EOF returns 0 (false), when the end-of-file record has not been read yet, and -1 (true), when it has been read.
When used with direct access files, EOF returns -1 (true) if the last executed GET statement was unable t o read an entire record because of an attempt t o read beyond the physical end of the file.
Sample
Program
The following sequence of lines reads numeric data from &ta.txt into the array AO. When the last data character in the file is read. the EOF test in Line 30 is true. so the program branches out of the disk access loop.
1 4 7 0 D I M A ( 1 0 0 ) ' A S S U M I N G T H I S
1 4 8 0 OPEN " I " , 1 , " d a t a . t x t "
1 4 9 0 I % = 0
1 5 0 0 I F E O F ( 1 1 T H E N GOTO 1 5 4 0
1 5 1 0 I N P U T # I ,
1 5 2 0
A(I%)
I % = I %
+
1
1 5 3 0 GOTO 1 5 0 0
1 5 4 0 REM PROG. C O N T . HERE A F T E R
I S A S A F E V A L U E
D I S K I N P U T
150
Chapter 10 I BASIC Keywords
E
OF/Communications
EOF(
buffer)
Function
Detects an empty input queue for communications files.
Buffer is the number assigned t o the file when you opened it. It must access an open file.
The value EOF returns depends on the mode (ASCII or binary) in which the file was opened. In ASCII mode, EOF returns a -1
(true) if a CONTROL-Z is received. EOF remains true until the device is closed. In binary mode, EOF returns a -1 (true) when the input queue is empty. EOF becomes false when the input queue is not empty.
Sample Program
These lines are useful in a program when you want t o run the program while waiting for communications activity.
A S 1 1 0 OPEN " C O M 1 : 3 0 0 , N , 8 , 1 "
2 0 COM(1) ON
3 0 ON COM(1 1 G O S U B 1 0 0 0
1 0 0 0 " C o m m u n i c a t i o n S u b r o u t i n e B e g i n 5 H e r e i o 5 0 I F E O F ( 1 ) T H E N RETURN
Line 10 opens a file for Communications Channel 1 and allocates
Buffer 1. Line 30 causes BASIC to perform the subroutine begin- ning at Line 1000 as soon as there is activity on the communi- cations channel. When all the communications data has been processed, Line 1050 returns to the main program.
151
C h m t e r 10 I BASIC Keywords
Statement
ERASE
array[ ,array,.
. .I
Erases one or more arrays from memory.
This lets you either redimension arrays or use their previously allocated space in memory for other purposes.
If one of the parameters of ERASE is a variable name that is not used in the program, a n “Illegal function call” occurs.
Example
4 5 8 E R A S E C , F
4 6 8 D I M F ( 9 9 )
Line 450 erases arrays
C
and F. Line 460 redimensions array
F.
152
Chapter 10 I BASIC Keywords
ERDEV
ERDEV
Advanced Function
Returns the value of a device error within MS-DOS as set by the
Interrupt
24
handler. The lower 8 bits of ERDEV contain the In- terrupt
24
error code.
For more information on device drivers and errors, see the
Pro- grammer’s Reference
manual for your computer. It is available a t your Radio Shack Computer Store.
See also ERDEV$.
153
Chapter 10 I BASIC Keywords
ERDEV$
ERDEV$
Advanced Function
Returns the name of the device (as set by the Interrupt 24 han- dler) when a device error occurs.
If the error occurred on a character device, ERDEV$ returns the
8-byte character device name.
If
the error does not occur on a character device, ERDEV$ re- turns the 2-character block device name.
For more information on device drivers and errors, see the
grammer’s Reference
manual for your computer.
Pro-
It
is available at your Radio Shack Computer Store.
See also ERDEV.
154
Chapter 10 I BASIC Ke-ywords
ERL
ERL
Statement
Returns the number of the line in which an error has occurred.
This function is primarily used inside an error-handling routine.
If
no error has occurred, ERL returns a 0. If a statement entered at BASIC’s prompt causes the error, ERL returns line number
65535
(the largest number that can be represented in 2 bytes).
Examples
P R I N T ERL prints the line number of the error.
E
=
E R L stores the error’s line number in variable E.
Sample Program
See ERROR.
155
Chapter 10 I BASIC Keywords
Statement
ERR
Returns the error code if a n error has occurred.
ERR is only meaningful inside a n error-handling routine ac- cessed by ON ERROR GOTO.
See Chapter 12 for a list of error codes.
Example
I F E R R = 7 T H E N 1 0 0 0 E L S E 2 0 0 0 branches to Line 1000 if the error is a n “Out of memory” error
(code 7); if it is any other error, control goes t o Line 2000.
Sample Program
See ERROR.
156
Chapter 10 I BASIC Kevwords
ERROR
ERROR
code
Statement
Simulates a specified error during program execution.
Code
is an integer expression in the range 0 to 255 specifying one of
BASIC's
error codes.
This statement is used mainly for testing a n ON ERROR GOTO routine. When the computer encounters a n ERROR statement, it proceeds as if the error corresponding t o that code has occurred.
(Refer t o Chapter 1 2 for a l i s t i n g of error codes and t h e i r meanings.)
Example
ERROR 1 causes a "NEXT without reaches this line.
FOR" error (Code 1) when
BASIC
Sample Program
1 1 0 ON ERROR GOTO 4 0 0
1 2 0 I N P U T "WHAT I S YOUR B E T " ; B
1 3 0 I F B > 5 0 0 0 THEN ERROR 2 1 E L S E GOTO 4 2 0
4 0 0 I F ERR = 2 1 THEN P R I N T "HOUSE L I M I T I S
$ 5 0 0 0"
4 1 0 I F E R L = 1 3 0 THEN RESUME 5 0 0
4 2 0 S =
S + B
4 3 0 GOTO 1 2 0
5 0 0 P R I N T "THE T O T A L AMOUNT OF YOUR B E T 1 S " ; S
5 1 0 END
This program receives and totals bets until one of them exceeds the house limit.
157
Chapter 10 1 BASIC Keywords
EXP
EXP(number)
Function
Returns the natural exponent of ral logarithms) number, that is, e (base of natu- t o the power of number.
Number must be less than or equal to 88.02968
This function is the inverse of the LOG function; therefore, num- ber
=
EXP(LOG(number)).
BASIC
always returns the result as a single precision number unless you specified the /D switch when starting up
BASIC.
Example
P R I N T E X P C - 2 ) prints the exponential value .1353353.
Sample Program
31 0 I N P U T "NUMBER"; N
3 2 0 P R I N T "E R C I I S E D T O THE"N"P0WER I S " E X P C N )
158
Chapter 10 I BASIC Keywords
FIELD Statement
F I E L D b u f f e r , l e n g t h variable,
...I
AS
v a r i a b l e [ , l e n g t h A S
Divides a direct access buffer into fields so that you can send data from memory to disk and disk to memory. Each field is identified by variable and is the length you specify.
Buffer is the number assigned to the file when you opened it.
Variable must be a string variable.
Length is a n integer in the range 1 to 255 representing the length of that field. The sum of all field lengths are equal t o the record length assigned when you opened the file.
An OPEN statement assigning the buffer number must precede the FIELD statement. FIELD must precede GET and PUT.
You may use the FIELD statement any number of times to re- field a file buffer. Fielding a buffer does not clear the buffer’s con- tents; it only alters the way the buffer is accessed. You may ac- cess the same disk file any number of ways simply by re-fielding it.
Note:
All data-both strings and numbers-must be placed into the buffer in string form. There are three p a i r s of functions (MKI$/CVI, MKS$ICVS, a n d
MKD$/CVD) for converting numbers t o strings and strings to numbers.
See also Chapter
LSET, and RSET.
7, “Disk Files,” OPEN, CLOSE, PUT, GET,
159
Chapter 10 I BASIC Keywords
Examples
F I E L D 3, 1 2 8 A S A $ , 1 2 8 A S B S
BASIC
assigns 128-byte fields t o the variables
A$
and
B$.
If you now print
A$
or B$, you can see the contents of the field. Of course, this value would be meaningless unless you previously have used GET t o read a 256-byte record from disk.
F I E L D 3 , 16 A S N M $ , 2 5 A S
S T $ , 7 A S Z P $
A D S , 1 0 A S C Y $ , 2 A S
BASIC
assigns the first 16 bytes of Buffer 3 t o field next 25 bytes t o
AD$;
the next 10 to
CY$;
NM$;
the next 2 t o the
ST$; and the next 7 t o
ZP$.
160
Chapter 10 I BASIC Keywords
FILES
FILES [pathname]
Statement
Displays the names of the files and directories on a disk.
Pathname is a standard file specification as described in Chapter
1.
If you specify pathname, BASIC lists all files which match that pathname. If you omit pathname, BASIC lists all files and direc- tories in the current directory on the current drive. Pathname may contain question marks and asterisks as wild cards. See the section on wild cards in Chapter 1 for more information.
If you specify a drive a s part of pathname, then BASIC lists all files that match the specified pathname on
that
drive.
If you omit the filename when specifying pathname, BASIC lists all files and directories in the specified directory.
If you omit the path in pathname, FILES looks for the file in the current directory.
161
Chapter 10 I BASIC Keywords
Examples
F I L E 5 lists all files and directories in the current directory on the cur- rent drive.
F I L E S " \ B O O K S \ " lists all files in the directory BOOKS.
F I L E 5 " *
. b a s " lists all files in the current directory on the current drive with the extension .bas.
F I L E S " p a y ? ? ? ? ? lists all files beginning with pay followed by any other five or fewer characters, in the current directory on the current drive, with the extension .bas.
162
Chawter 10 I BASIC Kevwords
FIX
FIX(
num ber)
Function
Returns the truncated integer of number.
Unlike CINT, FIX does not round the fractional portion of num- ber when making it an integer. Instead, FIX simply strips the fractional portion from number so that the resultant value is a whole number. The result is the same precision as the argument
(except for the fractional portion).
Unlike INT, FIX does not return the next lower number for a negative number.
FIX is the same as:
SGN(number)*INT(ABS(number)).
See also CINT and INT, which also return integer values.
Examples
P R I N T F I X C 2 . 6 ) prints 2.
P R I N T F I X ( - 2 . 6 ) prints -2.
163
Chapter 10 I BASIC Keywords
FOR/NEXT
Statement
FOR
variable
=
initial value
TO
final value
[STEP
increment]
NEXT
[variable]
Establishes a program loop that allows a series of program state- ments to be executed a specified number of times.
Variabb must be either integer or single precision. Each FOR/
NEXT loop must have a unique variable.
Increment is the number BASIC adds to the initial value each time the loop is executed. If you omit increment, BASIC incre- ments by 1. If increment is a negative value, BASIC decreases the initial value each time through the loop. In this case, the f i - nal value must be less than the initial value.
BASIC executes the program lines following the FOR statement until it encounters a NEXT. At this point, it increases initial value by the STEP increment. If initial value is less than or equal to final value, BASIC branches back to the line after FOR and repeats the process. When initial value is greater than final value, the loop is completed, and BASIC continues with the state- ment after NEXT.
Note:
BASIC skips the body of the loop if initial value is greater than final value when increment is positive or if final value is greater than initial value when in- crement is negative.
164
Chapter 10 I BASIC Keywords
Sample Program
BASIC always sets the final value for the loop variable before setting the initial value. For example:
8 2 0 I = 5
8 3 0 FOR I = 1 TO I
+
5
8 4 0 P R I N T I ;
8 5 0 N E X T executes the loop 10 times, which prints:
1 2 3 4 5 6 7 8 9 1 0
Nested Loops
FORiNEXT loops may be nested; that is, a FOR/NEXT loop may be placed within the context of another FOR/NEXT loop.
The NEXT statement for the inside loop must appear before the
NEXT for the outside loop. If nested loops have the same end point, a single NEXT statement may be used for all of them.
Sample Program
8 8 0 FOR I = 1 TO 3
8 9 0 P R I N T "OUTER LOOP"
9 0 0
9 1 0
FOR J = 1 TO 2
P R I N T
"
I N N E R LOOP"
9 2 0 N E X T J
9 3 0 N E X T I
This program performs
3
outer loops and 2 inner loops within each of the outer loops. BASIC prints the following:
OUTER LOOP
I N N E R LOOP
I N N E R LOOP
OUTER LOOP
I N N E R LOOP
I N N E R L O O P
OUTER LOOP
I N N E R LOOP
I N N E R LOOP
165
Chapter 10 I BASIC Keywords
By listing the counter variable, you can use the NEXT state- ment to close nested loops. (Be sure not to type the variables out of order.) For example, delete Line 920 and change 930 to:
930 N E X T J , I
If you
omit the variables in nested loops,
BASIC
matches the most recent FOR statement.
166
Chapter 10 I BASIC Keywords
FRE
F RE
(dummy
arg u
Function
Returns the number of bytes in memory not being used by
BASIC.
Dummy argument can be any string or numeric constant or vari- able. If you specify a numeric argument, BASIC returns the amount of memory available. If you specify a string argument,
BASIC compresses t h e d a t a before returning the amount of memory available. This frees unused memory that was once used for strings.
BASIC automatically compresses data if it runs out of work- space. This may take a few seconds.
Examples
P R I N T F R E ( 4 4 ) prints the amount of memory left.
167
Chapter 10 I BASIC Keywords
GET
GET
[#lbuffr[,recordl
Statement
Reads a record from a direct access disk file and places it in the specified buffer.
Buffer is the number assigned to the file when you opened it.
The number sign is optional.
It
is provided for compatibility with other BASICs.
Record is a n integer in the range 0 to 16,777,215 that specifies which record number you want to access. If you omit record,
BASIC reads the next sequential record (after the last
GET).
When BASIC encounters
GET,
it reads the record number from the file and places it into the buffer. The actual number of bytes read equals the record length set when the file is opened.
Examples
GET 1 reads the next record into Buffer 1.
GET 1 , 2 5 reads Record 25 into Buffer 1.
168
Chapter 10 1 BASIC Keywords
GET/C
ommunic a t ions
GET
[#I
buffer,number
Statement
Transfers data from the communications line to the communica- tions buffer.
Buffer must be the same buffer assigned t o the file when it was opened. The number sign (#) is optional. It is provided for com- patibility with other BASICs.
Number is the number of bytes to transfer. Number cannot ex- ceed the value used in the LEN option of the OPEN COM statement.
Note:
Because of the low performance associated with telephone line communications, we recommend that you
not
use GET and PUT statements in such appli- cations. Instead, use the other disk 110 statements.
Sample Program
1 0 OPEN "CUM1 : " A S 1
2 0 F I E L D 1 , 8 A S A $
3 0 OPEN " R " , 2 , " F I 8
4 0 F I E L D 2 , 8 A S E$
5 0 1 = 1
6 0 GET 1 , 8
7 0 P R I N T " C O M M U N I C A T I O N S B U F F E R C A S ) = " ; A $
8 0 L S E T E $ = & $
9 0 P R I N T "NOW F I L E B U F F E R
1 0 0 P U T 2 , 1
1 1 0 I = I
+
1
1 2 0 I F I N K E Y $ < > "a"
< E $ ) C O N T A I N S : " ; E $
THEN GOT0 6 0
1 3 0 CLOSE
This program gets data from Communications Channel 1 and places it in the communications buffer.
169
Chapter 10 I BASIC Ke-ywords
GET/Graphics
~ ~
Statement
Transfers points from a n area on the display t o an array.
( x 1 , y l ) specifies the coordinates where the image begins.
(x2,y2) specifies the coordinates where the image ends. x is the horizontal coordinate and y is the vertical coordinate.
The ranges for the coordinates depend on the screen mode. See
C h a p t e r 8, “ D i s p l a y i n g Color a n d G r a p h i c s , ” for more information.
Array is a numeric array to hold the image. It must be dimen- sioned large enough t o hold the entire image. To ensure that the a r r a y i s l a r g e enough formula: t o hold t h e image, use t h e following
4
+
(INT((F, * b
+
7)/8)
* u ) where: h is the length of the horizontal side of the image. b the number of bits per point (2 in Screen Modes
1,
4, and u
6; 1 in Screen Mode 2; and
4
in Screen Modes 3 and 5). is the length of the vertical side of the image.
For example, t o store a n image that is
10
by
12
in Screen Mode
1, type:
4
+
(INT((10
*
2
+
7)/8)
*
12) =
40
The array must store
40
bytes. The number of bytes per element of an array are 2 for integer,
4
for single precision, and 6 for dou- ble precision.
170
Chapter 10 I BASIC Ke-ywords
For this example, you need an integer array with 20 elements, a single precision array with 10 elements, or a double precision ar- ray with 7 elements.
The information from the display is stored in the array as:
Element 0
Element 1 the x dimension of the image the y dimension of the image
The remaining elements of the array store the data bits of the image. Numeric data is stored low byte first and then high byte, but the data is transferred high byte first and then low byte.
You use the GETiGraphics and PUTiGraphics statements to- gether for animation and high-speed object motion in the graph- ics modes. See also PUTiGraphics statement.
Note:
GET and PUT work faster in all resolutions if: xIMOD(8/bits per point)
=
0
(See Chapter 5 for an explanation of MOD.)
Sample Program
1 0
2 0
DIM A ( 5 0 , 5 0 )
SCREEN 1
30 CIRCLE ( 3 0 , 3 0 ) , 2 0
4 0 P A I N T ( 3 0 , 3 0 1
5 0 GET ( 1 0 , 0 ) - ( 5 0 , 5 0 ) , A
6 0 PUT ( 2 0 0 , 1 0 0 1 , A
7 0 END
Line 10 sets up a n array for storage. Line 20 selects the screen mode. Line 30 draws a circle, and Line 40 fills in the circle. Line
50 gets the circle, and stores it in Array A(). Line 60 retrieves the circle, and puts it on the screen in a new location.
171
Chapter 10 I BASIC Keywords
GOSUB
GOSUB
line
Statement
Branches to the subroutine beginning a t the specified line num- ber.
Every subroutine must end with a RETURN. You can call a sub- routine a s many times a s you want. When BASIC encounters a
RETURN statement in the subroutine, it returns to the state- ment that follows the GOSUB.
GOSUB is similar t o GOT0 in that it may be preceded by a test st a t ement .
Example
GOSUB 1 0 0 0 branches to the subroutine a t Line 1000.
Sample Program
2 6 0 GOSUB 2 8 0
2 7 0 P R I N T "BACK FROM S U B R O U T I N E " : END
2 8 0 P R I N T " E X E C U T I N G THE S U B R O U T I N E "
2 9 0 RETURN transfers control from Line 260 to the subroutine beginning a t
Line 280. Line 290 instructs the computer to return to the statement immediately following GOSUB.
172
Chapter 10 I BASIC Keywords
GOTO
GOTO line
Statement
Branches to the specified line.
When used alone, GOTO results in a n unconditional branch.
However, test statements, such as IFITHEN, may precede the
GOTO to effect a conditional branch. Note that the GOTO is op- tional in IFiTHEN statements. For example:
I F X = O THEN 3 6 0 E L S E 2 0 0
BASIC branches t o Line 360 if branches t o Line 200
X equals 0. If not, BASIC
You can use GOTO in the command mode as an alternative t o
RUN. This lets you pass values assigned as a command t o vari- ables used in the program.
Example
GOTO 1 0 0
BASIC transfers control t o Line 100.
Sample Program
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
READ R
I F R = 1 3 THEN 8 0
PR I N T "R = " ;
A = 3 . 1 4 * R A 2
P R I N T "AREA = " ; A
GOTO 1 0
D A T A 5 , 7 , 1 2 , 1 3
END
Line 10 reads each of the data items in Line 70; Line 60 returns program control
to
Line
10.
This enables BASIC
to
calculate the area for each of the data items until it reaches item 13.
173
Chapter
10
I BASIC Keywords
HEX$
HEX$(nurnber)
Function
Calculates the hexadecimal value of number.
HEX$ returns a string that represents the hexadecimal value of number. Since the value returned is like any other string, you cannot use i t i n a numeric expression. You cannot add hex strings; however, you can concatenate them.
Examples
P R I N T H E X $ < 3 0 ) , H E X $ < 5 0 ) , H E X t C 9 0 ) prints the following strings l E , 32, and
5A.
Y $ = H E X $ < X / 1 6 )
Y$ is the hexadecimal string representing the integer quotient
Xl16.
174
Chapter 10 I BASIC Keywords
IF/THE
N/E
LSE
Statement
IF expression T H E N statemnt(s)[ELSE statemnt(s)]
Tests a conditional expression and makes a decision regarding program flow.
Expresswn is any numeric or string expression, usually making logical or relational comparisons.
Statement can be 1 or more valid BASIC statements.
If
there is more than 1 statement, they must be separated by colons. You c a n also specify a l i n e number for BASIC t o branch a s a statement .
If expression is true, BASIC executes the THEN statement. If expresswn is false, BASIC executes the matching ELSE statement or the next program line.
You can also use IFiTHEN t o test the numeric value of a vari- able. If the variable contains a 0, the expression is true; other- wise, the expression is false.
Examples
I F X > 1 2 7 T H E N P R I N T "OUT OF RANGE" : END passes control t o PRINT and, then t o END if X is greater than
127. If X is not greater than 127, BASIC executes the next line in the program, skipping the PRINT and END statements.
I F A < B T H E N P R I N T " A < E" E L S E P R I N T "E < = A " tests the first expression.
Otherwise, BASIC jumps
B < =
A .
If it is true, BASIC prints
A
< B . t o the ELSE statement and prints
175
Chapter 10 I BASIC Ke.ywords
I F X > 0 AND Y < > 0 THEN Y = X
+
1 8 0 assigns the value X
+
180 to Y if both exm-essions are true.
Otherwise, BASIC executes the next program- line, skipping the
THEN clause.
IF A $ = " Y E S " THEN 2 1 0 E L S E I F A $ = " N O " THEN
4 0 0 E L S E 3 7 0 branches t o Line 210 if A$ is YES. If not, BASIC skips to the first ELSE, which introduces a new test. If A$ is NO, then
BASIC branches t o Line 400. If A$ is any value besides NO or
YES, BASIC branches to Line 370.
Sample
Program
IFiTHENiELSE statements may be nested. However, you must take care t o match up the IFs and ELSEs. (If the statement does not contain the same number of ELSEs and IFs, each ELSE is matched with the closest unmatched IF.)
1 0 4 0 I N P U T "ENTER TWO NUMBERS"; A , B
1 0 5 0 I F A < = B THEN I F A < B THEN P R I N T A ; E L S E
P R I N T
"
N E I T H E R " ; E L S E P R I N T E ;
1 0 6 0 P R I N T
"
I S SMALLER THAN THE OTHER"
This program prints the relationship between the 2 numbers entered.
176
Chapter 10 I BASIC Keywords
INKEY$
INKEY$
Function
Reads or 2- byte string. INKEY$ does not echo the character t o the display.
0
A 0-byte (null) string indicates that no key is pressed.
0
A 1-byte string is a n actual character read from the keyboard.
0
A 2-byte string indicates that the key pressed is one of the special keys that has an extended code. The first byte is hex 00. See Ap- pendices B and
D
for a complete list of extended codes.
INKEY$ is invariably put inside some sort of loop. If not, pro- gram execution passes through the line containing INKEY$ be- fore you can press a key.
The [ C T R L I [ m ! and IHOLD] keys are not passed to INKEY$. Also
[ALT][CTRLI[],
INKEY$. which does a system reset, is not passed to
Note:
If your program contains an INKEY$ and you press a function key, BASIC returns 1 character of the key assignment a t a time. For example, suppose this statement is executed:
A $ = I N K E Y F
Now suppose you press [, which initially has the value LIST. The first time the statement is executed
A$ equals L, the second time A$ equals I, and so on.
Keep this in mind when writing a BASIC routine t o trap for a certain key. Your routine may not perform as expected if you accidently press a function key.
You can assign the result of INKEY$ t o a string variable and test the length of the string t o determine whether a 0-, 1-, or 2- character string is returned by
INKEY$. Example:
1 0 A $ = I N K E Y $ : I F A $ = " " T H E N 1 0
2 0 I F L E N ( A $ ) > l T H E N P R I N T A S C ( M I D ( A $ , l , I ) ) ,
A S C ( M I D $ ( A $ , 2 , 1 ) ) E L S E P R I N T A S C C A $ )
3 0 G O T 0 1 0
1 0 A $ = I N K E Y S
2 0 I F A $ =
" I '
T H E N 1 0 causes the program to wait for you t o press a key.
177
Chapter 10 I BASIC Ke.ywords
INP
INP(port)
Function
Returns the byte read from
port.
Port
may be any integer from 0 t o 65535.
INP is the complementary function of the OUT statement.
Example
1 0 0 A = I N P ( 2 5 5 ) returns the byte read from port
255
into variable
A.
178
Chapter 10 I BASIC Keywords
Statement INPUT
Accepts data from the keyboard and inputs it into 1 or more variables. When BASIC encounters this statement, it stops exe- cution and displays a question mark. This means that the pro- gram is waiting for you t o type something.
Prompt is a string constant that BASIC displays before display- ing the question mark prompt. Prompt must be enclosed in quo- tation marks, and follow the keyword INPUT. If, instead of a semicolon, you type a comma after prompt, BASIC suppresses the question mark when printing the prompt.
Variable may be 1 or more string or numeric variables t o receive the input. If you specify more than 1 variable, separate them by commas.
If INPUT is immediately followed by a semicolon (;I, BASIC does not echo the [ENTER] key when you press it as part of a response.
When typing multiple pieces of data on 1 line, separate the data items with a comma. The number of data items you supply must be the same as the number of variables you specify.
Responding type t o INPUT with too many items or with the wrong of value (including numeric type) causes BASIC t o print the message “?Redo from start.” No values are assigned until you provide a n acceptable response.
179
Chapter 10 I BASIC Kevwords
Examples
I N P U T Y % when BASIC reaches this line, you must type any number and press
JENTERI
before the program can continue.
I N P U T SENTENCES when BASIC reaches this line, you must type in a string. The string does not have t o be enclosed in quotation marks unless it contains a comma, a colon, or a leading blank.
I N P U T "ENTER YOUR NAME, AGE"; N $ , A prints the prompt string on the screen, which helps the user en- ter the right kind of data.
Sample Program
5 0 I N P U T " H O W MUCH DO YOU WEIGH"; X
6 0 P R I N T " O N MARS YOU WOULD WEIGH ABOUT"
C I N T C X *
.38) "POUNDS."
180
Chawter 10 I BASIC Kevioords
INPUT#
INPUT#
buffer, .I
Statement
Accepts data from a sequential device or file and stores it in a program variable.
Buffer is the number assigned to the file when you opened it.
Variable is any s t r i n g or numeric variable to contain the in for mat ion.
The sequential file may be a disk file, a data stream from a com- munications device, or the keyboard device.
With INPUT#, data is input sequentially. That is, when the file is opened, a pointer is set to the beginning of the file. The pointer advances each time data is input. To start reading from the beginning of the file again, you must close the file buffer and reopen it.
INPUT# does not care how you place the data in the file- whether you use a single PRINT# statement or 10 different
PRINT# statements. INPUT# looks only for the position of the terminating characters and the end-of-file (EOF) marker.
When inputting data into a variable, BASIC ignores leading blanks. When t h e f i r s t nonblank character is encountered,
BASIC assumes it has encountered the beginning item. of the data
181
Chapter
10
I BASIC Ke-ywords
The data item ends when BASIC encounters a terminating char- acter or when a terminating condition occurs. The terminating characters vary, depending on whether BASIC is inputting t o a numeric or a string variable:
Numeric:
BASIC ends input when it encounters a carriage return or a comma.
String:
BASIC ends input when it encounters a carriage re- turn or a comma, unless the first character is a quotation mark(
"
). If the first character is a quotation mark, BASIC ends input when it encounters a second quotation mark.
Thus, a quoted string may not contain a quotation mark as a character.
Examples
I N P U T # l , A , B sequentially inputs 2 numeric data items from the file opened to
Buffer 1 and places them in A and B.
I N P U T # 4 , A $ , E $ , C $ sequentially inputs 3 string data items from the file opened to
Buffer 4 and places them in A$, B$, and C$.
182
Chapter 10 I BASIC Ke.ywords
INPUT$
INPUT$
(
nu m ber
[ , ]
buffer])
Statement
Accepts a string of characters from either the keyboard or a se- quential access file.
Number is the number of characters t o be input. It must be a value in the range 1 to
255.
Buffer is a buffer that accesses a sequential input file. If you in- clude buffer, BASIC inputs the string from sequential access file.
If you omit buffer, BASIC inputs the string from the keyboard.
The number sign (#) is optional. It is provided for compatibility with other BASICs.
When inputting the string from the keyboard, BASIC waits until the user enters the number of characters specified by
You do not need to press
[m] number. ter(s) you type are not displayed on the screen. Any character, except F][EiEK], for input.
When inputting from a sequential file, BASIC inputs the num- ber of bytes specified by number from the file assigned t o buffer.
Examples
A $
= I N P U T $ ( S ) assigns a string of 5 keyboard characters t o A$. Program execu- tion halts until 5 characters are typed at the keyboard.
A $
= I N P U T $ C 1 1 , 3 ) assigns a string of 11 characters to A$. The characters are read from the file associated with Buffer 3.
183
Chapter 10 I BASIC Keywords
Sample
Programs
This program shows how you can use INPUT$ to have a n opera- tor input a password for accessing a protected file. By using IN-
PUT$, you can type in the password without anyone seeing it on the video display. To see the full file specification, run the pro- gram. When the BASIC prompt returns, type PRINT F$.
1 1 0 L I N E I N P U T " T Y P E I N T H E F I L E N A M E . E X T " ; F $
1 2 0 P R I N T " T Y P E I N THE PASSWORD - - MUST T Y P E 8
CHARACTERS :
"
;
1 3 0 P $ = I N P U T $ < 8 )
1 4 0 F $ = F $
+
' I . "
+
P $
1 5 0 P R I N T "YOUR F I L E N A M E I S " ; FS
In the program below, Line 100 opens a sequential input disk file (which we assume has been previously created). Line 200 re- trieves a string of 70 characters from the file and stores them in
T$. Line 300 closes the file.
1 0 0 OPEN " I " , 2 , " t e s t . d a t "
2 0 0 T $ = I N P U T $ < 7 0 , 2 )
3 0 0 C L O S E
184
INSTR
Chapter 10 I BASIC Keywords
Function
Searches for the first occurrence of turns the position string2 in stringl and re- at which the match is found.
Number specifies the position in stringl to begin searching for string2. Number must be a n integer in the range 1 to 255. If you omit number, INSTR starts searching a t the first character in stringl .
If
BASIC finds string2 in stringl, it returns the starting position of the match; otherwise, it returns zero. If the entire substring is not contained in the search string, BASIC returns a zero.
Examples
Suppose A$ = “LINCOLN”
Statement
INSTR(A$, “INC”)
INSTR(A$, “12”)
INSTR(A$, “LINCOLNABRAHAM”)
BASIC returns
2
0
0
For a slightly different use of INSTR, try:
1 0 X = I N S T R ( 3 , “ 1 2 3 2 1 2 3 ” , “ 1 2 ” )
20 P R I N T X which p r i n t s character.
5, because t h e s e a r c h s t a r t e d a t t h e t h i r d
185
Chapter 10 I BASIC Ke-ywords
Sample Program
The program below uses INSTR t o search through the addresses contained in the program's DATA statements.
It
counts the num- ber of addresses with a specified county zip code (761--) and re- turns that number. The zip code is preceded by an asterisk
to
distinguish it from the other numeric data found in the address.
360 RESTORE
3 7 0 COUNTER = 0
3 9 0 READ ADDRESS$
3 9 5 I F ADDRESS$ = "SEND" THEN 4 1 0
4 0 0 I F I N S T R C A D D R E S S S , " * 7 6 1 " ) < > 0 THEN COUNTER
= COUNTER
+
1 E L S E 390
4 0 5 GOT0 390
4 1 0 P R I N T "NUMBER OF TARRANT COUNTY, TX
ADDRESSES I S " COUNTER : END
4 2 0 DATA " 5 9 5 0 GORHAM D R I V E , BURLESON, TX
* 7 6 1 4 8 "
4 3 0 DATA " 7 1 F I R S T F I E L D ROAD, G A I T H E R S B U R G , MD
* 2 0 7 6 0
'I
4 4 0 DFlTA " 1 0 0 0 TWO TANDY CENTER, FORT WORTH, TX
* 7 6 1 0 2 "
4 5 0 DATA " 1 6 6 3 3 SOUTH CENTRAL EXPRESSWAY,
R I C H A R D S O N , TX ~ 7 5 0 8 0 "
4 6 0 DATA . " $ E N D "
186
Chapter 10 I BASIC Keywords
INT
INT(
num
ber)
Function
Converts number to the largest integer that is less than or equal t o number.
Number is not limited t o the integer range - 32768 to 32767
The result has the same precision as number (except for the frac- tional portion).
Unlike CINT, INT does not round positive numbers. It does, however, round negative numbers.
Examples
P R I N T I N T C 7 9 . 8 9 ) prints 79.
P R I N T INT ( - 1 2 . 1 1 ) prints -13.
187
Chapter
10
I BASIC Keywords
IOCTL
IOCTL
[#lbuffr,string
Advanced Statement
Sends a control data string to a device driver. Control data can be sent to a drive only after it has been opened.
Buffer is the number assigned t o the driver when you opened it.
The number sign (#) is optional. It is provided for compatibility with other BASICs.
String is a string expression containing a series of commands called "control data." The commands are generally 2 t o
3
charac- ters long and may be followed by a n alphanumeric argument.
The commands are separated by semicolons (;). String may be a maximum of
255
bytes.
For more information on device drivers, see the Programmer's
Reference manual for your computer. It is available a t your Radio
Shack Computer Store.
Example
If you write your own driver t o replace PRN to set the page length, the IOCTL command may be:
PLn where n is the new page length.
To open the new PRN driver and set the page length at per page, use the following statements:
56 lines
1 0 O P E N "PRN" FOR OUTPUT a 5 1
20 I O C T L 1 , " P L 5 6 "
188
Chapter
10 I BASIC
Keywords
IOCTL$
IOCTL$([#lbuf,r)
Advanced Function
Returns the control data string from a device driver that you have opened previously.
Buffer
is the number assigned to the driver when you opened it.
The number sign (#) is optional. It is provided for compatibility with other BASICs.
You can use the IOCTL$ function to confirm that a IOCTL statement succeeded (or failed). You can also use IOCTL$ to get information from the device.
For more information on device drivers, see the
Programmer's
Reference
manual for your computer. It is available a t your Radio
Shack Computer Store.
Example
1 0 OPEN " \ D E V \ P R N " FIS 1
2 0 I F I O C T L $ C I ) = "NR" THEN P R I N T " P R I N T E R NOT
REFIDY"
189
Chapter 10 I BASIC Keywords
KEY/Set/Display
KEY number,string
KEY ON
KEY OFF
KEY LIST
Statement
KEY number,string
Assigns or displays function key values.
Number is an integer in the range 1 t o 12 that indicates the
.
I
(m
String is the string expression assigned t o the key and may con- tain a maximum of 15 characters.
You can program the function keys on your computer to generate a specific string of characters. When you press the key, BASIC displays the string on the screen just as if you had typed every character. Initially, the function keys have these values:
F1 LIST
F2 R U N =
F3 LOAD“
F4 SAVE“
F5 CONT-
F6 , “ L P T l : ” m
F7 T R O N m
F8 TROFF-
F9 KEY
F1O SCREEN 0,0,0 [ENTERI
F11 none
F12 none
Function keys 11 and 12 do not have initial values. You can use the KEY statement to define these keys. You also can use the
KEY statement t o redefine t h e other function keys s o t h a t
BASIC displays the strings you use most often.
You can remove the string from a function key by assigning it a string length of zero (“ ”). For example:
K E Y 1
,
“‘I
Key no longer has a string assigned to it. Moreover, assign- ing a null string (length zero) t o a function key disables it as a soft key. If the soft key is disabled, INKEY$ returns a 2-byte string. See Appendix D for more information.
190
Chapter 10 I BASIC Keywords
KEY ON
KEY ON displays the function key assignment values on Line 25 of the screen. If the screen width is 40, the screen shows 5 func- tion key assignments at a time. If the width is 80, the screen shows 10 assignments a t a time. Press (CTRL] p) t o display the next screen and t o erase the display after the last assignments are shown.
KEY OFF
KEY OFF erases the soft key assignments from Line 25. The as- signments are still active, but the screen does not display them.
BASIC reserves Line 25 for the function key display. Even if the display is turned off, BASIC does not display program lines on
Line 25.
Note:
Instead of using KEY ON and KEY OFF, you can use
[m] signment display on and off, as well as to cause it t o advance t o the next screen.
KEY LIST
KEY LIST displays all 15 characters ments on the screen. of all 12 soft key assign-
Note:
If your program contains an INKEY$ and you press a function key, BASIC returns 1 character of the key assignment a t a time. For example, suppose this st a t ement is executed:
A$
= INKEY$
Now suppose you press value LIST. The first time the statement is executed
A$ equals L, the second time A$ equals I, and so on.
Keep this in mind when writing a BASIC routine
t o
trap for a certain key. Your routine may not perform as expected if you accidentally press a function key.
191
Chapter
10
I BASIC Keywords
KEY/Trap
KEY(number)
action
Statement
Turns on, turns off, or temporarily halts key trapping for a spec- ified key.
Action may be any of the following:
ON
OFF
STOP enables key trapping disables key trapping temporarily suspends key trapping
Number may be a number in the range 1 to 20, indicating the number of the key to trap. Function keys use their corresponding function key number (1-10). The cursor direction key trap num- bers are:
11
12
13
14
User-defined keys are 15-20. Use the following syntax t o define your own user keys:
KEY
number, CHR$(key)
+
CHR$(scan)
Key is one of or a combination of the following:
&H40
&H20
&H08
&H04
&H02
&H01
[CAPSJ lock key
(NUMLOCK] key
(ALT) key
[CTRLJ key
Left
Right key key
Scan is the scan code for a physical key on the keyboard.
See the appendices for a list of scan codes.
Notes:
Trapped keys do not go into the keyboard buffer.
0
Defining a function key or a cursor-direction key has no effect. BASIC considers them pre-defined.
192
Chapter 10 I BASIC Keywords
0
You can trap any key, including (CTRL] @J, [CTRLIIBREAK), and [CTRL] [DELETE) (the soft boot). This feature makes it possible t o prevent BASIC application users from con- trol-breaking out of a program or from accidentally reset- ting the computer.
You can use the KEYITrap statement in a key trapping routine with the ON KEYO GOSUB statement to detect when a specific key is pressed.
The KEYO ON statement turns on key trapping for a specific key. BASIC checks after each program statement t o see if the specified key has been pressed. If so, BASIC transfers program control t o the line number specified in the statement. For example:
ON KEY() GOSUB
K E Y ( 3 ) ON
ON K E Y ( 3 ) G O S U B 1 0 0 0
BASIC turns on a trap for m.
BASIC continues t o execute the other program statements, checking after each statement t o see if iF3] has been pressed. When IF3] is pressed, BASIC branches t o the subroutine beginning a t Line 1000.
KEYO STOP temporarily halts trapping for the specified key. If the specified key is pressed, BASIC does not transfer program control to the ON KEYO GOSUB until you turn on key trapping again with a KEY() ON statement. BASIC remembers that the key was pressed and branches t o the subroutine immediately after key trapping is turned on again.
KEY() OFF turns off key trapping. BASIC does not remember that the key has been pressed when key trapping is turned on again.
Note:
Key trapping only occurs while BASIC is run- ning a program.
See ON KEY() GOSUB for more information on key trapping.
Sample Program
See ON KEY() GOSUB.
193
Chapter 10 I BASIC Keywords
KILL
KILL pathname
Statement
Kills (deletes) pathnume from disk.
Pathname is a standard file specification as described in Chapter
1.
You may delete any type of disk file. However, if the file is cur- rently open, a "File already open" error occurs. You must close the file before deleting it.
Example
K I L L " f i
1 e . b a
3 " deletes file.bas if i t exists in the current directory.
K I L L "A:\REPORT\data" deletes data from the directory REPORT in Drive
A.
194
Chapter 10 I BASIC Keywords
LCOPY
LCOPY
Statement
Copies all text data on the screen t o the printer
Sample Program
5 5 0 FOR
5 6 0
1 = 1 TO 2 4
PRINT S T R I N G $ ( 7 9 , 3 3 )
5 7 0 NEXT
5 8 0 L C O P Y
I
This program segment prints exclamation points on the screen, and dumps them t o the printer.
195
Chapter
10 I BASIC
Keywords
LEFT$
LEFT$(string, num ber)
Function
Returns the specified number of characters from the left portion of
string.
Number
must be an integer in the range 1 to 255. If
number
is equal t o or greater than the length of the
string,
BASIC returns the entire string.
Examples
P R I N T L E F T S ( " B A T T L E S H I P S " , 6 ) prints BATTLE.
P R I N T L E F T $ C " B I G F I E R C E DOG", 28)
Since BIG FIERCE DOG is fewer than 20 characters, BASIC prints the whole phrase.
Sample Program
7 4 0 A $ = " T I M O T H Y "
7 5 0 E $ = L E F T $ ( A $ ,
7 6 0 P R I N T E $ ;
3 )
" - - T H A T ' S SHORT FOR " ; A B
When you run this program, BASIC prints:
T I M - - T H A T ' S SHORT FOR T I M O T H Y
Line 750 gets the 3 left characters of A$ and stores them in
B$.
Line 760 prints these 3 characters, a string, and the original contents of A$.
196
Chapter 10 I BASIC Keywords
LEN
LEN(string)
Function
Returns the number of characters in string. Blanks are counted.
Examples
X = L E N < S E N T E N C E S ) gets the length of SENTENCE$ and stores it in X.
P R I N T L E N ( " C A M B R 1 D G E " )
+
L E N < " B E R K E L E Y " ) prints 17.
P R I N T L E N ( " O R L A N D 0 , F L O R I D A " ) prints 16.
197
Chapter 10 I BASIC Ke-ywords
LET
[LET]
variable
=
expression
Statement
Assigns the value of expression t o variable.
Variable is a numeric or string variable.
Expression is a numeric or string constant or expression.
A
BASIC function can be substituted for expression.
BASIC does not require assignment statements t o begin with
LET, but you might want t o use LET to be compatible with ver- sions of BASIC that do require it.
Examples
L E T A $ = " A ROSE I S A ROSE"
L E T E1 = 1 . 2 3
L E T X = X - 2 1
L E T X = S Q R C B )
In each case, the variable sign is assigned the value function on the right side. on the left side of the equals of the constant, expression, or
Sample Program
5 5 0 P = 1 0 0 1 : P R I N T "P
1
P
5 6 0 L E T P = 2 0 0 1 : P R I N T " N O W P = " P
198
LINE/Graphics
Chapter 10 I BASIC Ke.ywords
Statement
Draws a line or a box on the video display.
The STEP option tells BASIC that the (x,y) coordinates are rela- tive t o the last point referenced. If you use STEP with the sec- ond set of coordinates, the coordinates are relative to the first set of coordinates.
(x1,yl) specifies the point a t which t o begin the line. x l is the horizontal coordinate, and y l is the vertical coordinate. If you omit ( x 1 , y l ) BASIC begins the line a t the last point referenced on the screen.
(x2,yZ) specifies the point at which to end the line. x2 is the hor- izontal coordinate and y 2 is the vertical coordinate.
Color indicates the color of the line.
See Chapter 8, “Displaying Color and Graphics” for information on coordinates and colors for the current screen mode.
If you specify coordinates that are not in the range of the cur- rent viewport, BASIC displays only that portion of the line that is within the viewport.
With the
B
option, BASIC draws a box. The points that you specify are opposite corners.
If you specify both the
B
and
F
options, BASIC draws a box and fills the box in with color.
Style lets you select the line-style used when drawing normal lines and unfilled boxes. Style is a 16-bit integer. Each bit repre- sents a point in the line. If the bit equals
1,
then the point
is
drawn. If the bit equals zero, then the point is not drawn.
A
zero bit does not erase a previously drawn point; therefore, you may want to draw a background line first to have a known back- ground. The style pattern is repeated as necessary, to complete the line drawing.
199
Chapter 10 I BASIC Keywords
Here are some sample styles showing the bit representation, the line drawn, and their hex equivalents:
0
1
0 1 0 1 0 1 0 1 0 1 0
1
0 1 equals &H5555
_ _
- is drawn
1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 lequals&HDBGD
_ _
_ _
- -
_ _ - _
- is drawn
Examples
You can try these examples in Screen Modes 1 to 6. The color, size, and position of the image on the display vary, depending on the current screen mode.
L I N E -(319, 199) draws a line from the last point referenced t o point 319,199 in the default color. This is the simplest form of the LINE state- ment. Note that when you omit the beginning points you must still include the hyphen.
L I N E (0,0)-(319,199) draws a diagonal line on the display in the default color.
L I N E C 0 , 1 0 0 1 ,l draws a horizontal line across the display in Color 1.
L I N E ( 0 , 0 ) - < 1 0 0 , 1 0 0 ) , , E draws a box in the upper left corner of the display.
L I N E (0,0)-(200,200),1 ,BF draws a box on the display and fills it in with Color 1.
L I N E (0,0)-(200,200),1,B, & H 5 5 5 5 draws a box outlined by a dashed line.
200
10 I BASIC Keywords
Sample Programs
1 0 C L S
2 0 L I N E -(RND*319,RND*199),RND+4
3 0 G O TO 2 0
In Screen Modes 1 to 6, Lines 10-30 create a loop that draws random lines on the video display.
4 0 F O R X=O TO 3 1 9
5 0 L I N E ( X , B ) - ( X , 1 9 9 ) , X
6 0 N E X T
FlND 1
In Screen Modes 1 to 6, Lines 40-60 draw an alternating pat- tern, turning on and off the line.
1 0 C L S
2 0 L I N E - ( R N D * 6 3 9 , R N D * 1 9 9 ) , R N D * 2 , B F
3 0 GO TO 2 0
This program draws a random filled box to 6.
201
Chapter 10 I BASIC Keywords
LINE INPUT Statement
Accepts a n entire line (a maximum of 255 characters) from the keyboard. LINE INPUT is a convenient way t o input string data without accidental entry of delimiters (commas, quotation marks, etc.).
Prompt is a string constant enclosed in quotation marks that
BASIC prints before waiting for input.
String variable is the variable t o receive the input.
The only way t o terminate the string input is t o press
However, if LINE INPUT is immediately followed by a semicolon, pressing [ENTER] does not echo a carriage return t o the display.
Note:
You m u s t place a space between LINE and
INPUT.
LINE INPUT is similar t o INPUT, except:
0
0
0
0
BASIC does not display a ? when waiting for input.
Only 1 variable can be assigned a t a time.
Commas and quotation marks can be entered in the string input.
Leading blanks are not ignored.
Examples
L I N E I N P U T A $ waits for input t o A$ without displaying a prompt.
L I N E I N P U T " L A S T NAME, F I R S T NAME?
' I ;
N $ displays the message and waits for input.
202
Chapter 10 I BASIC Ke.ywords
LINE INPUT#
LINE INPUT#
buffer, variable
Statement
Accepts an entire line variable. of data from a sequential file to a string
Buffer
is the number assigned to the file when you opened it.
This statement is useful when you want to read an ASCII format
BASIC program file as data or when you want to read in data without following the usual restrictions regarding leading char- acters and terminators.
LINE INPUT# reads everything from the first character up to:
0
0 the end-of-file the 255th data character a carriage return
Other characters encountered-quotation marks, commas, lead- ing blanks-are included in the string.
Note:
You must place a space between LINE and
INPUT#.
Example
If a ASCII format program file looks like this:
1 0 CLEAR 5 0 0
2 0 OPEN " I " , " p r o g " then the statement:
L I N E I N P U T # l , A $ can be used repeatedly t o read each program line, one at a time.
203
Chapter 10 I BASIC Keywords
LIST
LIST startline-endline [,deuicel
Statement
Lists a program in memory t o the display.
Startline specifies the first line t o be listed. If you omit startline,
BASIC starts with the first line in your program.
Endline specifies the last line t o be listed. If you omit endline,
BASIC ends with the last line in your program.
If you omit both program. startline and endline, BASIC lists the entire
Device
may be either SCRN: (screen) or LPT1: (line printer 1). you omit
deuice,
the lines are listed to the screen.
You can temporarily stop the listing by pressing [HOLD1. Press
[HOLDJ again to continue the listing.
You can substitute a period (.) for either indicate the current line number. startline or endline t o
Examples
LIST displays the entire program.
LIST 5 0 - 8 5 , " ' s C R N : " displays lines in the range
50
t o
85
on the screen.
LIST
. - displays the program line that you have entered or edited and all higher numbered lines on the screen.
LIST - 2 2 7 displays all lines up t o and including
227
on the screen.
LIST 2 2 7 - , " L P T l : " lists Line
227
and all higher numbered lines t o the printer.
204
Chapter 10 I BASIC Ke-ywords
LLIST
LLIST startline-endline
Statement
Lists program lines in memory t o the printer.
Startline specifies the first line t o be listed. If you omit startline,
BASIC starts with the first line in your program.
Endline specifies the last line t o be listed. If you omit endline,
BASIC ends with the last line in your program.
If you omit both program. startline and endline, BASIC lists the entire
You can substitute a period (.) for either indicate the current line number. startline or endline to
LLIST assumes a n 80-character-wide printer. You may change this by using the WIDTH statement with the LPRINT option.
Examples
LLIST lists the entire program press (. t o the printer. To stop this process,
This causes a temporary halt in the computer’s out- put t o the printer. Press IHOLD] again to continue printing.
LLIST 68-90 prints lines in the range 68 t o 90.
205
Chapter
10
I BASIC Keywords
LOAD
LOAD pathname
[,Rl
Statement
Loads a BASIC program from disk into memory.
Pathname is a standard file specification used to save the file to disk.
The R option tells BASIC to run the program. (LOAD with the
R
option is equivalent to the command specify the
RUN pathname.) When you
R option, BASIC leaves all open files open and runs the program automatically. If you omit the R option, BASIC wipes out any resident BASIC program, clears all variables, and closes all open files.
You can use either of these commands inside programs to allow program chaining (one program calling another).
If you attempt t o LOAD a non-BASIC file, a "Direct statement in file" error occurs.
Example
LOAD " A : p r o g l
. b a 5 " loads progl .bas from Drive A, and then returns to the command mode.
LOAD " p r o g l . b a s " loads progl .bas. Because no drive is specified, BASIC searches for progl .bas on the current drive.
206
Chapter 10 I BASIC Ke.ywords
LOC
LOC(buffer)
Function
Returns the current record position within a file.
Buffer is the number assigned to the file when you opened it.
You use LOC to determine the current record position, that is, the number of the last record processed since you opened the file.
When used with direct access files, LOC returns the record number accessed by the last GET or PUT statement.
When used with sequential files, LOC returns the number of
128-byte blocks that have been read or written.
Example
I F L O C ( 1 ) > 5 5 THEN END
Program execution ends, if the current record position is greater than 55.
Sample Program
1 3 1 0 CIS = "WILLICIM W I L S O N "
1 3 2 0 GET 1
1 3 3 0 I F N $ = A $ THEN P R I N T "FOUND I N RECORD"
L O C C I ) : C L O S E : END
1 3 4 0 G O T 0 1 3 2 0
This is a portion of a direct access program. Elsewhere the file has been opened and fielded. N$ is a field variable. If N$ matches A$, the record number in which it was found is printed.
207
Chapter 10 I BASIC Keywords
LO
C/C ommunic at ions
LOC
(buffer)
Function
Returns the number of characters in the input queue if that number is
255
or less.
If the queue contains
256
or more characters, the LOG function returns the number
255.
Since a string is limited to
255
charac- ters, this limit eliminates the need for testing string size before reading data into the queue.
Buffer is the number assigned t o the file when you opened it.
The default size for the input queue is
256
characters, but you can change the size by using the / C : option when loading BASIC.
1 0 X = L O C < I )
2 0 I f X > 0 THEN A $ = I N P U T $ ( L O C ( l ) , # I )
Line 18 checks t o see if there are any characters in the input queue and stores the number of characters in the variable X.
Line
20
tests the value of X. If X is greater than
0,
there are characters in the input queue, and Line
28
returns the charac- ters in the buffer into A$.
Notice from the example that INPUT$ is preferred over LINE
INPUT# or INPUT# when reading communications files. This preference is because all ASCII characters might be significant in communications. INPUT$ allows all characters to be read.
The other statements do not. LINE INPUT# stops at a carriage return. INPUT# stops at a comma or a carriage return.
208
Chapter
10 I BASIC
Keywords
LOCATE
LOCATE
[row][,
Statement
Positions the cursor on the screen.
Row
is a numeric expression in the range 1 t o 25 that indicates the screen row where you want t o position the cursor.
Column is a numeric expression that indicates the screen column where you want t o position the cursor. It may be in the range to 40 or 1 to 80, depending on the current screen width.
1
Cursor indicates whether the cursor is visible or invisible. Set cursor to 1 for a visible cursor and t o 0 for an invisible cursor.
Start specifies the starting scan line of the cursor. There are 7 scan lines available for the cursor. Start must be an integer in the range 0 to 7, where 0 is the top line and 7 is the bottom line.
Stop specifies the ending scan line of the cursor. Stop must be in the same range as start. If stop is less then start BASIC displays a split cursor.
Cursor, start, and stop are only effective in Screen Mode 0.
Examples
LOCATE 1 0 , 2 0 , 1 , 4 positions a half cursor on Row 10 in Column 20. positions an underline cursor in the first position of the last line.
209
Chapter 10 I BASIC Keywords
LOF
LOF
( buffer)
Function
Returns the length of the file in bytes.
Buffer is the number assigned to the file when you opened it.
Example
Y = L O F ( 5 ) assigns the length of the file in bytes to variable Y
Sample Programs
During direct access to a n existing file, you often need a way to know when you have read the last valid record. LOF provides a way:
1 5 4 0 OPEN " R " , 1 , " u n k n o w n . t x t " ,
1 5 5 0 F I E L D 1 , 1 2 8 AS A $
1 2 8
1 5 6 0 RCNUM% = 1 ' S T A R T AT B E G I N N I N G OF F I L E
1 5 7 0 R C S I Z % = 1 2 8
1 5 8 0 I F RCNUM%
' S E T RECORD S I Z E
* R C S I Z % > L O F ( 1 ) GOTO 1 6 4 0
1 5 9 0 'CHECK FOR END OF F I L E
1 6 0 0 G E T 1 , RCNUMX 'RECORD NUM. TO B E ACCESSED
1 6 1 0 P R I N T A S
1 6 2 0 RCNUM% = RCNUM%
+
1 ' I N C R E M E N T RECORD NUM
1 6 3 0 GOTO 1 5 8 0
1 6 4 0 CLOSE
If you attempt to GET record numbers beyond the end-of-file,
BASIC gives you a n error.
These lines use LOF to determine where to start adding when you want to add to the end of a file:
1 7 0 0 RCNUM% = ( L O F C 1 ) / R C S I Z % )
+
1
1 7 2 0 ' H I G H E S T E X I S T I N G RECORD
1 7 2 0 P U T 1 , RCNUM% 'ADD N E X T RECORD
210
Chapter 10 I BASIC Ke,ywords
LOFlC ommunications
LOF
( buffer)
Function
Returns the amount of free space in the input queue.
Buffer is the number assigned t o the file when you opened it.
You can use LOF to determine when an input queue is getting full so that transmission is stopped.
The default length of the communications receive buffer is 256 bytes. If you wish, you can specify a different length by using the iC: switch when loading BASIC. (See “Options for Loading
BASIC” in Chapter 2.)
211
Chapter 10 I BASIC Keywords
LOG
LOG(
number)
Function
Returns the natural logarithm of number.
Number must be greater than zero. LOG is the inverse of the
EXP function.
BASIC always returns the result as a single precision number.
Examples
P R I N T LOG(3.14159) prints the value 1.144729
Z = 1 0 LOG(PS/Pl) performs the indicated calculation and assigns the value t o Z.
Sample Program
This program demonstrates the use of LOG. It utilizes a formula taken from space communications research.
5 4 0 I N P U T " D I S T A N C E S I G N A L M U S T T R A V E L (MILES)";
D
5 5 0 I N P U T " S I G N A L F R E Q U E N C Y (GIGAHERTZ)".
5 6 0 L = 9 6 . 5 8
+
( 2 0
F
* LOGCF))
+
(20 LOiCD))
5 7 0 P R I N T " S I G N A L S T R E N G T H L O S S I N F R E E S P A C E
IS" L " D E C I B E L S . "
212
Chapter 10 I BASIC Keywords
LPOS
LPOS(num ber)
Function
Returns the logical position of the print head within the printer’s buffer.
Number
can be 0 or 1 to indicate LPT1:.
LPOS is only useful for checking the position of the print head after a LPRINT statement that is terminated by a semicolon to suppress the automatic carriage return. The statement contain- ing LPOS is not executed until the LPRINT statement is fin- ished printing.
LPOS does not necessarily give the physical position head if the printed string contains the ASCII code of the print for a carriage return. For example, if you are printing a string of 20 characters and the 10th character is the ASCII code for a carriage return, the printer advances to the next line after printing the ninth character before printing the remaining 10 characters. If the string is terminated by a semicolon to supress the automatic line feed, the physical location of the print head is at position 10, but
LPOS returns a value of 21 because that is the logical location of the print head.
Example
You may want t o use LPOS t o determine whether there is enough room to continue printing more variables on the same line.
1 0 0 I F L P O S ( X ) > 6 0 THEN L P R I N T
If the printer has printed more than 60 characters, a carriage return is sent so that the printer skips t o the next line.
213
Chapter
10
I BASIC Keywords
LPRINT
LPRINT
[USING format;] data[,data,
...I
Statement
Prints data on the printer.
LPRINT assumes a print width of 80 characters. You may change the width using the WIDTH statement with the LPRINT option.
See PRINT and PRINT USING for more information on format- ting the output.
Examples
L P R I N T ( A * 2 ) / 3 prints the value of expression (A * 2)/3 on the printer.
L P R I N T TAB(S0) " T A B B E D 5 0 " moves t h e p r i n t e r c a r r i a g e
TABBED 50. (Refer t o t o t a b position the TAB function.)
50 a n d p r i n t s
LPR I N T U S I N G
' I # # # # # .
2 . 1 7 sends the formatted value bbbb2.2 t o the printer.
214
Chapter 10 I BASIC Keywords
LSET
LSET field name
=
data
Statement
Moves data to the direct access buffer and places it in
field
name, in preparation for a PUT statement.
Field
name is a string variable defined in a FIELD statement.
You must have used FIELD to set up buffer fields before using
LSET.
You must convert numeric values to string values before they are
LSET. See MKI$, MKD$, MKS$.
You use LSET to left-justify the variable in the field. If the field is larger than the variable it is receiving, the field is filled with blanks on the right. If the variable is larger than the field, char- acters are truncated on the right. The complement command to
LSET is RSET.
See also Chapter
PUT, and RSET.
7, “Files,” and OPEN, CLOSE, FIELD, GET,
Example
Suppose NM$ and
AD$
have been defined as field names for a direct access file buffer. NM$ has a length of 18 characters; AD$ has a length of 25 characters. The statements:
L S E T N M $ = “ J I M C R I C K E T , JR.”
L S E T AD$ = “ 2 0 0 0 E A S T P E C A N ST.” set the data in the buffer as follows:
J I M M C R I C K E T , J R . B B M 2 0 0 0 M E A S T M P E C A N M S T . M M B ~ B M
Notice that filler blanks are placed to the right of the data strings in both cases. If we use
RSET
statements instead of
LSET, the filler spaces are placed to the left. This is the only difference between LSET and RSET.
215
Chapter 10 I BASIC Keywords
MERGE
MERGE pathname
Statement
Loads
a BASIC program and merges it with the program cur- rently in memory.
Pathnume is a standard file specification as described in Chapter
1. The filename is required. The file must be in ASCII format; that is, it must have been saved with the A option.
Program lines in pathname are inserted into the resident pro- gram in sequential order. For example, suppose that
3
lines from pathname are numbered 75, 85, and 90, and 3 lines from the resident program are numbered 70, 80, and 90. When you use
MERGE on the 2 programs, this portion of the merged program is now numbered 70, 75, 80, 85, 90.
If line numbers on the new program coincide with line numbers in the resident program, the new program's lines replace the res- ident program's lines.
MERGE closes all files and clears all variables. Upon completion,
BASIC returns its prompt.
Example
Suppose you have a BASIC program on disk, prog2.M (saved in
ASCII), that you want memory:
to
merge with the program you have in
MERGE " p r o g 2 . t x t " merges the 2 programs.
216
Chapter 10 I BASIC Keywords
Sample Programs
MERGE provides a convenient means of putting program mod- ules together. For example, a n often-used set tines can accompany a variety of BASIC subrou- of programs with this command.
Suppose the following program is in memory:
8 0 REM M A I N PROGRAM
9 0 REM L I N E NUMBER R E S E R V E D FOR S U B R O U T I N E H O O K
1 0 0 REM
1 1 0 REM
1 2 0 REM
PROGRAM L I N E
PROGRAM L I N E
PROGRAM L I N E
1 3 0 E N D
And suppose the following subroutine, in ASCII format: sub.txt, is stored on disk
9 0 GOSUB 1 0 0 0 ' S U B R O U T I N E H O O K
1 0 0 0 REM
1 0 1 0 REM
1 0 2 0 REM
1 0 3 0 REM
1 0 4 0 RETURN
B E G I N N I N G OF S U B R O U T I N E
S U B R O U T I N E L I N E
S U B R O U T I N E L I N E
S U B R O U T I N E L I N E
You can MERGE the subroutine with the main program with:
MERGE " 5 u b . t x t "
The new program in memory is:
8 0 REM
9 0 GOSUB 1 0 0 0
1 0 0 REM
1 1 0 REM
1 2 0 REM
1 3 0 E N D
1 0 0 0 REM
1 0 1 0 REM
1 0 2 0 REM
1 0 3 0 REM
1 0 4 0 RETURN
M A I N PROGRAM
' S U B R O U T I N E H O O K
PROGRAM L I N E
PROGRAM L I N E
PROGRAM L I N E
B E G I N N I N G OF S U B R O U T I N E
S U B R O U T I N E L I N E
S U B R O U T I N E L I N E
S U B R O U T I N E L I N E
217
Chapter 10 I BASIC Ke.ywords
MID$
=
newstring
Statement
Replaces a portion
of
oldstring with newstring.
Oldstring is the variable name of the string you want t o change.
Start is a number specifying the position of the first character you want t o change.
Length is a number specifying the number of characters you want t o replace.
Newstring is the string to replace a portion of oldstring.
The length of the resultant string is always the same as the original string. If newstring is shorter than length, the entire re- placement string is used.
Examples:
1 0 FIB = " L I N C O L N "
20 MID$CAB,3,4)
=
" 1 2 3 4 5 " : P R I N T A B prints LI1234N.
Replace Line 20 with:
2 0 MIDB<FIB,S)
=
" 0 1 " : P R I N T A $ and BASIC prints LINC01N
218
Chapter 10 I BASIC Ke.ywords
MID$
MID$(string, start [,length]>
Function
Returns a substring of a string.
Length is the number of characters in the substring. It must be in the range 1 t o
255.
Start specifies the position in the string from which t o get the substring.
If you omit length or if there are fewer than that number of char- acters t o the right of start position, BASIC returns all characters to the right character of the character at the start position including that at start.
If start is greater than turns a null string. number of characters in string, BASIC re-
Examples
1 0 A $ = "WEATHERFORD"
2 0 P R I N T M I D S C A S , 3 , 2 ) prints AT
F $
= M I D $ < A $ , 3) puts ATHERFORD into F$.
Sample Program
2 0 0 I N P U T "AREA CODE AND NUMBER ( N N N - N N N - N N N N ) " ;
P H $
2 1 0 E X $ = M I D $ C P H $ , 5 , 3)
2 2 0 P R I N T "NUMBER I S I N T H E
"
E X $
"
EXCHANGE."
The first
3
digits of a local phone number are sometimes called the exchange of the number. This program looks at a complete phone number (area code, exchange, last the exchange.
219
Chapter 10 I BASIC Ke.ywords
MKDIR
MKDIR
dirpath
Statement
Creates the directory specified by dirpath.
Dirpath is a standard directory specification as described in
Chapter 1. If you omit the drive identifier, the directory is cre- ated on the current drive.
If
you omit the root directory symbol
( \ I , the directory is created in the current directory.
Examples
M K D I R " A : \ A C C T S \ P A Y A B L E " creates the directory PAYABLE in the ACCTS directory on Drive
A.
MKDI R " \ A D D R E S S " creates the directory ADDRESS in the root directory on the cur- rent drive.
MKDI R "NAMES" creates the directory current drive.
NAMES in the current directory on the
220
Chapter 10 I BASIC Ke.ywords
MKD$, MKI$, MKS$
MKD$(double precision expression)
MKI$( integer expression)
MKS$(single precision expression)
Function
Converts numeric values t o string values.
Any numeric value that is placed in a direct file buffer with an
LSET or RSET statement must be converted to a string.
These
3
functions are the inverse of CVD, CVI, and CVS. The byte values that make up the number are not changed; only 1 byte, the internal data-type specifier, is changed so that numeric data can be placed in a string variable.
MKD$ returns an 8-byte string; MKI$ returns a 2-byte string; and MKS$ returns a 4-byte string.
Example
L S E T A V G S . = M K S S ( 0 . 1 2 3 )
Sample Program
1 3 5 0 OPEN " R " , 1
I
" t e ~ t . d a t " , 1 4
1 3 6 0 F I E L D 1 , 2 A S 1 1 9 , 4 A S I 2 $ , 8 A S 13s
1 3 7 0 L S E T I I S = MKIS(3000)
1 3 8 0 L S E T 12s = M K S S C 3 0 0 0 . 1 )
1 3 9 0 L S E T 13s = M K D S C 3 0 0 0 . 0 0 0 0 1 )
1 4 0 0 P U T 1 , 1
1 4 1 0 C L O S E 1
For a program that retrieves the data from
CVI/CVS. test.dat, see CVDi
221
Chapter 10 I BASIC Keywords
NAME
NAME
old filename
AS
new filename
Statement
Renames old filename as new filename.
With this statement, the data in the file is left unchanged. The new filename may not c o n t a i n p a t h , password, or d r i v e specification.
You can only rename a file in the current directory.
Example
N Q M E " f i l e . b a 5 " A S "f ile.old"
BASIC
renames file.bas as file.old.
222
Chawter 10 I BASIC Kevwords
NEW
NEW
Statement
Deletes the program currently in memory and clears all vari- ables.
NEW
also closes all open files, turns off the trace function and resets the music background.
Example
NEW
223
Chapter 10 I BASIC Keywords
NOISE
NOISE source, volume,d uratwn
Statement
Generates noise through a TV monitor’s speaker (external speaker).
Source is an integer in the range
0
to
7.
It selects the type of noise. If source is in the range
0
t o
3,
periodic noise is selected.
If source is in the range
4
t o
7,
white noise is selected.
Periodic
0
1
2
3
White
Frequency
4 35795451512
5
6
7
357954511824
357954512048
f r e q u e n c y
SOUND). of Voice
2
( s e e
Volume is an integer in the range
0
to
15
where
0
is the quietest and
15
is the loudest.
Duration is a numeric expression in the range
0
to
65535.
It sets the duration of the noise.
A
duration of
18.2
equals
1
second.
You must execute a SOUND ON statement before using NOISE or BASIC returns an “Illegal function call” error.
Sample Programs
1 0 SOUND ON
2 0 FOR I = 0 TO 7
3 0 N O I S E
4 0 P L A Y
I 1 I, II I,
“ V 0 ”
5 0 N E X T I
I ,
>
1 5 , 2 5 0
,
224
Chapter 10 I BASIC Keywords
OCT$
OCT$( num ber)
Function
Returns the octal value of number.
OCT$ returns a string that represents the octal value of a deci- mal number. The value returned is like any other string-it can- not be used in a numeric expression.
Examples
P R I N T O C T $ C 3 0 ) , O C T $ ( S 0 ) , O C T $ ( 9 0 ) prints the strings
36, 62,
and
132.
Y $ = O C T $ < X / 8 4 )
Y$ is a string representation of the integer quotient X/84 to base
8.
225
ChaDter 10 I BASIC Kevwords
ON
COM(
) GOSUB
ON COM(channeZ) GOSUB
line
Statement
Transfers program control to a subroutine beginning at line when activity occurs on the communication channel.
Channel selects communications Channel 1 or 2.
Line is the first line of the subroutine t o be executed when activ- ity occurs on the specified communications channel. If you spec- ify Line 0, you t u r n off communications trapping. Use the
RETURN statement t o exit the subroutine.
BASIC executes the ON COM( ) GOSUB statement only if a
COM( ON statement has been previously executed t o enable communication trapping. If a COM( ) STOP statement has been issued to halt communication trapping temporarily, BASIC exe- cutes the subroutine immediately after the next COM( 1 ON statement.
When you execute the ON COM( ) GOSUB statement, BASIC immediately issues a COM( 1 STOP statement to prevent recur- sive traps. When BASIC executes the RETURN from the subrou- tine, it automatically executes another COM( ) ON statement t o enable communication trapping again, unless the subroutine exe- cutes a COM( OFF statement.
226
Chapter 10 I BASIC Keywords
Example
1 0 C O M ( 1 ) ON
2 0 0 ON COM(1 1 GOSUB 1 0 0 0
Line 10 turns on communication trapping on Channel 1. After each program statement is executed, BASIC checks t o see if the communication buffer contains characters. If it does, BASIC im- mediately executes the subroutine beginning at Line 1000.
1 0 C O M ( 1 ) ON
2 0 ON COMC1) GOSUB 1 0 0 0
3 0 FOR I = 1 TO 1 0
4 0 P R I N T I
5 0 N E X T I
1 0 0 0
’
S U B R O U T I N E CODE
1 0 5 0 R E T U R N 2 0 0
If activity occurs on Communication Channel 1 while the FOR/
NEXT loop is executing, BASIC immediately executes the sub- routine beginning at Line 1000. But the subroutine returns t o
Line 200 instead of completing the FORiNEXT loop. This results in a “For without next” error because any GOSUB, FOR, or
WHILE statement remains active during key trapping.
If the RETURN statement does not include a line number, pro- gram control returns to complete the FOR/NEXT loop, and no error occurs.
227
Chapter 10 I BASIC Keywords
ON ERROR
GOTO
ON ERROR GOTO
line
Statement
Transfers control to line if a n error occurs.
This lets your program recover from a n error and continue exe- cution. (Normally, you have a particular type of error in mind when you use the ON ERROR GOTO statement.)
You must execute a n ON ERROR GOTO before the error occurs.
To disable it, execute a n ON ERROR GOTO 0, which causes
BASIC to stop execution and print a n error message. This is rec- ommended for errors that are trapped and from which you can- not recover.
Note:
If a n error occurs during execution of a n error- handling routine, that error message is printed and execution terminates. Error trapping does not occur within the error handling routine.
The error-handling routine must be terminated by a RESUME statement.
Example
1 0 ON E R R O R GOTO 1 5 0 0 branches program control to Line where after Line 10.
1500 if a n error occurs any-
Sample Program
See ERROR.
228
Chapter 10 I BASIC Keywords
ONIGOSUB
ON n
GOSUB
... I
Statement
Looks at n and transfers program control t o the subroutine indi- cated by the nth line listed.
For example, if n equals 1, BASIC branches to the first line listed; if n equals 2, BASIC branches to the second line listed.
Line is the subroutine line at which execution begins when
BASIC makes the branch.
N
must be a number in the range 0 to 255. If necessary, BASIC rounds n t o an integer before evaluating it. If n is 0 or greater than the number of line numbers listed, BASIC continues with the next statement. If n is negative or is greater than 255, an
“Illegal function call” error occurs.
Use the RETURN statement t o exit the subroutine.
Example
1 0 ON Y G O S U B 1 0 0 0 , 2 0 0 0 , 3 0 0 0
If Y equals 1, BASIC branches to a subroutine, beginning at
Line 1000. If Y equals 2, BASIC branches to a subroutine, be- ginning at Line 2000. If Y equals 3, BASIC branches to a sub- routine, beginning at Line 3000.
If Y is outside the range 1 t o 3, BASIC either continues with the next statement or generates an “Illegal function call,” as men- tioned earlier.
229
Chapter
10
I BASIC Keywords
Sample Program
4 3 0 I N P U T "CHOOSE 1 , 2 , OR 3" ; I
4 4 0 ON I GOSUB 5 0 0 , 6 0 0 , 7 0 0
4 5 0 E N D
5 0 0 P R I N T " S U B R O U T I N E # I " : RETURN
6 0 0 P R I N T " S U B R O U T I N E # 2 " : RETURN
7 0 0 P R I N T " S U B R O U T I N E #3": RETURN
230
Chapter 10 I BASIC Keywords
ON/GOTO
ON
n
GOTO
... I
Statement
Looks at n and transfers program control t o the nth line listed.
For example, if n equals
1,
BASIC branches to the first line listed; if n equals
2,
BASIC branches t o the second line listed.
N must be in the range
0
to
255.
If necessary, BASIC rounds n t o an integer before evaluating it. If
n
is
0
or is greater than the number of line numbers listed, BASIC continues with the next statement. If n is negative or is greater than
255,
an "Illegal function call" error occurs.
Example
1 0 ON M I GOTO 1 5 0 , 1 6 0 , 1 7 0 , 1 5 0 , 1 8 0 tells BASIC
Line t o evaluate MI. If MI equals
1,
BASIC branches to
150;
if MI equals
2,
BASIC branches to Line
160;
and so on. If MI is outside of the range
1
t o
5,
BASIC either continues with the next statement or generates an Illegal function call, as mentioned earlier.
Sample Program
5 REM < C A P S > MUST B E ON
1 0 I N P U T "ENTER A , B , o r C,";LS
2 0 L=CISC C L S )
3 0 O N L - 6 4 GOTO 5 0 , 6 0 , 7 0
4 0 P R I N T "TRY AGCI1N":GOTO 1 0
5 0 PRINT 9 1 ~ 0 ~
6 0 P R I N T "YOU T Y P E D ' B ' " : E N D
7 0 P R I N T "YOU T Y P E D ' C ' " : E N D
231
Chapter 10 I BASIC Keywords
ON KEY() GOSUB
ON KEY(number)
GOSUB
line
Statement
Transfers program control to a subroutine when you press the specified key.
Number may be a number in the range 1 to 20, indicating the number of the key to trap. Function keys use their corresponding function key numbers. The cursor direction keys are numbered: m
El
El
El
11
12
13
14
User keys are numbered with the KEY statement.
15 through 20. User keys are defined
Line is the first line number in the subroutine to execute when the specific key is pressed. If you specify Line 0, you turn off key trapping for that key.
It
is the same as executing a KEYO OFF statement. Use the RETURN statement to exit the subroutine.
BASIC executes the ON KEYO GOSUB statement only if a
KEYO ON statement has been executed previously to enable key trapping for that key.
If a KEYO STOP statement has been issued to halt key trapping for that key temporarily, BASIC executes the subroutine immedi- ately after the next KEYO ON statement for that key.
When you execute the ON KEY() GOSUB statement, BASIC im- mediately issues a KEYO STOP statement for that key to pre- vent recursive traps. When BASIC executes the RETURN from the subroutine, it automatically executes another KEY () ON statement for that key to enable key trapping again, unless the subroutine executes a KEYO OFF statement for that key.
232
Chapter 10 I BASIC Ke-ywords
Sample Program
1 0 K E Y ( 5 ) ON
2 0 K E Y ( 1 ) ON
3 0 ON K E Y ( 5 ) GOSUB 8 0
4 0 ON K E Y ( 1 ) GOSUB 1 0 0
5 0 FOR I = 1 TO 1 0 0
6 0 P R I N T "NO K E Y PRESSED"
7 0 N E X T 1 : E N D
8 0 P R I N T " K E Y ( 5 ) PRESSED
* * * * * * * * * * * * ' I
9 0 RETURN
1 0 0 P R I N T " K E Y ( 1 P R E S S E D ; ; ; ; ; ; ; ; ; ; ;
1 1 0 RETURN
This program sets up [F51 and t o be trapped.
233
Chapter
10
1 BASIC Keywords
ON
PEN
GOSUB
ON
PEN GOSUB
line
Statement
Transfers program control to a subroutine when you activate the light pen.
Line
is the first line number in the subroutine to execute when the light pen is activated. If you specify Line 0, you turn off trapping for the pen. It is the same as executing a PEN OFF statement. Use the RETURN statement to exit the subroutine.
BASIC executes the ON PEN GOSUB statement only if a PEN
ON statement has been executed previously to enable light pen trapping.
If a PEN STOP statement has been issued to halt trapping for the pen temporarily, BASIC executes the subroutine immediately after the next PEN ON statement.
When you execute the ON PEN GOSUB statement, BASIC im- mediately issues a PEN STOP statement to prevent recursive traps. When BASIC executes the RETURN from the subroutine, it automatically executes another PEN ON statement to enable pen trapping again, unless the subroutine executes a PEN OFF statement.
234
Chapter 10 I BASIC Keywords
Example
1 0 P E N ON
2 0 ON P E N GOSUB 1 0 0 0
3 0 REM
5 0 0 END
1 0 0 0 REM P R O C E S S I N G R O U T I N E
1 1 0 0 RETURN ' 3 0
Line 10 turns on pen trapping. After each program statement is executed, BASIC checks to see if the pen has been activated. If i t has, BASIC immediately executes the subroutine a t Line 1000.
235
Chapter 10 I BASIC Keywords
ON PLAY() GOSUB
ON PLAY(number)
GOSUB
line
Statement
Transfers program control to a subroutine when the number of notes in the background music buffer goes from number to num- ber minus 1. This event trapping allows continuous music by let- ting you maintain a full music buffer.
Number is a n integer in the range 1 to 32, indicating that con- trol should transfer to line when the number of notes left in the music buffer is less than number.
Line is the first line of the subroutine to execute when the num- ber of notes in the music buffer is less than number. If you spec- ify Line 0, you turn off play event trapping. Use the RETURN statement to exit the subroutine.
BASIC executes the ON PLAY() GOSUB statement only when playing background music (PLAY “MB”) and if the PLAY statement has been executed to enable event trapping.
ON
If a PLAY STOP statement has been issued to halt event trap- ping temporarily, BASIC executes the subroutine immediately after the next PLAY ON statement.
When you execute the ON PLAY() GOSUB statement, BASIC immediately issues a PLAY() STOP to prevent recursive traps.
When BASIC executes the RETURN from the subroutine, it au- tomatically executes another PLAY0 ON statement t o enable trapping again, unless the subroutine executes a PLAY0 OFF statement .
Notes:
BASIC does not issue a play event trap if the background music queue is already empty when you execute a PLAY ON.
The PLAY statement is supported by a 32-element mu- sic queue. Given that “normal” and “staccato” notes are constructed from 2-note elements, the queue can contain as few as 16 notes or as many as 32 notes.
236
Chapter 10 I BASIC Keywords
Therefore, select conservative values for the trap num- ber. For example, if number is set a t 32, event traps might happen so often that there is little time t o exe- cute the rest of your program. It is suggested that the trap number be less than 16 for better performance.
Example
1 0 P L A Y ON
2 0 ON P L A Y ( 2 )
3 0 REM
G O S U B 1 0 0 0
5 0 0 E N D
1 0 0 0 REM P R O C E S S I N G R O U T I N E
1 1 0 0 R E T U R N 3 0
Line 10 turns on play trapping. After each program statement is executed, BASIC checks t o see if the number of notes in the mu- sic buffer is less than 2 notes. If it is, BASIC immediately exe- cutes the subroutine at Line 1000.
237
Chapter 10 I BASIC Keywords
ON STRIG() GOSUB
ON
STRIG(number)
GOSUB
line
Statement
Transfers program control to a subroutine when you press one of the joystick’s buttons.
Number specifies the number of the button pressed and is one of the following:
0
2 left joystick, button 1 right joystick, button 1
4 left joystick, button 2
6 right joystick, button 2
Line is the first line number of the subroutine to be executed when you press one of the joystick’s buttons. If you specify Line
0, you turn off trapping for the joysticks. Use RETURN to exit the subroutine.
BASIC executes the ON STRIGO GOSUB statement only if a
STRIG ON statement has been executed previously to enable joystick trapping.
If a STRIG STOP statement has been issued to halt joystick trapping temporarily, BASIC executes the subroutine immedi- ately after the next STRIG ON statement.
When the ON STRIGO GOSUB statement is executed, BASIC immediately issues a STRIG STOP statement to prevent recur- sive traps. When BASIC executes the RETURN from the subrou- tine, it automatically executes another STRIG ON statement to enable joystick button trapping again, unless the subroutine exe- cutes a STRIG OFF statement.
238
Chawter 10 I BASIC Kevwords
Sample Program
5 S T R I G ( 0 ) O N : S T R I G ( 2 ) ON
1 0 ON S T R I G C 0 ) GOSUB 1 0 0 0
2 0 ON S T R I G C 2 ) GOSUB 2 0 0 0
3 0 P R I N T " P r e s s o n e o f t h e j o y s t i c k b u t t o n s . "
4 0 FOR I
= 1 TO 3 0 0 0 : N E X T I
5 0 GOTO 3 0
1 0 0 0 P R I N T "You p r e s s e d t h e left button."
: R E T U R N
2 0 0 0 P R I N T
: RETURN
"You p r e s s e d t h e r i g h t button."
Lines 10 and 20 turn on joystick trapping. Line 30 instructs you to press one of the buttons. Line 40 waits for you to press a but- ton. If you press the left button, BASIC transfers program control to the subroutine at Line 1000. If you press the right button,
BASIC transfers program control to the subroutine at Line 2000.
If you do not press a button, Line 50 returns to print the mes- sage again. This program is a continuous loop. To end the pro- gram, press [ C T R L ] m ] .
239
Chapter 10 I BASIC Keywords
ON
TIMER() GOSUB
ON TIMER(number1 GOSUB line
Statement
Transfers program control t o a subroutine when the specified pe- riod of time has elapsed.
Number indicates the number of seconds. Number may be a value in the range 1 to 86400 (86400 seconds = 24 hours).
Line is the first line number in the subroutine to execute when the specified time has passed. If you specify Line 0, you turn off trapping for the timer. Use RETURN t o exit the subroutine.
BASIC executes the ON TIMER0 GOSUB statement only if a
TIMER ON statement has been executed previously to enable time event trapping.
If a TIMER STOP statement has been issued to halt time event trapping temporarily, BASIC executes the subroutine immedi- ately after the next TIMER ON statement.
When you execute the ON TIMER() GOSUB statement, BASIC immediately issues a TIMER STOP to prevent recursive traps.
When BASIC executes the RETURN from the subroutine, it au- tomatically executes another TIMER ON statement to enable trapping again, unless the subroutine executes a TIMER OFF statement.
240
Chapter 10 I BASIC Keywords
Example
1 0 T I M E R ON
2 0 ON T I M E R ( 6 0 ) GOSUB 1 0 0 0
3 0 REM
5 0 0 E N D
1 0 0 0 REM P R O C E S S I N G R O U T I N E
1 1 0 0 RETURN 3 0
Line 10 turns on timer trapping. After each statement is exe- cuted, BASIC checks to see
if
the specified time has elapsed. If it has, BASIC immediately executes the subroutine a t Line
1000.
241
Chapter 10 I BASIC Keywords
OPEN
Statement
OPEN [pathname][dev:l
[FOR
model AS [#]buffer
[LEN
=
record length1
Establishes a n input/output path for a file or device.
Buffer is a n integer in the range buffer in memory
1
t o
15.
It specifies the 110 t o use when accessing the file. The number sign (#) is optional. It is provided for compatibility with other
BASICs.
Pathname is a standard file specification as described in Chapter
1. If you omit pathname, you must include deu:. deu: specifies the device t o be opened for communication.
Record length is a n integer in the range
1
t o
32768
that sets the record length for direct access files. If you omit record length,
BASIC assumes a default record length of this option with sequential access files.
128
bytes.
Do not
use
Mode specifies any of the following:
0
I or OUTPUT or INPUT
A or APPEND
R or RANDOM sequential output mode sequential input mode sequential output and extend mode direct inputloutput mode
In the first form of the syntax, you must use the abbreviated form of mode, and it must be enclosed in quotation marks.
In the second form of the syntax, you must specify the complete word for mode. You may not specify RANDOM. If you want t o use direct access in the second form of the syntax, omit mode.
242
Chapter 10 I BASIC Kevwords
You may open a file for output in only one buffer at a time. Once you assign a buffer t o a file with the not use that buffer in another
OPEN
statement, you can-
OPEN statement until you close the first file. However, BASIC lets you access the same file for in- put by opening it in different buffers. You may keep several rec- ords from the same file in memory for quick access.
If you open a file for input that does not exist, BASIC returns a
"File not found" error.
If you open a file for output that does not exist, BASIC creates the file.
If you open a file for append that does not exist, BASIC creates the file and sets the mode t o RANDOM.
If you open a file for direct access with a record length that does not match the record length assigned t o the file when it was cre- ated, an error occurs.
Examples
OPEN " R " , 2 , " t e s t . d a t " opens the file test.dat in direct access mode, using Buffer 2. If test.dat does not exist, BASIC creates it on the current drive.
The record length is 128 bytes.
OPEN " L P T I
: I '
FOR OUTPUT A S 2 opens the printer for sequential output using Buffer 2.
OPEN " A : \ P A Y R O L L \ d a t a . b a 3 " F O R I N P U T A S 1 opens the file databas in the PAYROLL directory on Drive A for sequential input using Buffer 1.
243
Chapter 10 I BASIC Ke.ywords
OPE N/C ommunications
OPEN
“ C O M c h a n ~ l :
Statement
[,PEl [,LFl” AS
[#lbuffer[LEN
=
number]
Opens a file and allocates a buffer for RS-232C (Asynchronous
Communications Adapter) communication.
Channel can be 1 or 2 t o select the communications channel t o be opened.
Speed is a n integer specifying the transmit and receive rate in bits per second (bps). Valid speeds are 75, 110, 150, 300, 600,
1200, 2400, 4800, and 9600. If you omit speed, BASIC sets the speed at 300 bps.
Parity is a constant specifying the parity t o be used when the data is transmitted and received. The constant must be one of the following:
E
0
M
S
N
EVEN t r a n s m i t parity, EVEN receive parity checking.
ODD t r a n s m i t p a r i t y , ODD receive p a r i t y checking. parity bit always transmitted and received as a mark (1 bit). parity bit always transmitted and received as a space (0 bit). no transmit parity, no receive parity checking.
If you omit parity, BASIC assumes E (EVEN).
Data is a n integer specifying the number of transmit and re- ceive bits. Valid values are
5 ,
6, 7, and 8. If you do not specify data, BASIC assumes 7 .
Note:
Eight data bits
with
parity is illegal.
Stop must be either 1 or 2 t o indicate the number of stop bits. If you omit stop, 75 and 110 bps transmit 2 stop bits, and all other speeds transmit 1 stop bit.
244
Chapter 10 I BASIC Keywords
Buffer
is a number in the range 1 t o 15, indicating the buffer that accesses the file. The number sign (#) is optional. It is pro- vided for compatibility with other BASICs.
Number
specifies the maximum number of bytes that can be ac- cessed in the communications buffer by GET and PUT state- ments. If you omit the LEN option, BASIC assumes 128 bytes.
The parameters
speed, parity, data,
and
stop
are all positional.
That is, they must be in the order specified in the syntax. If you omit one of the parameters, you must still include the comma t o hold its position.
The remaining parmeters are not positional. They may be in any order, or you may omit them. They control the software commu- nication signal lines between 2 terminals. If you omit the CS,
DS, or CD options, the signals are not checked a t all. Include them only if you are testing these software signals.
The RS option suppresses the Request To Send (RTS) signal. Re- quest To Send is a signal that is sent from the sending terminal to the receiving terminal to ensure that the receiving terminal is ready t o accept communication data. When you execute an
OPEN COM statement, the RTS line is turned on, unless you in- clude the RS option.
The CS option controls the Clear To Send (CTS) signal which is sent from the receiving terminal t o the sending terminal to let the sending terminal know that the receiving terminal is ready t o receive.
You can think of RTS and CTS as a hand-shaking exercise, in which the 2 terminals let each other know that they are ready to send and/or receive data. RTS is an output signal from the send- ing terminal, and CS is an input signal to the sending terminal.
The DS option controls the Data Set Ready (DSR) signal. The
DSR signal ensures that a data set, such as a modem, is present to transmit the data.
The CD option controls the Carrier Detect (CD) signal. The CD signal is an input signal which ensures that the data set is ready t o transmit the data.
245
Chapter 10 I BASIC Keywords
The seconds argument in the CS, DS, and CD options specifies the number of milliseconds to wait for the signal before return- ing a “Device Timeout” error. Seconds may be in the range 0 to
65535. If you omit seconds or specify a zero, the signal is not checked.
If you specify RS, seconds defaults to zero for CS. If you omit RS, the default for CS is 1000. Either a n RS or a CS is required.
That is, if you omit RS, the Clear To Send signal is not checked.
If you include RS, OPEN COM waits 1 second for CS before issu- ing a “Device Timeout” error.
If you omit seconds after the DS option, the default value is
1000, and OPEN COM waits 1 second before issuing a “Device
Timeout” error. If you omit seconds after CD, the default is zero and the signal is not checked.
IiO statements to a communications file do not execute if these signals are off. The system waits 1 second before returning a
“Device Timeout” error. Specifying these options lets you ignore these signals or specify the length of time t o wait for the signal.
The LF option sends a line feed character after every carriage return. This is useful if you are printing the communication data to a serial line printer. A line feed is also sent after the carriage return that is the result of the width setting. Note that when you specify the LF option, INPUT# and LINE INPUT# stop when they read a carriage return and ignore the line feed.
Mode specifies the type of data that is transmitted. It may be either BIN for binary mode or ASC for ASCII mode. If you omit mode, OPEN COMl opens the device in binary mode.
If you specify the BIN mode, OPEN COM does not expand tabs to spaces, does not force a carriage return does not recognize Control at the end of the line,
Z
as a n end-of-file, and ignores the
LF option.
If you specify the ASC mode, OPEN COM expands tabs t o spaces, forces a carriage return a t the end of the line, and recog- nizes Control
Z
as the end-of-file. When you close the channel,
Control
Z
is sent over the RS-232 line.
246
Chapter 10 I BASIC Keywords
The PE option enables parity checking. With the option on, par- ity errors cause Device IiO errors and turn on the high order bit for 7 or fewer data bits. The default is no parity checking. Fram- ing and overrun errors always cause Device IiO errors and turn on the high order bit, regardless of whether or not you use the
PE option.
Examples
O P E N "CUM1 : " A S 1 opens Buffer 1 for Communications Channel 1 at a rate of 300 bps with even parity, 7 data bits, and 1 stop bit. RTS is sent.
O P E N " C O M 2 : 9 6 0 0 , N , 8 , 1 ,BIN" A S 2 opens Buffer 2 for Communications Channel 2 at a rate of 9600 bps with no parity, 8 data bits, and 1 stop bit. The data is binary.
O P E N "COM1 : 4 8 0 0 , ,
,
, C S 3 0 0 0 A S 1 opens Buffer 1 for Communications Channel 1 at a rate of 4800 bps with even parity, 7 data bits, and 1 stop bit. RTS is sent.
OPEN COM issues a "Device Timeout" error if there is no CS signal after 3 seconds and no DS signal after 2 seconds. Note that even though parity, data, and stop are not included, the commas are required.
247
Chapter 10 I BASIC Keywords
OPTION BASE
OPTION
BASE value
Statement
Sets value as the minimum value for an array subscript.
Value may be 1 or 0. The default is 0.
If you use this statement in a program, it must precede the DIM statement.
If the statement:
O P T I O N BFISE 1 is executed, 1 is the lowest value a n array subscript may have.
248
Chapter 10 I BASIC Keywords
OUT
OUTport, data byte
Statement
Sends a data byte t o a machine output port. A port is an input/ output location in memory.
Port is an integer in the range 0 t o
65535.
Data byte is an integer in the range 0 t o
255.
Example
OUT 3 2 , 1 0 0 sends 100 to port
32.
249
Chapter 10 I BASIC Ke.ywords
PAINTlGraphics
Statement
Fills in a n area on the display with a selected color or pattern.
(x,y) x is the horizontal coordinate, and y is the vertical coordinate.
Color can be either a number.or a string expression. If color is a number it specifies a color number available in the current screen mode.
If color is a string expression, it specifies the mask t o be used for tiling. The tiling mask describes a pattern to be used when painting and is in the form:
Border specifies the border color a t which t o stop painting, and must be a color number in the current palette. If you omit bor- der, BASIC assumes the value of color.
See Chapter 8, “Displaying Color and Graphics,” for information on coordinates and colors for the current screen mode.
Background is a 1-byte string expression specifying which color t o skip when checking for borders while paint tiling.
BASIC begins ify with to change the color of pixels a t the point you spec- x and y coordinates. BASIC continues to change the color of every pixel that is not the same color as color. When
BASIC paints 1 line of pixels without changing the color of any pixel in that line, PAINT is complete.
However, you may continue past this point while tile painting.
The background option tells PAINT what background tile pattern or color byte t o skip when checking for the boundary.
250
Chapter 10 I BASIC Keywords
This means that instead of stopping when 1 line of points has been painted without changing the color, PAINT can continue, if you specify
background.
For example, normally you cannot draw alternating blue and red lines on a red background because
PAINT stops after painting the first red line. However, by speci- fying red as the background color (&HAA), you can draw the red line over the red background.
PAINT must start on a nonborder point. If the point is already
border
o r
color
color, BASIC does not execute t h e PAINT statement.
PAINT can fill any figure, but painting jagged edges or very complex figures may result in a n “Out of memory” error. If this happens, you must use the CLEAR statement t o increase the amount of stack space available.
Tiling
Tiling lets you select a pattern t o be used when painting an area on the screen. The tile mask is 8-bits wide and may be a maxi- mum of
X,Y
0,0
0,1
012
073
8 7 6 5 4 3 2 1 tile byte
.
.
.
.
.
.
.
.
1
.
.
.
.
.
.
.
.
2
. . . . . . . . 3
.
.
.
.
.
.
.
.
4
0,63 .
.
.
.
.
.
.
.
64
Each byte in the mask represents 8 points along the horizontal row and 1 point along the vertical row. PAINT repeats the tile mask pattern (horizontally and vertically) pattern over the entire area being painted.
t o
create a uniform
251
Chapter 10 I BASIC Keywords
In high resolution graphics, 1 bit of the tile mask equals 1 point on the screen. Therefore, each position in the tile mask with the bit value one (1) is drawn. You can paint a pattern of Xs with this tile mask: byte 8 7 6 5 4 3 2 1
0 1 0 0 0 0 0 0 1 CHR$(&H8 1)
1 0 1 0 0 0 0 1 0
2 0 0 1 0 0 1 0 0
3 0 0 0 1 1 0 0 0
CHR$(&H42)
CHR$(&H24)
CHR$(&H18)
4 0 0 0 1 1 0 0 0
5
0 0 1 0 0 1 0 0
CHR$(&H18)
CHR$( &H24)
6 0 1 0 0 0 0 1 0
7 1 0 0 0 0 0 0 1
CHR$(&H42)
CHR$(&H81)
In 4-color graphics, 2 bits correspond to each point on the screen.
That is, each byte of the tile mask describes only 4 points. These
2 bits describe the color for the point being drawn. The following chart shows the values for the given colors. Remember, Color 0 is the set background color. (See COLOR.)
Palette
0 green red brown
Palette
1 cyan magenta white binary value
01
10
11
The following tile mask sets up a brown using Palette 0 star pattern in green and or in cyan and white using Palette 1.
BYTE
0
1
2
3
4
01 00 00 01 CHR$(&H41)
00 01 01 00 CHR$(&H14)
11 11 11 11 CHR$(&HFF)
00 01 01 00 CHR$(&H14)
01 00 00 01 CHR$(&H41)
In 16-color graphics (Modes 3 and
5 ) ,
4 bits correspond t o each point on the screen. That is, each byte of the tile mask describes only 2 points.
252
Chapter 10 I BASIC Keywords
PALETTE/Graphics
PALETTE [color,dispZay
Statement
Changes the color associated with a particular color number in the current palette.
Color specifies the color number in the current palette you want t o change. It is a n integer in the range 0 t o 15. See Chapter 8,
“Displaying Color and Graphics,” for information on colors in the current screen mode.
Display color specifies the new color you want BASIC t o display when color is specified. Display color may be a number in the range 0 t o 15 as described in Chapter 8, “Displaying Color and
Graphics .”
For example, in Screen Mode 3, Color 1 is blue. You can use the
PALETTE statement to change it so that Color 1 is associated with a different color. For example, use this statement t o change
Color 1 to magenta:
P A L E T T E 1 , 5
When you execute a PALETTE statement t o change the default values, the new values remain in effect until you execute another
COLOR/Graphic, PALETTE or PALETTE USING statement. A
PALETTE statement without parameters forces the position numbers t o return t o their default values.
You can change only 1 position in the palette each time you exe- cute a PALETTE statement. To change more than 1 position in a palette, see the PALETTE USING statement.
253
Chapter 10 I BASIC Ke.ywords
Example
P A L E T T E 3 , 7 changes the third position from cyan t o gray.
P A L E T T E changes all positions in the current palette to their default values.
Sample Program
5 SCREEN 1
1 0 COLOR 0 , 0
2 0 P A L E T T E 3 , l
3 0 L I N E ( 0 , 1 - 0 0 ) - ( 3 1 9 , 1 0 0 ) , 3
4 0 P A I N T ( 1 , 1 0 0 ) , 3 , 6
5 0 P A L E T T E
Line 10 selects Palette 0 as the current palette. Line 20 changes position Number 3 from brown t o blue. Line 30 draws a horizon- tal blue line across the center lower half of the screen. Line 40 colors the of the screen blue. Line 50 causes the palette to re- turn t o its original value. Position 3 is now brown again.
254
Chawter 10 I BASIC Kevwords
PALETTE
USING/Graphics
PALETTE USING array(subscrippt)
Statement
Changes the colors associated with more than 1 of the color numbers in the current palette.
Array is the name of an integer array in which you define the order in which colors are to be put in the current palette.
Subscript is the array position that contains the value of the color that you want put in the first palette position. After BASIC assigns that value t o the first position, it assigns the remaining array values to the remaining palette positions. Therefore, when you create the array, have at least 16 elements following sub- script so that PALETTE USING has enough colors to fill the palette.
The array can be larger than the palette. PALETTE USING stops filling the current palette when it reaches the last position in the palette.
To create the array, load a color number into each element.
Group color numbers that you use most often. For example, if you use 2 shades of blue with 2 shades of numbers adjacent in the array, like this: green, place their color
Subscript
0
1
2
3
Color
2 (Green)
10 (Light Green)
1 (Blue)
9 (Light Blue)
In the statement:
P A L E T T E U S I N G A C 0 )
Color 0 becomes green, Color 1 becomes light green, Color 2 be- comes blue, and Color
3
becomes light blue.
If you assign a value of -1 to a position in the array, PALETTE
USING does not change the corresponding color from its previous value.
255
Chapter 10 1 BASIC Keywords
The array may be larger than the palette. PALETTE USING stops filling the current palette when it reaches the last position in the palette.
0
1
2
3
4
5
6
7
8
9
If you use the 2 shades of blue with the 2 shades of red, and also use the 2 shades of blue with the 2 shades of cyan, you can put the numbers for the shades of blue in your array as often as you need them. You can expand the previous array, for example:
Subscript Color
2 (Green)
10 (Light Green)
1 (Blue)
9 (Light Blue)
12 (Light Red)
9 (Light Blue)
1 (Blue)
3 (Cyan)
11 (Light Cyan)
Examples
P A L E T T E USING A ( 2 ) loads the palette with the blues and reds. Color 1 is still blue.
Color 2 becomes light blue, Color
3
becomes red, Color 4 light red.
P A L E T T E USING A ( 4 ) puts the same colors in the palette in a different order. In this case, Color 1 becomes red, Color 2 becomes light red, Color 3 be- comes light blue, Color
256
Chawter 10 I BASIC Kevwords
PCOPY
PCOPY source page,destinatwn page
Statement
Copies one video page to another.
Source page is an integer representing a video page in memory to be copied. The range depends on the current screen mode and the amount of video memory.
Destination page represents the video page to receive the copy.
Its
requirements are the same as source page.
Example
P C O P Y 3 , 5 copies the contents of Video Page 3 to Video Page 5.
257
Chawter 10 I BASIC Kevwords
PEEK
PEEK(mmry
location)
Function
Returns a byte from memory location
Memory
location must be in the range -32768 to 65535.
The value returned is an integer in the range 0 t o 255. (For the interpretation of a negative value of memory location, see the
VARPTR statement.)
PEEK is the complementary function of the POKE statement.
Example
A = P E E K ( 6 H S A 0 0 )
BASIC returns the value stored at address 5A00 and stores it in variable A.
258
Chapter 10 I BASIC Keywords
PEN
PEN(
num ber)
Function
Returns the light pen’s coordinates.
Number is a number in the range 0 t o 9 that tells BASIC what t o return. Values 0-5 return x,y coordinates corresponding to the current screen mode. Values 6-9 return the character row umn position. or col-
0 Returns a -1 if pen button has been pressed since last poll. Returns a 0 if not.
1
2
Returns the x coordinate (horizontal) where the pen was last activated.
Returns the y coordinate (vertical) where the pen was last activated.
3
4
5
6
7
8
Returns a -1 if the pen button is being pressed. Re- turns a 0 if it is up.
R e t u r n s t h e l a s t k n o w n v a l i d x c o o r d i n a t e
(horizontal).
Returns the last known valid y coordinate (vertical).
Returns the character row position where the pen was last activated.
Returns the character column position where the pen was last activated.
Returns the last known character row position.
9 Returns the last known character column position.
You must execute
a
PEN ON statement before executing the
PEN function. If you do not, an “Illegal function call” error occurs.
Example
A = P E N C 1 ) returns the x coordinate of the pen.
259
Chapter 10 I BASIC Keywords
PEN/Trap
PEN
action
Statement
T u r n s on, t u r n s off, or t e m p o r a r i l y h a l t s l i g h t pen event trapping,
Actwn may be any of the following:
ON
OFF enables event trapping disables event trapping.
STOP temporarily suspends event trapping.
Use the PEN/Trap statement in a light pen trap routine with the ON PEN statement to detect when the light pen has been activated.
The PEN ON statement turns on the trap. BASIC checks after each program line to see if the light pen has been activated. If so, BASIC transfers program control to the line number specified in the ON PEN GOSUB statement.
The PEN STOP statement temporarily halts light pen trapping.
If the light pen is activated, BASIC does not transfer program control to the ON PEN GOSUB statement until you turn on trapping again by executing a PEN ON statement. BASIC re- members that the light pen was activated and branches to the subroutine immediately after trapping is turned on again.
The PEN OFF statement turns off light pen trapping. BASIC does not remember if the light pen was activated when trapping is turned on again.
You must also use PEN ON before executing the PEN function.
See ON PEN GOSUB for more information about light pen trapping.
260
Chapter 10 I BASIC Ke-ywords
PLAY
PLAY string
Statement
Plays the musical notes specified by string. PLAY supports 3 separate strings t o allow independent control of each of 3 voices.
String is a string expression consisting of
1
or more single-char- acter music commands. String must be enclosed in quotation marks.
The single character music commands are:
A - G
plays notes
A
through G of one musical scale.
You may include a n optional number sign (#) or plus sign ( + ) t o indicate a sharp note or a minus sign (-) t o indicate a flat note. You may only specify sharp or flat notes that correspond to the black keys on a piano. The letters and
A,
C, D, F,
G may be followed by a plus because they are followed by black keys on a piano. The letters
A,
B, D, E, and G may be followed by minus be- cause they a r e preceded by black keys on a piano.
Ln
sets the duration of the notes that follow. n may be a value in the range
1
t o the more common lengths:
64.
Here are a few of
On
1
indicates a whole note.
2 indicates a half note.
4 indicates a quarter note.
8 indicates a n eighth note.
16
indicates a sixteenth note.
If you want t o change the duration for only
1
note, place n immediately after the note,
omit-
ting the
L16A.
L.
For example,
A16
is equivalent t o sets the current octave. There are 7 octaves, 0 through with
6.
Each octave starts with C and ends
B.
Octave
3
starts with middle C. If you omit n,
BASIC
assumes Octave 4.
261
Chapter 10 I BASIC Keywords
>
<
N n
P n
Tn
MF
MB
MN
Changes the current octave octave. t o the next higher
Changes the current octave octave. t o the next lower plays a note. n may be in the range 0
to
84. In the 7 possible octaves, there are 84 notes. Instead of specifying the letter and the octave of the note, you may specify its number 1 to 84. Specifying zero means rest. rests. n may be in the range 1 t o
64
and has the same meaning as n with the L option. sets the number of quarter notes in 1 minute. n may be in the range of 32 t o 255. If you omit n,
BASIC assumes 120 quarter notes in 1 minute.
T h a t is a moderate tempo. See t h e SOUND statement for information on beats per minute for common tempos. plays as a dotted note. BASIC plays the note one- half its length longer. You may use more than one dot after each note. BASIC scales the length of time accordingly. Dots may also appear after the P option to scale the length of the rest. plays the music in the foreground, which includes sounds made by both PLAY and SOUND. This means that each subsequent note or sound does not start until the previous note or sound is fin- ished. If you omit MF and MB, BASIC assumes
MF. plays the music in the background, which in- cludes sounds made by both PLAY and SOUND.
This means that each note or sound is placed in a buffer allowing the BASIC program to continue execution while music plays in the background. maximum
A of 32 notes and/or rests can play in background at a time. sets “music normal”; each note plays 718 of the duration as set by the L option. If you omit MN and MS, BASIC assumes MN.
262
Chapter 10 I BASIC Keywords
ML
MS
X variable;
sets "music legato"; each note plays the full du- ration as set by the L option. sets "music staccato"; each note plays 3/4 duration as set by the
L
option. of the executes a substring. The X command lets you execute a second substring from a string, much like GOSUB. You can have one string execute another, which executes a third, and so on. Vari- able is a string variable in your program that contains the substring you want to execute. Vari- able may contain an
X
command to execute an- other substring. The semicolon after the variable name is required.
V n
sets the volume. n must be in the range 0 t o 15.
You must execute a SOUND ON statement to use this option. If you omit volume, BASIC uses 8.
With the 0,
N,
P, and T commands, n may also be a numeric variable in your BASIC program. Do not space between the com- mand and the n or between the command and the variable. You must include a semicolon after the variable name.
The lowest note the multi-voice sound chip can produce is Note A of Octave 0, which is 110Hz. If you try to play a lower note,
BASIC does not return an error. Instead, BASIC plays Note
A
of
Octave 0.
Ex
ample
1 0 P L A Y " C 4 F . C 8 F 8 . C 1 6 F 8 . G 1 6 A 2 F 2 "
2 0 I N P U T "CAN YOU NAME T H A T TUNE " ; A $
4 0 I F A $ = " T H E E Y E S OF T E X A S " THEN GOTO 5 0 E L S E
P R I N T "TRY A G A I N " : GOTO 1 0
5 0 P R I N T " T H A T ' S R I G H T ! "
263
Chapter 10 I BASIC Keywords
PLAY
Function
Returns the number of notes currently in the background music queue.
Number
is a dummy argument when SOUND is OFF. If you exe- cute a SOUND ON, then
number
may be one of the following:
0
1
2 returns the number of notes left to play on voice channel 0. returns the number of notes left to play on voice channel 1. returns the number of notes left t o play on voice channel 2.
If you specify a number other than 0-2, BASIC assumes 0.
The PLAY function returns a 0 when the program is running in music foreground mode.
See also SOUND
Sample
Program
1 0 P L A Y
2 0
"ME ABCDEFG"
IF P L A Y ( 0 ) = 4 GOTO 4 0
30 GOTO 20
4 0 P L A Y "GFEDCBA"
Line 10 sends notes to the music buffer. When the number notes is less than 4. Line 40 sends more notes to the buffer. of
264
Chapter 10 I BASIC Keywords
PLAY/Trap
PLAY
action
Statement
Turns on, turns off, or temporarily halts background music event trapping.
Action
may be any of the following:
ON enables play event trapping.
OFF disables play event trapping.
STOP temporarily suspends play event trapping.
Use the PLAY/Trap statement in a background music trap rou- tine with the ON PLAY GOSUB statement t o detect when the number of notes in the background music queue goes from
num- ber
t o
number
minus 1.
The PLAY number
ON statement turns on the trap. BASIC checks the of notes in the background music queue after each pro- gram line. If the number is equal t o that in the ON PLAYO GO-
SUB statement, BASIC transfers program control t o the line number specified.
The PLAY STOP statement temporarily halts background music trapping. If the number of notes equals the specified number,
BASIC does not transfer program control to the ON PLAYO GO-
SUB statement until you turn on trapping again by executing a
PLAY ON statement. BASIC remembers that the number of notes was equal and branches t o the subroutine immediately after trapping is turned on again.
The PLAY
OFF
statement turns off background music trapping.
BASIC does not remember if the number of notes in the queue is equal t o the number specified when trapping is turned on again.
See ON PLAYO GOSUB for more information about background music trapping.
265
Chapter 10 I BASIC Ke.ywords
PMAP
PMAP(coordinate,action)
Function
Returns the physical nate. or world coordinate for the specified coordi-
Coordinate is any x- or y-coordinate. If coordinate is a physical coordinate, it must be within the limits of the screen. If coordi- nate is a world coordinate, it may be any single precision floating point number.
Action is one of the following:
0 r e t u r n s the physical x-coordinate for the specified world coordinate.
1 r e t u r n s the physical y-coordinate for the specified world coordinate.
2
3 returns the world x-coordinate for the specified physi- cal coordinate. returns the world y-coordinate for the specified physi- cal coordinate.
Example
A = P M A P ( 2 0 0 , B ) returns the physical x-coordinate of the world coordinate 200 and places it in
A.
266
Chawter 10 I BASIC Kevwords
POINT/Graphics
POINT
POINT
(x,y)
(action)
Function
Returns the color number of a point on the screen or returns the current physical or world coordinates.
(x,y) specify the coordinates of the point. x is the horizontal point, and y is the vertical point. The x and y coordinates must be absolute values. If you specify a point that is out of range,
BASIC returns a -1.
See Chapter 8, “Displaying Color and Graphics,” for information on coordinates for the current screen modes.
Action is one of the following:
0 returns the current physical x-coordinate (horizontal).
1 returns the current physical y-coordinate (vertical).
2 If WINDOW is active, returns the world x-coordinate.
Otherwise, returns the physical x-coordinate.
3 If WINDOW is active, returns the world y-coordinate.
Otherwise, returns the physical y-coordinate.
When retrieving the color number, POINT returns the number as it is defined in the current palette. color
Example
1 0 SCREEN 2
20 I F P O I N T ( 1 , 1 ) < > 0 THEN P R E S E T ( 1 ,I E L S E P S E T
( 1 , I )
If point 1,l is any foreground color, PRESET changes it t o the background color.
If
the point is
the
background
color,
PSET changes it to Color
3.
1 0 SCREEN 1
20 X = P O I N T ( 0 ) : Y = P O I N T ( I )
3 0 P R I N T X , Y
BASIC prints the coordinates of the graphics cursor.
267
Chapter 10 I BASIC Keywords
POKE
POKE
memory location, data byte
Statement
Writes data byte into memory location.
Both memory location and data byte must be integers. Memory lo- cation must be in the range
-32768
t o
65535.
POKE is the complementary statement of PEEK. The argument t o PEEK is a memory location from which a byte is to be read.
PEEK and POKE are useful for storing data efficiently, loading assembly-language subroutines, and passing arguments (or re- sults) t o and from assembly-language subroutines.
See also VARPTR.
Example
P O K E & H 5 A 0 0 , CHFF writes a hexadecimal FF into memory location 5A00.
268
Chapter 10 I BASIC Keywords
POS
POS(
number)
Function
Returns the current column position of the cursor.
Number
is a dummy argument.
POS returns a number in the range 1 t o 80, indicating the cur- rent cursor-column position on the display.
Example
P R I N T T F I B ( 4 0 ) P O S ( 0 ) prints 40. The PRINT TAB statement moves the cursor t o Posi- tion 40; therefore, POS(0) returns the value 40. (However, be- cause a blank is inserted before the “4” t o accommodate the sign, the “4” is actually at Position 41.)
Sample Program
1 5 0 C L S
1 6 0 FIF = I N K E Y F
1 7 0 I F A $ =
I”’
THEN 1 6 0
1 8 0 I F P O S ( X ) > 7 0 THEN I F A $ = C H R F ( 3 2 ) THEN FIB
= C H R F ( 1 3 )
2 0 0 L P R I N T F I B ;
2 1 0 G O T 0 1 6 0
This program lets you use your printer as a typewriter (except that you cannot correct mistakes). Your computer keyboard is the typewriter keyboard. Everything you type is printed on your printer. The program also makes sure that no word is divided between two lines.
269
Chapter 10 I BASIC Keywords
...I
Statement
Prints numeric or string data on the display. You can substitute a question mark (?) in place of the word PRINT.
Data is any numeric or string constant or variable. If you omit datu, BASIC prints a blank line. If you specify more that 1 data item in the statement, separate them by commas, semicolons, or spaces.
If you use commas, the cursor automatically advances to the next tab position before printing the next item. (BASIC divides each line into print zones containing 14 positions each, a t col- umns 14, 28, 42, 56, and 70.)
If you use semicolons or spaces t o separate the data items,
PRINT prints the items without any spaces between them.
BASIC begins the next PRINT item where the last one stopped.
If no trailing punctuation is at the end of the PRINT statement, the cursor drops to the beginning of the next line.
If BASIC tries t o print a string longer than it can
fit
on the cur- rent line, it moves t o the next line and prints the string.
Single precision numbers with 7 or fewer digits that can be accu- rately represented are printed in regular format rather than ex- ponential format. For example, 1E-7 is printed as .0000001; 1E-8 is printed as 1E-08.
Double precision numbers with 16 or fewer digits that can be ac- curately represented are printed in regular format rather than e x p o n e n t i a l f o r m a t . For e x a m p l e , 1 D - 1 5 i s p r i n t e d a s
.000000000000001; 1D-16 is printed as 1D-16.
BASIC prints all numbers with a trailing blank and prints posi- tive numbers with a leading blank. Negative numbers are pre- ceded by a minus sign.
String constants must be enclosed in quotation marks.
270
Chapter 10 I BASIC Ke-ywords
Examples
P R I N T "DO"; "NOT"; " L E A V E " ; "SPACES"; "BETWEEN";
"THE S E
"
; "W URDS" displays DONOTLEAVESPACESBETWEENTHESEWORDS
Sample Program
6 0 I N P U T "ENTER T H I S YEAR"; Y
7 0 I N P U T "ENTER YOUR AGE";A
80 I N P U T "ENTER f? YEAR I N T H E F U T U R E " ; F
90 N = A
+
( F - Y )
1 0 0 PRINT "IN THE Y E A R " F - Y O U WILL B E " N " Y E A R S
OLD"
Because F and N are positive numbers, PRINT inserts a space before and after them; therefore, your display should
look
similar to this (depending on your input):
I N T H E YEAR 2 0 0 4 YOU W I L L B E 4 6 YEARS OLD
If we had separated each expression in Line 100 by a comma:
1 0 0 P R I N T " I N T H E Y E A R " , F , " Y O U W I L L B E " , N , " Y E A R S
OLD"
BASIC would move to the next tab position after printing each data item.
271
Chapter 10 I BASIC Keywords
USING
PRINT USING
... I
Statement
Prints data using a format you specified. This statement is espe- cially useful for printing report headings, accounting reports, checks, or any other documents that require a specific format.
Format consists of 1 or more field specifierb), or any alphanu- meric character. Format must be enclosed in quotation marks.
Data may be string and/or numeric value(s1. If you specify more than 1 data item in the statement, use the same separators as described in PRINT.
With PRINT USING, you may use certain characters called field specifiers, t o format the field. You may use more than 1 field spe- cifier, except as indicated.
Specifiers for String Fields:
! prints the first character in the string only
P R I N T U S I N G
' I ! " ;
"PERSONNEL"
BASIC prints P.
\spaces \ prints 2
+ n characters from the string (n is the num- ber of spaces between the slashes). If you type the backslashes without any spaces, BASIC prints 2 char- acters; with one space, BASIC prints 3 characters, and so on. If the string is longer than the field, the extra characters are ignored. If the field is longer than the string, the string is left-justified and padded with spaces on the right.
P R I N T U S I N G " \ B B B \ " ; "PERSONNEL"
&
BASIC prints PERSO prints the string without modifications.
1 0 A $ = " T A K E " : B $ = " R A C E "
2 0 P R I N T U S I N G " ! " ; A $ ;
3 0 P R I N T U S I N G "&";E$
When this program is run, BASIC prints TRACE.
272
Chapter 10 I BASIC Keywords
Specifiers for Numeric Fields:
# prints the same number of digit positions as number signs (#I. Numbers are rounded as necessary.
You may insert a decimal point at any position. BASIC always prints the digits preceding the decimal point. If there is no number, BASIC prints a zero.
If the number to be printed has fewer digits than posi- tions specified, the number is right-justified (preceded by spaces). If the number t o be printed is larger than t h e specified numeric field, a percent sign (%) is printed in front of the number.
P R I N T US I NG
' I # # .
P R I N T U S I NG
' I # # .
P R I N T US I NG
I . # # # .
1 1 1 . 2 2
+
-
BASIC prints %111.22, 0.75 and 876.57, respectively.
If the number of digits specified exceeds 24, an "Illegal function call" occurs. prints the sign of the number. The plus sign may be typed at the beginning or at the end of the format string.
P R I N T U S I N G
' I + # # . # # ' I ;
- 9 8 . 4 5 , 3 . 5 0 , 2 2 . 2 2 , - . 9
BASIC prints: -98.45
+
3.50
+
22.22 -0.90
P R I N T U S I N G
' I # # . # # +
- 9 8 . 4 5 , 3 . 5 0 , 2 2 . 2 2 , - . 9
BASIC prints: 98.45- 3.50
+
22.22
+
0.90-
(Note the use of spaces at the end of a format string t o separate printed values.) prints a negative sign
after
negative numbers (and a space after positive numbers).
P R I N T U S I N G l o # # # . # - " ;
-768.668
BASIC prints 768.7-.
273
Chapter 10 I BASIC Ke.ywords fills leading spaces with asterisks. The 2 asterisks also establish 2 more positions in the field.
P R I N T U S I N G
" * * # # # # " ;
4 4 . 0
BASIC prints ****44 prints a dollar sign immediately before the number.
This specifies 2 more digit positions, one of which is the dollar sign. You may not use exponent format with
$$.
P R I N T U S I N G
" $ $ # # . # # ' I ;
BASIC prints $112.79 fills leading spaces with asterisks and prints a dollar sign immediately before the number.
P R I N T U S I N G
" * * $ # # . # # ' I ;
BASIC prints """$8.33 prints a comma before every third digit to the left of the decimal point. The comma establishes another digit position.
P R I N T US I NG 1 2 3 4 . 5
BASIC prints 1,234.50 prints in exponential format. The 4 exponent signs are placed after t h e digit position characters. You may specify any decimal point position. You may not use $$ with exponent format.
P R I N T U S I NG
" .
BASIC prints .88893
+
06
888888
Prints next character as a literal character
P R I N T US I NG "-!
# #
.
# # - !
" ;
BASIC prints ! 1
274
Chapter 10 1 BASIC Keywords
Sample Program
4 2 0 C L S : A $ =
' I * * $ # # , # # # # # # . # #
4 3 0 I N P U T "WHAT I S YOUR F I R S T NAME"; F $
4 4 0 I N P U T "WHAT I S YOUR M I D D L E NAME"; M $
4 5 0 I N P U T "WHAT I S YOUR L A S T NAME". L $
4 6 0 I N P U T "ENTER AMOUNT P A Y A B L E " ; b
4 7 0 C L S : P R I N T " P A Y T O T H E ORDER OF
' I ;
4 8 0 P R I N T U S I N G " ! ! ! !
' I ;
F S ;
' I . " ;
M B ;
"
0
4 9 0 P R I N T L F
5 0 0 P R I N T : P R I N T U S I N G A $ ; P
In Line 480, each ! picks up the first character of one of the fol- lowing strings (F$, ".", M$, and
"." again). Notice the 2 spaces in
"!!b!!b".
These 2 spaces insert the appropriate spaces after the initials of the name (see below). Also notice the use of the vari- ables A$ for format and use
P
for item list in Line 500. Any serious of the PRINT USING statement would probably require the use of variables rather than constants, a t least for data items.
(We have used constants in our examples for the sake of better illustration.)
When the program above is run, the display shows:
WHAT I S YOUR F I R S T N A M E ? J O H N
WHAT I S YOUR M I D D L E NAME? P A U L
WHAT I S YOUR L A S T N A M E ? J O N E S
E N T E R AMOUNT P A Y A B L E ? 1 2 3 4 5 . 6
P A Y T O T H E ORDER OF J . P . J O N E S
* * * * * $ 1 2 , 3 4 5 . 6 0 D O L L A R S
275
Chapter
10
I BASIC Keywords
PRINT#
Statement
PRINT#
buffr,[USING format] data[,data,
...I
Writes data items t o a sequential disk file.
Buffer
is the number assigned t o the file when you opened it.
When you first open a file for sequential output, BASIC sets a pointer to the beginning of the file-that is where PRINT# starts writing the data items. At the end of each PRINT# opera- tion, the pointer advances so that data items are written in sequence.
A PRINT# statement creates a disk image similar sure to the image a PRINT to the display creates on the screen. For this reason, be to delimit the data so that it will be input correctly from the disk.
PRINT# does not compress the data before writing it t o disk. It writes a n ASCII-coded image of the data.
When you include the USING option, data is written in the format you specify. See PRINT USING. t o the disk
Examples
I f A = 1 2 3 . 4 5
PRINT# l , A writes this 9-byte character sequence to the file as: h l 1 2 3 . 4 5 h l c a r r i a g e r e t u r n
The punctuation in the PRINT list is very important. Unquoted commas and semicolons have the same effect a s they do in regu- lar PRINT statements to the display. For example:
A = 2 3 0 0
B = 1 . 3 0 3
PRINT# 1 , A , B writes the data on disk as: hl 2 3 0 0 hlhlhlbhlbhlhlhlhl 1 . 3 0 3 M c a r r i a g e r e t u r n
276
Chapter 10 I BASIC Keywords
The comma tells BASIC t o tab between A and B, which creates
10 extra spaces in the file. Generally you do not want t o use up storage space this way, so you use semicolons instead of commas.
P R I N T # 1 , A ;
”
,
This time BASIC writes the data as:
1 2 3 . 4 5 , l . 3 0 3
An INPUT# statement reads this as 2 separate fields.
If s t r i n g variables contain commas, semicolons, or leading blanks, enclose them in quotation marks. For example:
A $ = CAMERA, A U T O M A T I C
E$ = 1 0 2 3 8 2
P R I N T # 1 , A $ ; E$ writes the data as:
C A M E R A M M M ~ M M M M ~ ~ A U T O M A T I C l 8 2 3 8 2
An INPUT# statement reads this as 2 separate fields
A $ = CAMERA
E $
= A U T O M A T I C 1 8 2 3 8 2
To separate these 2 strings properly in the file, write quotation marks using the hexadecimal representation CHR$(34). For example :
P R I N T # 1 , C H R $ ( 3 4 ) ; A $ ; C H R $ ( 3 4 ) ; E $ ; C H R s ( 3 4 )
BASIC writes the following image t o the file:
“CAMERA, A U T O M A T I C ” 1 0 2 3 8 2 ”
T h e s t a t e m e n t I N P U T # 1, A $ , B$ r e a d s “ C A M -
ERA,AUTOMATIC” into A$ and “102382” into B$.
277
Chapter 10 I BASIC Ke,ywords
You c a n w r i t e files i n a carefully controlled format using
PRINT# USING. You also can use this option to control how many characters of a value are written to disk.
For example, suppose
A$
= “LUDWIG’,
B$
=
“VAN”, and
C $
=
“BEETHOVEN’. Then the statement:
P R I N T # 1 , U S I N G ” ! . ! . \ M M \ “ ; A $ ; B $ ; C $ writes the data in nickname form:
L
.
V . B E E T
(In this case, we did not want to add any explicit delimiters.) See
PRINT USING for more information on the USING option.
278
Chapter 10 I BASIC Ke.ywords
PSET/PRESET/Graphics
PSET [STEP] (x,y)[,colorl
PRESET [STEP] (x,y)[,colorl
Statement
Draws a point on the display.
The STEP option tells BASIC that the
(XJ) coordinates are rela- tive to the last point referenced.
(XJ) specify the coordinates in which to draw the point. X is the horizontal coordinate and y is the vertical coordinate.
Color specifies the color of the point.
See Chapter 8, “Displaying Color and Graphics” for information on coordinates and colors.
The only difference between the PSET and PRESET statements is the default values for color. If you use PSET, color defaults to the foreground color. If you use PRESET, color defaults to the background color.
Note:
BASIC does not print and does not issue a n er- ror message for points the coordinate values of which are beyond the edge of the screen. However, values outside the integer range (-32768 to 32767) cause a n overflow error.
Sample
Program
5 SCREEN 3
1 0 FOR 1 = 0 TO 1 0 0
2 0 P S E T ( 1 , I )
3 0 N E X T I’draw a d i a g o n a l l i n e t o ( 1 0 0 , 1 0 0 )
4 0 FOR I = 1 0 0 TO 0 S T E P -1
5 0 P R E S E T (1,1),0
6 0 N E X T I
7 0 ‘ c l e a r t h e l i n e b y s e t t i n g e a c h p i x e l t o 0
8 0 SCREEN 0
Lines 10 to 30 draw a diagonal line on the screen from the home position to Position 100,100. Lines 40 to 60 erase the line by drawing another line a t the same position in the background color.
279
Chapter
10 I BASIC
Keywords
PUT
PUT
[#lbuffr[,recordl
Statement
Puts a
record
in a direct access disk file.
Buffer
is the number assigned t o the file when you opened it.
The number sign (#) is optional. It is provided for compatibility with other BASICs.
Record
is the record number you want t o write to the file. It is an integer in the range
1
to
16,777,215.
If you omit
record,
the current record number is used.
If
record
is higher than the end-of-file record number, then
rec- ord
becomes the new end-of-file record number.
The first time you use PUT after opening a file you must specify the
record.
The first time you access a file via a particular buffer the next record is set equal t o one greater than the last record accessed.
See Chapter
7,
“Disk Files,” for programming information.
Examples
P U T 1 writes the next record from Buffer 1 t o a direct access file.
PUT 1 , 2 5 writes Record
25
from Buffer 1 to a direct access file.
280
Chapter 10 I BASIC Keywords
PUT/Communications
PUT
[ # 1
b u f f r
,
ber
Statement
Transfers data from the communications buffer to the communi- cations line.
Buffer is the number assigned t o the file when you opened it.
The number sign (#) is optional. It is provided for compatibility with other BASICs.
Number is the number of bytes to transfer. It cannot exceed the value you used in the LEN option in the OPEN COM statement.
Note:
Because of the low performance associated with telephone line communications, we recommend that you
not
u s e GET a n d PUT s t a t e m e n t s i n such applications.
Example
PUT 2 , 8 0 transfers 80 bytes from communications buffer (Buffer 2) to the communications line.
Sample Program
1 0 OPEN "COM1
: I '
A S 1
2 0 F I E L D 1 , 8 A S A $
3 0 OPEN " R " , 3 , " d a t a
. f i 1
4 0 F I E L D 3 , 8 A S N S
"
,8
5 0 FOR I = 1 TO 7
6 0 GET 3 , I
7 0 L S E T A $ = N $
8 0 PUT 1 ,8
9 0 N E X T I
1 0 0 C L O S E
This program moves the data from the data.fiZ file buffer t o the communications buffer. Line 80 sends the data in the communi- cations buffer to the communications line.
281
Chapter
10 I BASIC
Keywords
PUT/Graphics Statement
Transfers a n image stored in a n array onto the screen.
You get t h e GET/Graphics and PUTiGraphics statements to- gether for animation and high-speed object motion in Screen
Modes 1, 2,
3,
or 4. The GET/Graphics statement transfers the screen image described by specified points of the rectangle in the array. The PUT/Graphics statement transfers the image from the array to the display. x is the horizontal coordinate and y is the vertical coordinate.
The x and y coordinates specify the coordinate of the upper left corner of the image. If you omit x and y , BASIC begins the im- age at the last point referenced on the screen. See Chapter 8,
“Displaying Color and Graphics” for information on coordinates for the current screen mode.
BASIC returns a n “Illegal function call” error if the image is too large to fit on the current viewport.
Array
is the array variable name that holds the image.
Action
sets the type of interaction between the transferred image and the image already on the screen.
Action
may be PSET, PRE-
SET,
AND, OR, or XOR. If you omit action, BASIC assumes
XOR. The following describes each type of action:
PSET transfers the data to the screen exactly as it was stored in the array.
282
Chapter 10 I BASIC Keywords
PRESET produces a n opposite image on the screen. With
2
color sets, white becomes black on the screen. With
4
and
16
color sets, the color value becomes the numeric opposite on the screen. This table shows the color displayed for each possible value:
4
color
Array
Value
Screen
Color
Array
Value
16
color
Screen
Color
0
1
2
3
3
2
1
0
0
1
2
3
4
8
9
10
11
12
5
6
7
13
14
15
10
9
8
7
6
5
4
3
15
14
13
12
11
2
1
0
AND transfers the image over existing image. The result is a logical AND of the array and the image on the screen.
OR superimposes a n image onto an existing image. The re- sult is a logical
OR
of the array and the image on the screen.
XOR inverts the points on the screen where a point exists in the array image. When a n image is PUT against a com- plex background twice, the background is restored
un-
changed. This lets you move an object around the screen without obliterating the background.
283
Chapter
10
I BASIC Keywords
Animation
To perform object animation, follow these steps:
1. Put the object on the screen using XOR.
2. Calculate the next position of the object.
3.
Put the object on the screen a second time at the previous lo- cation to remove the previous image.
4. Repeat Step 1, putting the object at the next location.
If you do movement this way, the background is not changed. You can reduce flicker by minimizing the time between Steps 4
1 and by ensuring enough time delay between Steps 1 and 3. If you are animating more than 1 object, process every object at the same time, 1 step a t a time.
If preserving the background is not important, you can perform animation using the PSET action rather than the XOR action.
Leave a border around the image as large or larger than the maximum distance the object moves. When you move an object, this border effectively erases any points. This method may be faster than the XOR method described before, because only one
PUT is required t o move an object.
Sample Program
See the GET/Graphics statement.
284
Chapter 10 I BASIC Ke.ywords
RANDOMIZE
RANDOMIZE
[number]
Function
Reseeds the random number generator.
Number may be a n integer, or single- or double precision num- ber. If you omit number, BASIC suspends program execution and prompts you for a number before executing RANDOMIZE:
R a n d o m N u m b e r S e e d ( - 3 2 7 6 8 t o 32767)7
If the random number generator is not reseeded, the RND func- tion returns the same sequence of numbers each time it is exe- cuted. To change the sequence of random numbers every time the RND function is executed, place a RANDOMIZE statement before the RND function.
You can use the TIMER function to ensure that the random number generator is reseeded with a different value each time
BASIC executes the RANDOMIZE function. For example, the statement:
R A N D O M I Z E T I M E R uses the value returned by TIMER as the seed. TIMER returns the number of seconds that have elapsed since midnight or the last system reset. Because the seconds are constantly changing, number has a different value each time BASIC executes this st a t ement .
Sample Program
1 0 C L S
2 0 R A N D O M I Z E T I M E R
3 0 I N P U T " P I C K A N U M B E R B E T W E E N 1 A N D 1 0 0 " ; A
4 0 B = I N T < R N D * 1 0 0 )
5 0 I F A = B T H E N 8 0
6 0 P R I N T "You l o s e , t h e a n s w e r is"B;"--try i n
.
"
7 0 G O T 0 2 0
8 0 P R I N T "You p i c k e d t h e r i g h t n u m b e r - - y o u win."
285
Chawter 10 I BASIC Keywords
READ
READ
variable[,uariable,..
.I
Statement
Reads values from a DATA statement and assigns them t o variables.
BASIC assigns values from the DATA statement on a one-to-one basis. The first time READ is executed, the first value in the first DATA statement is assigned to the first variable; the second time, the second value is assigned to the second variable, and so on.
A single READ may access 1 or more DATA statements, or sev- eral READS may access the same DATA statement. If a program contains multiple DATA statements, BASIC reads them in the or- der they appear.
The values read must agree with the variable types specified in a list of variables; otherwise, a “Syntax error” occurs.
If the number of variables in the READ statement exceeds the number of elements in the DATA statement(s1, BASIC returns an
“Out of DATA” error message. If the number of variables speci- fied is less than the number of elements in the DATA state- ment(s), the next READ statements begin reading data at the first unread element.
To reread DATA statements from the start, use the RESTORE statement.
Example
READ T reads a numeric value from a DATA statement and assigns it t o variable T.
286
Chawter 10 I BASIC Kevwords
Sample Program
This program illustrates a common application for the READ and DATA statements.
4 0 P R I N T "NAME", "AGE"
5 0 READ N $
6 0 I F N $ = " E N D " THEN P R I N T "END OF L I S T " : END
7 0 READ AGE
8 0 I F A G E < 1 8 THEN P R I N T N $ , AGE
9 0 G O T 0 5 0
1 0 0 DATFI " S M I T H , JOHN", 3 0 , "ANDERS, T . M . " 2 0
1 1 0 D A T A " J O N E S , B I L L " , 1 5 , "DOE, S A L L Y " , ;1
1 2 0 D A T A " C O L L I N S , W . P . " , 1 7 , "END"
287
Chapter 10 I BASIC Kevwords
Statement
REM
Inserts a remark line in a program.
REM instructs the computer t o ignore the rest of the program line, which lets you insert remarks in your program for docu- mentation. Thus, when you look at a listing of your program, you can quickly interpret it.
If
REM is used in a multistatement program line, it must be the last statement in the line.
You may use an apostrophe ('1 as an abbreviation for REM.
Sample Program
1 1 0 D I M V < 2 0 )
1 2 0 REM C A L C U L A T E AVERAGE V E L O C I T Y
1 3 0 FOR 1 = 1 TO 2 0
1 4 0 SUM=SUM
+
V C I )
1 5 0 N E X T I
OR
1 1 0 D I M V ( 2 0 )
1 2 0 F O R 1 = 1 TO 2 0
1 3 0 S U M = S U M
+
V < I )
1 4 0 N E X T I
' C A L C U L A T E AVERAGE V E L O C I T Y
288
ChaDter 10 I BASIC Kevwords
RENUM
RE NUM
[new
Line1
[
[ I
inel
[
increment1
I
Statement
Renumbers the program currently in memory. You can renumber the entire program or renumber from a specific line t o the end.
Line is the line in the program where BASIC starts renumber- ing. If you omit line, it renumbers the entire program.
New line is the new line number assigned t o line. If you omit new line, BASIC starts numbering at Line 10.
Increment tells BASIC how to number the successive line. If you omit increment, it increments by 10.
RENUM also changes all line number references appearing after
GOTO, GOSUB, THEN, ON/GOTO, ONiGOSUB, ON ERROR
GOTO, RESUME, and ERL.
You cannot use RENUM t o change the order of program lines.
For example, if a program has lines numbered 10, 20, and 30, the command RENUM 15,30 is illegal, since this would place
Line 30 before Line 20.
Also RENUM cannot create line numbers greater than 65529. If you attempt to do this, BASIC returns an “Illegal function call” error and leaves the program unchanged.
If BASIC finds an undefined line number within the program, it prints a warning message, “Undefined line xxxx in yyyy,” where xxxx is the undefined line number and yyyy is the line where it appears. RENUM renumbers the program despite this warning message. It does not change the incorrect line number reference, but it does renumber yyyy.
289
Chapter 10 I BASIC Keywords
Examples
RENUM renumbers the entire program, using an increment of new number of the first line is 10.
10. The
RENUM 6 0 0 , 5 0 0 0 , 1 0 0 renumbers from Line 5000 renumbered line becomes to the end of the program. The first of 100 is used between subsequent lines.
RENUM 1 0 0 , , 1 0 0 renumbers the entire program, starting with a new line number retained even though the middle argument is not used.
290
Chapter 10 I BASIC Ke-ywords
RESET
RESET
Statement
Closes all open files on all drives.
If a disk contains any open files, RESET writes all blocks in memory t o disk.
RESET ensures that all files on all diskettes are closed before you remove them from the drives. RESET is the same as a
CLOSE statement for each open file.
291
Chapter 10 I BASIC Keywords
RESTORE
RESTORE [Line]
Statement
Restores a program’s access to previously read DATA statements.
Line is a line number that contains a DATA statement. If you specify line, the next READ statement accesses the first item in the specified DATA statement. If you omit the first DATA statement in the program. line, BASIC resets to
This lets your program reuse the same DATA lines.
Sample Program
1 6 0 RECID X $
1 7 0 RESTORE
1 8 0 READ Y $
1 9 0 P R I N T X S , Y $
2 0 0 DCITFI F I R S T I T E M , SECOND I T E M
When you run this program, BASIC prints:
F I R S T I T E M F I R S T I T E M
Because of the RESTORE statement in Line 170, the second
READ statement starts over with the first DATA item.
292
Chapter 10 I BASIC Ke.ywords
RESUME
RESUME
[line]
RESUME
NEXT
Statement
Resumes program execution after an error-handling routine.
RESUME without an argument and RESUME 0 both cause the computer t o return t o the statement in which the error has occurred.
RESUME line line number. causes the computer to branch t o the specified
RESUME NEXT causes the computer t o branch t o the state- ment following the point at which the error has occurred.
Examples
RESUME
If an error has occurred, this line transfers program control the statement in which it has occurred. t o
RESUME 1 0
If an error has occurred, transfers control to Line 10.
Sample Program
1 0 ON ERROR G O T 0 9 0 0
9 0 0 I F < E R R = 2 3 0 ) A N D ( E R L = 9 0 ) T H E N P R I N T "TRY
A G A I N " : RESUME 8 0
293
Chapter 10 I BASIC Keywords
RETURN
RETURN
[Line]
Statement
Returns control to the line immediately following the most re- cently executed GOSUB.
Line tells BASIC to return to a specific line in the program. If you omit line, BASIC goes to the line immediately following the
GOSUB.
Use caution when specifying a line number with RETURN. Any other GOSUB, WHILE, or FOR statement remains active while a GOSUB subroutine is executing. If BASIC returns to a line number that is outside these loops, a n error occurs because the loops were left incomplete.
If the program encounters a RETURN statement without execu- tion of a matching GOSUB, a n error occurs.
Example
RETURN 4 0 returns from the subroutine to Line 40 in the program.
Sample Program
3 3 0 P R I N T " T H I S PROGRAM F I N D S THE AREA OF A
C I R C L E "
3 4 0 I N P U T " T Y P E I N FI V A L U E FOR THE R A D I U S " ; R
3 5 0 GOSUB 3 7 0
3 6 0 P R I N T "AREA I S " ; A : END
3 7 0 A = 3 . 1 4 R
*
R
3 8 0 RETURN
294
Chapter 10 I BASIC Keywords
RIGHT$
RIGHT$(string,nurnber)
Function
Returns the specified number of characters from the far right portion of string.
Number is an integer in the range 1 t o 255.
If number is equal to or greater than the length returns the entire string. of string, BASIC
Examples
P R I N T R I G H T $ ( " W A T E R M E L O N " , 5 ) prints MELON.
P R I N T R I G H T $ ( " M I L K Y W A Y " , 2 5 ) prints MILKY WAY.
Sample Program
8 5 0 RESTORE : ON ERROR GOTO 8 8 0
8 6 0 R E A D COMPANY$
8 7 0 P R I N T R I G H T $ C C O M P A N Y $ , 2 1 , : GOTO 860
8 8 0 E N D
8 9 0 D A T A "BECHMAN LUMBER COMPANY, S E A T T L E , W A "
9 0 0 D A T A " E D NORTON SEWER S E R V I C E , B R O O K L Y N , NY"
9 1 0 D A T A "HAMMON M A N U F A C T U R I N G COMPANY
~
HAMMOND,
I N "
This program prints the name of the state in which each com- pany is located.
295
Chapter 10 I BASIC Keywords
RMDIR
RMDIR
dirpath
Statement
Removes (deletes) the directory specified by dirpath.
Dirpath is a standard directory specification as described in
Chapter 1. If you omit the drive identifier, the directory is de- leted from bol ( \ the current drive. If you omit the root directory sym-
1, the directory is deleted from the current directory.
The directory being deleted must be empty except for the "." and
".." symbols. Use the MS-DOS COPY command to move those files you want to save; then use KILL to remove all files from the directory.
Examples
R M D I R " A : \ A C C T S \ P A Y A B L E " removes the directory
Drive
A.
PAYABLE from the ACCTS directory on
R M D I R " \ A D D R E S S " removes the directory ADDRESS from the root directory on the current drive.
RMDI R "NAMES" removes the directory current drive.
NAMES from the current directory on the
296
Chapter 10 I BASIC Keywords
RND
RND[(nurnber)]
Function
Returns a random number in the range 0 and 1.
BASIC uses the current seed when generating a random number and produces the same sequence of random numbers each time the program is run unless you reseed the random number gener- ator. Use the RANDOMIZE statement t o reseed the random number generator
If number is negative, RND starts the sequence of random num- bers at the beginning. If number is 0, RND repeats the last number generated. If you omit number or specify a positive value, RND returns the next number in the sequence.
Example
P R I N T RND(1 1 prints the next decimal fraction in the sequence.
Sample Program
1 0 F O R I = 1 TO 5
20 P R I N T I N T ( R N D * l 0 0 ) ;
30 N E X T I
This program produces
5
random integers. Line 20 converts the decimal fraction returned by RND to a real number and trun- cates the real number t o an integer.
297
Chapter
10
I BASIC Keywords
RSET
RSET
field name
=
data
Statement
Sets data in a direct access buffer field name in preparation for a
PUT statement.
Field name is a string variable defined in a FIELD statement.
This statement is similar t o LSET. The difference
RSET, data is right-justified in the buffer. is that with
See LSET for details.
298
Chapter 10 I BASIC Ke.ywords
RUN
RUN [line]
RUN pathmme[,R]
Statement
Executes a program.
Line is the program line where BASIC begins execution. If you omit line, BASIC executes the program from the beginning.
Pathnume specifies the disk file for BASIC t o load into memory and execute.
If you specify the
R
option, BASIC does not close the open files before loading the new program into memory. If you omit the R option, BASIC closes all open files before loading the program.
RUN automatically clears all variables.
R U N starts execution a t the beginning of the program.
R U N 1 0 0 starts execution a t Line 100.
R U N
" r o g r a m
. a" loads and executes pr0gram.a.
R U N " \ W O R D \ e d i t d a t a " , R loads and executes editdata from the WORD directory without closing any open files.
299
ChaDter 10 I BASIC Kevwords
SAVE
SAVE
pathname
[,AI
SAVE
pathname [,PI
Statement
Saves a program on disk with the specified name.
Pathname is a standard file specification as described in Chapter
1. When you save a file to disk, you must specify the filename. If the file already exists on disk, its contents are lost when the file is re-created.
The A option tells BASIC to save the program in ASCII format.
If you omit the A option, BASIC saves the file in a compressed format.
The compressed format takes less disk space than ASCII format.
Also BASIC can save and load in compressed format faster than in ASCII format. BASIC programs are stored in RAM using compressed format.
Use the ASCII format if you plan to use the MERGE command to merge the program with another. Also, data programs that be read by other programs usually must be in ASCII.
When using the ASCII option, be sure your program has no embedded line feeds; otherwise, the computer will not be able t o read it properly. Embedded line feeds are produced by typing
For compressed-format programs, a useful convention is the ex- tension .bas. For ASCII-format programs, use .txt.
The P option protects the file by saving it in a n encoded binary format. When a protected file is later run (or loaded), any at- tempt to list or edit it fails. The only operations that you can perform on a protected file are RUN, LOAD, and CHAIN.
300
\
Chapter 10 I BASIC Ke.ywords
Examples
S A V E " A : f i le1 . b a s " saves the resident program in compressed format as filel.bas.
The file is placed on Drive
A:
in the current directory.
S A V E " \ E D U C \ m a t h p a k . t x t " , A saves the resident program in
ASCII
form, using the name math-
pak.txt,
on the current drive in the directory EDUC.
301
Chapter 10 I BASIC Ke,ywords
SCREEN
SCREEN (row,
num
ber])
Function
Returns the ASCII code for the character at the specified row and column.
Row
is an integer in the range 1 t o 25.
Column is an integer in the range 1 t o 80. Column must be in the range for the current screen mode.
Number is applicable only for text mode. If you specify a non-zero number, SCREEN returns the color attribute, rather than the
ASCII code, to of the character. The attribute is in the range of 0
255, and can be translated as follows: attribute MOD 16
= foreground color (((attribute
- foreground)/
16)MOD 128)
= background color
(number>l27) is true (-1) if the character is blinking and false
( 0 ) if it is not.
In the graphics modes, if the location does not contain a stan- dard ASCII character, BASIC returns a value of zero.
Sample Program
1 0
20
L O C Q T E 2 0 , 2 0
P R I N T “ R O B B I E ”
30 A = S C R E E N ( 2 0 , 2 0 ) : B = S C R E E N ( 2 0 , 2 l )
4 0 P R I N T A,B
Line 10 positions the cursor to Row 20, Column 20. Line 20 prints the message at the current cursor position. Line 30 stores the ASCII code for “R’ in the variable A and the ASCII code for
40 prints:
8 2 79
302
Chapter 10 I BASIC Ke.ywords
SCREEN Statement
[,erasell
Sets the screen attributes to be used by all other graphics state- ments (CIRCLE, LINE, DRAW, POINT, PSET, PRESET, PAL-
ETTE, and PALETTE USING).
Mode is a n integer in the range 0 to 6 that sets the valid coordi- nates and the number of colors you can use. Screen Mode de- scriptions a r e given i n C h a p t e r
Graphics .”
8, “Displaying Color and
When you change from one mode to another, BASIC stores the new screen mode, erases the video display, and sets the fore- ground color
to white
and the background and border colors
to
black.
Burst enables or disables color. In Screen Mode 0 (text mode), set burst to 0 to disable color or 1 to enable color. In medium resolu- tion (Screen Modes 1 and 4), burst to 0 t o enable color or 1 to disable color. Burst has no effect in high and low resolution
(Screen Modes 3,
5,
and 6) where color is always enabled or in
Screen Mode 2, which is black and white.
Active page is a n integer that selects the video page to which
BASIC will write. All output statements to the screen go t o the selected active page. The range depends on the screen mode and the amount of video memory available. If you omit
BASIC assumes the current active page. active page,
Active page is initially set to Page 0.
Display page is a n integer that selects the video page for BASIC to display. The range is the same as active page. If you omit dis- play page, BASIC uses the same page as active page. BASIC au- tomatically sets display page to active page if the program halts because of a n END or STOP statement or because of a n error.
All video pages share one cursor. Therefore, when switching ac- tive pages, you should save the cursor position with the POS and
CSRLIN statements. Then when you return to a n active page, you can restore the cursor with the LOCATE statement.
303
Chapter 10 I BASIC Ke.ywords
Erase is an integer in the range 0 to 2 that tells BASIC how much video memory to erase. Erase can be one of the following:
0 Do not erase video memory, even if the screen mode changes.
1
2
Erase the union of the new page and old page if mode or burst change.
Erase all video memory if mode or burst changes.
If you omit erase, BASIC assumes 1.
If you omit any parameters (except the previous values. display page), BASIC uses
For more information on the graphics statements and video pages, see CLEAR and Chapter 8, “Displaying Color and
Graphics.”
Examples
1 0 SCREEN 0 , 0 selects text mode with color off.
6 0 SCREEN 2 changes t o high resolution, 2-color, graphics mode.
304
Chapter 10 I BASIC Keywords
SGN
S GN( num
her)
Function
Determines number's sign.
If
If
If number is a negative number, SGN returns -1. number is a positive number, SGN returns 1. number is zero, SGN returns 0.
Examples
Y = SGN<A * E) determines the sign propriate number of the expression
A
(-1,0,1) t o Y.
* B, and passes the ap-
Sample Program
6 1 0 I N P U T "ENTER A NUMBER"; X
6 2 0 ON SGNCX)
+
2 GOT0 6 3 0 , 6 4 0 , 6 5 0
6 3 0 P R I N T " N E G A T I V E " : END
6 4 0 PRINT " Z E R O - : END
6 5 0 P R I N T " P O S I T I V E " : END
305
Chapter 10 I BASIC Keywords
SHELL
SHELL
[command]
Advanced Statement
Loads and executes another program (.EXE or .COW or a n in- ternal command as a child process to the original program.
After the child process ends, control returns to the BASIC pro- gram a t the statement following the SHELL statement.
Command is a string expression containing the name of the pro- gram you want to run. You may also specify command argu- ments on the command line. Use a space to separate arguments from the program name. If you omit command, SHELL transfers control t o COMMAND. You can now execute MS-DOS commands as allowed by COMMAND. To return to BASIC, use the MS-
DOS EXIT command.
SHELL sends the command information to COMMAND.COM, the MS-DOS command processor. If you omit the extension in the program name, COMMAND looks for the program with a
.COM extension, then with a n .EXE extension and finally with a
.BAT
extension. If COMMAND still cannot find the program, it issues a “File not found” error to SHELL.
Note:
Do not specify BASIC as the command string of
SHELL. If you do, BASIC might not function properly.
For more information on child processes and COMMAND.COM, see the MS-DOS Reference and the Programmer’s Reference man- uals for your computer. They are available through your Radio
Shack Computer Store.
306
Chapter 10 I BASIC Keywords
Examples
S H E L L transfers control to COMMAND.COM. You can execute MS-DOS commands such as:
D I R
T I N E and then type EXIT to return to BASIC.
The following command uses redirection of input and output and the MS-DOS SORT command.
S H E L L " S 0 R T < d a t a . > d a t a . o u t " sorts the text from data.in and writes it to data.out.
307
Chapter 10 I BASIC Ke.ywords
SIN
SIN(number)
Function
Returns the sine of
number
SIN returns the angle (in radians) whose cosine is
number.
Number
must be in radians. To obtain the sine of
number
when
number
is in degrees, use SIN(number
*
PI/180), where PI equals 3.141593.
BASIC always returns the result as a single precision number unless you set the /D switch when starting up BASIC.
Examples
P R I N T S I N C 7 . 9 6 ) prints .9943854.
Sample Program
6 6 0 I N P U T "FINGLE I N DEGREES"; A
6 7 0 P R I N T " S I N E IS"; S I N < A .01745329)
308
Chapter 10 I BASIC Keywords
SOUND
SOUND
frequency,
duration[,
SOUND ON
SOUND
OFF
Statement
Generates a sound with the frequency and duration specified.
When a SOUND statement is producing sound, the program con- tinues t o execute. See the PLAY statement for more information about executing program lines during SOUND. frequency specifies the desired tone in Hertz, and is an integer in the range 1 to 32767. The lowest frequency the multi-voice sound chip can produce is 110 Hz. Any value below 110 Hz sounds at
110 Hz. The frequency 32767 is treated as the silence frequency.
To create periods of silence, use SOUND 32767, duration.
Possible frequencies are given later.
Duration is an integer in the range 1 to 65535, specifying the duration in clock ticks. Clock ticks occur 18.2 times per second.
The shortest sound is 1, and the longest is 65535. If
0, BASIC turns off duration is any currently running SOUND statement.
Otherwise, BASIC completes the first SOUND statement before executing the next one.
Volume is an integer in the range 0 to 15, where 0 is the lowest volume and 15 is the highest volume. If you omit uolurne, BASIC uses 8. You must execute a SOUND ON statement t o use uolurne.
Voice is an integer in the range 0 t o 2. If you omit voice, BASIC uses 0. You must execute a SOUND ON statement to use
voice.
You can set each voice to a different tone to produce a 3-note chord.
SOUND ON
enables the external speaker that supports multi- voice sounds using the PLAY or SOUND statements.
SOUND OFF disables the external speaker.
You can use SOUND with BEEP t o direct sound to either the computer’s speaker or external speaker. See also BEEP for the results of the different combinations of BEEP and SOUND.
309
Chapter 10 i BASIC Keywords
This statement can be especially useful in educational applica- tions. For example, you can have the computer respond with a sound if a user has answered a program’s prompt incorrectly (or vice versa).
You can use the SOUND
or PLAY
statements
to
generate musi- cal notes from your computer. This chart shows the frequency you specify t o generate the notes in the octave above middle C.
Middle C is the first note in the chart.
Note Frequency
C
D
E
F
G
A
B
C
523.25
587.33
659.26
698.46
783.99
880.00
987.77
1046.50
To
generate notes in the octave below middle C, find the fre- quency of the note’s letter in the chart and divide that number by 2. For example, the note A in the octave below middle C has a frequency of 440.00
To generate notes in the octave above middle C, find the fre- quency of the note’s letter in the chart and multiply that number by 2. For example, the note
A
in the octave above middle C has a frequency of 1760.00
There are 1092 clock ticks per minute. To determine the number of clock ticks for 1 beat, divide the beats per minute into 1092.
The chart below shows the number of clock ticks for some typical tempos.
Tempo
Beats per minute
Ticks per minutes
Largo
Larghetto
Adagio
Andante
Moderato
Allegro
Presto
40- 60
60- 66
66- 76
76-108
108-120
120-168
168-208
27.3 -18.2
18.2 -16.55
16.55-14.37
14.37-10.11
10.11- 9.1
9.1
6.5
- 6.5
- 5.25
310
Chapter 10 I BASIC Keywords
Sample Program
5 SOUND ON
1 0 I N P U T " I N HONOR OF WHOM WAS T H E C O N T I N E N T OF
A M E R I C A NAMED"; A $
2 0 I F A $ = " A M E R I G O V E S P U C C I " T H E N SOUND 5 0 0 , 5 0
E L S E G O T 0 4 0
3 0 P R I N T " T H A T ' S R I G H T ! " : E N D
4 0 SOUND 37,2 : P R I N T " T H E CORRECT ANSWER I S
AMER I GO VESPUCC I
"
311
Chapter 10 I BASIC Keywords
SPACE$
SPACE
$(
num
ber)
Function
Returns a string of
number
spaces.
Number
must be in the range 0 to
255.
Example
PR I
" Q U A N T I T Y "
I ON" SPFICE $ ( 4 1 " T Y P E " SPACE $ ( 9 1 prints DESCRIPTION, 4 spaces, TYPE, 9 spaces, QUANTITY.
Sample Program
9 2 0 P R I N T " H e r e "
9 3 0 P R I N T S P & C E $ C 1 3 )
" i 5 "
9 4 0 P R I N T SPFICESC26) "an"
9 5 0 P R I N T S P A C E S ( 3 9 ) " e x a m p l e "
9 6 0 P R . I N T S P A C E $ < S E ) " o f "
9 7 0 P R I N T S P F I C E $ < 6 5 ) " S P A C E $ "
312
Chapter 10 I BASIC Keywords
SPC
SPC(number)
Function
Skips number spaces in a PRINT statement.
Number is in the range 0 t o 255. A semicolon is assumed to fol- low the SPC(number) command.
You may use SPC only with PRINT, LPRINT, or PRINT# .
See also SPACE$.
Example
P R I N T " H E L L O " S P C ( 1 5 ) "THERE" prints:
H E L L O THERE
313
Chapter 10 I BASIC Ke.ywords
SQR
SQR( number)
Function
Returns the square root of number.
Number must be greater than zero.
BASIC always returns the result as a single precision number unless you specified the /D switch when starting up BASIC.
Example
P R I N T S Q R ( 1 5 5 . 7 ) prints 12.47798.
Sample Program
6 8 0 I N P U T " T O T A L R E S I S T A N C E ( O H M S ) " ; R
690 I N P U T " T O T A L REACTANCE (OHMS)"; X
7 0 0 2 = S Q R ( ( R * R )
+
( X * X I )
7 1 0 P R I N T " T O T A L IMPEDANCE (OHMS) I S " 2
This program computes the total impedance for series circuits.
314
Chapter 10 I BASIC Keywords
STICK
STICK (action)
Function
Returns the coordinates of the joysticks.
Action may be one of the following:
0
1
2
3 reads all 4 coordinates, and returns the horizontal (x) coordinate for Joystick
A.
returns the vertical (y) coordinate for Joystick
A.
returns the horizontal returns the vertical (y) coordinate for Joystick B.
B.
The coordinates returned by STICK(l), STICK(2), and STICK(3) are those previously read by STICK(0).
Joystick A is the left joystick and Joystick B is the right joystick.
Sample
Program
The following program continually displays the coordinates of joystick B.
1 0 CLS
2 0 LOCATE 1 ,I
2 5 T = S T I C K < 0 )
3 0 P R I N T " B : " ;
4 0 P R I N T " B :
"
I C K
(
3 1
5 0 GOT0 2 0
315
Chapter 10 I BASIC Keywords
STOP Statement
Stops program execution.
When BASIC encounters a STOP statement, it prints the mes- sage “BREAK IN xax,” xxxx is the line number that con- tains the STOP. STOP is primarily a debugging tool. During the break in execution, you can examine variables values. or change their
Use the CONT statement if you want t o resume execution. If the program itself has been altered during the break, you cannot use CONT.
Unlike the END statement, STOP does not close files.
Sample Program
2 2 6 0 X =
2 2 7 0 STOP
R N D C l 0 )
2 2 8 0 GOT0 2 2 6 0
A random number in the range 1 to 10 is assigned to X and then program execution halts at Line 2270. You can now exam- ine the value X with PRINT X. Type CONT to start the cycle again.
316
Chapter 10 I BASIC Keywords
STR$
STR$( number)
Function
Converts number t o a string.
If number is positive, STR$ places a blank before the string. If number is negative, STR$ places a minus sign (-1 before the string.
While arithmetic operations may be performed on number, only string functions and operations may be performed on the string.
The complementary function to STR$ is
VAL.
Example
S $ = S T R $ < X ) converts the number X into a string and stores it in S$.
Sample Program
1 0 A = 1 .6 : B#
= A : C # = V A L ( S T R $ ( A ) )
C O N V E R S I O N "
30 P R I N T B # T A B ( 4 0 ) C #
317
Chapter 10 I BASIC Ke.ywords
STRIG
STRIG ON
STRIG
OFF
Stat
emen
t
Enables the STRIG function.
STRIG ON
STRIG ON lets you execute STRIG function statements to return the status of the joystick buttons. If you attempt to execute a
STRIG function before you execute a STRIG ON statement,
BASIC issues a n “Illegal function call” error.
STRIG OFF
If you execute a STRIG OFF statement, you can not execute
STRIG function. Executing a STRIG function after a STRIG
OFF statement results in an “Illegal function call” error.
When you load BASIC, the default is STRIG OFF and you cannot execute STRIG/Function statements.
You cannot place a STRIG function in a subroutine that you branch to as a result of a n ON STRIGO GOSUB statement.
BASIC does not keep track of which button was pressed after the
ON STRIG() GOSUB statement is executed. If you wish t o trap both buttons and perform a different procedure for each button, you must execute a STRIG/Trap for each button, and you must branch to different subroutines with different
SUB statements.
ON STRIGO GO-
See STRIG Function, STRIG/Trap, and ON STRIGO GOSUB for additional information on joystick trapping.
318
Chapter 10 I BASIC Keywords
STRIG
STRIG(
number)
Function
Returns the status of joystick buttons.
Number is an integer in the range 0 t o 7 t o test the status of the joystick buttons.
Variable is a numeric variable t o receive the value returned by number
.
Each number tests for a different status of the buttons and re- turns a numeric value in variable regarding the results of the test. The numbers and their functions are:
Tests t o see if Trigger A 1 has been pressed and re- leased since the last STRIG(0) function was executed.
BASIC returns a -1 if it has been pressed and a 0 if not.
Tests to see if you are currently pressing Trigger Al.
BASIC returns a -1 if you are pressing it and a 0 if not.
Tests to see if Trigger B1 has been pressed and re- leased since the last STRIG(2) function was executed.
BASIC returns a -1 if it has been pressed and a not.
0 if
Tests t o see if you are currently pressing Trigger B1.
BASIC returns a -1 if you are pressing it and a 0 if not.
Tests to see if Trigger A2 has been pressed and re- leased since the last STRIG(4) function was executed.
BASIC returns not. a -1 if it has been pressed and
a
0 if
Tests to see if you are currently pressing Trigger A2.
BASIC returns a -1 if you are pressing it and a 0 if not.
319
Chapter 10 I BASIC Keywords
6
Tests to see if Trigger B2 has been pressed and re- leased since the last STRIG(6) function was executed.
BASIC returns a -1 if it has been pressed and a 0 if not.
7 Tests to see if you are currently pressing Trigger B2.
BASIC returns a -1 if you are pressing it and a 0 if not.
Joystick A is the left joystick and Joystick B is the right joystick.
You must execute a STRIG ON statement before you can execute a STRIG function. If you attempt to execute a STRIG function before you execute a STRIG ON, BASIC issues a n “Illegal func- tion call” error. See STRIG/Trap.
You cannot place a STRIG function in a subroutine that you branch to as a result of a n ON STRIGO GOSUB statement.
BASIC does not keep track of which button was pressed after the
ON STRIG() GOSUB statement is executed. If you wish to trap both buttons and perform a different procedure for each button, you must execute a STRIGiTrap for each button, and you must branch to different subroutines with different ON STRIGO GO-
SUB statements.
Sample Program
This program tells BASIC to beep whenever the trigger on Joys- tick A is pressed.
1 0
20
S T R I G ON
I F STRIG(0) T H E N B E E P
30 G O T 0 20
320
Chapter 10 I BASIC Keywords
STRIG/Trap
STRIG(number)
ON
STRIG(number) OFF
STRIG(number) STOP
Statement
Turns on, turns off, or temporarily halts joystick trapping.
Number is a value of 0, 2, you are trapping:
4, 6 t o indicate the joystick button
0 indicates Trigger Al.
2 indicates Trigger B1.
4 indicates Trigger A2.
6
indicates Trigger B2.
Joystick A is the left joystick and Joystick B is the right joystick.
STRIGO ON
STRIGO ON enables joystick trapping with the
GOSUB statement. If you execute a STRIGO
ON
STRIGO
ON statement,
BASIC checks after every program statement to see if you pressed a joystick button. If you press a joystick button, BASIC transfers program control t o the line number specified in the ON
STRIGO GOSUB statement. See ON STRIGO GOSUB.
Note:
Do not confuse the STRIG/Trap statement with the STRIG function statement. These are 2 separate statements that perform 2 distinct functions in BASIC.
STRIGO STOP
STRIG() STOP temporarily halts joystick trapping. If you press a joystick button after a STRIGO STOP statement is executed,
BASIC does not transfer program control to the subroutine until trapping is turned on again with a STRIGO ON statement.
BASIC remembers that the joystick buttons were pressed and transfers program control t o the subroutine immediately after joystick trapping is turned on again.
321
Chapter 10 I BASIC Keywords
STRIG OFF
STRIGO
OFF
turns
GOSUB statement. off joystick trapping with the ON STRIGO
When you load
BASIC, STRIGO OFF
is the default, because joy- stick trapping slows program execution. Therefore, if you execute a STRIGO ON statement to enable joystick button trapping, we recommend that you also execute a STRIGO OFF statement when you no longer need to check for joystick button activity.
If you press a joystick button after a STRIG() OFF statement is executed, BASIC does not remember that the joystick buttons were pressed when joystick trapping is turned on again.
Example
See STRIG.
322
Chapter 10 I BASIC Keywords
STRING$
STRING$( number,character)
Function
Returns a string containing the specified number of character.
Number must be in the range 0 t o 255.
Character is a string or an ASCII code. If you use a string con- stant, you must enclose it in quotation marks. All the characters in the string have either the ASCII code, or the first letter of the string specified.
STRING$ is useful for creating graphs or tables.
Examples:
B $ = S T R I N G S C 2 5 , “ X ” ) puts a string of 25 “X”s into B$.
P R I N T S T R I N G $ ( 5 0 , 1 0 ) prints 50 blank lines on the display, because 10 is the ASCII code for a line feed.
Sample Program
1 0 4 0 C L E A R 3 0 0
1 0 5 0 I N P U T “ T Y P E
1 5 9 ” ; N 1
1 0 6 0 C L S : FOR
I N 3 N U M B E R S B E T W E E N 3 3 A N D
,
N 2 , N 3
I = 1 TO 4 : P R I N T S T R I N G $ ( 2 0 , Nl):
N E X T I
1 0 7 0 F O R J = 1 TO 2: P R I N T S T R I N G s ( 4 0 , N2): N E X T
J
1 0 8 0 P R I N T S T R I N G B ( 8 0 , N3)
This program prints corresponding to one
3
strings. Each string has the character of the ASCII codes provided.
323
Chapter 10 I BASIC Keywords
Statement
SWAP varia
blel
paria ble2
Exchanges the values of 2 variables.
You may swap variables of any type (integer, single precision, double precision, or string). However, both must be of the same type; otherwise, a "Type mismatch" error results.
Either or both variables may be elements both of of arrays. If one or the variables are non-array variables that have not been assigned values, an "Illegal function call" error results.
Example
SWAP F 1 # , F 2 # swaps the values of F1# and F2#. The contents of F2# are put into F1#, and the contents of F1# are put into F2#.
Sample Program
1 0 A $ = " O N E " : B $ = " A L L " : C $ = " F O R
"
2 0 P R I N T A $ C $ E$
30 SWAP A S , E $
4 0 P R I N T A $ C $ E $
When run, the program displays:
O N E FOR A L L
A L L FOR ONE
324
Chapter 10 I BASIC Ke.ywords
SYSTEM
SYSTEM
Statement
Returns you to the MS-DOS command level.
BASIC closes all files before returning to MS-DOS. Your resident
BASIC program is not retained in memory.
Examples
S Y S T E M returns you to MS-DOS. Your resident BASIC program is lost.
325
Chapter 10 I BASIC Keywords
TAB
TAB (number)
Function
Spaces t o position number on the display.
Number must be in the range 1 t o 255 and specifies the charac- ter position t o which t o tab. The leftmost position is 1, and the rightmost position is the set width minus 1 (WIDTH-1).
If
the current print position is already beyond space number,
TAB goes t o that position on the next line.
You cannot use TAB t o move the cursor t o the left.
You cannot use TAB more than once in a print list.
You may u s e TAB only w i t h t h e P R I N T a n d L P R I N T statements.
Sample Program
1 0 P R I N T "NAME" T A B ( 2 5 ) "AMOUNT": P R I N T
2 0 READ A $ , B $
3 0 P R I N T A $ T A B C 2 5 ) B S
4 0 D A T A " G . T . J O N E S " , " $ 2 5 . 0 0 "
When you run this program, the display shows:
NAME AMOUNT
G. T . JONES $ 2 5 . 0 0
326
Chapter 10 I BASIC Ke.ywords
TAN
TAN( num ber)
Function
Returns the tangent of number.
Return the angle (in radians) whose arc tangent is number.
Number must be in radians. To obtain the tangent of number when it is in degrees, use TAN (number
*
PI/180), where PI equals 3.141593.
BASIC always returns the result as a single precision number unless you set the /D switch when starting up BASIC.
Example
P R I N T T A N ( 7 . 9 6 ) prints -9.396959.
Sample Program
This programs asks you t o turns the tangent in radians. input an angle in degrees and re-
7 2 0 I N P U T "ANGLE I N DEGREES". ANGLE
7 3 0 T = TANCCINGLE * . 0 1 7 4 5 3 2 4 )
7 4 0 P R I N T " T A N I S " T
327
Chapter 10 I BASIC Keywords
TIME$
TIME$[
=
string]
Function
Sets or retrieves the current time.
String is a literal, enclosed in quotation marks, that sets the time by assigning its value t o TIME$. If you omit string, BASIC retrieves the current time.
BASIC uses a 24-hour clock. For example, it sets 8:15 P.M. as
20:15:00.
Setting the Time
You set the time in the following format: hh:mm:ss
The hours ( h h ) may be any number 0-23.
The minutes (mm) (ss) may be any number
0 through 59.
If you omit the minutes, minutes
and
seconds default t o zero. If you omit the seconds, seconds default t o zero.
Although you may omit leading zeros in each of the values, you must include at least 1 digit of the preceding value. For example, you may type 1:5 to set the the time to 1:05 A.M. However, :5 is invalid.
Retrieving the Time
BASIC always returns the time in the 8-character (hh:mm:ss) format, with leading zeros. You may set the time before you enter
BASIC. If you do not set the time at the MS-DOS time prompt or with the TIME$ statement, BASIC returns the length time that has elapsed since you turned on the computer. of the
328
Chapter
10
I BASIC Keywords
Examples
T I M E S = " 1 4 : 1 5 " sets the current time t o 14:15:00.
T I M E S = "3:3:3" sets the current time t o 03:03:03.
FIS = T I S assigns the current time t o the variable A$.
P R I N T T I M E S prints the current time.
329
Chapter 10 I BASIC Keywords
TIMER
TIMER
Function
Returns the number of seconds since midnight or since the last system reset.
BASIC always returns a single precision number.
You can use TIMER as the argument for the RANDOMIZE statement
DOMIZE t o reseed the random number generator. See RAN- for more information.
Example
A = T I M E R stores the number returned by TIMER into variable A.
330
Chawter 10 I BASIC Kevwords
TIME R/Trap
TIMER
action
Statement
Turns on, turns off, or temporarily halts timer event trapping.
Action
may be any of the following:
ON
OFF
STOP enables timer event trapping. disables timer event trapping. temporarily suspends timer event trapping.
The TIMER ON statement turns on the trap. BASIC checks the the value of timer after each program line. If the number is equal to that in the ON TIMERO GOSUB statement, BASIC transfers program control t o the line number specified.
The TIMER STOP statement temporarily halts timer trapping.
If the timer equals the specified number, BASIC does not trans- fer program control to the ON TIMERO GOSUB statement until you turn on trapping again by executing a TIMER ON state- ment. BASIC remembers that the timer value was equal and branches t o the subroutine immediately after trapping is turned on again.
The TIMER OFF statement turns off timer trapping. BASIC does not remember if the value of timer equals the number spec- ified when trapping is turned on again.
Sample Program
See ON TIMERO GOSUB for an example.
331
Chapter 10 I BASIC Keywords
TROFF, TRON
TROFF
TRON
Statements
Turn the trace function on/off.
TRON turns on the tracer and TROFF turns it off.
The tracer lets you follow program flow. This is helpful bugging and for analyzing the execution for de- of a program. After a program is debugged, you can remove the TRON and TROFF statements .
Each time the program advances to a new line, the tracer dis- plays that line number inside a pair of brackets.
Sample Program
2 2 9 0 TRON
2 3 0 0 X = X
2 3 1 0 TROFF
*
3 . 1 4 1 5 9
Lines
2290
and
2310
assure you that Line
2300
is actually being executed, because
[23001
is printed on the display each time it is executed.
5 TRON
1 0 K = 1 0
2 0 FOR J = l TO 2
3 0 L = K + 1 0
4 0 P R I N T J ; K ; L
5 0 K = K + 1 0
6 0 N E X T J
7 0 TROFF
8 0 END
When you run this program, BASIC prints:
[ 1 0 1 [ 2 0 1 [ 3 0 1 ~ 4 0 1 1 0 2 0
[ 5 0 1 [ 6 0 1 [ 7 0 1
332
Chapter 10 I BASIC Keywords
USR
USR[ nu m ber] (argument)
Function
Calls a user’s assembly-language subroutine identified with number and passes argument t o that subroutine.
The number you specify must be the same as the corresponding
DEF
USR
statement for that routine. If you omit number, BASIC assumes zero.
USR
lets you call as many as 10 assembly-language subroutines and then continue execution of your BASIC program.
Before you can execute a
USR
function call, you must define the subroutine’s address in a DEF SEG and DEF
USR
statement.
The DEF SEG defines the address of the segment containing the subroutine. The DEF
USR
statement defines the subroutine being called and its offset from the beginning of the segment set by DEF SEG. See DEF SEG, DEF facing With Assembly-Language Subroutines” in Chapter 11.
333
Chapter 10 I BASIC Ke-ywords
VAL
VAL(string)
Function
Calculates the numerical value of string.
VAL
is the inverse of the STR$ function; it returns the number represented by the characters in a string argument. This num- ber may be integer, single precision, ing on the range constants . or double precision, depend- of values and the rules used for typing all
VAL
terminates its evaluation on the first character that has no meaning in a numeric value.
If
the string is nonnumeric or null,
VAL
returns a zero.
Ex ample
s
P R I N T V A L ( " 1 0 0 D O L L A R S " ) prints 100.
P R I N T V A L ( " 1 2 3 4 E 5 " ) prints 123400000.
Sample Programs
1 0 READ N A M E $ , C I T Y $ , S T A T E $ , Z I P $
2 0 I F V A L C Z I P $ ) < 9 0 0 0 0 O R V A L ( Z I P $ ) > 96699
THEN P R I N T NAME$ T A B C 2 5 ) "OUT OF S T A T E "
3 0 I F V A L ( Z I P $ ) > 9 0 8 0 1 AND V A L < Z I P $ ) < = 9 0 8 1 5
THEN P R I N T NAME$ T A B ( 2 5 ) "LONG BEACH"
This program searches for zip codes within the specified ranges t o determine if they are within Long Beach or "out of state.''
334
Chapter 10 I BASIC Keywords
VARPTR
VARPTR
(variable)
VARPTR
([#]buffer)
Function
Returns the offset into BASIC’s data segment of a variable or a disk buffer.
Variable is a numeric or string variable.
Buffer is the number assigned t o the file when you opened it.
The number sign is optional. It is provided for compatibility with other BASICs.
VARPTR can help you locate a value in memory. When used with Variable, it returns the address of the first byte
of
data identified with variable. See the section “How Variables are
Stored” in Chapter 11 for the format.
If the variable you specify has not been assigned a value, an “Il- legal function call” occurs.
When used with sequential access files, VARPTR returns the starting address of the disk buffer. When used with direct access files, VARPTR returns the address of the FIELD buffer.
If you specify a buffer that was not allocated when loading
BASIC, a “Bad file number” error occurs. (See Chapter
2
for in- formation on how t o load BASIC.)
The offset returned is an integer in the range
-32768
t o
32767.
It is always an offset into BASIC’s data segment, regardless of whether you have executed a DEF SEG to change the segment.
VARPTR is used primarily to pass a value to a n assembly language subroutine via USR. Since VARPTR returns an offset that indicates where the value
of
a variable is stored, you can pass this address t o an assembly-language subroutine as the ar- gument of USR. The subroutine can then extract the contents of the variable with the help of the address that you have supplied to it.
If VARPTR returns a negative address, add it t o
65536
to obtain the actual address.
335
Chapter 10 I BASIC Keywords
VARPTR$
VARPTR$( uaria
ble)
Function
Returns a character form of the memory address of the variable.
VARPTR$ is primarily used with PLAY and
DRAW
in programs that are later executed.
Variable is a numeric or string variable.
VARPTR$ returns a 3-byte string: byte 0 = byte 1 = byte 2 = type
low
byte of address
high
byte of address
Type is 2 for integer variables,
3
for string variables, precision variables, and 8 for double precision variables.
Note:
Because array and string addresses and file data blocks change whenever you assign a new vari- able, do not store the contents variable. of VARPTR$ into a
Example
1 0 PLCIY " X " + V A R P T R $ < A $ ) uses the PLAY subcommand argument for PLAY.
X,
plus the contents of A$, as the
336
VIE WIGraphics
ChuDter 10 I BASIC Keywords
Statement
Creates a viewport that redefines the screen parameters. This defined area, a window, becomes the only place you can draw graphic displays.
, y I ) specifies the upper-left coordinates for the rectangular viewport. viewport.
All coordinates must be within the limitations of the screen.
Color lets you fill in the specified viewport with the specified color. See Chapter 8, “Displaying Color and Graphics,” for infor- mation on color.
Border is a n integer i n the range 0 to 15. See Chapter 8, “Dis- playing Color and Graphics,” for information on the 16 color set used for borders.
SCREEN specifies that all coordinates used in drawing are ab- solute to Point 0,0 on the screen. If you omit SCREEN, all coor- dinates specified are relative to the viewport coordinates.
If you omit all options, BASIC sets the viewport to define the en- tire screen.
Examples
V I E W (10,10)-(100,1 0 0 ) sets up a viewport with the upper-left corner at 10,10 and the lower-right corner a t 100,100. Since SCREEN is omitted, all subsequent coordinates are relative to the viewport. For example,
PSET
(5,5),3
V I E W S C R E E N < 2 0 , 2 5 ) - < 1 0 0 , 1 5 0 ) sets up a viewport. Because SCREEN is specified, all subse- quent coordinates are absolute. For example, PSET (5,5),3 does not appear because it within the viewport. is outside the viewport. PSET (30,30),3 is
337
Chapter
10
I BASIC Keywords
Notes:
0
BASIC ignores any points t h a t a r e outside the viewport's limits.
0
RUN, SCREEN, and WINDOW statements, without parame- ters, define the entire screen as
the
viewport.
CLS clears only the active viewport.
Sample Program
1 0 SCREEN 1
2 0 V I E W ( 1 0 , 1 0 ) - ( 2 0 0 , 1 0 0 ) , 2
3 0 P S E T ( 1 0 0 , 5 0 )
4 0 DRFlW " L 4 0 E 2 0 F 2 0 "
Line 20 sets the viewport. Line 30 sets the starting point for the
DRAW statement in Line 40.
~
338
ChuDter 10 I BASIC Keywords
VIEW PRINT
VIEW PRINT
top line
TO
bottom line
Statement
Creates a text viewport that redefines the text screen parame- ters. All statements and functions that normally function within the text viewport now function in the new text screen parame- ters. Cursor movement and scrolling are also limited to the text viewport.
Top line specifies the first line of the text viewport. It may be in in the range 1 to omit top
24,
but must be less than bottom line. If you line, BASIC assumes Line 1 as the beginning of the text viewport.
Bottom line specifies the last line of the text viewport. in the range 1 to
It
may be
24,
but must be greater than top line. If you omit bottom line, BASIC assumes Line
24
as the end of the text viewport.
If you omit all parameters, screen as the text viewport.
VIEW PRINT defines the entire
Example
V I E W P R I N T 1 TO 1 5
BASIC defines the first 15 lines of the video as the text viewport.
All cursor movements, scrolling, and text screen functions and statements are limited to these boundaries.
339
Chapter 10 I BASIC Keywords
WAIT
WAIT port, numberl [,number21
Statement
Suspends program execution until a machine input port develops a specified bit pattern. (A port is a n inputloutput location.)
Number1 and Number2 are integers in the range 0 to
255.
BASIC reads the data at the specified port and XORs i t with number2, if given. If you omit number2, BASIC XORs the data with zero. BASIC then ANDs the result with sult is numberl. If the re- zero, BASIC starts again with reading the data a t the port again. If the result is nonzero, BASIC continues with the next statement.
It is possible to enter a n infinite loop with the WAIT statement.
In this case, you must restart the machine manually. To avoid this, WAIT must have the specified value a t port number during some point in program execution.
Example
1 0 0 W A I T 3 2 , 2
340
Chapter
10
I BASIC Ke.ywords
WHILE
...
WEND
WHILE
expression
WEND
Statement
Executes a series of statements in a loop as long as a given con- dition is true.
Expression is any numeric or string expression, usually making logical or relational comparisons.
If expression is true, BASIC executes the statements after the
WHILE statement until it encounters a WEND statement. Then
BASIC returns t o the WHILE statement and checks expression.
If it is still true, BASIC repeats the process. If it is not true, exe- cution resumes w i t h t h e s t a t e m e n t following t h e WEND statement.
You may nest WHILE/WEND loops to any level. Each WEND matches the most recent WHILE. An unmatched WHILE state- ment causes a “WHILE without WEND” error, and an un- matched WEND causes a “WEND without WHILE” error.
Sample Program
9 0 “ B U B B L E S O R T A R R A Y A $
1 0 0 F L I P S = l “ F O R C E O N E P A S S T H R U L O O P
1 1 0 W H I L E F L I P S
1 1 5 F L I P S = 0
1 2 0
1 3 0
1 4 0
F O R I = l TO J-1
IF A$(I)>A$(I+l)THEN S W A P A $ < I ) ,
A $ ( I + 1 1 : F L I P S = l
N E X T I
1 5 0 W E N D
This program sorts the elements in array A$. Control falls out of the WHILE loop when no more swaps are performed on Line
130.
341
ChaDter 10 I BASIC Kevwords
WIDTH
WIDTH [LPRINT]
size
WIDTH
buffer,
size
WIDTH device, size
Statement
Sets the line width in number of characters for the display, line printer, or communication channel.
Size may be a n integer in the range 0 to 255 that specifies the number of characters in a line. For the screen, size may be only
20, 40, or 80.
Buffer is an integer in the range 0 t o 15 and specifies the buffer used in the OPEN statment.
When you specify buffer, BASIC changes the width immediately.
This lets you change the width when the file is open. To return to the previous width, execute another WIDTH statement,.
Device is a valid device enclosed in quotation marks that speci- fies on which device valid device names. you want t o set the width. See Chapter 1 for
When you specify device, BASIC stores the new width and does not change the current width of the device. When a subsequent
OPEN statement opens that device, BASIC uses the new width while the file is open. After you close the file, the device returns to the previous width.
When you set the width for the line printer, BASIC sends a car- riage return after every size character. For example:
1 0 W I D T H L P R I N T 1 0 0
20 L P R I N T " T h i s l i n e is 1 0 0 c h a r a c t e r 5 l o n g . S e e w h a t h a p p e n 5 w h e n y o u p r i n t a 5 t r i n g l o n g e r t h a n w i d t h
.
"
Line 10 sets the printer width t o 100 characters. After printing
100 characters, BASIC issues a carriage return. The carriage re- turn causes the printer t o print the remaining characters on the next line.
342
Chapter 10 I BASIC Keywords
When you set the width for the communications channel, BASIC sends a carriage return after every size character up to and in- cluding 254.
A
width of 255, however, is interpreted as “infinite” width and disables line wrapping. The default for communica- tions channels is 255.
To set WIDTH at the screen, you may omit the LPRINT option in the first form of the syntax, like this:
W I D T H 4 0 or you may use the third form device: of the syntax and specify the
W I D T H “ S C R N : ” , 4 0
You may only use the WIDTH statement t o select a width of 80 if you are using the VM-2 Monochrome Monitor or CM-2 Color
Monitor. If you are using the VM-2 Monochrome Monitor
CM-2 Color Monitor, you should note the following: or the
If you change the screen width, BASIC clears the screen and sets the background t o black and the foreground to white.
Changing the screen width does not affect the color enabling/ disabling value (burst value).
If the current video memory (allocated through a CLEAR statement) is not enough t o accommodate the new screen mode, BASIC issues an “illegal function call” error.
0
The table below shows the actual effect of WIDTH statements on the various modes. Given a particular mode and width, the table shows the new screen mode that results.
20
Width
40 80
8
0
1
2
4 s
+ o
3
8 E
4 b
5 u 5
6
~
343
Chawter 10 I BASIC Kevwords
If you attempt to select a size outside the range 0 to 255, an "il- legal function call" error results.
Examples
W I D T H L P R I N T 1 3 2
WIDTH "LPTI
: ' I ,
1 3 2
Both these statements change the printer width t o 132. The sec- ond statement does not change the printer width until LPT1: is specified as the device in an OPEN statement.
1 0 W I D T H L P R I N T 80
1 0 0 O P E N "LPT1
: I '
F O R O U T P U T A S # 1
1 5 0 P R I N T # 1
1 0 0 0 W I D T H # I , 4 0
Line 10 changes the width of the printer t o 80 characters. Line
150 prints the records as 80 characters each. After BASIC exe- cutes Line 1000, Line 150 prints the records as 40 characters each.
344
Chapter 10 I BASIC Ke.ywords
Statement WINDOW
Lets you change the physical coordinates of the screen (or cur- rent viewport) by defining “world coordinates.” World coordinates can be any single-precision floating point numbers, including numbers outside the physical range of the screen as defined by the VIEW statement.
Note:
The viewport is set t o the entire screen by de- f a u l t . For more information on viewports, see t h e
VIEW command. the screen. coordinate. x is the horizontal coordinate, and y is the vertical the display. x is the horizontal coordinate, and y is the vertical coordinate.
The SCREEN option tells BASIC t o set the coordinates like the screen display where the lesser y-coordinate is in the upper-left corner of the screen. If you omit screen, BASIC inverts the y-coordinates t o show a true Cartesian coordinate system. That is, t h e lesser y-coordinate i s i n t h e lower-left corner of t h e screen.
WINDOW lets you plot points outside the normal screen coordi- nate limits by setting new world coordinates to the screen. WIN-
DOW transforms the new world coordinates onto the screen, usually altering the aspect ratio.
Note:
CIRCLE, GET, and PUT do not use world coordinates.
You can easily plot graphs by specifying coordinates that are di- rectly proportional t o the limits of the graph. For example, t o plot the increase of sales from
1984
t o
1987
with sales averaging
100,000 t o 300,000, you can use the following command:
W I N D O W < 1 9 8 4 , 1 0 0 0 0 0 ) - < 1 9 8 7 , 3 0 0 0 0 0 )
345
ChaBter 10 I BASIC Kevwords
The coordinates can be pictured for commands that use world coordinates:
If you give the command:
W I N D O W S C R E E N ( 1 9 8 4 , 1 0 0 0 0 0 ) - ( 1 9 8 7 , 3 0 0 0 0 0 ) the coordinates can be pictured a s follows for commands that use world coordinates:
346
Note:
RUN, SCREEN and WINDOW statements, without parameters, define the entire screen as the window.
Chapter 10 I BASIC Ke.ywords
WRITE
WRITE
...I
Statement
Writes data t o the screen.
Data can be any string or numeric expression or variables. If you omit data, BASIC outputs a blank line.
The only difference between WRITE and PRINT is that WRITE prints commas between the data items and prints quotation marks around strings.
347
Chapter 10 I BASIC Keywords
WRITE#
WRITE#buffer,
..I
Statement
Writes data t o a sequential access disk file.
Buffer is the number assigned t o the file when you opened it.
Data may be numeric or string expressions. If you specify more than one data item, separate the items with commas.
WRITE# inserts commas between the data items it writes disk. to
It
delimits strings with quotation marks. Therefore, it is not necessary t o put explicit delimiters between the data.
WRITE# inserts a carriage return after writing the last data item to disk.
Example
A$="MICROCOMPUTER": B$="NEWS"
W R I T E # I
,
A $ , E $ writes the following image t o disk:
"MICROCOMPUTER" ,"NEWS"
348
Chapter
11
TECHNICAL INFORMATION
This chapter provides various information of a technical nature.
If you are just beginning to use BASIC, you may want this chapter until later. t o skip
Interfacing with Assembly-Language
Subroutines
This section is for users who call subroutines written in other languages from their BASIC programs. BASIC provides for inter- facing with subroutines through the USR function and through the CALL and the CALLS statements.
You can load your assembly language subroutine into BASIC’s work area or into another segment of memory. We will show you both methods.
Memory Allocation Outside BASIC’s
Work
Area
When you load BASIC, the DS (data segment) register is set to the address of BASIC’s work area. To access a n area of memory outside this work area, execute a DEF SEG statement to specify the address of the segment of memory you are accessing. If you don’t execute a DEF SEG statement, your CALL, CALLS, or
USR statements transfer control to a n area within BASIC’s work area. After returning from the subroutine, execute another DEF
SEG statement to restore the DS register to its original value.
See DEF SEG in Chapter 10 for more information.
Memory Allocation Inside BASIC’s Work Area
To set aside memory space for a n assembly language subroutine within BASIC’s work area, use the
BASIC. See Chapter
2
for a review
IM: switch when you load
of
the start-up procedure.
351
Chapter 11 I Technical Information
The /M: switch sets the highest memory address that BASIC can use. The value that you specify with the /M: switch tells BASIC that it can use all memory up t o that offset. Load your subrou- tine at that offset. Using the iM: switch prevents BASIC from de- stroying your subroutine. For example,
B A S I C / M : & H F 0 0 0 sets the highest memory location that BASIC can use at hexade- cimal address
EFFF.
This reserves the highest 4K bytes of memory for your subroutine. You can load your subroutine at hexadecimal address &HF000 like this:
BLOAD " S U B A . A S M " , g H F 0 0 0
Stack Space
If you need more stack space when you call an assembly lan- guage subroutine, you can save the BASIC stack and set up a new stack for the subroutine. You must restore the BASIC stack before returning from the subroutine. You save the stack, create a new stack, and restore the stack in your subroutine.
Loading the Subroutine into Memory
You can use the operating system or the POKE statement to load the subroutine into memory. You may assemble the routines with the Macro Assembler (available through your Radio Shack dealer) and link them with Linker. The Linker is part of the
MS-DOS package. To load t h e program file, observe these guidelines:
0
Be s u r e t h a t t h e subroutines do not contain any long references.
Skip the first 512 bytes read in the rest of the file. of the LINK output file and then
352
Chapter 11 I Technical Information
Poking a Subroutine into Memory
You can code short subroutines in machine language and use the
POKE statement t o put the code into memory.
To
do so, follow these steps:
1. Code the machine language instructions for your subroutine.
2. Put the assembly instruction code for each byte of the ma- chine language code into DATA statements, preceded by the
&H symbols t o denote that they are hexadecimal values.
3. Execute a loop that reads the DATA statements and POKES them into an area of memory.
For example, the instruction code for the statement
P U S H BP is 55. The DATA statement for that instruction is
D A T A & H 5 5
After the loop is complete, the subroutine is in memory. Whether you are using the USR function or the CALL statement t o call the subroutine, you must set the value of the subroutine entry point as the location specified in the first POKE statement.
CALL Statement
When the CALL statement is executed, the following occurs:
1. For each parameter in the parameter list, the two-byte offset of the parameter’s location within the data segment (DS) is pushed onto the stack. If the parameter is a string variable, the offset points to the string descriptor. See the section “Ac- cessing String Parameters” in this appendix.
2. The BASIC return address code segment (CS) and offset (IP) are pushed onto the stack.
3. Control is transferred t o the subroutine by an 8086 long call t o the segment address given in the last DEF SEG state- ment and the offset given in uariabk.
353
Chapter 11 I Technical Information
When the CALL statement is executed, the operating system loads the CS (code segment) register with the value specified in the last DEF SEG statement. If you are CALLing a subroutine within BASIC’s work area, and no DEF SEG is required, the CS register is loaded with the address of BASIC’s work area. This address
is
shifted left 4
bits;
in other words, which is the same as multiplying it by 16 decimal (10 hexadecimal). Then, the off- set of the subroutine is added to the segment address.
Example
1 7 1 0 0
+
0 0 2 0 = 1 7 1 2 0
17120 is the absolute address of the first instruction in the subroutine.
Technical Functions
The called routine may destroy the previous contents of all regis- ters. If you want to save the contents of the registers, the first instructions in the subroutine must be a PUSH for each register, and the last instructions in the subroutine must be a POP to re- store the registers to their original value. You must execute a
POP for every PUSH t o maintain stack integrity.
The subroutine may refer to the passed parameters as positive offsets to the Base Pointer (BPI. The CALLed routine must
PUSH BP on the stack and then move the current stack pointer into BP. BP should be the first register you PUSH so that the parameters may be referenced as a n offset t o BP. The first 4 bytes of the stack contain the I P and CS register values that
BASIC saves when the CALL is executed. To calculate the pa- rameters offset from the BP, use this equation:
2
*
(total parameters - parameter position)
+
6 = offset
For example, the address of parameter 1 is a t 10(BP), parameter
2 is a t S(BP), and parameter 3 is a t 6(BP).
354
Chapter 11 I Technical Information
Exiting the Subroutine
The called routine must execute a RET number statement t o ad- just the stack to the start of the calling sequence. The value of number is 2 times the number of parameters in the parameter list.
CALLS Statement
The CALLS statement is the same as CALL except the argu- ments are passed as segmented addresses. CALLS should be used t o access MS-FORTRAN routines.
Because MS-FORTRAN routines need to know the segment value for each argument passed, the segment is pushed lowed by the offset. CALLS pushes first, fol-
4 bytes for each argument; therefore, the number in the RET statement (RETn) must be times the number of arguments.
4
USR Function
When the USR statement is executed, the operating system loads the CS (code segment) register with the value specified in the last DEF SEG statement. If you are accessing a subroutine within BASIC’s work area and no DEF SEG is required, the CS register is loaded with the address of BASIC’s work area. This address is shifted left 4 by 16 decimal (10 hexadecimal). Then the offset of the subrou- tine is added t o the segment address.
Example
17100
+
0020
=
17120
This is t h e absolute address of t h e first instruction in t h e subroutine.
355
Chapter 11 I Technical Information
Technical Functions
When the USR function call is made, register AL contains a value that specifies the type of argument that was given. The value in AL may be one of the following:
Value in AL Type of Argument
2
3
4
8
2-byte integer (two’s complement)
String
Single precision floating-point number
Double precision floating-point number
If the argument is a string, the DX register pair points to the
“string descriptor.” See the section “Accessing String Variables” in this chapter.
If the argument is a number, the BX register pair points to the
Floating-point Accumulator (FAC) where the argument is stored:
FAC is the exponent minus 128, and the binary point is the left of the most significant bit of the mantissa. t o
FAC-1 contains the highest 7 bits of mantissa with leading
1 suppressed (implied). Bit 7 is the sign of the number
(0 1 negative).
If the argument is a n integer:
FAC-2 contains the upper 8 bits of the argument.
FAC-3 contains the lower 8 bits of the argument.
If the argument is a single precision floating-point number:
FAC-2 contains the middle 8 bits of mantissa.
FAC-3 contains the lowest 8 bits of mantissa.
If the argument is a double precision floating-point number:
FAC-4 through FAC-7 contain 4 more bytes of mantissa
(FAC-7 contains the lowest 8 bits).
Exiting the Subroutine
The subroutine must execute a RET 7 statement t o adjust the stack to the start of the calling sequence.
356
Chapter 11 I Technical Information
How Variables are Stored
BASIC stores variables in its data segment as follows:
Byte
Byte 0
Bytes 1 and 2
Byte 3
Byte 4
+ integer stored in Byte 3
Byte 4
+ length
Contents
Type
Name
Integer
3 - 38
Name
Data
Description
Identifies the type able stored of vari- at this location:
2 integer
3 string
4 single precision
8 double precision
The first 2 characters variable name. of the
Integer is the number of ad- ditional characters in t h e variable name.
The remainder of the vari- able name is stored at bytes
4
+ the integer stored in
Byte 3.
The contents of the variable are stored in the bytes im- mediately following the vari- able name. The data can be
2,
3,
4, depending on t h e type of data.
At least 3 bytes are required t o store any variable name. A 1- or
2-character variable name occupies exactly 3 bytes. Bytes 1 and
2 for the first 2 characters and Byte 3 to contain a zero to indi- cate that there are no additional characters in the variable name. If the variable name only contains 1 or 2 characters, the data is stored beginning a t Byte of the first actual byte of data depends on the length of the vari- able name. VARPTR returns the offset of the first actual byte of data, not the offset of the beginning of the storage area.
357
Chapter 11 1 Technical Information
Accessing String Variables
If the parameter passed in a CALL statement is a string expres- sion, the parameters offset points to the string descriptor. If the argument passed in a USR function call is a string expression, the DX register points t o the string descriptor.
The string descriptor is a 3-byte area of memory that points to t h e text of t h e s t r i n g . The string descriptor contains t h e following:
Byte 0 contains the length of the string (0 to 255).
Byte 1 contains the lower 8 bits of the string starting address in BASIC’s data segment.
Byte 2 contains the upper 8 bits of the string starting address in BASIC’s data segment.
The text of the string may be altered by the subroutine, but the length of the string
must not
be changed. BASIC cannot cor- rectly manipulate strings if their lengths are modified by exter- nal routines.
Since the string descriptor points t o an area of memory in your
BASIC program, you must be careful not to alter or destroy your program. To avoid unpredictable results, add the concatenation symbol ( + ) to the string. This forces the string t o be copied into string space, where the string may be modified without affecting the program.
Example
20 A$
=
“MONTHLY SALES REPORT”
+
“ ”
File Control Block
A file control block is a storage area in BASIC’s data segment that contains information BASIC needs for all functions per- formed on that file. When you execute the VARPTR function and specify the buffer number, BASIC returns the address of the
BASIC file control block for that file. Note that this is the BASIC file control block, not the DOS file control block. The address is specified as an offset into BASIC data segment. In this section we define the information in the file control block. Offsets are relative t o the value returned by VARPTR. Length is in bytes.
358
Chapter 11 I Technical Information
OFFSET LENGTH DESCRIPTION
0 1 Mode The mode in which the file was opened:
1 - Input Only
2
4
- Output Only
- Random I/O
16 - Append Only
32
64
- Internal use
- Future use
128 - Internal use
1 38 FCB Disk File Control Block.
Refer to DOS User's Guide for contents.
39
2
CURLOC
41 1 ORNOFS
Number of sectors read or w r i t t e n for sequential ac- cess. For random access, it c o n t a i n s t h e l a s t r e c o r d number
+
1 read or written.
Number of bytes i n sector when read or written.
42
43
46
47
48
1
3
1
1
1
NMLOFS
***
DEVICE
WIDTH
POS
Number of bytes left in In- put buffer.
Reserved for future expansion.
Device number:
0-9 - Disks A: thru J:
255 - KYBD:
254 - SCRN:
253 - LPT1:
251 - COM1:
250
249
- COM2:
- LF'T2: (not available)
248 - LF'T3: (not available)
Device width.
P o s i t i o n i n b u f f e r for
PRINT#.
359
Chapter 11 I Technical Information
OFFSET LENGTH DESCRIPTION
49 1 FLAGS Internal use during LOAD/
SAVE; not u s e d for d a t a files.
50 1 OUTPOS Output position used during tab expansion.
51 128 BUFFER
179
181
183
2
2
2
VRECL
PHYREC
LOGREC
Physical d a t a buffer. Used t o t r a n s f e r d a t a between
DOS and BASIC. Use this offset t o examine d a t a i n
Sequential I/O mode.
Variable length record size.
D e f a u l t i s 1 2 8 . S e t by l e n g t h o p t i o n i n O P E N statement.
C u r r e n t p h y s i c a l r e c o r d number.
C u r r e n t l o g i c a l r e c o r d number.
***
185
186
188
1
2
<n>
OUTPOS
FIELD
Future use.
Disk files only. Output posi- tion for PRINT#, INPUT#, and WRITE#.
Actual FIELD data buffer.
Size i s determined by /S: switch. VRECL bytes a r e t r a n s f e r r e d b e t w e e n
BUFFER and FIELD on I/O operations. Use this offset to examine File data in Ran- dom I/O mode.
360
Chapter 11 I Technical Information
User Installed Devices
When writing device drives to use with BASIC, note the follow- ing rules:
0
BASIC sends only a carriage return as a n end of line. If the device requires a line feed also, you must provide for this in your driver.
BASIC must read and write control information t o the device.
Reading and writing Device Control data is handled by the
BASIC IOCTL statement and the IOCTL$ function.
0
Your driver must provide the following control functions:
The driver must set a maximum line width as requested by the OPEN statement.
The driver must return the current maximum line width when BASIC asks for it.
I n p u t Devices m u s t r e t u r n a n “end-of-file’’ condition to
BASIC if you want to be able to close sequential input files open to the device driver. This is used by the EOF statement.
Input Devices should return a
“.Z
[CTRL] [q t o read past the end of the device input stream. BASIC uses this to give a n “Input past end” error.
For more information on device drives, see the Programmer’s
Reference Manual for your computer. It is available a t your Radio
Shack Computer Store.
Information for Creating Child Processes
When writing programs for use as child processes from BASIC, please note the following rules and information:
Child processes that use the screen device might modify the screen mode parameters. ters from BIOS.
If
necessary, restore these parame-
0
Save and restore interrupt vectors the child process uses.
BASIC places many hardware devices in specific states.
These devices include a n Interrupt Controller, Counter Tim- ers, DMA Controller, I/O Latch, and Uarts.
361
Chapter 11 I Technical Information
Be careful when altering any files opened by the BASIC par- ent program. The BASIC parent program should close all files before executing SHELL and then reopen them upon return.
0
When the SHELL command executes, BASIC tries t o free any memory not being used. However, BASIC does not free memory preserved with the /M switch. This may cause a n
“Out of memory” error.
To
avoid this, load your machine language routines before en- tering BASIC. Use Interrupt 27 to let the routines to exit
MS-DOS but still remain in memory.
For more information, see the ual for your computer.
Programmer’s Reference man-
It
is available through your Radio
Shack Computer Center.
0
Never use Interrupt 27 on a child process. If you attempt to terminate a child process but have it remain in memory,
BASIC may not have enough room to expand its workspace to its original size. If the workspace cannot be restored, BASIC closes all files, prints the error message “SHELL can’t con- tinue,” and exits to MS-DOS.
You cannot run BASIC as a child process t o itself.
362
Chapter 12
BASIC ERROR CODES AND MESSAGES
Number Message
1
2
3
4
5
NEXT without FOR
BASIC executed a NEXT statement without previ- ously executing a FOR statement, or a variable in a
NEXT statement does not correspond to a previ- ously executed FOR statement.
Syntax error
BASIC encountered a line that contains an incor- rect sequence of characters (such as unmatched pa- r e n t h e s e s , m i s s p e l l e d s t a t e m e n t , i n c o r r e c t punctuation, etc.). BASIC automatically enters the edit mode at the line that caused the error.
RETURN without GOSUB
BASIC executed a RETURN statement without pre- viously executing a GOSUB statement.
Out of DATA
When executing a READ statement, BASIC could not find any DATA statements or unread d a t a items.
Illegal function call
A parameter that is out of range was passed t o a math or string function. This error may also occur as the result of: negative array subscript or a n unreasonably large array subscript. negative or zero argument with
LOG.
0 negative argument to SQR.
0 negative mantissa with a noninteger exponent.
0 invalid exponential number
365
Chavter 12 I Error Messages
Number Message
6
7
8
9
10
0 a call t o a USR function without a starting ad- dress set by DEF USR.
0 improper argument to MID$, LEFT$, RIGHT$,
PEEK, POKE, TAB, SPC, STRING$, SPACE$,
INSTR, or LEFT$, RIGHT$, P E E K , POKE,
TAB, S P C , STRING$, SPACE$, INSTR, or
ON ... GOTO.
0 negative record number used with GET or PUT.
Overflow
The result of a calculation was too large t o be rep- resented in BASIC numeric format. If underflow oc- curs, the result is zero, and execution continues without a n error.
Out of memory
A program is too large, has too many FOR loops or
GOSUBs, has too many variables, or has expres- sions that are too complicated.
Undefined line number
A nonexistent line was referenced in a GOTO, GO-
SUB, IF ... THEN ... ELSE, or DELETE statement.
Subscript out of range
An array element is referenced with a subscript outside the dimensions of the array or with the wrong number of subscripts.
Redimensioned Array/Duplicate Definition
BASIC encountered 2 DIM statements for the same array, or a DIM statement after the default dimen- sion of array.
10 had already been established for that
366
Chapter 12 I Error Messages
Number
11
12
13
14
15
16
17
Mess age
Division by zero
An expression includes division by zero, or the oper- ation of involution results in zero being raised to a negative power. BASIC supplies machine infinity with the sign of the numerator a s the result of the division, or it supplies positive machine infinity a s t h e r e s u l t of t h e involution. Execution then continues.
Illegal direct
A
statement that is illegal as a command was en- tered at BASIC’s prompt.
Type mismatch
A string variable name was assigned a numeric value or vice versa. A string function was given a numeric argument or vice versa.
Out of string space
The amount of memory used by string variables ex- ceeded the amount of free memory.
String too long
An attempt was made to create a string more than
255 characters.
String formula too complex
A string expression is too long or too complex. The e x p r e s s i o n s h o u l d b e b r o k e n i n t o s m a l l e r expressions.
Can’t continue
An attempt was made to continue a program that: halted because of a n error.
0 was modified during a break in execution.
0 does not exist.
367
Chapter 12 I Error Messages
Number
18
19
20
21
22
23
24
25
26
27
Message
Undefined user function.
A USR function was called before providing a func- tion definition (DEF USR statement).
No RESUME
BASIC executed an error-handling routine that did- not have a RESUME statement.
RESUME without error
BASIC executed a RESUME statement when no er- ror had occurred.
Unprintable error
An error message is not available for the error that occurred.
Missing operand
BASIC encountered an expression that contained an operator but no operand.
Line buffer overflow
The line being input is too long.
Device Timeout
BASIC did not receive information from an 110 de- vice within a predetermined amount of time.
Device Fault
An incorrect device designation has been entered.
FOR without NEXT
BASIC executed a FOR statement that did not have a matching NEXT.
Out of Paper
BASIC received a n out printer. of paper status from the
368
Chapter 12 I Error Messages
Number Message
29
30
WHILE without WEND
BASIC encountered a WHILE statement that did not have a matching WEND.
WEND without WHILE
BASIC executed a WEND statement before execut- ing a WHILE statement.
369
Chapter 12 I Error Messages
Disk
Errors
Number Mess
age
50
FIELD overflow
A
FIELD statement is allocating more bytes than the specified record length of the direct access file.
51
52
53
54
55
57
58
Internal error
An internal malfunction has occurred in BASIC.
Report to Radio Shack the conditions under which the message appeared.
Bad file number
BASIC encountered a reference to a buffer number that is not open or is out of the range of the number of files specified when BASIC was loaded.
File not found
A LOAD, KILL, or OPEN statement references a file that does not exist on the current disk.
Bad file mode
An attempt was made t o use PUT, GET, or LOF with a sequential file, t o LOAD a direct file, or to execute a n OPEN statement with a file mode other than I, 0, R, E or D.
File already open
BASIC encountered a n OPEN statement for sequen- tial output, or a KILL statement, for a file that is already open.
Device I/O Error
An Input/Output error occurred. This is a fatal er- ror; the operating system cannot recover it.
File already exists
The filename specified in a NAME statement is identical to a filespec already in use on the disk.
370
Chapter 12 I Error Messages
~~
Number
61
62
63
64
66
67
68
Message
Disk full
All disk storage space is in use.
Input past end
BASIC executed a n INPUT statement after all the data in the file had been read, or BASIC executed a n INPUT statement to a null (empty) file.
To
avoid this error, use the EOF function to detect the end- of-file.
Bad record number
In a GET or PUT statement. the record number e i t h e r g r e a t e r t h a n t h e m a x is m u m allowed
(16,777,215) or equal to zero.
Bad file name
An i l l e g a l p a t h n a m e was used
SAVE, KILL, or OPEN statement filename with too many characters). w i t h a LOAD,
(for example, a
Direct statement in file
Information in a non-ASCII format was encountered while LOADing a n ASCII-format file. The LOAD is terminated.
Too many files
The diskette already contains the maximum num- ber of files allowed. This usually occurs on SAVE or
OPEN. An attempt was made t o create a new file
(using SAVE or OPEN) when all directory entries are full.
Device Unavailable
An attempt was made to open a file to a non- existent device. It may be that hardware does not exist to support the device, such as LPT2: or LPT3:, or that the device is disabled. This occurs if a n
OPEN “COM1: ... statement is executed but the user disabled RS232 support via the /C:0 switch direc- tive on the command line.
371
Chapter 12 I Error Messages
Number
Message
69
70
71
72
73
74
75
76
77
Communication buffer overflow
Not enough space has been reserved for the commu- nications buffer.
Disk write protected
Occurs when a n attempt is made to write to a diskette that is write-protected. Use a n ON ERROR
GOTO statement to detect this situation and re- quest user action.
Disk not Ready
Occurs when the diskette drive door is open or a diskette is not in the drive. Use a n ON ERROR
GOTO statement to recover.
Disk media error
Occurs when the FDC controller detects a hardware or media fault. This usually indicates harmed me- dia. Copy any existing files to a new diskette and re-format the damaged diskette. FORMAT flags any bad tracks and records them in a special file. The remainder of the diskette is then usable.
Advanced Feature
Rename across disks
An attempt was made to rename a file with a new drive designation. BASIC does not allow this.
PatWFile Access Error
During a n OPEN, MKDIR, CHDIR, or RMDIR op- eration, MS-DOS was unable to make a correct
Path-to-Filename connection.
Path not found
The OPEN, MKDIR, CHDIR, or RMDIR statement references a path that does not exist.
Deadlock
372
Appendix A
BASIC
Reserved Words and Derived Functions
Reserved
BASIC
Words
ABS
AND
ASC
CALL
CDBL
CINT
CIRCLE
DELETE IOCTL OR
DIM IOCTL$ OUT
DRAW KEY PAINT
EOF
EQV
CHAIN ERASE
CHDIR ERDEV
CHR$ ERDEV$
ERL
ERR
LINE PLAY
LIST PMAP
LLIST
LOAD
LOC
LOF
POINT
POKE
POS
LOCATE PRESET
SOUND
SPACE$
SPC (
ATN
AUTO
EDIT
ELSE
KEY$ PALETTE
KILL
SQR
PALETTE USING STEP
BEEP END LEFT$ PCOPY
BLOAD ENVIRON LEN PEEK
BSAVE ENVIRON$ LET PEN
STICK
STOP
STR$
STRIG
STRING$
SWAP
SYSTEM
TAB(
TAN
TERM
CLEAR ERROR
CLOSE EXP
CLS FIELD
COLOR FILES
COM FIX
COMMON FN
CONT FOR cos
FRE
CSRLIN GET
CSNG
CVD
GOSUB
GOT0
CVI HEX$ cvs
IF
DATA IMP
DATE$ INKEY$
DEF INP
DEFDBL INPUT
DEFINT INPUT#
DEFSNG INPUT$
DEFSTR INSTR
D E F F N INT
DEF USR INTER$
LOG
LPOS
PRINT#
PSET
LPRINT PUT
LSET RANDOMIZE
MERGE READ
MID$ REM
MKDIR RENUM
MKD$ RESET
MKI$ RESTORE
MKS$ RESUME
MOD RETURN
MOTOR RIGHT$
NAME RMDIR
NEW RND
NEXT
RSET
NOISE RUN
NOT SAVE
OCT$ SBN
OFF
ON
SCREEN
SGN
OPEN SHELL
OPTION SIN
THEN
TIME$
TIMER
TO
TROFF
TRON
USING
USR
VAL
VARPTR
VARPTR$
VIEW
WAIT
WEND
WHILE
WIDTH
WINDOW
WRITE
WRITE#
XOR
375
Appendix
A
~
Derived BASIC Functions
Functions which are not intrinsic t o BASIC may be calculated as follows:
Function BASIC Equivalent
SECANT
COSECANT
CONTANGENT
INVERSE SINE
INVERSE COSINE
INVERSE SECANT
SEC(X)
=
1/COS(X)
CSC(X) = l/SIN(X)
COT(X)
= l/TAN(X)
ARCSIN(X)
=
ATN(X/
SQR( -
+
1))
ARCCOS(X)
=
-
SQR(
-
+
1)) 1.5708
ARCSEC(X) = ATN(X/
SQR(X*X
-
+
(SGN(X)
-
*1.5708
ARCCSC(X)
=
ATN(X/SQR(X*X - 1))
+
(SGN(X) - 1)*1.5708
ARCCOT(X)
=
ATN(X)
+
1.5708
INVERSE
COSECANT
INVERSE
COTANGENT
HYPERBOLIC
SINE
HYPERBOLIC
COSINE
HYPERBOLIC
TANGENT
HYPERBOLIC
SECANT
HYPERBOLIC
COSECANT
HYPERBOLIC
COTANGENT
INVERSE
HYPERBOLIC
SINE
INVERSE
HYPERBOLIC
COSINE
INVERSE
HYPERBOLIC
TANGENT
SINH(X) = (EXP(X) - EXP(
-
COSH(X)
=
(EXP(X)
+
EXP(
-
X))/2
TANH(X) = (EXP(X)
(EXP(X)
+
EXP(
-
X))
EXP(
-
SECH(X) = 2/(EXP(X)
+
EXP(
-
XI)
CSCH(X)
=
SI(EXP(X1- EXP( -
COTH(X) = (EXP(X)
+
(EXP(
-
X))/
(EXP(X)
-
X))
ARCSINH(X1 = LOG(X
+
SQR(X*X
+
1))
ARCCOSH(X)
=
LOG(X
+
SQR(X*X
-
1))
ARCTANH(X) = LOG((l+ XI/( 1
2
376
Function
INVERSE
HYPERBOLIC
SECANT
INVERSE
HYPERBOLIC
COSECANT
INVERSE
HYPERBOLIC
COTANGENT
Appendix A
BASIC Equivalent
ARCSECH(X) = LOG((SQR
( x * x
+
1)
+
1)/X)
ARCCSCH(X) = LOG((SGN(X)*
SQR(X*X
+
1)
+ l)/X)
ARCCOTH(X) =LOG((X
+ l)/(X - 1))/2
377
Appendix
B
KEYBOARD AND CHARACTER CODE
CHARTS
Keyboard ASCIIiScan Codes
The first table in this appendix lists the keys on the Tandy 1000 keyboard in scan code order, along with the ASCII codes they generate. For each key, the following entries are given:
Scan Code
identifies the physical key on the keyboard that is pressed.
Keyboard Legend
there is more than one marking, the upper one is listed first.
ASCII Code
- four modes are:
Normal
the indicated key is depressed).
SHIFT
- is also depressed).
CTRL
- also depressed.)
ALT
also depressed).
Remarks
-
The following special symbols appear in the table: x Values preceded by an “x”
(codes preceded by an ASCII are extended ASCII codes
NUL, 00).
- No ASCII code is generated.
*
No ASCII code is generated, but the special function described in the Remarks column is performed.
379
A p p e n d i x B
The IALT] key provides a way t o generate the ASCII codes of deci- mal numbers in the range 1 t o
255.
Hold down the [ALTI key while you type on the numeric keypad any decimal number in the range 1 t o
255.
When you release [, the ASCII code of the number typed is generated and displayed.
Note:
When the
NUM LOCK
light is off, the Normal and SHIFT columns for these keys should be reversed.
All numeric values in the table are expressed in hexadecimal.
Scan
Code
Keyboard
Legend
10
11
12
13
14
15
0A
OB
0 c
0D
BE
0 F
16
17
18
19
1A
01 ESC
02 1
03
04
05
06
07
08
09
2
3
4
5
6
7
8
1B
1c
1D
9
TAB
Q
W
E
R
T
Y
U
I
0
P
[
1
ENTER
CTRL
!
@
#
$
%
&?
*
(
0 1
-
+
BACK SPACE
{
I
1E
1F
20
21
22
A
S
D
F
G
QWERTY (USA)
-
Normal
1B
31
32
33
34
35
36
37
38
39
30
2D
3D
08
09
71
77
65
72
74
79
75
69
6F
70
5B
5D
61
73
64
66
67
ASCII Codes
SHIFT CTRL
ALT
26
2A
28
29
5F
2B
08 x0F
51
57
45
1B
21
40
23
24
25
5E
52
54
59
55
49
4F
50
7B
7D
1B x8B x E l x78 x03 x79 xE3 x7A xE4 x7B xE5 x7C
1E x7D xE7 x7E xE8 x7F xE9 x80 xE0 x81
1F x82 xF5 x83
7F x8C x8D x8E
11 x10
17 x l l
05 x12
12 x13
14 x14
19 x15
15 x16
09 x17
OF x18
10 x19
1B xEB
1D xF0
41
53
44
46
47
Remarks
* *
Control Mode
01 x l E
13 x l F
04 x20
06 x21
07 x22
380
Appendix
B
Scan
Code
Keyboard
Legend
3A
3B
3c
3D
3E
47
48
49
4A
4B
4 c
4D
4E
4F
50
3F
40
41
42
43
44
45
46
51
52
53
29
2A
2B
2 c
2D
2E
2F
30
23
24
25
26
27
28
31
32
33
34
35
36
37
38
39
H
J
K
L
U P ARROW
SHIFT
LEFT ARROW
Z
X
C
V
B
N
M
<
>
I
SHIFT
?
ALT space bar
CAPS
F1
F2
F3
F4
F5
F6
F7
F8
F9
F1O
NUM LOCK
HOLD
7
8
\
9 PG U P
DOWN ARROW
4
I
5
6
RIGHT ARROW
1 END
2
3
0
PG DN
DELETE
10
*
20
* x3B x 3 c x3D x3E x3F x40 x4 1 x42 x43 x44
*
*
37
38
39 x50
34
35
36 x4D
31
32
33
30
2D x4B
7A
78
63
76
62
6E
6D
2 c
2E
68
6A
6B
6C
3B
27
*
*
*
20
* x54 x55 x56 x57 x58 x59 x5A x5B x 5 c x5D
*
*
5 c
7E x49 x86
7C xF3 xF4
X 8 8 x4F
60 x5 1 x9B x53
X87
5A
58
43
56
42
4E
4D
3 c
3E
Normal
ASCII Codes
SHIFT CTRL
ALT
48
4A
4B
4 c
3A
22
08
OA
OB
0 c xF6 xF7 x90
* x73
1A
18
03
16
02
OE
OD xF9 xFA xFB
* x72
*
20
* x5E x5F x60 x6 1 x62 x63 x64 x65 x66 x67
*
* x93 x94 x84 x96 x95 xFC xFD x74 x75 x9A x76 x9c x9D
Remarks x97
*
*
* xEA
*
*
*
* x9E x23 x24 x25 x26 xF8 xF1 x91
*
Left SHIFT x92 x 2 c x2D x2E x2F x30 x3 1 x32 x89 x8A xF2
*
Right SHIFT x46 SCR Print Toggle
* Alternate Mode
20
*
Caps lock x68 x69 x6A x6B x6C x6D x6E x6F x70 x7 1
* number lock
*
Freeze display
*
*
381
Appendix
B
Scan
Code
Keyboard
Leeend
54 BREAK
55
56 .
+
57 ENTER
58 HOME
59 F11
5A F12
INSERT
Normal xOO
2B
2E
0D x47 x98 x99
ASCII Codes
SHIFT CTRL
ALT Remarks xOO
* *
CTRLBREAK is the control brk routine
(INT 1BH).
ALT BREAK is the scroll lock bit toggle. x52 x9F xA0 xAl xA4 xA5 Numeric keypad
OD x4A
0A x8F Numeric keypad x77 xA6 xA2 xAC xB6 xA3 xAD xB7
382
Awwendix B
ASCII Character Codes
The previous table listed the ASCII codes (in hexadecimal) gen- erated by each key. The following table lists the characters gen- erated by those ASCII codes. (Note: All ASCII codes in this table are expressed in
decimal
form.)
You can display the characters listed by doing either following: of the
0
Using the BASIC statement PRINT CHR$(code), where code is the ASCII code.
0
Pressing and, without releasing it, typing the ASCII code on the
numeric keypad.
For Codes 0-31, the table also lists the standard interpretations.
The interpretations are usually used for control functions or communications.
Note:
The BASIC program editor has its own special interpretation of character listed. some codes and may not display the
383
Appendix B
~
ASCII code
ASCII CHARACTER CODES
~
Character
Control
Character
000
00 1
01 1
01 2
013
01 4
015
01 6
01 7
01 8
01 9
020
02 1
022
023
024
025
026
027
028
029
030
03 1
002
003
004
005
006
007
008
009
01 0
(null)
@ e
V
+
(beep)
13
(tab)
(line feed)
(home)
(form feed)
(carriage return)
43
1J b
4 t
!!
ll
5
I
1 t
J
* t
(cursor right)
(cursor left)
(cursor up)
(cursor down)
NUL
SOH
STX
ETX
EOT
ENQ
ACK
BEL
0s
HT
LF
VT
FF
CR so
SI
DLE
DC 1
DC2
DC3
DC4
NAK
SY N
ETB
CAN
EM
SUB
ESC
FS
GS
RS us
384
Appendix B
ASCII
Code
ASCII CHARACTER CODES
Character
ASCII
Code Character
055
056
057
058
059
060
06 1
062
063
064
065
066
067
050
05 1
052
053
054
039
040
041
042
043
044
045
046
047
048
049
032
033
034
035
036
037
038
?
@
A
E?
C
,
<
-
>
(
1
+
-
(space)
!
9 ,
#
$
%
&
3
4
5
6
7
I
0
1
2
8
9
087
088
089
090
09 1
092
093
094
095
096
097
098
099
100
101
102
103
08 1
082
083
084
085
086
075
076
077
078
079
080
068
069
070
071
072
073
074 a b
C d e f
9
W
X
Y
Z
S
T
U
V
G
H
I
J
K
L
M
D
E
F
N
0
P
Q
R
1
A
[
\
-
385
Appendix B
F
6
e e
0 ii c e
,. a a a
Q
X
Y z
{
I
I
1
-
I
Character h
I j k
I m n
0
P q r
U v
W
S t
ASCII
Code
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
112
113
114
115
116
117
118
119
120
121
122
123
124
104
105
106
107
108
109
110
111
ASCII CHARACTER CODES
ASCII
Code
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
151
152
153
154
155
156
157
158
140
141
142
143
144
145
146
147
148
149
150
159
Character
I
I
A
R
E
E
R
A
0
0
6
U b
Y
0
U a
E
'f
Pt a
I
6
-
-
N o_ a i r
1
'/2
'/4
I
((
))
386
205
206
207
208
209
21 0
21 1
197
198
199
200
201
202
203
204
189
190
191
192
193
194
195
196
183
184
185
186
187
188
176
177
178
179
180
181
182
ASCII
Code
ASCII CHARACTER CODES
Character
ASCII
Code
1
-
+ t
It e ti=
11
=I
7
L
T
- i r
1:
=il
3
3
2
i%
I
I1
21 2
213
214
215 i
=I
21 7 i l n
-
220
21 8
219
J 4
221
216
222
223
224
I
233
234
235
236
237
238
239
240
225
226
227
228
229
230
23 1
232
4;
I
Y
=F
7
L
24 1
242
243
244
245
246
247
Character
Appendix
B
387
Appendix B
ASCII
Code
ASCII CHARACTER CODES
Character
ASCII
Code Character
248
249
250
251
0
0
0
If
252
253
254
255
'I
2 w
(blank 'FF')
388
Appendix
C
VIDE 0 DISPLAY
WORKSHEET
The following page contains a video display worksheet text screen’s coordinates. This map is provided of your t o help you quickly position the cursor cially useful for screen prints. You’ll find it espe- for creating visually pleasing, easy-to-follow screen menus. See the CSRLIN and POS function for information on re- turning the current cursor position. See the LOCATE statement and the TAB function for information on positioning the cursor.
389
390
Appendix
D
EXTENDED CODES
For certain keys and key combinations, INKEY$ returns
a
2- character code. The first character is
a
null character (ASCII
Code 00). The second is usually the scan code of the key(s) pressed. The key(s) and associated ASCII codes (in decimal) are listed below.
Second Key(s)
Character Pressed
15
16
17
18
19
20
21
22
23
24
25
30
31
32
33
34
35
36
37
38
49
50
59
60
61
62
63
44
45
46
47
48
Second Key($
Character Pressed
93
94
95
96
97
98
99
100
101
87
88
89
90
9 1
92
8 1
82
83
84
85
86
64
65
66
67
68
7 1
72
73
75
77
79
391
Appendix
D
Second Key(s)
Character Pressed
102
103
104
105
113
115
116
117
118
119
120
1 2 1
106
107
108
109
110
111
112
Second KeyW
Character Pressed
153
162
163
172
173
182
183
122
123
124
125
126
127
128
129
130
1 3 1
132
152
392
INDEX
Arrays 59-63, 117, 142, 152, 248
ASCII codes 98, 112, 302, 379-388
Assembly-language subroutines 351-356
BASIC
393
Index
BASIC (cont.)
Branching 172, 173, 175-176, 231
Buffer 8, 159-160, 215, 242, 298,
Buffer, communications 21, 208, 211, 244-247, 279,
Clear 117-118, 120, 223 memory 117-118, 223
Color 78-79, 121-122, 123, 250, 253, 255, 303
394
Index
Communications 151, 169, 208, 211, 244-247, 281
Communications buffer 21, 208, 211, 281
Communications trapping 124-125, 226-227
classifying 47-49 declaring 47-49
Converting precision 49-50 107, 113, 129
Coordinates 82 absolute 82 relative 82
physical 266, 267, 345-346 world 266, 267, 345-346
converting 49-50, 107, 113, 129, 221
double
221 integers 44, 113, 131, 163, 187, 221
single precision 45, 48, 49, 129, 221
strings 44, 49, 136, 185-186, 196, 197, 295
395
Index
see also Disk files
see also Disk files
Disk files
buffer 8, 159-160, 215, 242, 298
396
Index
Disk files (cont.)
Environment String Table 148, 149
397
Index
398
Files see Disk files
Formatting printing 214, 270-275
Function keys 190-193 assigning 190
Graphics 77-82, 114-116, 143-145, 199-201,
250-252, 253-254, 255-256, 279,
Input
disk 168, 181-182, 183-184, 203
keyboard 177, 179-180, 183-184, 202
Index
399
Index
Integers (cont.)
93, 189
Keyboard input 177, 179-180, 183-184, 202
Light pen
Loading
400
Memory image file 102-103, 104
Memory size 20, 117-118, 138, 167
Index
401
Index
Numbers
converting 49-50, 107, 113, 129, 221 double precision 45, 48, 49, 107, 131, 221
integers 44, 113, 131, 163, 187, 221
single precision 45, 48, 49, 129, 221
Operators 51-56 arithmetic 51-52
output
display 204, 270-271, 272-275, 279, 326, 347
402
Output (cont.)
Physical coordinates 266, 267, 345-346
Precision conversion 49-50, 107, 113, 129
Printer 205, 213, 214, 326, 342-344
Printing, formatted 214, 270-275
Program
line numbers 43, 289-290 lines 33, 289-290
Index
403
Index
404
SOUND St 91, 101, 117, 224, 264,
Index
405
Index
St
44, 46-47, 49, 136, 185-186, 196, 197, 295
Subroutines 105, 140, 172, 229, 294, 333
Text mode 80, 123, 303-304, 339
Trap p i n g
communication 124-125, 226-227
406
Index
Variables 47, 117, 198, 324, 335, 336,
declaring 47-49, 136 numeric 47, 136
Video memory 79-80, 117-118, 257
Video pages 79-80, 117-118, 257
World coordinates 266, 267, 345-346
407
دانلود
تبلیغات