XRef 

by Brian Smith

 

Issue 23

Sep/Oct 86

Next Article >>

<< Prev Article

 

 

Find all your variables

AtariLister - requires Java

 

If you have ever tried to modify a BASIC program from a magazine (or even one of your own!) then you know how useful it is to have a list of which variables have been used and where they are used. This machine code utility fits into page 6 and does both tasks at high speed. It was developed from a BASIC program given in the ATARI BASIC SOURCE BOOK (Compute! Publications).

The BASIC program below POKEs the code for the utility into page 6 and will work with either Revision A or Revision C BASIC ROMs. Once the code is in place, any BASIC program can be loaded and a simple USR call used to generate a cross reference (or XREF) listing. The utility can operate in three modes and can send output to the screen, a printer or a disk file.

FULL CROSS REFERENCE MODE

To produce a list of every variable name currently held by BASIC together with a list of the line numbers where these names are used enter an immediate mode USR call (i.e. don't use a line number) like:

X = USR (1536,1)

where 1536 is the start address of the utility and the '1' selects full XREF mode. This will output the full XREF to the screen. Use Control-1 to pause and restart the listing otherwise it will scroll past too fast for it to be read!

NAMES ONLY MODE

To produce just a list of the names of all the variables use a USR like:

X = USR (1536,0)
or X = USR (1536)

These two commands have the same effect, the names are listed to the screen, one per line. Each name is followed by its 'variable number' which is allocated by BASIC. This number, which is in the range 128 to 255, can be used in the following mode.

SINGLE VARIABLE CROSS REFERENCE MODE

This mode is called up by a USR call like:

X = USR (1536,NUMBER)

where NUMBER is the variable number of the required variable (use 'Names Only' Mode to find the number of the variable of interest). This command will list on the screen the name of the specified variable followed by the numbers of all the lines which refer to this variable. For example, X=USR(1536,128) will list all references to the first name shown in a 'Names Only' listing.

SENDING OUTPUT TO PRINTER OR DISK

It is quite easy to switch the XREF output to a printer or a disk file. In order to keep the size of the utility below 256 bytes (so it could fit into page 6) a channel to the appropriate device has to be opened before XREF is called. When XREF is called, it is given an extra parameter which identifies the channel to be used. For example to use a printer use commands like:

OPEN #4,8,0,"P:"

X = USR (1536,0,4)

to send a names only listing to a printer. Once the channel is open, subsequent XREF calls can be made, e.g.:

OPEN #4,8,0,"P:"

X = USR (1536,0,4)
X = USR (1536,128,4)

X = USR (1536,150,4)

CLOSE #4

To use a disk file instead of a printer simply change the OPEN command.

ERROR MESSAGES

XREF has one error message, ERROR 0, which means too many parameters have been supplied. In addition to this, the normal I/O error messages may be generated by BASIC, e.g. if the USR call specifies channel 3 for output when that channel is closed then ERROR 133 will be produced.

To stop the XREF output, e.g. if a full XREF was requested by accident, press the BREAK key (there may be a short delay before the STOPPED message appears).

Note that XREF can show when it is time to clean up the Variable Name Table. If XREF does not list any line numbers for a variable then either the lines which referred to it have been deleted or else some immediate mode commands have used it. These 'unused' variables take up space which can be reclaimed by LISTing the BASIC program to disk (or tape), typing NEW to clear out the old program and then ENTERing the program from disk (or tape).

top