User`s guide

FORMAT FOR THE
RECORD#
COMMAND:
PRINT#l5,
"P"
+CHR$
(channel
#+96)
+ CHR$ (<record
#)+CHR$
(>record
#)
+ CHR$ (offset)
where
"channel
#"
is
the channel number specified
in
the current Open statement for
th
e
specified file,
"<record#"
is
the low byte of the desired record number, expressed
as
a
two byte integer,
">record
#"
is
the high byte
of
the desired record number, and
an
optional
"offset"
vaiue, if present,
is
the byte within the record
at
which a following
Read or Write should begin.
To fully understand this command,
we
must understand how most integers are stored
in
computers based on the 6502 and related microprocessors.
In
the binary arithmetic used
by
the microprocessor,
it
is
possible to express any unsigned integer from 0-255
in
a
single byte.
It
is
also possible to store any unsigned integer from 0-65535
in
2 bytes, with
1 byte holding the part
of
the number that
is
evenly divisible
by
256, and any remainder
in
the other byte.
In
machine language, such numbers are written backwards, with the low-
order byte (the remainder) first, followed by the high order byte .
In
assemhly langua
ge
programs written with the Commodore Assembler. the low part
of
a two byte number is
indicated by preceding
its
label with the less-than
character(<)
. Similarly. the high
pa
rt
of
the number
is
indicated by greater-than
(>).
SAFETY NOTE: GIVE EACH
RECORD#
COMMAND TWICE!
To avoid the remote possibility
of
corrupting relative
file
data.
it
is
necessary
to
give Record# commands
twice-once
before a record
is
read or written. and again
immediately afterwards.
EXAMPLES:
To position the record pointer for
file
number 2
to
record number 3,
we
could type:
PRINT
#15,
"P"
+CHR$
(98)
+CHR$
(3)
+CHR$
(0)
The CHR$(98) comes from adding the constant (96)
to
the desired channel number
(2
).
(96 + 2 = 98) Although the command appears to work even when 96
is
not added to
th
e
channel number, the constant
is
no1
mally added to maintain compatibility with the w
ay
Record# works on Commodore's CBM and PET computers.
Since 3
is
less than 256, the high byte
of
its
binary representation
is
0, and the entire
value
fits
into the low byte. Since
we
want
to
read or write from the beginning
of
th
e
record,
no
offset value
is
needed.
Since these calculations quickly become tedious, most
program~
arc written to do
them for you . Here
is
an example
of
a program which inputs a record number and converts
it into the required low byte/high byte form:
58
450 INPUT"RECORD # DESIRED";RE
460"IF
RE<l
OR RE>65535 THEN 450
470
RH=
INT(RE/256)
480 RL = RE-256*RH
490
PRINT#l5,
"P"
+CHR$
(98)
+CHR$
(RL)
+CHR$
(RH)
Assuming
RH
and RL are calculated as
in
the previous example, programs may 3lso use
variables for the channel, record, and offset required:
570 INPUT "CHANNEL, RECORD, & OFFSET DESIRED";CH,RE,OF
630
PRINT#I5,
"P"+CHR$(CH
+96)
+CHR$
(RL)
+CHR$
(RH)
+CHR$
(OF)
ANOTHER
RECORD#
COMMAND
Basic
4.0
on Commodore's PET and CBM models includes a Basic Record#
command not found in any
of
the serial bus computers. However, some available
utility programs for these models include it.
It
serves the same function as the
Record#
command explained above, but has a simplified syntax:
RECORD#file
#,record
#,offset
where
"file#"
is the relative file number being used, not the command channel's
file,
"record
#"
is
the desired record number, and
"offset"
is
as
above.
If
you see a
Record#
command written
in
Bllsic 4 form
in
a program you want
to use, simply convert it into the usual form for both Basic 2 and 3.5 described
in
this section.
COMPLETING RELATIVE FILE CREATION
Now that we have learned how to use both the Open and Record# commands, we are
almost ready to properly create a relative file. The only additional fact
we
need to know
is
that CHR$(255)
is
a special character in a relative
file
.
It
is
the character used by the DOS
to
fill
relative records as they are created, before a program fills them with other
information. Thus, if we want to write the last record we expect to need
in
our
file
with
dummy data that will not interfere with our later work, CHR$(255)
is
the obvious choice.
Here
is
how
it
works
in
an actual program which you may copy for use
i~
your own
relative
file
programs.
1020 OPEN 15,8,
15
1380
INPUT"
ENTER RELATIVE FILE NAME";F1$
1390
INPUT"
ENTER
MAX.#
OF RECORDS";NR
1400
INPUT"ENTER
RECORD LENGTH" ;RL
59
Open command channel
Select
file
parameters