BASIC 1.1 May82
BASIC 1.1 May82
--
- - -- -
---
-
-
-
-
---
---
Personal Computer
--_.-
--- - ---
-- Hardware Reference
Library
BASIC
Second Edition (May 1982)
Version 1.10
Changes are periodically made to the information herein; these
changes will be incorporated in new editions of this
publication.
iv-b
• The STRIG function for Advanced BASIC now
reads four joystick buttons. This is useful if you
happen to have four one-dimensional paddles.
iv-c
NOTES
iv-d
Preface
iii
How to Use This Manual
In order to use this manual, you should have some
knowledge of general programming concepts; we
are not trying to teach you how to program in this
manual.
iv
Syntax Diagrams
Each of the commands, statements, and functions
described in this book has its syntax described
according to the following conventions:
v
Related Publications
The following manuals contain related information
that you may find useful:
vi
Summary of Changes
vii
• The STRIG function in Advanced BASIC now
reads four joystick buttons. This is useful if you
have four one-dimensional paddles.
viii
CONTENTS
ix
Numeric Expressions and Operators ...... 3-21
Arithmetic Operators ................. 3-21
Relational Operators ................. 3-23
Logical Operators .................... 3-25
Numeric Functions ................... 3-29
Order of Execution ................... 3-29
String Expressions and Operators ........ 3-31
Concatenation.. .. .. .. .. .. .. .. .. .. .... 3-31
String Functions ...................... 3-32
Input and Output ........................ 3-33
Files ................................. 3-33
Using the Screen ..................... 3-38
Other 1/0 Features ................... 3-44
xi
LET Statement ........................... 4-139
LINE Statement ......................... 4-141
LINE INPUT Statement ................. 4-144
LINE INPUT # Statement ............... 4-145
LIST Command ......................... 4-147
LLIST Command ........................ 4-149
LOAD Command ....................... 4-150
LOC Function ........................... 4-153
LOCATE Statement ..................... 4-155
LOF Function ........................... 4-158
LOG Function .......................... 4-159
LPOS Function .......................... 4-160
LPRINT and LPRINT USING
Statements ............................ 4-161
LSET and RSET Statements ............. 4-163
MERGE Command ...................... 4-165
MID$ Function and Statement .......... 4-167
MKI$, MKS$, MKD$ Functions ......... 4-170
MOTOR Statement ..................... 4-172
NAME Command ....................... 4-173
NEW Command ........................ 4-174
OCT$ Function ......................... 4-175
ON COM(n) Statement .................. 4-176
ON ERROR Statement .................. 4-178
ON... GOSUB and ON... GOTO
Statements ............................ 4-180
ON KEY(n) Statement .................. 4-182
ON PEN Statement ..................... 4-185
ON STRIG(n) Statement ................ 4-187
OPEN Statement ........................ 4-189
OPEN "COM... Statement ............... 4-194
OPTION BASE Statement ............... 4-200
OUT Statement ......................... 4-201
PAINT Statement ....................... 4-203
PEEK Function ......................... 4-205
PEN Statement and Function ........... 4-206
PLAY Statement ........................ 4-209
POINT Function ........................ 4-213
POKE Statement ........................ 4-214
POS Function ........................... 4-215
PRINT Statement ....................... 4-216
PRINT USING Statement ............... 4-219
PRINT # and PRINT # USING
Statements ............................ 4-225
xii
PSET and PRESET Statements .......... 4-228
PUT Statement (Files) ................... 4-230
PUT Statement (Graphics) .............. 4-232
RANDOMIZE Statement ................ 4-236
READ Statement ........................ 4-238
REM Statement ......................... 4-240
RENUM Command ..................... 4-241
RESET Command ....................... 4-243
RESTORE Statement .................... 4-244
RESUME Statement ..................... 4-245
RETURN Statement .................... 4-247
RIGHT$ Function ...................... 4-248
RND Function .......................... 4-249
RUN Command ......................... 4-251
SAVE Command ........................ 4-253
SCREEN Function ...................... 4-255
SCREEN Statement ..................... 4-257
SGN Function .......................... 4-260
SIN Function ........................... 4-261
SOUND Statement ...................... 4-262
SPACE$ Function ....................... 4-265
SPC Function ........................... 4-266
SQR Function ........................... 4-267
STICK Function ........................ 4-268
STOP Statement ........................ 4-270
STR$ Function .......................... 4-272
STRIG Statement and Function ......... 4-273
STRIG(n) Statement .................... 4-275
STRING$ Function ..................... 4-276
SWAP Statement ........................ 4-277
SYSTEM Command ..................... 4-278
TAB Function ........................... 4-279
TAN Function .......................... 4-280
TIME$ Variable and Statement .......... 4-281
TRON and TROFF Commands .......... 4-283
USR Function ........................... 4-284
VAL Function ........................... 4-285
VARPTR Function ...................... 4-286
VARPTR$ Function ..................... 4-288
WAIT Statement ........................ 4-290
WHILE and WEND Statements ......... 4-292
WIDTH Statement ...................... 4-294
WRITE Statement ...................... 4-298
WRITE # Statement ..................... 4-299
xiii
APPENDIX A. MESSAGES ............... A-5
APPENDIX B. BASIC DISKETTE INPUT
AND OUTPUT ......................... B-1
Specifying Filenames. . . . . . . . . . . . . . . . . . . . . B-2
Commands for Program Files ............ B-2
Diskette Data Files - Sequential and
Random I/O ............................ B-4
Sequential Files ...................... B-4
Random Files ........................ B-8
Performance Hints ....................... B-15
APPENDIX C. MACHINELANGUAGE
SUBROUTINES . . . . . . . . . . . . . . . . . . . . . . . . . C-1
Setting Memory Aside for Your
Subroutines ............................ C-2
Getting the Subroutine Code into
Memory ................................ C-3
Poking a Subroutine into Memory .... C-4
Loading the Subroutine from a File ... C-5
Calling the Subroutine from Your
BASIC Program......................... C-8
Common Features of CALL and USR ... C-8
CALL Statement ..................... C-10
USR Function Calls .................. C-14
xiv
APPENDIX E. MATHEMATICAL
FUNCTIONS ........................... E-1
APPENDIX H. HEXADECIMAL
CONVERSION TABLE................. H-1
xv
NOTES
xvi
CHAPTER 1. THE VERSIONS OF
BASIC
Contents
The Versions of BASIC ................... 1-3
1-1
NOTES
1-2
The Versions of BASIC
• Cassette
• Disk
• Advanced
The three versions of BASIC are upward compatible;
that is, Disk BASIC does everything Cassette BASIC
does, plus a little more, and Advanced BASIC does
everything Disk BASIC does, plus a little more. The
differences between the versions are discussed in
more detail below.
The BASIC commands, statements, and functions
for all three versions of the BASIC interpreter are
described in detail in "Chapter4. BASIC Commands,
Statements, Functions, and Variables." Included in
each description is a section called Versions:, where
we tell you which versions of BASIC support the
command, statement, or function.
For example, if you look under "CHAIN Statement"
in Chapter 4, you will note that it says:
1-4
Disk BASIC
1-5
Advanced BASIC
1-6
CHAPTER 2. HOWTOSTARTAND
USE BASIC
Contents
Getting BASIC Started ................... 2-3
Options on the BASIC Command. . . . . . 2-4
2-1
NOTES
2-2
Getting BASIC Started
2-3
Options on the BASIC Command
You can include options on the BASIC or BASICA
command when you start Disk or Advanced BASIC.
These options specify the amount of storage BASIC
uses to hold programs and data, and for buffer areas.
You can also ask BASIC to immediately load and run
a program.
2-4
IS:bsize sets the buffer size for use with random files.
The record length parameter on the OPEN
statement may not exceed this value. The default
buffer size is 128 bytes. The maximum value you
may enter is 32767. We suggest you use /S:512 for
improved performance when using random files.
2-5
Some examples of using the BASIC command:
BASIC PAYROLL.BAS
BASICA INVEN/F:6
BASIC /M:32768
BASICA B:CHKWRR.TST/F:2/M:&H9000
This command sets the maximum workspace
size to hex 9000. This means Advanced BASIC
will be able to use up to 36K-bytes of memory.
Also, file control blocks are set up for two files,
and the program CHKWRR. TST on the
diskette in drive B is loaded and executed.
2-6
Modes of Operation
Indirect Mode •
You enter programs using indirect mode. To tell
BASIC the line you are ent:ering is part of a program,
you begin the line with a line number. The line is then
stored as part of the program in memory. The
program in storage can be executed by entering the
RUN command. For example:
Ok
1 PRINT 20+2
RUN
22
Ok
Direct Mode
Direct mode means you are telling BASIC to perform
your request immediately after the request is
entered. You tell BASIC to do this by not preceding
the statement or command with a line number. You
can display results of arithmetic and logical
operations immediately or store them for later use,
but the instructions themselves are not saved after
they are executed. This mode is useful for debugging
as well as for quick computations that do not require
a complete program. For example:
Ok
PRINT 20+2
22
Ok
2-7
~
t-.)
I
ex>
t:T
(D
~
(D
&
o
~
I-t
p..
------
Function
keys
Typewriter keyboard
- - -
Numeric
keypad
~
The keyboard is divided into three general areas:
Function Keys
Function
Keys
The function keys can be used:
Shift Keys
2-10
·W
,"·";'
L', ."
».1,
~,l~~~~~~~~~'~~"ri·"~
,
~~--~--~-~----~~~-~--~
Special Symbols:
'"
m
F9 FlO'
~~------------~~=-~--~
2-11
Uppercase:
' >2
rn
'F9' FlO:
~~-------------
Backspace:
2-13
The Alt key enables easy entry of BASIC statement
keywords. This key allows you to type an entire
BASIC keyword with a single keystroke.
A AUTO N NEXT
B BSAVE 0 OPEN
C COLOR P PRINT
D DELETE Q (no word)
E ELSE R RUN
F FOR S SCREEN
G GOTO T THEN
H HEX$ U USING
I INPUT V VAL
J (no word) W WIDTH
K KEY X XOR
L LOCATE Y (no word)
M MOTOR Z (no word)
The Alt key is also used with the keys on the numeric
keypad to enter characters not found on the keys.
This is done by holding down the Alt key and typing
the three-digit ASCII code for the character. (See
"Appendix G. ASCII Character Codes" for a
complete list of ASCII codes.)'
2-14
The Ctrl key is also used to enter certain codes and
characters not otherwise available from the
keyboard.
You also use the Ctrl key together with other keys
when you edit programs with the program editor.
Numeric Keypad
Numeric Keypad
2-15
Usually you will be using the numeric keypad keys
for their functions with the program editor. These
keys allow you to move the cursor up, down, right,
and left. You can insert and delete characters using
these keys. Refer to the following section, "The
BASIC Program Editor," for complete information.
m
"J":
,,:
.".
~~------------~--~~--~
Keypad Shift: You can use the Num Lock key to set
the numeric keypad so it works more like a
calculator keypad. Pressing the Num Lock key shifts
the numeric keypad into its own uppershift mode, so
that you get the numbers 0 through 9 and the
decimal point, as indicated on the key tops. Pressing
Num Lock again will return the keypad to its normal
cursor control mode. As with Caps Lock, you can
temporarily reverse the Num Lock state by pressing
one of the Shift Keys.
2-16
Special Key Combinations
You should be aware of the special functions of the
following combinations of keys:
Ctrl-Break
Ctrl-Num Lock
2-18
The BASIC Program Editor
Any line of text typed while BASIC is at command
level is processed by the BASIC program editor. The
program editor is a "screen line editor." That is, you
can change a line anywhere on the screen, but you
can only change one line at a time. The change will
only take effect if you press Enter on that line.
"
Use of the program editor can save a lot of time
during program development. To become familiar
with its features, we suggest you enter a sample
program and practice all the editing capabilities. The
best way for you to get a "feel" for the editing
process is to try editing a few lines while studying the
information that follows.
2-19
Key(s) Function
Home
Ctrl-Home
2-20
Key(s) Function
t
(Cursor Up)
(Cursor Down)
2-21
Key(s) Function
(Cursor Left)
(Cursor Right)
2-22
Key(s) Function
Ctrl- ~
(Next Word)
2-23
Key(s) Function
Ctrl- ~
(Previous Word)
LI NE (L 1, LOW2) - (MAX,48) ,3 , BF
LINE (Ll,LOW2)-(MAX,48) ,3 , !F
2-24
Key(s) Function
End
Ctrl-End
2-25
Key(s) Function
Ins
2-26
Key(s) Function
Del
2-27
Key(s) Function
(Backspace)
Esc
2-28
Key(s) Function
Ctrl-Break
2-29
Key(s) Function
(Tab)
10 REM th is is a rema rk
2-30
Key(s) Function
( continued)
10 REM th is a remark
2-31
How to Make Corrections on the Current
Line
Since any line of text typed while BASIC is at
command level is processed by the program editor,
you can use any of the keys described in the previous
section under" Special Program Editor Keys."
BASIC is always at command level after the prompt
Ok and until a RUN command is given.
2-32
For example, suppose we have typed the following:
LOAD "V; PROG_
DEELETE
DEELETE
2-33
Then we press the Del key:
DELETE
DELETE
DELETE 20
DELETT
DELET
DELETE 20
2-34
For example, suppose we've typed the following:
LIS 10
LI S 10
10 REM -;'(";,(";"
2-35
Entering or Changing a BASIC Program
Any line of text that you type that begins with a
number is considered to be a prografl! line.
Warning:
If your line reaches maximum length, the 255th
character must be Enter.
2-36
For example, if you enter the following:
10 he 1 10 Do r i
10
2-37
Or you may use the DELETE command to delete a
group of program lines. Refer to "DELETE
Command" in Chapter 4 for details.
2-38
For example, you could duplicate a line in the
program this way: Move the cursor to the line to be
duplicated. Change the line number to the new line
number by just typing over the numbers. When you
press Enter, both the old line and the new line will be
in the program.
2-39
Syn tax Errors
When a syntax error is discovered while a program is
running, BASIC automatically displays the line that
caused the error so you may correct it. For example:
Ok
10 A = 2$12
RUN
Syntax error in 10
Ok
10 A = 2$12
• All variables and arrays are lost. That is, they are
reset to zero or null.
2-40
CHAPTER 3. GENERAL
INFORMATION
ABOUT
PROGRAMMING IN
BASIC
Contents
Line Format .............................. 3-3
Line Numbers ................... 3- 3
BASIC Statements ............... 3- 3
Comments ....................... 3-4
3-1
String Expressions and Operators ........ 3-31
Concatenation ........................ 3-31
String Functions ...................... 3-32
3-2
Line Format
3-3
You may, if you wish, have more than one BASIC
statement on a line, but each statement on a line
must be separated from the last one by a colon, and
the total number of characters must not exceed255.
For example:
Ok
10 FOR 1=1 TO 5: PRINT I: NEXT
RUN
1
2
3
4
5
Ok
Character Set
3-4
The following special characters have specific
meanings in BASIC:
Character Name
blank
equal sign or assignment symbol
+ plus sign or concatenation symbol
minus sign
* asterisk or multiplication symbol
/ slash or division symbol
\ backslash or integer division symbol
caret or exponentiation symbol
( left parenthesis
) right parenthesis
% percent sign or integer type declaration
character
# number (or pound) sign, or
double-precision type declaration
character
$ dollar sign or string type declaration
character
exclamation point or single-precision
type declaration character
& ampersand
comma
period or decimal point
single quotation mark (apostrophe), or
remark delimiter
semicolon
colon or statement separator
? question mark (PRINT abbreviation)
< less than
> grea ter than
" double quotation mark or string
delimiter
underline
3-5
Reserved Words
ABS CVD
AND CVI
ASC CVS
ATN DATA
AUTO DATE$
BEEP DEF
BLOAD DEFDBL
BSAVE DEFINT
CALL DEFSNG
CDBL DEFSTR
CHAIN DELETE
CHR$ DIM
CINT DRAW
CIRCLE ED IT
CLEAR ELSE
CLOSE END
CLS EOF
COLOR EQV
COM ERASE
COMMON ERL
CONT ERR
COS ERROR
CSNG EXP
CSRLIN FIELD
3-6
FILES NOT
FIX OCT$
FNxxxxxxxx OFF
FOR ON
FRE OPEN
GET OPTION
GOSUB OR
GOTO OUT
HEX$ PAINT
IF PEEK
IMP PEN
I NKEY$ PLAY
INP POINT
INPUT POKE
INPUT# pas
INPUT$ PRESET
INSTR PRINT
INT PRINT#
KEY PSET
KILL PUT
LEFT$ RANDOMIZE
LEN READ
LET REM
LINE RENUM
LIST RESET
LLIST RESTORE·
LOAD RESUME
LaC RETURN
LOCATE RIGHT$
LOF RND
LOG RSET
LPOS RUN
LPRINT SAVE
LSET SCREEN
MERGE SGN
MID$ SIN
MKD$ SQUND
MKI $ SPACES
MKS$ SPC(
MOD SQR
MOTOR STEP
NAME STI CK
NEW STOP
NEXT STR$
3-7
STRIG USR
STRING$ VAL
SWAP VARPTR
SYSTEM VARPTR$
TAB( WAIT
TAN WEND
THEN WHILE
TIME$ WIDTH
TO WRITE
TROFF WRITE#
TRON XOR
USING
3-8
Constants
"HELL0 11
li$25,000.0011
"Number of Employees"
3-9
The E (or D) means "times ten to
the power of."
For example,
23E-2
235.988E-7
is equivalent to: .0000235988
2359D6
is equivalent to: 2359000000
&H76
&H32F
Octal Octal numbers with up to 6
digits, with the prefix &0 or just
&. Octal digits are 0 through 7.
Examples:
&0349
&1234
3-10
Numeric Precision
Numbers may be stored internally as either integer,
single-precision, or double-precision numbers.
Constants entered in integer, hex, or octal format
are stored in two bytes of memory and are
interpreted as integers or whole numbers. With
double-precision, the numbers are stored with 17
digits of precision and printed with up to 16 digits.
With single-precision, seven digits are stored and up
to seven digits are printed, although only six digits
will be accurate.
Single-Precision Double-Precision
46.8 345692811
-1.09E-06 -1.09432D-06
3489.0 3489.0#
22.5! 7654321.1234
3-11
Variables
3-12
A variable name may not be a reserved word, but
may contain imbedded reserved words. (Refer to
"Reserved Words," earlier in this chapter, for a
complete list of the reserved words.) Also, a variable
name may not be a reserved word with one of the
type declaration characters ($, %, !, #) at the end. For
example,
10 EXP = 5
is invalid, because EXP is a reserved word. However,
10 EXPONENT = 5
is okay, because EXP is only a part of the variable
name.
A$ = "SALES REPORT"
The dollar sign is a variable type declaration
character. It "declares" that the variable will
represent a string.
3-13
reasons you might want to declare a variable as a
particular precision.
3-14
Arrays
An array is a group or table of values that are referred
to with one name. Each individual value in the array
is called an element. Array elements are variables and
can be used in expressions and in any BASIC
statement or function which uses variables.
10 DIM B$(S)
20 DIM A (2,3)
3-15
To continue the preceding examples, array B$ could
be thought of as a list of character strings, like this:
B$(O)
B$( 1)
B$(2)
B$(3)
B$(4)
B$(5)
columns
50 SIS(3)=500
3-16
and the array SIS has not already been defined, the
array is set to a one-dimensional array with 11
elements, numbered SIS(O) through SIS(10). You
may only use this method of implicit declaration for
one-dimensional arrays.
Ok
10 DIM YEARS(3,4)
20 YEARS(2,3)=1982
30 FOR ROW=0 TO 3
40 FOR COLUMN=0 TO 4
50 PRINT YEARS(ROW,COLUMN);
60 NEXT COLUMN
70 PRINT
80 NEXT ROW
RUN
o 0 0 0 0
o 0 0 0 0
o 0 0 1982 0
00000
Ok
3-17
How BASIC Converts Numbers from
One Precision to Another
Ok
10 A% = 23.42
20 PRINT A%
RUN
23
Ok
Example:
Ok
10 C = 55.8834567#
20 PRINT C
RUN
55.88346
Ok
3-18
3. If you convert from a lower precision to a higher
precision number, the resulting higher
precision number cannot be any more accurate
than the lower precision number. For example,
if you assign a single-precision value (A) to a
double-precision variable (B#), only the first six
digits of B# will be accurate because only six
digits of accuracy were supplied with A. The
error can be bounded using the following
formula:
Example:
Ok
10 A = 2.04
20 B# = A
30 PRINT A;B#
RUN
2.04 2.039999961853027
Ok
3-19
4. When an expression is evaluated, all of the
operands in an arithmetic or relational
operation are converted to the same degree of
precision, namely that of the most precise
operand. Also, the result of an arithmetic
operation is returned to this degree of
precision.
Examples:
Ok
10 D# = 6#/7
20 PRINTD#
RUN
.8571428571428571
Ok
Ok
10 D = 6#/7
20 PRINT D
RUN
.8571429
Ok
The arithmetic was performed in
double-precision and the result was returned to
D (single-precision variable), rounded, and
printed as a single-precision value.
3-20
Numeric Expressions and Operators
• Ari thmetic
• Relational
• Logical
• Functions
Arithmetic Operators
The arithmetic operators perform the usual
operations of arithmetic, such as addition and
subtraction. In order of precedence, they are:
Exponentiation X'" Y
Negation -X
*, / Multiplication, X*y
Floating Point X/Y
Division
+, - Addition, X+Y
Subtraction X-Y
3-21
(If you have a mathematical background, you will
notice that this is the standard order of precedence.)
Although most of these operations probably look
familiar to you, two of them may seem a bit
unfamiliar - integer division and modulo
arithmetic.
For example:
Ok
10 A = 10\4
20 B = 25.68\6.99
30 PRINT A;B
RUN
2 3
Ok
For example:
Ok
10 A = 7 MOD 4
20 PRINT A
RUN
3
Ok
This result occurs because 7/4 is 1, with remainder 3.
Ok
PRINT 25.68 MOD 6.99
5
Ok
Equality X=Y
<> or >< Inequality X<>Y
x><y
< Less than x<y
> Grea ter than x>y
<= or =< Less than or x<=y
equal to x=<y
>= or => Greater than or x>=y
equal to x=>y
(The equal sign is also used to assign a value to a
variable. See "LET Statement" in Chapter 4.)
3-23
More examples:
Ok
10 x=100
20 I F X <> 200 THEN PR I NT IINOT EQUALI'
ELSE PRINT IIEQUAL 11
RUN
NOT EQUAL
Ok
3-24
Leading and trailing blanks are significant. For
example, all the following relational expressions are
true (that is, the result of the relational operation is
-1 ).
I IW R I I > "W RI I
Logical Operators
Logical opera tors perform logical, or Boolean,
operations on numeric values. Just as the relational
operators are usually used to make decisions
regarding program flow, logical operators are
usually used to connect two or more relations and
return a true or false value to be used in a decision
(see "IF Statement" in Chapter 4).
3-25
(disjunction), XOR (exclusive or), IMP
(implication), and EQV (equivalence). Each
operator returns results as indicated in the following
tahI e. ("T'" in d'icates a true, or non-zero va Iue. "F"
indicates a false, or zero value.) The operators are
listed in order of precedence.
NOT
X NOT X
T F
F T
AND
X Y XANDY
T T T
T F F
F T F
F F F
OR
X Y XORY
T T T
T F T
F T T
F F F
XOR
X Y XXORY
T T F
T F T
F T T
F F F
EQV
X Y XEQVY
T T T
T F F
F T F
F F T
IMP
X Y XIMPY
T T T
T F F
F T T
F F T
3-26
Some examples of ways to use logical operators in
decisions:
3-27
Thus, you can use logical operators to test for a
particular bit pattern. For instance, the AND
operator may be used to "mask" all but one of the
bits of a status byte at a machine I/O port.
A = 63 AND 16
B = -1 AND 8
C = 4 OR 2
x= 2
TWOSCOMP = (NOT X) + 1
3-28
Numeric Functions
A function is used like a variable in an expression to
call a predetermined operation that is to be
performed on one or more operands. BASIC has
"built-in" functions that reside in the system, such as
SQR (square root) or SIN (sine). All of BASIC's
built-in functions are listed under "Functions and
Variables" in the beginning of Chapter 4. Detailed
descriptions are also included in the alphabetical
section of Chapter 4.
Order of Execution
The categories of numeric operations were
discussed in their order of precedence, and the
precedence of each operation within a category was
indicated in the discussion of the category. In
summary:
a.
b. unary -
c. *, /
d. \
e. MOD
f. +, -
3. Relational operations are done next
3-29
4. Logical operations are done last, in this order:
a. NOT
b. AND
c. OR
d. XOR
e. EQV
f. IMP
X+2Y X+Y*2
X-Y X-y/z
Z
XY X*y/z
Z
X+Y (X+Y)/z
Z
X(-Y) X*(-Y)
3-30
String Expressions and Operators
• Concatenation
• Functions
Concatenation
] oining two strings together is called concatenation.
Strings are concatenated using the plus symbol (+).
For example:
Ok
1.0 COMPANY$ = "IBM"
2.0 TYPE$ = II Personal l l
3.0 FULLNAM E$ = TYPE$ + I I Co mput e r I I
4.0 PRINT COMPANY$+FULLNAME$
RUN
IBM Personal Computer
Ok
3-31
String Functions
A string function is like a numeric function except
that it returns a string result. A string function can be
used in an expression to call a predetermined
operation that is to be performed on one or more
operands. BASIC has "built-in" functions that reside
in the system, such as MID$, which returns a string
from the middle of another string, or CHR$, which
returns the character with the specified ASCII code.
All of BASIC's built-in functions are listed under
"Functions and Variables" in the beginning of
Chapter 4. Detailed descriptions are also included in
the alphabetical section of Chapter 4.
3-32
Input and Output
The remainder of this chapter contains information
on input and output (1/0) in BASIC. The following
topics are addressed:
Files
A file is a collection of information which is kept
somewhere other than in the random access memory
of the IBM Personal Computer. For example, your
information may be stored in a file on diskette or
cassette. In order to use the information, you must
open the file to tell BASIC where the information is.
Then you may use the file for input andlor output.
3-33
Naming Files
The physical file is described by itsfile specification, or
filespec for short.
The file specification is a string expression of the
form:
device:filename
3-34
Device N arne Chart
COMMUNICATIONS DEVICES
STORAGE DEVICES
3-35
Filename: The filename must conform to the
following rules.
name. extension
A through Z
o through 9
< > -() }
@ # $ % A &
\
3-36
Some examples of filenames for Disk and Advanced
BASIC are:
27HAL.OAO
VOL
PROGRAM1 . BAS
$ $@ ( !) . 123
3-37
Using the Screen
BASIC can display text, special characters, points,
lines, or more complex shapes in color or in black
and white. How much of this you can do depends on
which display adapter you have in your IBM Personal
Computer.
Display Adapters
The IBM Personal Computer has two display
adapters: the IBM Monochrome Display and Parallel
Printer Adapter, and the Color! Graphics Monitor
Adapter.
3-38
Text Mode
The screen can be pictured like this:
Character
position 1, 1
r--,-------
9 I
-----,
L_..J I
I I
I
I
I
I I
L _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ -.-1
Border
screen
3-39
Line 25 is usually used for "soft key" display (see
"KEY Statement" in Chapter 4), but it is possible to
write over this area of the screen if you turn the "soft
key" display off. The 25 th line is never scrolled by
BASIC.
0 Black 8 Gray
1 Blue 9 Light Blue
2 Green 10 Light Green
3 Cyan 11 Light Cyan
4 Red 12 Light Red
5 Magenta 13 Light Magenta
6 Brown 14 Yellow
7 White 15 High-intensity White
3-40
The statements you can use to display information in
text mode are:
CLS SCREEN
COLOR WI DTH
LOCATE WRITE
PRI NT
Graphics Modes
The graphics modes are available only if you have the
Color/Graphics Monitor Adapter.
3-41
The statements used for graphics in BASIC are:
CIRCLE PAl NT
COLOR PRESET
DRAW PSET
GET PUT
LINE SCREEN
POINT
3-42
High Resolution: In high resolution there are 640
horizontal points and 20,0 vertical points. As in
medium resolution, these points are numbered
starting with zero so that the lower right corner
point is (639,199).
STEP (xoffset,yoffset)
3-43
"Chapter 4. BASIC Commands, Statements,
Functions, and Variables," we will indicate what
each statement sets as the last point referenced.
3-44
Light Pen
BASIC has the following statements and functions to
allow input from a light pen.
Joysticks
Joysticks can be useful in an interactive
environment. BASIC supports two 2-dimensional
(x and y coordinate) joysticks, or four
one-dimensional paddles, each of which has a
button. (Four buttons are supported only in
Advanced BASIC.) The following statements and
functions are used for joysticks:
3-45
NOTES
3-46
CHAPTER 4. BASIC COMMANDS,
STATEMENTS,
FUNCTIONS, AND
VARIABLES
Contents
How to Use This Chapter. . . . . . . . . . . . . . . . . 4-3
A ......................................... 4-23
B ......................................... 4-28
C ......................................... 4-34
D ........................................ 4-64
E ......................................... 4-84
F ......................................... 4-94
4-1
G ....................................... 4-106
H ....................................... 4-115
I ........................................ 4-116
K ....................................... 4-131
L ........................................ 4-137
M ....................................... 4-165
N ....................................... 4-1 73
o ....................................... 4-1 75
P ........................................ 4-203
R ....................................... 4-236
S ........................................ 4-253
T ........................................ 4-279
U ....................................... 4-284
V ....................................... 4-285
W ....................................... 4-290
4-2
How to Use This Chapter
4-3
can use the CHAIN statement for programs written
in the Disk and Advanced versions of BASIC.
4-5
Commands
The following is a list of all the commands used in
BASIC. The syntax of each command is shown, but
not always in its entirety. You can find detailed
information about each command in the
alphabetical part of this chapter. You may also want
to check the next section in this chapter,
"Statements," for a list of the BASIC statements.
Command Action
BLOAD filespec,offset
Loads binary data (such as a
machine language program)
into memory.
4-6
Command Action
LIST linel-line2,filespec
Lists program lines on the
screen or to the specified file.
RENUM newnum,oldnum,increment
Renumbers program lines.
4-7
Statements
This section lists all the BASIC statements
alphabetically in two categories: I/O (Input/Output)
Statements and Non-I/O Statements. The list tells
what each statement does and shows the syntax. For
the more complex statements the syntax shown may
not be complete. You can find detailed information
about each statement in the alphabetical portion of
this chapter, later on.
Non-I/O Statements
Statement Action
COM(n) ON/OFF/STOP
Enables and disables trapping
of communications activity.
4-8
Statement Action
4-9
Statement Action
KEY(n) ON/OFF/STOP
Enables/disables trapping of
function keys or cursor
control keys.
LET variable=expression
Assigns the value of the
expression to the variable.
4-10
Statement Action
RESUME line/NEXT/O
Returns from error trap
routine.
4-11
Statement Action
STRIG(n) ON/OFF/STOP
Enables/ disables joystick
button trapping.
4-12
I/O Statements
Statement Action
GET (x1,yl)-(x2,y2),arrayname
Reads graphic information
from screen.
4-13
Statement Action
4-14
Statement Action
OPEN mode,#f,filespec,recl
Alternative fornl of preceding
OPEN.
OPEN "COMn:options" AS #f
Opens file for
communications.
PAINT (x,y),paint,boundary
Fills in an area on the screen
defined by boundary with the
paint color.
4-15
Statement Action
PUT (x,y),array,action
Writes graphic information
to the screen.
SOUND freq,duration
Generates sound through the
speaker.
4-16
Functions and Variables
The built-in functions and variables available in
BASIC are listed below, grouped into two general
categories: numeric functions, or those which
return a numeric result; and string functions, or
those which return a string result.
Each category is further subdivided according to the
usage of the functions. The numeric functions are
divided into general arithmetic (or algebraic)
functions; string-related functions, which operate
on strings; and input/output and miscellaneous
functions. The string functions are separated into
general string functions, and input/ output and
miscellaneous string functions.
Note: Only integer and single-precision
results are returned by the numeric functions,
except where indicated otherwise.
Function Result
STRING-RELATED
Function Result
Function Result
4-19
Function Result
4-20
String Functions (return a string value)
GENERAL
Function Result
4-21
Function Result
4-22
ABS
Function
Format: v = ABS(x)
Example: Ok
PRINT ABS(7*(-5))
35
Ok
4-23
Ase
Function
Purpose: Returns the ASCII code for the first character of the
string xl.
Format: v = ASC(x$)
Example: Ok
10 X$ = IITEST"
20 PRINT ASC(X$)
RUN
84
Ok
4-24
ATN
Function
Format: v = ATN(x)
Example: Ok
PRI NT ATN (3)
1.249046
Ok
10 PI=3.141593
20 RADIANS=ATN(l)
30 DEGREES=RADIANS*180/PI
40 PRINT RADIANS,DEGREES
RUN
.7853983 45
Ok
The first example shows the use of the ATN function
to calculate the arctangent of3. The second example
finds the angle whose tangent is 1. It is .7853983
radians, or 45 degrees.
4-25
AUTO
Command
4-26
AUTO
Command
If AUTO generates a line number that already exists
in the program, an asterisk (*) is printed after the
number to warn you that any input will replace the
existing line. However, if you press Enter
immediately after the asterisk, the existing line will
not be replaced and AUTO will generate the next
line number.
Example: AUTO
AUTO 100,50
AUTO 500,
AUTO ,20
4-27
BEEP
Statement
Format: BEEP
4-28
BLOAD
Command
Purpose: Loads a memory image file into memory.
4-29
BLOAD
Command
BLOAD and BSAVE are useful for loading and
saving machine language programs. (You may
perform machine language programs from within a
BASIC program by using the CALL statement.)
However, BLOAD and BSAVE are not restricted to
machine language programs. Any segment may be
specified as the target or source for these statements
via the DEF SEG statement. You have a useful way of
saving and displaying screen images: save from or
load to the screen buffer.
Warning:
BASIC does not do any checking on the address.
That is, it is possible to BLOAD anywhere in
memory. You should not BLOAD over BASIC's
stack, BASIC's variable area, or your BASIC
program.
4-31
BSAVE
Command
Purpose: Saves portions of the computer's memory on the
specified device.
4-32
BSAVE
Command
BLOAD and BSAVE are useful for loading and
saving machine language programs (which can be
called using the CALL statement). However,
BLOAD and BSAVE are not restricted to machine
language programs. By using the DEF SEG
statement, any segment may be specified as the
target or source for these statements. For example,
you can save an image of the screen by doing a
BSAVE of the screen buffer.
4-33
CALL
Statement
4-34
CDBL
Function
Format: v = CDBL(x)
Example: Ok
10 A = 454.67
20 PRINT A;CDBL(A)
RUN
454.67 454.6699829101563
Ok
4-35
CHAIN
Statement
CHAIN "A:PROG1"
CHAIN IA:PROG1",1000
line (1000 in this example) is not affected by a
RENUM command. If PROG 1 is renumbered, this
example CHAIN statement should be changed to
point to the new line number.
CHAIN "A:PROG1",1000,ALL
4-36
CHAIN
Statement
MERGE brings a section of code into the BASIC
program as an overlay. That is, a MERGE operation
is performed with the chaining program and the
chained-to program. The chained-to program must
be an ASCII file if it is to be merged. Example:
Notes:
4-37
CHR$
Function
Purpose: Converts an ASCII code to its character equivalent.
Format: v$ = CHR$(n)
Example: Ok
PRINT CHR$(66)
B
Ok
Ok
KEY 1, "AUTO " +CHR$ (13)
Ok
4-38
CHR$
Function
The following example is a program which shows all
the displayable characters, along with their ASCII
codes, on the screen in 80-column width. it can be
used with either the IBM Monochrome Display and
Parallel Printer Adapter or the Color/Graphics
Monitor Adapter.
10 CLS
20 FOR 1=1 TO 255
30 I ignore nondisplayable characters
40 IF (1)6 AND 1<14) OR (1)27 AND 1<32) THEN 100
50 COLOR 0,7 I black on white
60 PRINT USING 11###11; I ; J 3-digit ASCII code
70 COLOR 7,0 I white on black
80 PRI~T 11 "; CHR$(I); " ";
90 IF POS(0»75 THEN PRINT I go to next 1 ine
100 NEXT I
4-39
CINT
Function
Format: v = CINT(x)
Example: Ok
PRINT CINT(45.67)
46
Ok
PRINT CINT(-2.89)
-3
Ok
4-40
CIRCLE
Statement
4-41
CIRCLE
Statement
start and end specify where the drawing of the ellipse
will begin and end. The angles are positioned in the
standard mathematical way, with 0 to the right and
going counterclockwise:
PI/2
P100,2*PI
3*PI/2
10 PI=3. 141593
20 SCREEN 1
30 CI RCLE "(160,100) ,60 ,,-PI ,-PI/2
4-42
CIRCLE
Statement
Ifaspect is less than one, thenr is the x-radius. That is,
the radius is measured in points in the horizontal
direction. If aspect is greater than one, then r is the
y-radius. For example,
10 SCREEN 1
20 CI RCLE (1 60, 100) ,60, , , , 5/ 18
4-44
CLEAR
Command
Executing a CLEAR command turns off any sound
that is running and resets to Music Foreground.
Also, PEN and STRIG are reset to OFF.
CLEAR
CLEAR ,32768
The next example clears the data and sets the size of
the stack to 2000 bytes:
CLEAR ,,2000
CLEAR ,32768,2000
4-45
CLOSE
Statement
4-46
CLOSE
Statement
Example: 100 CLOSE 1,#2,#3
200 CLOSE
4-47
CLS
Statement
Format: CLS
4-48
COLOR
Statement
Foreground- 1 of 16 colors
Character blink, if desired
Background- 1 of 8 colors
Border- 1 of 16 colors
Background- 1 of 16 colors
Palette- 1 of 2 palettes with 3 colors each
The border is the same as the background color.
4-49
COLOR
Statement (Text)
Remarks: foreground is a numeric expression in the range 0 to
31, representing the character color.
o Black 8 Gray
1 Blue 9 Light Blue
2 Green 10 Light Green
3 Cyan 11 Light Cyan
4 Red 12 Light Red
5 Magenta 13 Light Magenta
6 Brown 14 Yellow
7 -White 15 High-intensity White
4-50
COLOR
Statement (Text)
If you have the IBM Monochrome Display and
Parallel Printer Adapter, the following values can
be used for foreground:
°
1
Black
Underlined character with white foreground
2-7 White
In a manner similar to the Color/Graphics Monitor
Adapter, adding 8 to the number of the desired color
gives you the color in high-intensity. For example, a
value of 15 gives you high-intensity white. A value of
9 gives you high-intensity white, underlined. You
can't make high-intensity black.
As with the Color/Graphics Monitor Adapter, you
can make the character blink by adding 16 to the
number of the desired color. Thus, 16 gives you
black blinking characters, and 31 gives you
high-intensity white blinking characters.
For background with the IBM Monochrome Display
and Parallel Printer Adapter, you may select the
following values:
0-6 Black
7 White
Note: White (color 7) as a background color
shows up as white on the IBM Monochrome
Display only when it is used with a foreground
color of 0, 8, 16, or 24 (black). This creates
reverse image characters.
Black (color 0, 8, 16, or 24) as a foreground
color shows up as black only when used with a
°
background color of (which makes the
characters invisible) or 7 (which creates reverse
image characters).
Other combinations of foreground and
background colors produce standard (white on
black) results on the IBM Monochrome
Display.
4-51
COLOR
Statement (Text)
Notes for either adapter:
COLOR ,7,
is invalid.
4-52
COLOR
Statement (Text)
The following example can be used with either the
Color/Graphics Monitor Adapter or the IBM
Monochrome Display and Parallel Printer Adapter:
4-53
COLOR
Statement (Graphics)
The COLOR Statement in Graphics Mode
Versions: Cassette Disk Advanced Compiler
*** *** *** ***
Graphics mode, medium resolution only.
4-54
COLOR
Statement (Graphics)
Any parameter may be omitted from the COLOR
statement. Omitted parameters assume the old
value.
Example: 5 SCREEN 1
10 COLOR 9,0
4-55
COM(n)
Statement
Format: COM(n) ON
COM(n) OFF
COM(n) STOP
4-56
COMMON
Statement
4-57
CONT
Command
Purpose: Resumes program execution after a break.
Format: CONT
4-58
CONT
Command
Example: In the following example, we create a long loop.
Ok
10 FOR A=1 TO 50
20 PRINT A;
30 NEXT A
RUN
1 2 3 4 5 6 7 8 9 10 11 12
13 14 15 16 17 18 19 20 21 22
23 24 25 26 27 28 29
(At this point we interrupt the loop by pressing
Ctrl-Break.)
•
•
•
Break in 20
Ok
CONT
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50
Ok
4-59
COS
Function
Purpose: Returns the trigonometric cosine function.
Format: v = COS(x)
Example: Ok
10 PI=3. 141593
20 PRINT cOS(PI)
30 DEGREES=180
40 RADIANS=DEGREES*PI/180
50 PRINT COS (RADIANS)
RUN
-1
-1
Ok
4-60
CSNG
Function
Purpose: Converts x to a single-precision number.
Format: v = CSNG(x)
Example: Ok
10 A# = 975.3421222#
20 PRINT A#; CSNG(A#)
RUN
975.3421222 975.3421
Ok
4-61
CSRLIN
Variable
Purpose: Returns the vertical coordihate of the cursor.
Format: v = CSRLIN
4-62
CVI, CVS, CVD
Functions
Purpose: Converts string variable types to numeric variable
types.
v = CVS(4-byte string)
v = CVD(8-byte string)
4-63
DATA
Statement
4-64
DATA
Statement
The variable type (numeric or string) given in the
READ statement must agree with the corresponding
constant in the DATA statement or a "Syntax error"
occurs.
4-65
DATE$
Variable and Statement
Format: As a variable:
v$ = DATE$
As a statement:
DATE$ = x$
mm-dd-yy
mm/dd/yy
mm-dd-yyyy
mm/dd/yyyy
4-66
DATE$
Variable and Statement
Example: Ok
10 DATE$= 118/29/8211
20 PRINT DATE$
RUN
08-29-1982
Ok
4-67
DEFFN
Statement
4-68
DEFFN
Statement
The function type determines whether the function
returns a numeric or string value. The type of the
function is declared by name, in the same way as
variables are declared (see "How to Declare Variable
Types" in Chapter 3). If the type of expression (string
or numeric) does not match the function type, a
"Type mismatch" error occurs. If the function is
numeric, the value of the expression is converted to
the precision specified by name before it is returned
to the calling statement.
Example: Ok
~~ ~~;3~~~~~~~R)=PI*RA2
30 INPUT "Radius? ",RADIUS
40 PRI NT "Area is II FNAREA (RAD I US)
RUN
Radius?
(Suppose you respond with 2.)
Radius? 2
Area is 12.56637
Ok
4-69
DEFFN
Statement
Line 20 defines the function FNAREA, which
calculates the area of a circle with radius R. The
function is called in line 40.
Ok
10 DEF FNMUD(X,Y)=X-(INT(X/Y)*Y)
20 A = FNMUD(7.4,4)
30 PRINT A
RUN
3.4
Ok
4-70
DEFSEG
Statement
4-71
DEFSEG
Statement
DEF and SEG must be separated by a space.
Otherwise, BASIC will interpret the statement
DEFSEG=100 to mean: "assign the value 100 to the
variable DEFSEG."
4-72
DEFtype
Statements
4-73
DEFtype
Statements
Example: Ok
10 DEFDBL L-P
20 DEFSTR A
30 DEFINT X,D-H
40 ORDER = 1#/3: PRINT ORDER
50 ANIMAL = 'ICAT": PRINT ANIMAL
60 X=10/3: PRINT X
RUN
.3333333333333333
CAT
3
Ok
4-74
DEFUSR
Statement
4-75
DELETE
Command
Purpose: Deletes program lines.
DELETE 40-100
4-76
DIM
Statement
4-77
DIM
Statement
If you try to dimension an array more than once, a
"Duplicate Definition" error occurs. You may,
however, use the ERASE statement to erase an array
so you can dimension it again. For more information
about arrays, see "Arrays" in Chapter 3.
Example: Ok
10 WRRMAX=2
20 DIM 515(12), WRR$(WRRMAX,2)
30 DATA 26.5, 37, 8,29,80, 9.9, &H800
40 DATA 7, 18, 55, 12, 5,43
50 FOR 1=0 TO 12
60 READ 5 IS ( I )
70 NEXT I
80 DATA SHERRY, ROBERT, IIA: II
90 DATA "H I, SCOTT", HELLO, GOOD-BYE
100 DATA BOCA RATON, DELRAY, MIAMI
110 FOR 1=0 TO 2: FOR J=0 TO 2
120 READ WRR$(I ,J)
130 NEXT J, I
140 PRINT 515(3); WRR$(2,0)
RUN
29 BOCA RATON
Ok
4-78
DRAW
Statem'ent
Un Move up.
Dn Move down.
Ln Move left.
Rn Move right.
4-80
DRAW
Statement
The following two prefix commands may precede
any of the above movement commands.
4-81
DRAW
Statement
In all of these commands, the n, x, or y argument can
be a constant like 123 or it can be =variable; where
variable is the name of a numeric variable. The
semicolon ( ; ) is required when you use a variable
this way, or in the X command. Otherwise, a
semicolon is optional between commands. Spaces
are ignored in string. For example, you could use
variables in a move command this way:
M+=Xl;,-=X2;
4-82
DRAW
Statement
Example: To draw a box:
5 SCREEN 1
10 A=20
20 DRAW "U=A; R=A; D=A; L=A; II
To draw a triangle:
10 SCREEN 1
20 DRAW IIE15 F15 L30 11
4-83
EDIT
Command
Purpose: Displays a line for editing.
4-84
END
Statement
Format: END
4-85
EOF
Function
Format: v = EOF{filenum)
40 INPUT #1,M(C)
50 C=C+1: GOTO 30
This example reads information from the sequential
file named "DATA". Values are read into the array M
until end of file is reached.
4-86
ERASE
Statement
4-87
ERASE
Statement
Example: Ok
10 START=FRE (1111)
20 DIM BIG(100, 100)
30 MI DDLE=FRE(IIII)
40 ERASE BIG
50 DIM BIG(10,10)
60 F INAL=FRE (1111)
70 PRINT START, MIDDLE, FINAL
RUN
62808 21980 62289
Ok
4-88
ERR and ERL
Variables
Format: v = ERR
v= ERL
Remarks: The variable ERR contains the error code for the last
error, and the variable ERL contains the line number
of the line in which the error was detected. The ERR
and ERL variables are usually used in IF ... THEN
statements to direct program flow in the error
handling routine (refer to "ON ERROR Statement"
in this chapter).
4-89
ERR and ERL
Variables
ERR and ERL can be set using the ERROR
statement (see next section).
4-90
ERROR
Statement
Purpose: • Simulates the occurrence of a BASIC error; or
Format: ERROR n
4-91
ERROR
Statement
Example: The first example simulates a "String too long"
error.
Ok
10 T = 15
20 ERROR T
RUN
String too long in 1 ine 20
Ok
4-92
EXP
Function
Format: v = EXP(x)
Example: Ok
10 X = 2
20 PRINT EXP(X-1)
RUN
2.718282
Ok
4-93
FIELD
Statement
The statement:
4-94
FIELD
Statement
FIELD does not "remove" data from the file either.
Information is read from the file into the random file
buffer with the GET (file) statement. Information is
read from the buffer by simply referring to the
variables defined in the FIELD statement.
4-95
FIELD
Statement
Example: 10 OPEN "A:CUST" AS #1
20 FIELD 1, 2 AS CUSTNO$, 30 AS CUSTNAME$,
35 AS ADDR$
30 LSET CUSTNAME$+"O'NEIL J NC'1
40 LSET ADDR$+"50 SE 12TH ST, NY, NY l
'
50 LSET CUSTNO$=MKI$(7850)
60 PUT 1,1
70 GET 1,1
80 CNUM%= CVI (CUSTNO$): N$ = CUSTNAME$
90 PRINT CNUM%, N$, ADDR$
This example opens a file named "CUST" as a
random file. The variable CUSTNO$ is assigned to
the first 2 positions in each record, CUSTNAME$ is
assigned to the next 30 positions, and ADDR$ is
assigned to the next 35 positions. Lines 30 through
50 put information into the buffer, and the PUT
statement in line 60 writes the buffer to the file. Line
70 reads back that same record, and line 90 displays
the three fields. Note in line 80 that it is okay to use a
variable name which was defined in a FIELD
statement on the right side of an assignment
statement.
4-96
FILES
Command
4-97
FILES
Command
Example: FILES
4-98
FIX
Function
Purpose: Truncates x to an integer.
Format: v = FIX(x)
Example: Ok
PRINT FIX(45.67)
45
Ok
PRINT FIX(-2.89)
-2
Ok
4-99
FOR and NEXT
Statements
Purpose: Performs a series of instructions in a loop a given
number of times.
4-100
FOR and NEXT
Statements
If the value of z is negative, the test is reversed. The
counter is decremented each time through the loop,
and the loop is executed until the counter is less than
the final value.
Nested Loops
FOR... NEXT loops may be nested; that is, one
FOR... NEXT loop may be placed inside another
FO R ... NEXT loop. When loops are nested, each
loop must have a unique variable name as its
counter. The NEXT statement for the inside loop
must appear before that for the outside loop. If
nested loops have the same end point, a single NEXT
statement may be used for all of them.
NEXT var1
NEXTvar2
NEXT var3
4-101
FOR and NEXT
Statements
The variable(s) in the NEXT statement may be
omitted, in which case the NEXT statement matches
the most recent FOR statement. If you are using
nested FOR... NEXT loops, you should include the
variable(s) on all the NEXT statements. It is a good
idea to include the variables in order to avoid
confusion; but it can be necessary if you do any
branching out of nested loops. (However, using
variable names on the NEXT statements will cause
your program to execute somewhat slower.)
Ok
10 J=10: K=30
20 FOR 1=1 TO J STEP 2
30 PRINT I;
40 K=K+10
50 PRINT K
60 NEXT
RUN
1 40
3 50
5 60
7 70
9 80
Ok
4-102
FOR and NEXT
Statements
In the next example, the loop does not execute
because the initial value of the loop is more than the
final value:
Ok
10 J=0
20 FOR 1+1 TO J
30 PRINT I
40 NEXT I
RUN
Ok
4-103
FRE
Function
Format: v = FRE(x)
v = FRE(x$)
4-104
FRE
Function
CLEAR ,n sets the maximum number of bytes for
the BASIC workspace. FRE returns the amount of
free storage in the BASIC workspace. If nothing is in
the workspace, then the value returned by FRE will
be 2.5K to 4K-bytes (the size of the reserved
interpreter workarea) smaller than the number of
bytes set by CLEAR.
Example: Ok
PRINT FRE(0)
14542
Ok
4-105
GET
Statement (Files)
4-106
GET
Statement (Files)
Example: 10 OPEN IIA: CUST AS # 1
II
4-107
GET
Statement (Graphics)
Purpose: Reads points from an area of the screen.
4-108
GET
Statement (Graphics)
The array is used simply as a place to hold the image
and must be numeric; it may be any precision,
however. The required size of the array, in bytes, is:
4+INT«x*bitsperptxel+7)/S)*y
• 2 for integer
• 4 for single-precision
• S for double-precision
4-109
GET
Statement (Graphics)
The data for each row of points in the rectangle is left
justified on a byte boundary, so if there are less than a
multiple of eight bits stored, the rest of the byte will
be filled with zeros.
4-110
GOSUB and RETURN
Statements
Purpose: Branches to and returns from a subroutine.
RETURN
4-111
GOSUB and RETURN
Statements
Example: Ok
10 GOSUB 40
20 PRINT "BACK FROM SUBROUTINE"
30 END
40 PRINT "SUBROUTINE";
50 PRINT II INII;
60 PRINT II PROGRESS"
70 RETURN
RUN
SUBROUTINE IN PROGRESS
BACK FROM SUBROUTINE
Ok
4-112
GOTO
Statement
4-113
GO TO
Statement
Example: Ok
5 DATA 5,7,12
10 READ R
20 PRINT IIR =II;R,
30 A = 3. 14,', R~ 2
40 PRINT IIAREA =II;A
50 GOTO 5
RUN
R= 5 AREA = 78.5
R= 7 AREA = 153.86
R = 12 AREA = 452.16
Out of data in 10
Ok
The GOTO statement in line 50 puts the program
into an infinite loop, which is stopped when the
program runs out of data in the DATA statement.
(Notice how branching to the DATA statement did
not add additional values to the internal data table.)
4-114
HEX$
Function
Format: v$ = HEX$(n)
4-115
IF
Statement
-4-116
IF
Statement
previously entered a line with the specified line
number.
4-117
IF
Statement
Nesting of IF Statements: IF ... THEN ... ELSE
statements may be nested. Nesting is limited only by
the length of the line. For example,
4-118
INKEY$
Variable
Purpose: Reads a character from the keyboard.
Format: v$ = INKEY$
4-119
INKEY$
Variable
If you press Enter in response to ~NKEY$, the
carriage return character passes through to the
program.
4-120
INP
Function
Format: v = INP(n)
4-121
INPUT
Statement
The type of each data item that you enter must agree
with the type specified by the variable name. (Strings
entered in response to an INPUT statement need
not be surrounded by quotation marks unless they
4-122
INPUT
Statement
contain commas or significant leading or trailing
blanks.)
If you respond to INPUT with too many or too few
items, or with the wrong type of value (letters
instead of numbers, etc.), BASIC displays the
message "? Redo from start". If a single variable is
requested, you may simply press Enter to indicate
the default values of 0 for numeric input or null for
string input. However, if more than one variable is
requested, pressing Enter will cause the "?Redo
from start" message to be printed because too few
items were entered. BASIC does not assign any of the
input values to variables until you give an acceptable
response.
Example: Ok
10 INPUT X
20 PRINT X "SQUARED IS" X"'2
30 END
RUN
?
? 5
5 SQUARED IS 25
Ok
4-123
INPUT
Statement
Ok
10PI=3.14
20 INPUT "WHAT IS THE RADIUS";R
30 A=P I ,"R'" 2
40 PRINT liTHE AREA OF THE CIRCLE ISII;A
50 END
RUN
WHAT IS THE RADIUS?
4-124
INPUT #
Statement
4-125
INPUT #
Statement
If BASIC is scanning the data for a string item,
leading spaces, carriage returns, and line feeds are
also ignored. The first character encountered that is
not a space, carriage return, or line feed is assumed
to be the start of the string item. If this first character
is a quotation mark ("), the string item will consist of
all characters read between the first quotation mark
and the second. Thus, a quoted string may not
contain a quotation mark as a character. If the first
character of the string is not a quotation mark, the
string is an unquoted string; it will end when a
comma, carriage return, or line feed, or after 255
characters have been read. If end of file is reached
when a numeric or string item is being input, the
item is cancelled.
4-126
INPUT$
Function
Format: v$ = INPUT$(n[,[#]fi"lenumD
4-127
INPUT$
Function
Example: The following program lists the contents of a
sequential file in hexadecimal.
4-128
INSTR
Function
Format: v = INSTR([n,]x$,y$)
Example: Ok
10 A$ = IIABCDEB"
20 B$ = "BII
30 PRINT INSTR(A$,B$); INSTR(4,A$,B$)
RUN
2 6
Ok
4-129
INT
Function
Format: v = INT(x)
Example: Ok
PRINT INT(45.67)
45
Ok
PRINT INT(-2.89)
-3
Ok
This example shows how INT truncates positive
integers, but rounds negative numbers upward (in a
negative direction).
4-130
KEY
Statement
Format: KEY n, x$
KEY LIST
KEY ON
KEY OFF
F1 LIST F2 RUN~
F3 LOAD" F4 SAVE"
F5 CONT~ F6 ,"LPT1:"~
F7 TRON~ F8 TROFF~
F9 KEY FlO SCREEN 0,0,0 ~
4-131
KEY
Statement
KEY ON causes the soft key values to be displayed
on the 25 th line. When the width is40, five of the ten
soft keys are displayed. When the width is 80, all ten
are displayed. In either width, only the first six
characters of each value are displayed. ON is the
default state for the soft key display.
KEY OFF erases the soft key display from the 25th
line, making that line available for program use. It
does not disable the function keys.
KEY LIST lists all ten soft key values on the screen.
All 15 characters of each value are displayed.
4-132
KEY
Statement
Note: To avoid complications on the input
buffer in Cassette BASIC, you should execute:
Example: 50 KEY ON
erases soft key display. The soft keys are still active,
but not displayed.
10 KEY 1,"FILES"+CHR$(13)
20 KEY 1, /1/1
4-133
KEY(n)
Statement
Format: KEY(n) ON
KEY(n) OFF
KEY(n) STOP
4-134
KEY(n)
Statement
IfKEY(n) is OFF, no trapping takes place and even if
the key is pressed, the event is not remembered.
4-135
KILL
Command
Purpose: Deletes a file from a diskette. The KILL .command
in BASIC is similar to the ERASE command in DOS.
SAVE "TEST"
4-136
LEFT$
Function
Purpose: Returns the leftmost n characters of x$.
Format: v$ = LEFT$(x$,n)
Example: Ok
10 A$ = I'BAS I C PROGRAM"
20 B$ = LEFT$(A$,S)
30 PRINT B$
RUN
BASIC
Ok
4-137
LEN
Function
Purpose: Returns the number of characters in xl.
Format: v = LEN(x/)
Example: 10 X$ = II B0 CA RAT 0 N, F L I I
20 PRINT LEN(X$)
RUN
14
Ok
4-138
LET
Statement
4-139
LET
Statement
Example: 110 LET DORI=12
120 LET E=DORI+2
130 LET FDANCE$="HORN'
4-140
LINE
Statement
Purpose: Draws a line or a box on the screen.
LINE -(X2,Y2)
This will draw a line from the last point referenced
to the point (X2,Y2) in the foreground color.
4-141
LINE
Statement
We can include a starting point also:
LINE (X1,Y1)-(X2,Y1)
LINE (X1,Y1)-(X1,Y2)
LINE (X2,Y1)-(X2,Y2)
LINE (X1,Y2)-(X2,Y2)
4-142
LINE
Statement
The BF means draw the same rectangle as B, but also
fill in the interior points with the selected color.
10 CLS
20 SCREEN 1,O: COLOR 0,0
30 LINE -(RND*319,RND*199),RND*2+1,BF
40 GOTO 30 'boxes wi 11 overlap
4-143
LINE INPUT
Statement
4-144
LINE INPUT #
Statement
4-145
LINE INPUT #
Statement
Example: The following example uses LINE INPUT to get
information from the keyboard, where the
information is likely to have commas or other
delimiters in it. Then the information is written to a
sequential file, and read back 9ut from the file using
LINE INPUT #.
Ok
10 OPEN IlLIST 11 FOR OUTPUT AS #1
20 LINE INPUT "Address? ";C$
30 PRINT #1, C$
40 CLOSE 1
50 OPEN "LIST" FOR INPUT AS #1
60 LINE INPUT #1, C$
70 PRINT C$
80 CLOSE 1
RUN
Address?
Suppose you respond with DELRAY BEACH, FL
33445. The program continues:
•
•
•
Address? DELRAY BEACH, FL 33445
DELRAY BEACH, FL 33445
Ok
4-146
LIST
Command
4-147
LIST
Command
When the dash (-) is used in a line range, three
options are available:
Example: LIS T
4-148
LLIST
Command
4-130a
NOTES
4-130b
LLIST
Command
Remarks: The line number ranges for LLIST work the same as
for LIST.
Example: LLI ST
LLI ST 35
LLiST 10-20
LLI ST 100-
LLIST -200
4-t50
LOAD
Command
If you are using Disk or Advanced BASIC, the DOS
default diskette drive is used if the device is omitted.
The extension .BAS is added to the filename if no
extension is supplied and the filename is eight
characters or less.
4-151
LOAD
Command
2. Note that Ctrl-Break may be typed at any time
during LOAD. Between files ot after a time-out
period, BASIC will exit the search and return to
command level. Previous memory contents
remain unchanged.
RUN "INVENT"
LOAD "B:REPORT.BAS"
LOAD "CAS1:"
4-152
LOC
Function
Format: v = LOC(jilenum)
Remarks: /ilenum is the file number used when the file was
opened.
4-153
LOC
Function
Example: 200 IF LOC (1) >5.0 THEN STOP
4-154
LOCATE
Statement
4-155
LOCATE
Statement
start and stop allow you to make the cursor any size
you want. You indicate the starting and ending scan
lines. The scan lines are numbered from 0 at the top
of the character position. The bottom scan line is 7 if
you have the Color/Graphics Monitor Adapter, 13 if
you have the IBM Monochrome Display and Parallel
Printer Adapter. If start is given and stop is omitted,
stop assumes the value of start. If start is greater than
stop, you '11 get a two-part cursor. T h " wraps"
e cursor
from the bottom line back to the top.
4-156
LOCATE
Statement
Example: 10 LOCATE 1,1
20 LOCATE ,,1
30 LOCATE ",7
Position and cursor visibility remain unchanged.
Sets the cursor to display at the bottom of the
character on the Color/Graphics Monitor Adapter
(starting and ending on scan line 7).
40 LOCATE 5,1,1,0,7
Moves the cursor to line 5, column 1. Makes the
cursor visible, covering the entire character cell on
the Color/Graphics Monitor Adapter, starting at
scan line 0 and ending on scan line 7.
4-157
LOF
Function
Purpose: Returns the number of bytes allocated to the file
(length of the file).
Format: v = LOF(filenum)
Remarks: filenum is the file number used when the file was
opened.
Example: These statements will get the last record of the file
named BIG, assuming BIG was created with a record
length of 128 bytes:
10 OPEN "BIG" AS #1
20 GET #1,LOF(1)/128
4-158
LOG
Function
Purpose: Returns the natural logarithm of x.
Format: v = LOG(x)
Ok
PRINT LOG(45/7)
1.860752
Ok
Ok
E= 2.718282
Ok
? LOG(E)
1
Ok
? LOG ( E,', E)
2
Ok
4-159
LPOS
Function
Format: v = LPOS(n)
o or 1 LPT1:
2 LPT2:
3 LPT3:
4-160
LPRINT and LPRINT USING
Statements
Purpose: Prints data on the printer (LPTI :).
4-161
LPRINT and LPRINT USING
Statements
Timeout" error on the second LPRINT. To avoid
this problem, do the following:
1 ON ERROR GOTO 65000
F:St:)(JIt-t 1 i rJe 2
Report 1 i nE? ::::;
J=::epoF't. l:i fie 4-
Condensed print; 132 char/line
RE~tur-n t.o nOF'mal
4-162
LSET and RSET
Statements
Purpose: Moves data into a random file buffer (in preparation
for a PUT (file) statement).
RSET stringvar = x$
4-163
LSET and RSET
Statements
Refer to "Appendix B. BASIC Diskette Input and
Output" for a complete explanation of using
random files.
110 A$=SPACE$(20)
120 RSET A$=N$
4-164
MERGE
Command
Purpose: Merges the lines from an ASCII program file into the
program currently in memory.
4-165
MERGE
Command
If the program being merged was not saved in ASCII
format (using the A option on the SAVE command),
a "Bad file mode" error occurs. The program in
memory remains unchanged.
4-166
MID$
Function and Statement
Purpose: Returns the requested part ofa given string. When
used as a statement, as in the second format,
replaces a portion of one string with another string.
Format: As a function:
v$ = MID$(x$,n[,m])
As a statement:
MID$(v$,n[,m]) = y$
4-167
MID$
Function and Statement
For the statement (MID$ ... =y$):
y$ is a string expression.
4-168
MID$
Function and Statement
Example: The first example uses the MID$ function to select
the middle portion of the string B$.
Ok
10 A$=IIGOOD II
2.0 B$="MORNING EVENING AFTERNOON"
30 PRINT A$;MID$(B$,9,7)
RUN
GOOD EVENING
Ok
Ok
1.0 A$="MARATHON, GREECE"
2.0 M10$ (A$, 11) ="FLOR I DA"
3.0 PRINT A$
RUN
MARATHON, FLORID
Ok
Note in the second example how the length of A$
was not changed.
4-169
MKI$, MKS$, MKD$
Functions
Purpose: Convert numeric type values to string type values.
4-170
MKI$, MKS$, MKD$
Functions
Example: This example uses a random file (#1) with fields
defined in line 100. The first field, D$, is intended to
hold a numeric value, AMT. Line 110 converts AMT
to a string value using MKS$ and uses LSET to place
what is actually the value of AMT into the random
file buffer. Line 120 places a string into the buffer
(we don't need to convert a string); then line 130
writes the data from the random file buffer to the
file.
4-171
MOTOR
Statement
10 MOTOR 1
20 MOTOR 0
30 HOTOR
4-172
NAME
Command
Purpose: Changes the name of a diskette file. The NAME
command in BASIC is similar to the RENAME
command in DOS.
4-173
NEW
Command
Purpose: Deletes the program currently in memory and clears
all variables.
Format: NEW
Example: Ok
NEW
Ok
4-174
OCT$
Function
Purpose: Returns a string which represents the octal value of
the decimal argument.
Format: vi = OCT$(n)
Example: Ok
PRINT OCT$(24)
30
Ok
4-175
ON COM(n)
Statement
4-177
ON ERROR
Statement
Purpose: Enables error trapping and specifies the first line of
the error handling subroutine.
4-178
ON ERROR
Statement
You use the RESUME statement to exit from the
error trapping routine. Refer to "RESUME
Statement" in this chapter.
4-179
ON... GOSUB and ON... GOTO
Statements
Purpose: Branches to one of several specified line numbers,
depending on the value of an expression.
ON n GOSUB line[,line] .. .
4-180
ON... GOSUB and ON... GOTO
Statements
Example: The first example branches to line 150 ifL-1 equals
1, to line 300 ifL-1 equals 2, to line 320 ifL-1 equals
3, and to line 390 ifL-1 equals 4. IfL-1 is equal to 0
(zero) or is greater than 4, then the program just
goes on to the next statement.
1390 RETURN
4-181
ON KEY(n)
Statement
Purpose: Sets up a line number for BASIC to trap to when the
specified function key or cursor control key is
pressed.
4-182
ON KEY(n)
Statement
If a KEY(n) STOP statement is executed, no
trapping takes place for the specified key. However,
if the key is pressed the event is remembered, so an
immediate trap takes place when KEY(n) ON is
executed.
4-183
ON KEY(n)
Statement
Example: The following is an example of a trap routine for
function key 5.
4-184
ON PEN
Statement
4-185
ONPEN
Statement
Event trapping does not take place when BASIC is
not executing a program. When an error trap
(resulting from an ON ERROR statement) takes
place all trapping is automatically disabled (including
ERROR, STRIG(n), PEN, COM(n), and KEY(n)).
Example: This example sets up a trap routine for the light pen.
650 RETURN 30
4-186
ON STRIG(n)
Statement
Purpose: Sets up a line number for BASIC to trap to when one
of the joystick buttons (triggers) is pressed.
o button Al
2 button BI
4 button A2
6 button B2
o button Al
2 button B 1
4 button A2
6 button B2
4-162a
NOTES
4-162b
OPEN
Statement
or:
Example: Ok
50 OPEN "LPT1:" AS #1 : WIDTH #1,255
60 PRINT #1, "Underline this"
70 PRINT # 1, STRING$ (14, "_")
80 WIDTH #1, 80 : PRINT #1
RUN
Underline this
Ok
4-166a
NOTES
4-166b
OPEN
Statement
Alternative form:
4-189
OPEN
Statement
For both formats:
PRINT # INPUT #
PRINT # USING LINE INPUT #
WRITE # GET
INPUT$ PUT
4-190
OPEN
Statement
GET and PUT are valid for random files (or
communications files - see next section). A diskette
file may be either random or sequential, and a
printer may be opened in either random or
sequential mode; however, all other devices may be
opened only for sequential operations.
4-191
OPEN
Statement
In Cassette BASIC, a maximum of four files may be
open at one time (cassette, printer, keyboard, and
screen). Note that only one cassette file may be
open at a time. For Disk and Advanced BASIC the
default maximum is three files. You can override
this value by using the IF: option on the BASIC
command.
4-192
OPEN
Statement
Either of the preceding two statements opens the
file named "SSFILE" on the diskette in drive B for
random input and output. The record length is 256.
25 FILE$ = "A:DATA.ART"
30 OPEN FILE$ FOR APPEND AS 3
4-193
OPEN "COM...
Statement
Purpose: Opens a communications file.
4-194
OPEN "COM ...
Statement
4-170a
OPEN "COM ...
Statement
4-170b
OPEN "COM ...
Statement
50 OPEN "COM1:1200""CS,DS2000" AS #1
4-170c
"iiJUUittiit.,ilii,,,iil.P
OPEN "COM ...
Statement
5 TRIES=6
10 ON ERROR GO TO 100
20 OPEN "COMl:300,N,8,2,CS,DS,CD10000" AS #1
30 ON ERROR GOTO a
40 CLOSE #1 ' works so can continue
50 GOTO 1000
100 TRIES=TRIES-l
110 IF TRIES=O THEN ON ERROR GOTO a ' give up
120 RESUME
4-170d
OPEN "COM...
Statement
E EVEN: Even transmit parity, even
receive parity checking.
4-195
OPEN "COM...
Statement
The RS, CS, DS, CD, and LF options affect the line
signals as follows:
4-197
OPEN "COM...
Statement
Example: 10 OPEN ItCOM 1 : It AS 1
20 OPEN ICOM2:1200,N,8" AS #1
4-198
OPEN "COM...
Statement
An OPEN statement may be used with an ON
ERROR statement to make sure a modem is
working properly before sending any data. For
example, the following program makes sure we get
Carrier Detect (CD or RLSD) from the modem
before starting. Line 20 is set to timeout after 10
seconds. TRIES is set to 6.so we give up if Carrier
Detect is not seen within one minute. Once
communication is established, we re-open the file
with a shorter delay until timeout.
5 TRIES=6
10 ON ERROR GOTO 100
20 OPEN "COM1:300,N,8,2,CS,DS,CD10000" AS #1
30 ON ERROR GOTO 0
40 CLOSE #1 I works so can cant i nue
50 GOTO 1000
100 TRIES=TRIES-1
110 IF TRIES=0 THEN ON ERROR GOTO 0 I give up
120 RESUME
20 OPEN "COM1:1200,N,8"CS10000,DS10000,
CD10000,LF" AS #1
4-199
OPTION BASE
Statement
Remarks: n is 1 or O.
OPTION BASE 1
4-200
OUT
Statement
4-201
OUT
Statement
OUT 980,2: OUT 981,85
4-202
PAINT
Statement
4-203
PAINT
Statement
Since there are only two colors in high resolution it
doesn't make sense for paint to be different from
boundary. Since boundary is defaulted to equalpaint we
don't need the third parameter in high resolution
mode.
Example: 5 SCREEN 1
10 LINE (0,0)-(100,150),2,B
20 PAINT (50,50),1,2
The PAINT statement in line 20 fills in the box
drawn in line 10 with color 1.
4-204
PEEK
Function
Purpose: Returns the byte read from the indicated memory
position.
Format: v = PEEK(n)
4-205
PEN
Statement and Function
Purpose: Reads the light pen.
Format: As a statement:
PENON
PEN OFF
PEN STOP
As a function:
v = PEN(n)
4-206
PEN
Statement and Function
4 Returns the last known valid x coordinate.
Range is 0 to 319 in medium resolution, or
o to 639 in high resolution.
5 Returns the last known valid y coordinate.
Range is 0 to 199.
4-207
PEN
Statement and Function
PEN OFF disables the PEN read function. For
Advanced BASIC, no trapping of the pen takes place
and action by the light pen is not remembered even
if it does take place.
Example: 50 PEN ON
60 FOR 1=1 TO 500
70 X=PEN(0): Xl=PEN(3)
80 PRINT X, Xl
90 NEXT
100 PEN OFF
This example prints the pen value since the last poll,
and the current value.
4-208
PLAY
Statement
4-209
PLAY
Statement
Ln Sets the length of the following notes. The
actual note length is lin. n may range from 1
to 64. The following table may help explain
this:
Length Equivalent
L1 whole note
L2 half note
L3 one of a triplet of three half notes
(1/3 of a 4 beat measure)
L4 quarter note
L5 one of a quintuplet (1/5 of a
measure)
L6 one of a quarter note triplet
X variable;
Execu tes specified string.
4-211
PLAY
Statement
You can also specify variables in the form
VARPTR$ (vart"able), instead of =variable;. This is
useful in programs that will later be compiled. For
example:
4-212
POINT
Function
5 SCREEN 2
10 IF POINT(I, 1)<>0 THEN PRESET(I, I)
ELSE PSET(I,I)
or
10 PSET(I,I),l-POINT(I,I)
4-213
POKE
Statement
Purpose: Writes a byte into a memory location.
Warning:
BASIC does not do any checking on the address.
So don't go POKEing around in BASIC's stack,
BASIC's variable area, or your BASIC program.
4-214
POS
Function
Format: v = POS(n}
4-215
PRINT
Statement
Print Positions
The position of each printed item is determined by
the punctuation used to separate the items in the
list. BASIC divides the line into print zones of 14
spaces each. In the list of expressions, a comma
causes the next value to be printed at the beginning
of the next zone. A semicolon causes the next value
to be printed immediately after the last value.
Typing one or more spaces between expressions has
the same effect as typing a semicolon.
4-216
PRINT
Statement
If a comma, semicolon, or SPC or TAB function
ends the list of expressions, the next PRINT
statement begins printing on the same line, spacing
accordingly. If the list of expressions ends without a
comma, semicolon, SPC or TAB function, a carriage
return is printed at the end of the line; that is, BASIC
moves the cursor to the beginning of the next line.
4-217
PRINT
Statement
Example: Ok
10 X=5
20 PRINT X+5, X-5, X*(-5)
30 END
RUN
10 0 -25
Ok
In this example, the commas in the PRINT
statement cause each value to be printed at the
beginning of the next print zone.
Ok
10 INPUT X
20 PRINT X "SQUARED IS" X"'2 "AND";
30 PRINT X "CUBED ISII X"3
RUN
? 9
9 SQUARED IS 81 AND 9 CUBED IS 729
Ok
RUN
? 21
21 SQUARED IS 441 AND 21 CUBED IS 9261
Ok
Here, the semicolon at the end of line 20 causes both
PRINT statements to be printed on the same line.
Ok
10 FOR X = 1 TO 5
20 J=J+5
30 K=K+10
40 ?J; K;
50 NEXT X
RUN
5 10 10 20 15 30 20 40 25 50
Ok
Here, the semicolons in the PRINT statement cause
each value to be printed immediately after the
preceding value. (Don't forget, a number is always
followed by a space and positive numbers are
preceded by a space.) In line 40, a question mark is
used instead of the word PRINT.
4-218
PRINT USING
Statement
list of expressions
consists of the string expressions or numeric
expressions that are to be printed, separated by
semicolons or commas.
String Fields
When PRINT USING is used to print strings, one of
three formatting characters may be used to format
the string field:
4-219
PRINT USING
Statement
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 in the field and padded with
spaces on the right.
Example:
10 A$="LOOK": B$="OUT"
20 PRINT USING "!II;A$;
30 PRINT USING "&";8$
RUN
LOUT
Numeric Fields
When PRINT USING is used to print numbers, the
following special characters may be used to format
the numeric field:
4-220
PRINT USING
Statement
A decimal point may be inserted at any
position in the field. If the format string
specifies that a digit is to precede the
decimal point, the digit will always be
printed (as 0 if necessary). Numbers are
rounded as necessary.
PRINT USING "##.##";.78
0.78
PRINT USING "###.##11;987.654
987.65
PRINT USING "##.## ";10.2,5.3,66.789, .234
10.20 5.30 66.79 0.23
4-221
PRINT USING
Statement
The ** also specifies positions for two
more digits.
PRINT USING 11 0
",0",#.# ";12.39,-0.9,765.1
*12.4 *-0.9 765.1
4-222
PRINT USING
Statement
The comma has no effect if used with the
exponential C''''''') format.
Ok
PRINT USING "##.##""''''''''';234.56
2.35E+02
Ok
PRINT USING ".###"' . . ""-";-88888
.889E+05-
Ok
PRINT USING "+.##""""''';123
+.12E+03
Ok
4-223
PRINT USING
Statement
If the number to be printed is larger than the
specified numeric field, a percent sign (%) is printed
in front of the number. If rounding causes the
number to exceed the field, the percent sign is
printed in front of the rounded number.
Ok
PRINT USING "##.##11;111.22
%111 .22
Ok
PRINT USING 11.##11;.999
%1 .00
Ok
Ok
PRINT USING IITHIS IS EXAMPLE J#II;
THIS IS EXAMPLE #1
Ok
4-224
PRINT # and PRINT # USING
Statements
Purpose: Writes data sequentially to a file.
PRINT #l,A;B;C;X;Y;Z
(If commas are used as delimiters, the extra blanks
that are inserted between print fields are also
wri tten to the file.)
4-225
PRINT # and PRINT # USING
Statements
String expressions must be separated by semicolons
in the list. To format the string expressions
correctly on the file, use explicit delimiters in the
list of expressions.
, For example, let A$="CAMERA" and
B$="93604-1". The statement
PRINT #1 ,A$;B$
would write CAMERA93604-1 to the file. Because
there are no delimiters, this could not be input as
two separate strings. To correct the problem, insert
explicit delimiters into the PRINT # statement as
follows:
PRI NT # 1 ,A $ ; II , I I ; B$
The image written to the file is
CAMERA,93604-1
which can be read back into two string variables.
If the strings themselves contain commas,
semicolons, significant leading blanks, carriage
returns, or line feeds, write them to the file
surrounded by explicit quotation marks using
CHR$(34).
For example, let A$="CAMERA, AUTOMATIC"
and B$=" 9 3604-1". The statement:
PRINT #l,A$;B$
writes the following image to the file:
4-227
PSET and PRESET
Statements
Purpose: Draws a point at the specified position on the
screen.
70 PSET ( I , I ) ,(0
4-228
PSET and PRESET
Statements
If an out of range coordinate is given to PSET or
PRESET no action is taken nor is an error given. If
color is greater than 3, this will result in an "Illegal
function call" error.
10 SCREEN 1
20 FOR 1=0 TO 100
30 PSET (1,1)
40 NEXT
50 'erase 1 ine
60 FOR 1=100 TO 0 STEP -1
70 PRESET(I,I)
80 NEXT
4-229
PUT
Statement (Files)
Purpose: Writes a record from a random buffer to a random
file.
4-230
PUT
Statement (Files)
PUT can be used for a communications file. In that
case number is the number of bytes to write to the
communications file. This number must be less than
or equal to the value set by the LEN option on the
OPEN "COM ... statement.
4-231
PUT
Statement (Graphics)
Purpose: Writes colors onto a specified area of the screen.
PSET
PRESET
XOR
OR
AND
4-232
PUT
Statement (Graphics)
PSET as an action simply stores the data from the
array onto the screen, so this is the true opposite of
GET.
AND
array value
0 1 2 3
0 0 0 0 0
s
c 1 0 1 0 1
r
e 2 0 0 2 2
e
n 3 0 1 2 3
4-233
PUT
Statement (Graphics)
OR
array value
0 1 2 3
s 0 0 1 2 3
c 1 1 1 3 3
r
e 2 2 3 2 3
e
n 3 3 3 3 3
XOR
array value
0 1 2 3
s 0 0 1 2 3
c 1 1 0 3 2
r
e 2 2 3 0 1
e
n 3 3 2 1 0
4-234
PUT
Statement (Graphics)
Movement done this way leaves the background
unchanged. Flicker can be reduced by minimizing
the time between steps 4 and 1, and making sure
there is enough time delay between steps 1 and 3. If
more than one object is being animated, every
object should be processed at once, one step at a
time.
4-235
RANDOMIZE
Statement
4-236
RANDOMIZE
Statement
Example: 10 RANDOMIZE
20 FOR 1=1 TO 4
30 PRINT RND;
40 NEXT I
RUN
Random Number Seed (-32768 to 32767)7
4-237
READ
Statement
4-238
READ
Statement
To reread data from any line in the list of DATA
statements, use the RESTORE statement (see
"RESTORE Statement" in this chapter).
Ok
10 PRINT "CITY " , f'STATE II , If ZIP"
20 READ C$,S$,Z
30 DATA "DENVER,'I, COLORADO, 80211
40 PRINT C$,S$~Z
RUN
CITY STATE ZIP
DENVER, COLORADO 80211
Ok
4-239
REM
Statement
4-240
RENUM
Command
Purpose: Renumbers program lines.
4-241
RENUM
Command
Example: RENUM
RENUM 300,,50
Renumbers the entire program. The first new line
number is 300. Lines increment by 50.
RENUM 1000,900,20
Renumbers the lines from 900 up so they start with
line number 1000 and increment by 20.
4-242
RESET
Command
Purpose: Closes all diskette files and clears the system buffer.
Format: RESET
4-243
RESTORE
Statement
Example: Ok
10 READ A,B,C
20 RESTORE
30 READ D,E,F
40 DATA 57, 68, 79
50 PR!NT ..A·R·r·n·F·F
,-,-,-,-,.
RUN
57 68 79 57 68 79
Ok
The RESTORE statement in line 20 resets the
DATA pointer to the beginning, so that the value~
that are read in line 30 are 57, 68, and 79.
4-244
RESUME
Statement
RESUME NEXT
RESUME line
RESUME or RESUME 0
Execution resumes at the
statement which caused the
error.
4-245
RESUME
Statement
A RESUME statement that is not in an error trap
routine causes a "RESUME without error" message
to occur.
4-246
RETURN
Statement
4-247
RIGHT$
Function
Purpose: Returns the rightmost n characters of string x$.
Format: v$ = RIGHT$(x$,n)
Example: Ok
10 A$="BOCA RATON, FLORIDA"
20 PRINT RIGHT$(A$,7)
RUN
FLORIDA
Ok
4-248
RND
Function
Format: v = RND[(x)]
4-249
RND
Function
Example: Ok
10 FOR 1=1 TO 3
20 PRINT RND(I); I x>0
30 NEXT I
40 PRINT: X=RND(-6) I x<0
50 FOR 1=1 TO 3
60 PRINT RND(I); I x>0
70 NEXT I
80 RANDOMIZE 853 'randomize
90 PRINT: X=RND(-6) I x<0
100 FOR 1=1 TO 3
110 PRINT RND; I same as x>0
120 NEXT I
130 PRINT: PRINT RND(0)
RUN
.6291626 .1948297 .6305799
.6818615 .4193624 .6215937
.6818615 .4193624 .6215937
.6215937
Ok
4-250
RUN
Command
Purpose: Begins execution of a program.
RUN filespec[, R]
4-251
RUN
Command
Example: Ok
10 PRINT 1/7
RUN
.1428571
Ok
10 PI=3.141593
20 PRINT PI
RUN 20
o
Ok
In this first example, we use the first form of RUN
on two very small programs. The first program is run
from the beginning. We used the RUN line option
for the second example to run the program from
line 20. In this case, line 10 does not get executed, so
PI does not receive its proper value. A 0 is printed
because all numeric variables have an initial value of
zero.
RUN 'ICAS1 :NEWFI L",R
4-252
SAVE
Command
SAVEfilespec [,P]
4-253
SAVE
Command
in ASCII format. For example, a file intended to be
merged must be saved in ASCII format. Programs
saved in ASCII may be read as data files.
SAVE "B:PROG",A
SAVE "A:SECRET.BOZ",P
4-254
SCREEN
Function
Purpose: Returns the ASCII code (0-255) for the character on
the active screen at the specified row (line) and
column.
Format: v = SCREEN(row,col[,z])
4-255
SCREEN
Function
Refer to "COLOR Statement" for a list of colors and
their associated numbers.
4-256
SCREEN
Statement
4-257
SCREEN
Statement
apage (active page) is an integer expression in the
range 0 to 7 for width 40, or 0 to 3 for
width 80. It selects the page to be written
to by output statements to the screen, and
is valid in text mode (mode=O) only.
4-258
SCREEN
Statement
Any parameter may be omitted. Omitted
parameters, except vpage, assume the old value.
20 SCREEN ,,1,2
30 SCREEN 2,,0,0
40 SCREEN 1,0
Switches to medium resolution color graphics.
50 SCREEN ,1
Sets medium resolution graphics with color off.
4-259
SGN
Function
Format: v = SGN(x)
4-260
SIN
Function
Purpose: Calculates the trigonometric sine function.
Format: v = SIN(x)
Example: Ok
10 PI=3.141593
20 DEGREES = 90
30 RADIANS=DEGREES * PI/180 I PI/2
40 PRINT SIN(RADIANS)
RUN
1
Ok
4-261
SOUND
Statement
Purpose: Generates sound through the speaker.
4-262
SOUND
Statement
The tuning note, A, has a frequency of 440. The
following table correlates notes with their
frequencies for two octaves on either side of middle
C.
C 130.810 C* 523.250
D 146.830 D 587.330
E 164.810 E 659.260
F 174.610 F 698.460
G 196.000 G 783.990
A 220.000 A 880.000
B 246.940 B 987.770
C 261.630 C 1046.500
D 293.660 D 1174.700
E 329.630 E 1318.500
F 349.230 F 1396.900
G 392.000 G 1568.000
A 440.000 A 1760.000
B 493.880 B 1975.500
4-263
SOUND
Statement
The next table shows typical tempos in terms of
clock ticks:
Beats/ Ticks/
Tempo Minute Beat
I
Larghetto 60-66 18.2-16.55
Grave
Lento
Adagio 66-76 16.55-14.37
,
slow Adagietto
t Andante 76-108 14.37-10.11
medium Andantino
Moderato 108-120 10.11-9.1
fast Allegretto
I
Allegro 120-168 9.1-6.5
Vivace
Veloce
Presto 168-208 6.5-5.25
very fast Prestissimo
4-264
SPACE$
Function
Format: v$ = SPACE$(n)
Example: Ok
10 FOR I = 1 TO 5
20 X$ = SPACE$(I)
30 PRINT X$; I
40 NEXT I
RUN
1
2
3
4
5
Ok
4-265
SPC
Function
Purpose: Skips n spaces in a PRINT statement.
Example: Ok
PRINT "OVER" SPC(lS) "THERE"
OVER THERE
Ok
4-266
SQR
Function
Purpose: Returns the square root of x.
Format: v = SQR(x)
Example: Ok
10 FOR X = 10 TO 25 STEP 5
20 PRINT X, SQR(X)
30 NEXT
RUN
10 3.162278
15 3.872984
20 4.472136
25 5
Ok
4-267
STICK
Function
Format: v = STICK(n)
4-268
STICK
Function
Example: 10 PRI NT IIJoyst i ck BII
20 PRINT "x coordinate","y coordinate"
30 FOR J=l TO 100
40 TEMP=STICK(0)
50 X=STICK(2): Y=STICK(3)
60 PRINT X,Y
70 NEXT
This program takes 100 samples of the coordinates
of joystick B and prints them.
4-269
STOP
Statement
Purpose: Terminates program execution and returns to
command level.
Format: STOP
4-270
STOP
Statement
Example: 10 I NPUT A, B
20 TEMP= A:;\°B
30 STOP
40 FINAL = TEMP+200: PRINT FINAL
RUN
? 26,2.1
Break in 30
Ok
PRINT TEMP
54.6
Ok
CaNT
254.6
Ok
This example calculates the value of TEMP, then
stops. While the program is stopped, we can check
the value of TEMP. Then we can use CONT to
resume program execution at line 40.
4-271
STR$
Function
Purpose: Returns a string representation of the value of x.
Format: v$ = STR$(x)
Ok
? STR$(321); LEN(STR$(321))
321 4
Ok
4-272
STRIG
Statement and Function
Format: As a statement:
STRIG ON
STRIG OFF
As a function:
v = STRIG(n)
4-273
STRIG
Statement and Function
In Advanced BASIC and the BASIC Compiler,
you can read four buttons from the joysticks.
The additional values for n are:
4-274
STRIG
Function
Format: v = STRIG(n)
4-234a
NOTES
4-234h
STRIG(n)
Statement
Purpose: Enables and disables trapping of the joystick
buttons.
Format: STRIG(n) ON
STRIG(n) OFF
STRIG(n) STOP
o button Al
2 button BI
4 button A2
6 button B2
Format: v$ = STRING$(n,m)
v$ = STRING$(n,x$)
Example: 0 k ".
10 X$ STRING$(10,4S)
20 PRINT X$ "MONTHLY REPORT" X$
RUN
----------MONTHLY REPORT----------
Ok
The first example repeats an ASCII value of 45 to
print a string of hyphens,
Ok
10 X$="ABCD"
20 Y$+STRING$(10,X$)
30 PRINT Y$
RUN
AAAAAAAAAA
Ok
4-276
STRIG(n)
Statement
Format: STRIG(n) ON
STRIG(n) OFF
STRIG(n) STOP
n may be 0, 2, 4, or 6, as follows:
o button Al
2 button.Bl
4 button A2
6 button B2
4-236a
NOTES
4-236b
SWAP
Statement
Example: Ok
1.0 A$=" ONE II : B$=" ALL"
20 PRINT A$ C$ B$
3.0 SWAP A$, B$
4.0 PRINT A$ C$ B$
RUN
ONE FOR ALL
ALL FOR ONE
Ok
4-277
SYSTEM
Command
Purpose: Exits BASIC and returns to DOS.
Format: SYSTEM
4-278
TAB
Function
L. M. JACOBS $25.00
Ok
4-279
TAN
Function
Purpose: Returns the trigonometric tangent of x.
Format: v = TAN(x)
Example: Ok
10 PI=3.141593
20 DEGREES=45
30 PRINT TAN(DEGREES*PI/180)
RUN
1
Ok
4-280
TIME$
Variable and Statement
Format: As a variable:
v$ = TIME$
As a statement:
TIME$ =x$
4-281
TIME$
Variable and Statement
A leading zero may be omitted from any of the
above values, but you must include at least one digit.
For example, if you wanted to set the time as a half
hour after midnight, you could enter
TIME$="O: 30", but not TIME$=": 30". If any of the
values are out of range, an "Illegal function call"
error is issued. The previous time is retained. If x$ is
not a valid string, a "Type mismatch" error results.
10 CLS
20 LOCATE 10,15
30 PRINT TIME$
40 GOTO 3.0
4-282
TRON and TROFF
Commands
Purpose: Traces the execution of program statements.
Format: TRON
TROFF
Example: Ok
10 K= 10
20 FOR J=l TO 2
30 L=K + 10
40 PRINT J;K;L
50 K=K+10
6CJ NEXT
70 END
TRON
Ok
RUN
[10J[20J[30J[40J 1 10 20
[50J[60J[30J[40J 2 20 30
[50J[60J[70J
Ok
TROFF
Ok
This example uses TRON and TROFF to trace
execution of a loop. The numbers in brackets are
line numbers; the numbers not in brackets at the
end of each line are the values of J, K, and L which
are printed by the program.
4-283
USR
Function
Format: v = USR[n](arg)
4-284
VAL
Function
Purpose: Returns the numerical value of string xl.
Format: v = VAL(x$)
V AL(" -3")
returns -3.
Example: Ok
PRINT VAL("3408 SHERWOOD BLVD.")
3408
Ok
4-285
VARPTR
Function
Purpose: Returns the address in memory of the variable or file
control block.
Format: v = VARPTR(vartable)
v = VARPTR(#/ilenum)
4-286
VARPTR
Function
form VARPTR(A(O» is usually specified when
passing an array, so that the lowest-addressed
element of the array is returned.
10 OPEN "DATA.FIL" AS #1
20 GET #1
30 'get address of control block
40 FCBADR = VARPTR(#l)
50 'figure address of data buffer
60 DATADR = FCBADR+188
70 'get first byte in data buffer
80 A% = PEEK(DATADR)
10 DEFINT A-Z
20 DATA1=500
30 P=VARPTR(DATA1)
40 V=PEEK(P) + 256*PEEK(P+1)
50 PRINT V
4-287
VARPTR$
Function
Format: v$ = VARPTR$(varlable)
2 integer
3 string
4 single-precision
8 double-precision
4-288
VARPTR$
Function
2 integer
3 string
4 single-precision
8 double-precision
VARPTR$
Function
CHR$(type)+MKI$(VARPTR(variable))
4-246b
VARPTR$
Function
The returned value is the same as:
4-289
WAIT
Statement
Purpose: Suspends program execution while monitoring the
status of a machine input port.
4-290
WAIT
Statement
The WAIT statement lets you test one or more bit
positions on an input port. You can test the bit
position for either a 1 or a o. The bit positions to be
tested are specified by setting l' s in those positions
in n. If you do not specify m, the input port bits are
tested for 1 'so If you do specify m, a 1 in any bit
position in m (for which there is a 1 bit in n) causes
WAIT to test for a 0 for that input bit.
4-291
WHILE and WEND
Statements
Purpose: Executes a series of statements in a loop as long as a
given condition is true.
(loop statements)
WEND
4-292
WHILE and WEND
Statements
Example: This example sorts the elements of the string array
A$ into alphabetical order. A$ was defined with J
elements.
4-293
WIDTH
Statement
4-294
WIDTH
Statement
If the screen is in high resolution graphics
mode (as would occur with a SCREEN 2
statement), WIDTH 40 forces the screen
into medium resolution (like a SCREEN 1
statement).
WIDTHjilenum,size
The width of the device associated with
/llenum is immediately changed to the new
size specified. This allows the width to be
changed at will while the file is open. This
form of WIDTH has meaning only for
LPTl: in Cassette BASIC. Disk and
Advanced BASIC also allow LPT2:, LPT3:,
COMl: and COM2:.
4-295
WIDTH
Statement
Any value entered outside of the ranges indicated
will result in an "Illegal function call" error. The
previous value is retained.
4-296
WIDTH
Statement
Example: 10 WIDTH "L PT 1 : " ,75
20 OPEN "LPT1:" FOR OUTPUT AS #1
4-297
WRITE
Statement
Purpose: Outputs data on the screen.
4-298
WRITE #
Statement
list of expressions
is a list of string and/or numeric
expressions, separated by commas or
semicolons.
4-299
WRITE #
Statement
Example: Let A$="CAMERA" and B$="93604-1". The
statement:
WRITE #l,A$,B$
"CAMERA" ,"93604-1"
A subsequent INPUT # statement, such as:
INPUT #1,A$,B$
4-300
APPENDIXES
Contents
APPENDIX A. MESSAGES ................ A-5
A-I
APPENDIX D. CONVERTING PROGRAMS TO
IBM PERSONAL COMPUTER BASIC... D-1
A-2
APPENDIX G. ASCII CHARACTER
CODES ................................. G-l
A-3
NOTES
A-4
Appendix A. Messages
Number Message
A-5
Number Message
4 Out of data
A READ statement is trying to read more
data than is in the DATA statements.
6 Overflow
The magnitude of a number is too large to
be represented in BASIC's number format.
Integer overflow will cause execution to
stop. Otherwise, machine infinity with the
appropriate sign is supplied as the result
and execution continues.
7 Out of memory
A program is too large, has too many FOR
loops or GOSUBs, too many variables,
expressions that are too complicated, or
complex painting.
A-7
Number Message
10 Duplicate Definition
You tried to define the size of the same
array twice. This may happen in one of
several ways:
11 Division by zero
In an expression, you tried to divide by
zero, or you tried to raise zero to a negative
power.
A-8
Number Message
12 Illegal direct
You tried to enter a statement in direct
mode which is invalid in direct mode (such
as DEF FN).
13 Type mismatch
You gave a string value where a numeric
value was expected, or you had a numeric
value in place of a string value. This error
may also be caused by trying to SWAP
variables of different types, such as
single- and double-precision.
A-9
Number Message
17 Can't continue
You tried to use CONT to continue a
program that:
19 No RESUME
The program branched to an active error
trapping routine as a result of an error
condition or an ERROR statement. The
routine does not have a RESUME
statement. (The physical end of the
program was encountered in the error
trapping routine.)
A-I0
Number Message
22 Missing operand
An expression contains an operator, such
as * or OR, with no operand following it.
24 Device Timeout
BASIC did not receive information from
an input/output device within a
predetermined amount of time. In
Cassette BASIC, this only occurs while the
program is trying to read from the cassette
or write to the printer.
25 Device Fault
A hardware error indication was returned
by an interface adapter.
A-tt
Number Message
27 Out of Paper
The printer is out of paper, or the printer is
not switched on.
A-12
Number Message
50 FIELD overflow
A FIELD statement is attempting to
allocate more bytes than were specified for
the record length of a random file in the
OPEN statement. Or, the end of the
FIELD buffer is encountered while doing
sequential I/O (PRINT #, WRITE #,
INPUT #) to a random file.
51 In ternal error
An internal malfunction occurred in
BASIC.
A-13
Number Message
A-14
Number Message
When receiving communications data,
this error can occur from overrun,
framing, break, or parity errors. When you
are receiving data with 7 or less data bits,
the eighth bit is turned on in the byte in
error.
61 Disk full
All diskette storage space is in use. Files
are closed when this error occurs.
A-15
Number Message
68 Device Unavailable
You tried to open a file to a device which
doesn't exist. Either you do not have the
hardware to support the device (such as
A-16
Number Message
SYSTEM
A-17
Number Message
73 Advanced Feature
Your program used an Advanced BASIC
feature while you were using Disk BASIC.
Unprintable error
An error message is not available for the
error condition which exists. This is
usually caused by an ERROR statement
with an undefined error code.
A-IS
Appendix B. BASIC Diskette Input
and Output
B-1
Specifying Filenames
B-2
RUN filespec [,R]
RUN filespec loads the program from
diskette into memory and runs it. RUN
deletes the current contents of memory
and closes all files before loading the
program. If the R option is included,
however, all open data files are kept open.
MERGE filespec
Loads the program from diskette into
memory, but does not delete the current
contents of memory. The program line
numbers on diskette are merged with the
line numbers in memory. If two lines have
the same number, only the line from the
diskette program is saved. After a MERGE
command, the "merged" program resides
in memory, and BASIC returns to
command level. .
KILL filespec
Deletes the file from the diskette.
Protected Files
If you wish to save a program in an encoded binary
format, use the P (protect) option with the SAVE
command. For example:
SAVE "MYPROG", P
B-3
Diskette Data Files - Sequential and
Random I/O
Sequential Files
Sequential files are easier to create than random files
but are limited in flexibility and speed when it
comes to accessing the data. The data that is written
to a sequential file is stored sequentially, one item
after another, in the order that each item is sent.
Each item is read back in the same way, from the
first item in the file, to the last item.
CLOSE WRITE #
INPUT # EOF
LINE INPUT # INPUT$
OPEN LOC
PRINT # LOF
PRINT # USING
B-5
Program 1
NAME? DONE
Ok
Now look at PROGRAM2. It accesses the file
"DATA" that was created in PROG RAMI and
displays the name of everyone hired in 1978.
Program 2
B-7
Random Files
Creating and accessing random files requires more
program steps than sequential files, but there are
l
CLOSE CVI
FIELD CVS
GET LOC
LSET/RSET LOF
OPEN MKD$
PUT MKI$
CVD MKS$
B-s
Creating a Random File
The following program steps are required to create a
random file.
B-9
Look at PROGRAM3. It takes information that is
entered at the keyboard and writes it to a random
file. Each time the PUT statement is executed, a
record is written to the file. The two-digit code that
is input in line 30 becomes the record number.
Program 3
B-IO
4. The data in the buffer may now be accessed by
the program. Numeric values must be
converted back to numbers using the
"convert" functions: CVI for integers, CVS for
single-precision values, and CVD for
double-precision values.
Program 4
B-11
An Example Program
B-12
220 INPUT "Quantity in stockB;QX
230 lSET Q$=HKI$(Q7.)
240 INPUT "Reorder leveP;R!
250 LSET R$=HKI$(R7.}
260 INPUT "Unit priceR;?
270 LSET P$=HKS$(P)
280 PUT 11,PART7.
290 RETURN
300 REM display entry
310 GOSUB 670
320 IF ASC(F$}=255 THEN PRINT uNull entry": RETURN
330 PRINT USING uPart number #iift;PARTZ
340 PRINT D$
350 PRINT USING RQuantity on hand t####";CVI(Q$)
360 PRINT USING ftReorder level Itlll";CVI(R$)
370 PRINT USING "Unit price $$II.#IR;CVS(P$)
380 RETURN
390 REM add to stock
400 BOSUB b70
410 IF ASC(F$)=255 THEN PRINT"Null entry":RETURN
420 PRINT D$:INPUT "Quantity to add";AX
430 QZ=CVI(U$)+AZ
440 lSET U$=MKI$(QZ)
450 PUT Il,PARTX
460 RETURN
470 REM remove from stock
480 BOSUS 670
490 IF ASC{F$}=255 THEN PRINT uNull entry": RETURN
500 PRINT D$
510 INPUT HQuantity to subtractH;Si.
520 Q7.=CVI(Q$}
530 IF (U7.-SZ}(O THEN PRINT"Only";Q7.;uin stock": GOTO 510
540 Qi.=UI-SI
550 IF QI.=(CVI(R$) THEN PRINT "Quantity nowH;Q!;
H, Reorder level";CVI(R$}
560 LSET Q$=MKI$(Q7.}
570 PUT 11,PARTi.
580 RETURN
590 REM list itellis below reorder level
600 FOR 1=1 TO 100
610 SET 11,1
620 IF ASC(F$)=255 THEN 640
630 IF CVI(Q$)(CVI(R$) THEN PRINT D$;" UuantityB;CVI(Q$)
TAB(50} uReorder leve}";CVI(R$}
640 NEXT I
650 RETURN
B-13
b60 REM get part record
670 INPUT "Part nUlber";PARTZ
680 IF PART1(1 OR PARTl)lOO
THEN PRINT uBad part nUiberH: 60TO 670
ELSE 6ET #l,PARTl: RETURN
690 REM initialize file
700 INPUT HAre you surel;B$: IF B$()IV AND B$<)Hyl
1
THEN RETURN
710 LSET F$=CHR$(255)
720 FOR 1=1 TO 100
730 PUT #1,1
740 NEXT I
750 RETURN
760 REM end application
770 CLOSE: END
B-14
Performance Hin ts
1 I example 1A
10 OPEN "ABC II FOR OUTPUT AS #1
20 FOR 1=1 TO 3000
30 PR I NT #1, IIMELHII
Lf0 NEXT
50 CLOSE #1: END
This example (lA) uses regular sequential I/O
to create a file with 3000 items in it.
B-15
1 I examp 1e 1B
10 OPEN I'ABC " FOR It~PUT AS #1
20 OPEN "DEF" FOR OUTPUT AS #2
30 IF EOF(l) THEN CLOSE: END
40 INPUT #l,A$
50 PRINT #2,A$
60 GOTO 30
70 END
1 I examp 1e 2B
10 OPEN "PQR" AS #1 LEN=512
20 OPEN "XYZ" AS #2 LEN=512
30 ON ERROR GOTO 80
40 GET #1
50 INPUT #l,A$
60 PRINT #2,A$
70 IF A$<>"/eaf" THEN 50 ELSE
ON ERROR GOTO 0: PUT #2: CLOSE: END
80 IF ERL=50 THEN GET #1: RESUME
ELSE PUT #2: RESUME
B-16
This final example copies the
"pseudo-sequential" file created in the
previous example to a new "pseudo-sequential"
file named "XYZ". It takes about half as long to
run as the example using sequential I/O.
B-17
NOTES
B-18
Appendix C. Machine Language
Subroutines
Reference Material
Rector, Russell and Alexy, George. The 8086
Book. Osborne/McGraw-Hill, Berkeley,
California, 1980. (includes the 8088)
C-l
Setting Memory Aside for
Your Subroutines
C-2
Inside the BASIC Workarea: In order to keep
BASIC from writing over your subroutines in
memory, use either:
10 CLEAR ,&HF000
or start BASIC with the DOS command:
N ASMROUT.EXE
L
C-5
5. Display the registers (use the R command) to
find out where the subroutine was placed in
memory. Record the values contained in the CS
and IP registers for later use.
C-6
Note: If the machine language routine is
self-relocatable, BLOAD can be used to
put the subroutine some place other than
where the linker originally placed it. If you
make such a change, be sure to make a
corresponding change to the DEF SEG
statement associated with the call so that
BASIC can find the subroutine at
execution time.
C-7
Calling the Subroutine from
Your BASIC Program
String Arguments
• If an input argument is a string, the value
received in the argument is the address of a
three-byte area called the string descriptor:
Warning:
The subroutine must not change the
contents of any of the three bytes of the string
descriptor.
A$ = "TEXT"
CALL SUBRT(A$)
C-9
• The stack pointer, at entry, indicates a stack
that has only 16 bytes (eight words) available
for use by the subroutine. If more stack space is
needed, the subroutine must set up its own
stack segment and stack pointer. You should
make sure that the location of the current stack
is recorded so its pointer can be restored just
prior to return.
CALL Statement
Machine language subroutines may be called using
the BASIC CALL statement. The format of the
CALL statement is:
C-IO
2. The return address specified in the CS register
and the offset are pushed on to the stack.
PUSH BP ;SAVE BP
MOV BP,SP ;MOVE SP TO BP
The offset into the stack of anyone particular
argument is calculated as follows:
where:
C-ll
Example: The following example adds the values
in A% and B% and stores the result in C%:
CSEG SEGMENT
ASSUME CS:CSEG
SUBRT PROC FAR
PUSH BP ;SAVE BP
MOV BP,SP ;SET BASE PARM LIST
MOV S I ,[BP]+10 ;GET ADDR PARM A
MOV AX,[SI] ;GET VALUE OF A
MOV SI ,[BP]+8 ;GET ADDR PARM B
ADD AX,[SI] ;ADD VALUE B TO REG
MOV DI,[BP]+6 ;GET ADDR PARM C
MOV [DIJ,AX ;PASS BACK SUM
POP BP ;RESTORE BP
RET 6 ;FAR RETURN TO BASIC
SUBRT ENDP
CS EG ENDS
END
C-12
As another example:
10 DEFINT A-Z
100 DEF SEG=&H1800
110 BLOAD "SUBRT.EXE",0
120 SUBRT=0
130 CALL SUBRT (A,B$,C)
The following sequence of Macro-Assembler code
shows how the arguments (including the address of a
string descriptor) are passed and accessed, and how
the result is stored in variable C:
PUSH BP ;SAVE BP
MOV BP,SP ;GET CURRENT STK POSITION INTO BP
MOV BX ,'[ BP J+8 ;GET ADDR OF B$ STRING DESCRIPTOR
MOV CL,[BXJ ;GET LENGTH OF B$ INTO CL
MOV DX,l[BXJ ;GET ADDR OF B$ TEXT INTO DX
Warning:
It is entirely up to you to make sure that the
arguments in the CALL statement match in
number, type, and length with the arguments
expected by the subroutine.
C-13
USR Function Calls
The other way to call machine language subroutines
from BASIC is with the USR function. The format of
the USR function is:
USR[n] (arg)
3 String
4 Single-precision number
8 Double-precision number
C-14
If the argument is a number and not a string, the
value of the argument is placed in the Floating Point
Accumulator (FAC), which is an eight-byte area in
BASIC's data space. In this case, the BX register
contains the offset within the BASIC data space to
the fifth byte of the eight-byte FAC. For the
following examples, assume that the FAC is in bytes
hex 49 F through hex 4A6; that is, BX contains hex
4A3:
C-15
Usually, the value returned by a USR function is the
same type (integer, string, single-precision, or
double-precision) as the argument that was passed
to it. However, a numerical argument of the
function, regardless of its type, may be forced to an
integer value by calling the FRCINT routine to get
the integer equivalent of the argument placed into
register BX.
C-16
RSEG SEGMENT AT 0F600H ;BASE OF BASIC ROM
ORG 3 ;OFFSET TO FORCE INTEGER
FRCJNT LABEL FAR
ORG 7 ;OFFSET TO MAKE INTEGER
MAKINT LABEL FAR
RSEG ENDS
CSEG SEGMENT
USRPRG PROC FAR ;ENTRY POINT
CALL FRCINT ;FORCE ARG IN FAC INTO [BX]
ADD BX,BX ;[BX] = [BX] * 2
CALL MAKINT ;PUT INT RSLT IN BX INTO FAC
RET ; INTER-SEGMENT RETURN TO BASIC
USRPRG ENDP
CSEG ENDS
C-17
NOTES
C-18
Appendix D. Converting Programs
to IBM Personal
Computer BASIC
File I/O
In IBM Personal Computer BASIC, you read and
write information to a file on diskette or cassette by
opening the file to associate it with a particular file
number; then using particular I/O statements which
specify that file number. I/O to diskette and cassette
files is implemented differently in some other
BASICs. Refer to the section in Chapter 3 called
"Files," and to "OPEN Statement" in Chapter 4 for
more specific information.
Graphics
How you draw on the screen varies greatly between
different BASICs. Refer to the discussion of
graphics in Chapter 3 for specific information about
IBM Personal Computer graphics.
D-l
IF ... THEN
The IF statement in IBM Personal Computer BASIC
contains an optional ELSE clause, which is
performed when the expression being tested is false.
Some other BASICs do not have this capability. For
example, in another BASIC you may have:
10 IF A=B THEN 30
20 PRI NT IINOT EQUAL 1 ' : GOTO 40
30 PRINT "EQUAL II
40 REM CONTINUE
D-2
Line Feeds
In other BASICs, when you enter a line feed, a line
feed character is actually inserted into the text. On
the IBM Personal Computer, entering a line feed
will pad the rest of the display line with spaces - it
does not insert the line feed character. If you try to
load a program with line feed characters in it, you
will get a "Direct statement in file" error.
Logical Operations
In IBM Personal Computer BASIC, logical
operations (NOT, AND, OR, XOR, IMP, and EQV)
are performed bit-by-bit on integer operands to
produce an integer result. In some other BASICs,
the operands are considered to be simple "true"
(non-zero) or "false" (zero) values, and the result of
the operation is either true or false. As an example
of this difference, consider this small program:
10 A=9: B=2
20 IF A AND B THEN PRINT "BOTH A AND B ARE TRUE"
10 A=9: 8=2
20 IF (A<>O) AND (B~>O)
THEN PRINT !JBOTH A AND B ARE TRUE!!
D-3
MAT Functions
Programs using the MAT functions available in
some BASICs must be rewritten using FOR... NEXT
loops to execute properly.,
Multiple Assignments
Some BASICs allow statements of the form:
10 LET 8=C=0
to set Band C equal to zero. IBM Personal
Computer BASIC would interpret the second equal
sign as a logical operator and set B equal to -1 if C
equaled o. Instead, convert this statement to two
assignment statements:
10 C=0:8=0
Multiple Statements
Some BASICs use a backslash (\) to separate
multiple statements on a line. With IBM Personal
Computer BASIC, be sure all statements on a line
are separated by a colon (:).
0-4
Relational Expressions
In IBM Personal Computer BASIC,the value
returned by a relational expression, such as A>B, is
either -1, indicating the relation is true, of 0,
indicating the relation is false. Some other BASICs
return a positive 1 to indicate true. If you use the
value of a relational expression in an arithmetic
calculation, the results are likely to be different
from what you want.
Remarks
Some BASICs allow you to add remarks to the end of
a line using the exclamation point (!). Be sure to
change this to a single quote (') when converting to
IBM Personal Computer BASIC.
Rounding of Numbers
IBM Personal Computer BASIC rounds single- or
double-precision numbers when it requires an
integer value. Many other BASICs truncate instead.
This can change the way your program runs, because
it affects not only assignment statements (for
example, 1%=2.5 results in 1% equal to 3), but also
affects function and statement evaluations (for
example, TAB(4.5) goes to the fifth position, A(1.5)
is the same as A(2), and X=11.5 MOD 4 will result in
°
a value of for X). Note in particular that rounding
may cause IBM Personal Computer BASIC to select
a different element from an array than another
BASIC - possibly one that is out of range!
D-5
String Handling
String Length: Since strings in IBM Personal
Computer BASIC are all variable length, you should
delete all statements that are used to declare the
length of strings. A statement such as DIM A$(I,]),
which dimensions a string array for] elements of
length I, should be converted to the IBM Personal
Computer BASIC statement DIM A$O).
D-6
Use of Blanks
Some BASICs allow statements with no separation
of keywords:
20FORI=1TOX
With IBM Personal Computer BASIC be sure all
keywords are separated by a space:
20 FOR 1=1 TO X
Other
The BASIC language on another computer may be
different from the IBM Personal Computer BASIC
in other ways than those listed here. You should
become familiar with IBM Personal Computer
BASIC as much as possible in order to be able to
appropriately convert any function you may
require.
D-7
NOTES
D-8
Appendix E. Mathematical Functions
Function Equivalent
E-1
If you use these functions, a good way to code them
would be with the DEF FN statement. For example,
instead of coding the formula for inverse hyperbolic
sine each time you need it, you could code:
DEF FNARCSINH(X) = LOG(X+SQR(X*X+l))
FNARCSINH(Y)
each time you need it.
E-2
Appendix F. Communications
Communication I/O
Since each communications adapter is opened as a
file, all input/output statements that are valid for
diskette files are valid for communications.
INPUT #
LINE INPUT #
INPUT$
PRINT #
PRINT # USING
WRITE #
F-l
GET and PUT for Communications Files
GET and PUT are only slightly different for
communications files than for diskette files. They
are used for fixed length I/O from or to the
communications file. In place of specifying the
record number to be read or written, you specify the
number of bytes to be transferred into or out of the
file buffer. This number cannot exceed the value set
by the LEN option on the OPEN "COM ...
statemen t. Refer to the GET and PUT sections in
Chapter 4.
110 Functions
The most difficult aspect of asynchronous
communication is being able to process characters
as fast as they are received. At rates of 1200 bps or
higher, it may be necessary to suspend character
transmission from the other coniputer long enough
to "catch up." This can be done by sending XOFF
(CHR$(19» to the other computer and XON
(CHR$(17» when ready to resume. XOFF tells the
other computer to stop sending, and XON tells it it
can start sending again.
INPUT$ Function
The INPUT$ function is preferred over the
INPUT # and LINE INPUT # statements when
reading communications files, since all ASCII
characters may be significant in communications.
INPUT # is least desirable because input stops when
a comma (,) or carriage return is seen. LINE
INPUT # stops when a carriage return is seen.
190 WEND
F-3
In order to process characters quickly, you should
avoid, if possible, examining every character as you
receive it. If you are looking for special characters
(such as control characters), you can use the INSTR
function to find them in the input string.
An Example Program
The following program enables the IBM Personal
Computer to be used as a conventional" dumb"
terminal in a full duplex mode. This program
assumes a 300 bps line and an input buffer of 256
bytes (the Ie: option was not used on the BASIC
command).
F-4
500 'send keyboard input to com 1 ine
510 B$=INKEY$: IF B$<>'"' THEN PRINT #l,B$;
520 'if no chars in com buffer, check key in
530 IF EOF(1) THEN 510
540 'i f buffer more than 1/2 full, then
550' set PAUSE flag to say input suspended,
560' send XOFF to host to stop transmission
570 IF LOC(1»128 THEN PAUSE=TRUE: PRINT #l,XOFF$;
580 'read contents of com buffer
590 A$=INPUT$(LOC(1) ,#1)
600 'get rid of 1 inefeeds to avoid double spaces
610' when input displayed on screen
620 LFP=0
630 LFP=INSTR(LFP+1 ,A$,CHR$(10)) 'look for LF
640 IF LFP>0 THEN MI D$ (A$, LFP ,1)=" ": GOTO 630
650 'display com input, and check for more
660 PRINT #2,A$;: IF LOc(1»0 THEN 570
670 'if transmission suspended by XOFF,
680' resume by send i ng XON
690 IF PAUSE THEN PAUSE=FALSE: PRINT #l,XON$;
700 'check for keyboard input again
710 GOTO 510
8999 'i f error, d i sp 1ay error number and ret ry
9000 PRINT "ERROR NO.";ERR: RESUME
• °
Note in line 51 that INKEY$ will return a null
string if no character is pending.
F-5
Operation of Control Signals
This section contains more detailed technical
information that you may need to know to
communicate with another computer or peripheral
from BASIC.
F-6
Use of Input Control Signals
Normally, if either the CTS (Clear To Send) or DSR
(Data Set Ready) lines are OFF, then an OPEN
"COM ... statement will not execute. After one
second, BASIC will return with a "Device Timeout"
error (error code 24). The Carrier Detect
(sometimes called Receive Line Signal Detect) can
be either ON or OFF; it has no effect on the
operation of the program.
F-7
If you need to test for any of these signals in a
program, you can check the bits corresponding to
these signals in the MODEM Status Register on the
Asynchronous Communications Adapter. To read
the eight bits in this register, you use the INP
function-use INP(&H3FE) to read the register on
an unmodified communications adapter, and
INP(&H2FE) to read the register on a modified
communications adapter. See the" Asynchronous
Communications Adapter" section of the Technical
Reference manual for a description of which bits in
the Status Register correspond to which control
signals. You can also use the Delta bits in this
register to determine if transient signals have
appeared on any of the control lines. Note that for a
control signal to have meaning, the pin
corresponding to that signal must be connected in
the cable to your modem or to the other computer.
You can also test for bits in the Line Status Register
on the Asynchronous Communications Adapter.
Use INP(&H3FD) to access this register on an
unmodified communications adapter, and
INP(&H2FD) to access it on a modified
communications adapter. Again, the bits are
described in the IBM Personal Computer Technical
Reference manuaL These bits ·can be used to
determine what types of errors have occurred on
receipt of characters from the communications line
or whether a break signal has been detected.
F-8
You can also modify bits in the Line Control
Register on the Asynchronous Communications
Adapter. You should be careful in modifying these
bits as most of the bits in this register have been set
by BASIC at the time an OPEN statement is
executed and changing a bit could cause
communications failure. The Line Control Register
is at address &H3 FB on an unmodified
communications adapter and at address &H2FB on a
modified communications adapter.
F-9
Communication Errors
Errors occur on communication files in the
following order:
F-IO
Appendix G. ASCII Character Codes
G-l
ASCII Control ASCII
value Character character value Character
000 (null) NUL 032 (space)
001 © SOH 033 !
002
003 •• STX
ETX
034
035 #=
004
005
•• EDT
ENG
036
037
$
%
006
007
•
(beep)
ACK
BEL
038
039
&
008 a 8S 040
009 (tab) HT 041
010 (line feed) LF 042 *
011 (home) VT 043 +
012 (form feed) FF 044
013 (carriage return) CR 045 ",.,.
014 n SO 046
015
016
017
..
~
....
SI
OLE
DC1
047
048
049
/
0
1
018 DC2 050 2
019 *!! DC3 051 3
020 qr DC4 052 4
021 § NAK 053 5
022
023
-
i
SYN
ETB
054
055
6
7
024 t CAN 056 8
025 ~ EM 057 9
026 .-.. SUB 058
027 ..- ESC 059
028 (cursor right) FS 060 <
029 (cursor left) GS 061
030 (cursor up) RS 062 >
031 (cursor down) US 063 ?
G-2
ASCII ASCII
value Character value Character
064 @ 096
065 A 097 a
066 B 098 b
067 C 099 c
068 D 100 d
069 E 101 e
070 F 102 f
071 G 103 9
072 H 104 h
073 I 105
074 J 106 j
075 K 107 k
076 L 108
077 M 109 m
078 N 110 n
079 0 111 0
080 P 112 p
081 Q 113 q
082 R 114
083 S 115
084 T 116 t
085 U 117 u
086 V 118 v
087 W 119 w
088 X 120 x
089 y 121 y
090 Z 122 z
091 [ 123
092 \ 124
093 ] 125
094 1\ 126
095 127 0
G-3
ASCII ASCII
value Character value Character
128 C 160 a
129 U 161
130 e 162 6
131 a 163 u
132 a 164 n
133 a 165 N
134 a 166 ~
135 <; 167 Q
A
136 e 168
137 e 169 r-
138 e 170 --,
139 I 171 Y2
140 172 %
141 173
142 A 174 «
143 A 175 »
144 E 176 .....
145 re 177 ::::;:;::
146 A: 178 :;:;:;:;:;:
147 '"0 179
148 0 180 --1
149 0 181 =9
150 ""u 182 --ll
151 U 183 11
153 0 185 ~I
154 U 186 II
155 ¢ 187 ~
156 £ 188 ~
158 Pt 190 d
159 f 191 -,
G-4
ASCII ASCII
value Character value Character
192 L 224 a
193 ...L 225 ~
194 I 226 r
195 I- 227 7r
196 228 L
197 + 229 a-
198 1= 230 fJ.
199 II- 231 T
205 237 0
.JL
206 ,r 238 E
207 ='= 239 n
208 ...IL.
240 =
209 '9 241 ±
210 .,., 242 ~
215 247
216
217
*+:
..J
248
249
0
•
~
218 r 250
219
220 •- 251
252 n
~
221 I 253
222
223
- I 254
255
•
(blank 'F F/)
G-5
Extended Codes
G-6
Second Code Meaning
G-7
NOTES
G-8
Appendix H. Hexadecimal
Conversion Table
1 1 10 16
2 2 20 32
3 3 30 48
4 4 40 64
5 5 50 80
6 6 60 96
7 7 70 112
8 8 80 128
9 9 90 144
A 10 Ao 160
B 11 BO 176
C 12 CO 192
D 13 DO 208
E 14 EO 224
F 15 FO 240
H-1
NOTES
H-2
Appendix I. Technical Information
and Tips
1-1
Memory Map
The following is a memory map for Disk and
Advanced BASIC. DOS and the BASIC extensions
are not present for Cassette BASIC. Addresses are in
hexadecimal in the form segment:offset.
0000:0000
system
0060:0000
.t
~h.::
DOS
~i
ps1:0000
PS1:0100 DOS workarea
1 .. t
~
~ Notes:
BASIC
GO
1~
c: ~
.i.:';
!: :t
extensions '" CI
is"i 'a CI
cc"i 1. PS refers to DOS
Program Segment
OS2:0000 1 ! »{~
interpreter E!~
Doo:t
2. DS refers to BASIC's
workarea
t~ Data Segment
BASIC
program
scalar variables
Te
:t
e
'j(
3. the number xxxx is in
locations DS: 30 and
DS: 31 (low byte, high
arrays e
In
byte)
~
¥
string 4. the number yyyy is in
space locations DS:358,
t",
I DS:359 (low byte,
1 high byte) .
top of memory In
BASIC N~
or
stack -
In .J:j>
OS2:FFFF
5. or set by CLEAR
command
;;,~ ~
J ~
AOOO:OOOO
system
(includes screen buffers)
F400:0000
read·only memory
1-2
How Variables Are Stored
Scalar variables are stored in BASIC's data area as
follows:
Byte o 2 3 4 4+/ength
~--~~------------------~ ~~v~~v-~----------~
name data
type char chars 2,3,4, or 8 bytes
~--~~--~~--~----~--~A~AVr-~------------
2 integer
3 string
4 single-precision
8 double-precision
J,;·3
For string variables, data is the string descriptor:
I-4
Information contained in the file control block is as
follows (offsets are relative to the value returned by
VARPTR):
43 3 (reserved)
46 1 Device number:
0,1 - Diskette drives A: and B:
248 - LPT3:
249 - LPT2:
250 - COM2:
251 - COM1:
252 - CAS1:
253 - LPT1:
254 - SCRN:
255 - KYBD:
1-5
Offset Length Description
47 1 Device width.
185 1 (reserved)
1-6
Keyboard Buffer
Characters typed on the keyboard are saved in the
keyboard buffer until they are processed. Up to 15
characters can be held in the buffer; if you try to
type more than 15 characters, the computer beeps.
INKEY$ will read only one character from the
keyboard buffer even if there are several characters
pending there. INPUT$ can be used to read
multiple characters; however, if the requested
number of characters are not already present in the
buffer, BASIC will wait until enough characters are
typed.
The system keyboard buffer may be cleared by the
following lines of code:
DEF SEG=0: POKE 1050, PEEK(1052)
This technique could be useful, for example, to
clear the buffer before you ask the user to "press any
key."
BASIC has its own line buffer, where the program
editor acts on characters that are received from the
system keyboard buffer. BASIC's line buffer may be
cleared using the following code:
DEF SEG: POKE 106,0
Switching Displays
If you have both the Color/Graphics Monitor
Adapter and the IBM Monochrome Display and
Parallel Printer Adapter in your IBM Personal
Computer, the one BASIC will normally write to
would be the Monochrome Display. However, you
can switch from one display to the other from
BASIC by using the following code:
1-8
Some Techniques with Color
Sixteen Background Colors: In text mode, if you
are willing to give up blink, you can get all 16 colors
(0-15) for the background color. Do the following:
1-9
Tips and Techniques
GENERAL
Do
100 FOR 1=1 TO 10: READ A(I): NEXT 1
Instead of
Do Instead of
1-10
• Use simple arithmetic. In general, addition
is performed faster than multiplication, and
multiplication is faster than division or
exponentiation.
Do Instead of
250 B=A,',. 5 250 B=A/2
500 8=A+A 5100 B=A,',2
650 B=A,',A,',A-k 650 B=A"'3
750 B%=A%\4 750 B%=INT(A%/4)
Do
10 FOR 1=1 TO 10
20 A ( I ) = 30 l i n i t i ali ze A
30 NEXT 1
Instead of
10 FOR 1=1 TO 10
15 l i n i t i ali ze A
20 A(I)=30
30 NEXT I
1-11
where in memory that line is. Therefore BASIC
has to search through the line numbers in the
program, starting at the beginning, to find the
line it's looking for.
LOGIC CONTROL
Do
200 IF A=B AND C=D THEN Z=12 ELSE Z=B
Instead of
1-12
A typical record group looks like this:
A Header
B Customer name and address
C Transaction
C Transaction
C Transaction
D Trailer
Do
1-13
Another example of ordering IF statements in a
cascade so less tests need to be performed:
DO
LOOPS
1-14
For example:
Do Instead of
290 1=1+1
300 NEXT I 300 IF 1<=10 THEN 210
• Remove unnecessary code from loops. This
includes statements which don't affect the
loop, as well as non-executable statements such
as REM and DATA. For example:
Do
10 A=B+l
20 FOR X=l TO 100
30 IF D(X»A THEN D(X)=A
40 NEXT X
Instead of
Do Instead of
200 DATA 5, 12, 19 1t3 200 FOR 1=1 TO 100
210 FOR 1=1 TO 100 210 DATA 5, 12, 1943
I-16
Appendix]. Glossary
J-1
alloca te: To assign a resource, such as a diskette
file or a part of memory, to a specific task.
J-2
baud: A unit of signalling speed equal to the
number of discrete conditions or signal events per
second.
J-3
call: To bring a computer program, a routine, or a
subroutine into effect, usually by specifying the
entry conditions and jumping to an entry point.
J-4
concatenation: The operation that joins two
strings together in the order specified, forming a
single string with a length equal to the sum of the
lengths of the two strings.
J-5
DOS: Disk Operating System. In this book, refers
only to the IBM Personal Computer Disk Operating
System.
J-6
fault: An accidental condition that causes a device
to fail to perform in a required manner.
J-7
function key: One of the ten keys labeled FI
through FlO on the left side of the keyboard.
J-8
initialize: To set counters, switches, addresses, or
contents of memory to zero or other starting values
at the beginning of, or at prescribed points in, the
operation of a computer routine.
J-9
light pen: A light sensitive device that is used to
select a location on the display by pointing it at the
screen.
J-10
matrix: An array with two or more dimensions.
J-11
operating system: Software that controls the
execution of programs; often used to refer to DOS.
J-12
prec1s10n: A measure of the ability to distinguish
between nearly equal values.
J-13
resolution: In computer graphics, a measure of
the sharpness of an image, expressed as the number
of lines per unit of length discernible in that area.
J-14
storage: A device, or part of a device, that can
retain data. Memory.
J-15
typematic key: A key that repeats as long as you
hold it down.
J-16
INDEX
X-I
buffer clearing the keyboard
communications 2-5,· buffer 1-7
4-195 clock 3-44, 4-262
keyboard 1-7 CLOSE 4-46
reading the 4-119, CLS 4-48
4-127 COLOR 4-49
random file 2-5, 4-106, in graphics modes
4-163, 4-230 3-42, 4-54
screen 3-41,4-31,4-33 in text mode 3-40, 4-49
built-in functions COM 4-56
See functions command level 2-7, 2- 3 2
commands 4-6
comments 3-4, 4-240
COMMON 4-36,4-57
c communications 4-194,
Appendix F
CALL 4-34, C-10 buffer size 2-5
cancelling a line 2- 35 trapping 4-56,4-176
capital letters 2-12 comparisons
Caps Lock 2-12 numeric 3-23
Cassette BASIC 1-4 string 3-24
cassette II 0 B-1 complement, logical 3-25
cassette motor 4-172 complement, two's 3-27,
CAS 1 : 3-35, 4-151 3-28
CDBL 4-35 computed GOSUB/GOTO
CHAIN 4-36,4-57 4-180
changing BASIC program COM1: 3-35, 1-8
2-36 COM2: 3-35, 1-8
changing characters 2- 32 concatenation 3- 31
changing line numbers 2- 39 conjunction 3-25
changing lines anywhere on constants 3-9
the screen 2- 38 CONT 4-58
changing program lines 2- 3 7 control block, file 1-5
character color 1-9 converting
character set 3-4, character to ASCII
Appendix G code 4-24
CHR$ 4-38, G-1 degrees to radians 4-60
CINT 4-40 from number to
CIRCLE 4-41 string 4-272
CLEAR 4-44 from numbers for random
clear screen 4-48 files 4-170
clearing memory 2-38, from numeric to
4-174 octal 4-175
X-2
hexadecimal 4-115, H-1 declaring variable types
numbers from random 3-13,3-14,4-73
files 4-63 DEF FN 4-68
one numeric precision to DEF SEG 4-71
another 3-18 DEF USR 4-75
radians to degrees 4-25 DEFtype (-INT, -SNG,
string to numeric 4-285 -DBL, -STR) 3-14, 4~73
converting programs to IBM Del key 2-27
Personal Computer BASIC DELETE 3-3,4-76
Appendix D deleting a file 4-136
coordinates 3-43 deleting a program 2-38,
copy display 2-13 4-174
correcting current line 2-32 deleting arrays 4-87
COS 4-60 deleting characters 2- 33
cosine 4-60 deleting program lines 2- 37,
CSNG 4-61 4-76
CSRLIN 4-62 delimiting reserved
Ctrl 2-13 words 3-6
Ctrl-Break 2-17, 2-29 descriptor, string 1-4
Ctrl-Num Lock 2-17 device name 3- 34, 3- 35
cursor 2-19 Device Timeout 4-162, A-II
cursor control keys 2-19 DIM 4-77
Cursor Down key 2-21 dimensioning arrays 3-15,
Cursor Left key 2-22 4-77
cursor position 4-62,4-155, DIR 4-97
4-215 direct mode 2- 7, 4-178
Cursor Right key 2-22 disjunction 3-25
Cursor Up key 2-21 diskette I/O Appendix B
CVI, CVS, CVD 4-63, B-ll display adapters 3-38,
4-31,4-205, 1-8
display pages 3-41, 4-258
display program lines 4-147
display screen, using 3- 38
division 3-21
D division by zero A-8
double-precision 3-11,
DATA 4-64,4-238 4-35
Data Segment 4-71 DRAW 4-79
DATE$ 4-66 DS (BASIC's Data
DEBUG C-7 Segment) 4-71
decisions 4-116 duplicating a program
declaring arrays 3-15, 4-7 7 line 2-38
X-3
E erasing variables 4-44
ERL 4-89
ERR 4-89
EDIT 3-3, 4-84
ERROR 4-91
editor 2-19
editor keys 2-19 error codes 4-89,4-91,
Backspace 2-28 Appendix A
Ctrl-Break 2-29 error line 4-89
error messages Appendix A
Ctrl-End 2-25
Ctrl-Home 2-20 error trapping 4-89, 4-91,
Cursor Down 2-21 4-178,4-245
Esc key 2-28
Cursor Left 2-22
Cursor Right 2-22 event trapping
COM(n) (communications
Cursor Up 2-21
activity) 4-56, 4-176
Del 2-27
KEY(n) 4-134,4-182
End 2-25
PEN 4-185,4-206
Esc 2-28
STRIG(n) (joystick
Home 2-20
button) 4-187, 4-275
Ins 2-26
Next Word 2-23 exchanging
Previous Word 2-24 variables 4-277
exclusive or 3-25
Tab 2-30
executable statements 3- 3
ELSE 4-116
executing a program 2-4,
END 4-85
End key 2-25 4-251
end of file 4-86, B-7 EXP 4-93
ending BASIC 4-278 exponential function 4-93
Enter key 2-11 exponentiation 3-21
entering BASIC expressions
numeric 3-21
program 2-36
entering data 2-19 string 3-31
extended ASCII codes G-6
EOF 4-86, B-7
extension, filename 3- 36
equivalence 3-25
EQV 3-25
ERASE 4-87
ERASE (DOS) 4-136
erasing a file 4-136 F
erasing a program 2-38,
4-174 false 3-23, 3-25
erasing arrays 4-87 FIELD 4-94
erasing characters 2- 3 3 file control block 1-5
erasing part of a line 2- 3 5 file specification 3-34
erasing program lines 2- 3 7 , filename 3-34,3-36
4-76 filename extension 3-36
X-4
files 3-33, Appendix B, D-l graphics statements
control block 1-4 CIRCLE 4-41
file number 3- 33 COLOR 4-54
maximum number 2-4 DRAW 4-79
naming 3-34 GET 4-108
opening 3-33, 4-189 LINE 4-141
position of 4-153 PAINT 4-203
size 4-158 POINT function 4-213
FILES 4-97 PSET and PRESET 4-228
FIX 4-99 PUT 4-232
fixed point 3-9
fixed-length strings 4-163
floating point 3-9 H
floor function 4-130
flushing the keyboard hard copy of screen 2-13
buffer 1-7 HEX$ 4-115
folding, line 2-27 hexadecimal 3-10, 4-115,
FOR 4-100,1-14 H-1
foreground 3-40, 4-49 hierarchy of operations 3-29
format notation v high resolution 3-43,4-257
formatting 4-219 high- in tensi ty
FRE 4-104 characters 4-50, 4-5 1
free space 2-5,4-44,4-104 hold 2-17
frequency table 4-263 Home key 2-20
function keys 2-9 housecleaning 4-104
functions 3-29, 3- 32, 4-5,
4-17, 1-11
derived Appendix E I
user-defined 4-68
I/O statements 4-13,
Appendix B
IF 4-116, D-2, 1-12
IMP 3-25
G implication 3-25
implicit declaration of
garbage collection 4-104 arrays 3-17
GET (files) 4-106, B-I0 index (position in string)
GET (graphics) 4-108 4-129
glissando 4-264 indirect mode 2-7
GOSUB 4-111,4-180 initialiZing BASIC 2- 3
GOTO 4-113,4-180 INKEY$ 4-119, G-6
graphics 3-38, D-l INP 4-121
graphics modes 3-41,4-257 INPUT 4-122
X-5
INPUT # 4-125 L
input and output 3-33
input file mode 4-189, B-5 last point referenced 3-43
INPUT$ 4-127, F-3 LEFT$ 4-137
Ins key 2-26 left-justify 4-163
insert mode 2-26 LEN 4-138
inserting characters 2- 34 length of file 4-158
INSTR 4-129 length of string 4-104,
INT 4-130 4-138
integer 3-9, 3-11 LET 4-139
converting to 4-40, 4-99, light pen 3-45,4-185,4-206
4-130 LINE 4-141
integer division 3-22 line feed 2-32, 4-191, D-3
interrupting program LINE INPUT 4-144
execution 2-17 LINE INPUT # 4-145
intrinsic functions lines
See functions BASIC program 3-3
invisible characters 4-51 drawing in graphics 4-141
folding 2-27
line numbers 2-7, 3-3,
4-26,4-241
J on screen 3-39
LIST 3-3,4-147
joystick 3-45, 4-268 list program lines 4-149
joystick button 4-187, listing files
4-273, 4-275 on cassette 4-15 1
jumping 4-113, 4-180 on diskette 4-97
LLIST 4-149
LOAD 4-150, B-2
loading binary data 4-29
K LOC 4-153
LOCATE 4-155
KEY 4-131 LOF 4-158
KEY(n) 4-134 LOG 4-159
keyboard 2-8 logarithm 4-159
buffer See buffer, logical line 2- 32
keyboard logical operators 3-25, D-3
input 4-119,4-122,4-127, loops 4-100, 4-292, 1-14
4-144 LPOS 4-160
KILL 4-136, B-3 LPRINT 4-161
KYBD: 3-35 LPRINT USING 4-161
X-6
LPT1: 3-35, 4-149, 4-160, NOT 3-25
4-161, 1-7 Num Lock 2-16
LPT2: 3-35,1-7 numeric characters 3-4
LPT3: 3-35, 1-7 numeric comparisons 3-23
LSET 4-163 numeric constants 3-9
numeric expressions 3-21
numeric functions 3-29,4-17
M numeric keypad 2-15
numeric variables 3-13
machine language
subroutines 4-34,4-75,
4-284, Appendix C
medium resolution 3-42,
o
4-257 OCT$ 4-175
memory image 4- 32 octal 3-10, 4-175
memory map 1-2 Ok prompt 2-7·
MERGE 4-36,4-165, B-3 ON COM(n) 4-176
messages AppendixA ON ERROR 4-178
MID$ 4-167, D-6 ON KEY(n) 4-182
MKI$, MKS$, MKD$ 4-170, ON PEN 4-185
B-9 ON STRIG(n) 4-187
MOD 3-22 ON... GOSUB 4-180
modulo arithmetic 3-22 ON... GOTO 4-180
MOTOR 4-172 OPEN (file) 4-189, B-4,
multiple statements on a B-9
line 3-3 OPEN "COM... 4-194, F-6
multiplication 3-21 operators
music 3-44, 4-209 arithmetic 3-21
concatenation 3-31
functions 3-29, 3-32
N logical 3-25
numeric 3-21
NAME 4-173 relational 3-23
naming files 3- 34 string 3-31
negation 3-21 OPTION BASE 4-200
NEW 4-174 options on BASIC
NEXT 4-100 command 2-4
See also FOR OR 3-25
Next Word 2-23 or, exclusive 3-25
non-executable statements order of execution 3-29
3-3 OUT 4-201
X-7
output file mode 4-189, B-4
overflow A-7
R
overlay 4-36 random files 4-94, 4-106,
overscan 3-40 4-189, B-8
random numbers 4-236,
p 4-249
RANDOMIZE 4-236
READ 4-64,4-238
paddles 3-45 record length
PAINT 4-203 maximum 2-5
palette 3-42, 4-54 setting 4-189
parentheses 3- 30 ?Redo from start 4-123
pause 2-17 related publications vi
PEEK 4-205, D-4 relational operators 3-23
PEN 4-206 relative form for specifying
performance hints B-15, coordinates 3-43
1-10 REM 4-240
Pg Up and Pg Dn 2-16 remarks 3-4, 4-240
PLAY 4-209 RENAME 4-173
POINT 4-213 renaming files 4-173, B-3
POKE 4-214, C-4, D-4 RENUM 4-36,4-89,4-241
POS 4-215 repeating a string 4-276
position in string 4-129 replacing program lines 2- 3 7
position of file 4-153 requirements See system
positioning the cursor 4-155 requirements
precedence 3-29 reserved words 3-6, 3-13
precision 3-11, 4-73 RESET 4-243
PRESET 4-228 RESTORE 4-244
Previous Word 2-24 RESUME 4-245
PRINT 4-216 resume execution 4-58
PRINT # 4-225 RETURN 4-111,4-247
PRINT # USING 4-225 reverse irnage characters
print fo~matting 4-219 4-51
print screen 2-13 RIGHT$ 4-248
PRINT USING 4-219 right-justify 4-163
printing 4-161 RND 4-249
program editor 2-19 rounding 3-18, D-5
protected files 4-253, B-3 rounding to an integer 4-40
PrtSc 2-13 RSET 4-163 .
PSET 4-228 RS232 See communications
PUT (files) 4-230, B-9 RUN 4-251, B-2
PUT (graphics) 4-232
X-8
s STR$ 4-272
STRIG 4-273
SAVE 4-253, B-2 STRIG(n) 4-275
saving binary data 4-32 string comparisons 3-24
screen 3-39 string constants 3-9
shifting 4-201 string descriptor 1-4
use of 3-38 string expressions 3- 31
SCREEN function 4-255 string functions 3-32 4-21
D-6 "
SCREEN statement 4-257
SCRN: 3-35 string space 2-5,4-44,4-104
Scroll Lock 2-16 string variables 3-13
scrolling 3-40 STRING$ 4-276
search order for adapters 1-7 subroutines 4-111, 4-180
~11 '
seeding random number
generator 4-236 subroutines, machine
segment of storage 4-71 language 4-34,4-75,4-284,
sequential files 4-189, B-4 Appendix C
SGN 4-260 subscripts 3-15,4-77,4-200
shifting screen image 4-201 substring 4-137, 4-167,
sign of number 4-260 4-248
SIN 4-261 subtraction 3-21
sine 4-261 SWAP 4-277
single-precision 3-11, 4-61 switching displays 1-8
soft keys 2-9, 4-1 31 syntax diagrams v
SOUND 4-262 syntax errors 2-40
sounds 3-44, 4-28, 4-209, SYSTEM 4-278
4-262 system functions
SPACE$ 4-265 See functions
spaces 3-6, D-7 system requirements
SPC 4-266 Advanced 1-6
special characters 3-5 Cassette 1-4
specification of files 3- 34 Disk 1-5
specifying coordinates 3-43 System Reset 2-18
SQR 4-267
square root 4-267
stack space 4-44 T
starting BASIC 2-3
statements 4-8 TAB 4-279
I/O 4-13 Tab key 2-30
non-I/O 4-8 TAN 4-280
STICK 4-268 tangent 4-280
STOP 4-270 technical information 1-1
X-9
telecommunications
See communications v
tempo table 4-264
terminating BASIC 4-278 VAL 4-285
text mode 3-39,4-257 variables 3-12
THEN 4-116 names 3-12
TIME$ 4-281 storage of 1-3
tips 1-10 VARPTR 4-286,1-3
trace 4-283 versions 1- 3
trigonometric functions visual page
arctangent 4-25 See display pages
cosine 4-60
sine 4-261
tangent 4-280 w
TROFF 4-283
TRON 4-283 WAIT 4-290
true 3-23, 3-25 WEND 4-292
truncation 4-99, 4-130 WHILE 4-292
truncation of program WIDTH 4-294
lines 2-36 word 2-23
two's complement 3-27, 3-28 workspace 2-5,4-44,4-104
type declaration WRITE 4-298
characters 3-14 WRITE # 4-299
typewriter keyboard 2-10
x
u XOR 3-25
underflow A- 7
underlined characters 4-51
uppershift 2-12
user workspace 2-5. 4-44.
4-104 ..
user-defined functions 4-68
using the screen 3-38
USR 4-75, 4-284, C-14
X-to
The Personal Computer
Software Library
BASIC 6025013
Comments:
9J94 PIO::!
6025013
Printed in USA