Classic Computer Magazine Archive COMPUTE! ISSUE 47 / APRIL 1984 / PAGE 147


Kenneth D. Day

"Variable Lister" displays a list of current values for all variables and array elements, while your program is running. This can be a most valuable debugging tool. This relocatable subroutine is written in machine language for VIC. Versions are also included for the 64 and Atari. Refer to the "Automatic Proofreader" article before typing in these programs.

"Variable Lister" is a 6502 machine language subroutine which prints the values of variables in BASIC programs. The program makes it easy to inspect the values of all variables in a BASIC program at various preselected points during execution or after the program has been aborted by the BASIC interpreter. The subroutine is called by SYS followed by the decimal address of the entry point into the subroutine (SYS 65336). The subroutine does not interfere with the execution of the BASIC program, so it may be called several times within a program.

When called, the subroutine clears the screen and prints the Variable Lister heading. It then begins to scroll through the values of all simple (nonarray) variables. The order in which the variables are listed is determined by their order of occurrence during the BASIC program's execution.

The variable name is printed, followed by an equal sign and the value of the variable. String variable values are enclosed in quotes so the user can distinguish blank strings (" ") from the null string (""). Integer (numbers with no decimal point) and floating-point values are printed exactly as they would be from BASIC, because the subroutine uses the output routines of the BASIC interpreter.

Each Array Element Is Listed

After simple variables are displayed, the program begins scrolling through arrays. A listing of an array begins with a display of the array name and its dimensions. The program then scrolls through each element, printing the element name followed by an equal sign and the value of the element. Values are printed using the same conventions as those for simple variables. Array elements are printed with leftmost subscripts changing the most quickly.

A pause feature is included so you can temporarily halt the scrolling display. To pause, press any key except HOME and the down arrow.

Scrolling is restarted by pressing RETURN or any printable character key.

To speed up your search through long variable lists, you can press the down-arrow key for a rapid scroll rate. To clear the screen and skip to the next array, press the HOME key. You can also use this feature while examining simple variables, to skip to the first array.

RETURN Ends The Listing

When you're ready to return from the subroutine, simply press the RETURN key.

Please note that the special functions of the HOME and RETURN keys are not available while the subroutine is in pause mode. Pressing these keys while the program is pausing only restarts the scrolling.

The relocating loader (Program 1) will load Variable Lister into any VIC-20 with or without memory expansion. Variable Lister requires 867 bytes, so the BASIC loader program will just fit into memory on an unexpanded VIC-20. The DATA statements in the loader program contain the hexadecimal equivalent of the unrelocated machine code for the Variable Lister subroutine. Had I separated the pairs of hexadecimal numbers representing each byte in memory with commas, the loader program could not have been run on an unexpanded VIC-20.

Memory Pointers Are Reset

Lines 6–7 move the top-of-memory pointers so that the Variable Lister subroutine will be protected from the operating system and BASIC interpreter.

Lines 9–13 POKE the unrelocated machine language into the area made available at the top of memory.

Lines 14–17 relocate addresses within the subroutine to be consistent with its location in the machine on which it is loaded.

Line 18 tells you how to call Variable Lister by printing SYS followed by the decimal address of the entry point of the machine language subroutine. This line also POKEs the entry point address into the address area used by the USR function. If you forget the decimal address of the entry point for Variable Lister, SYS PEEK(l) + PEEK (2)*256 instead.

You can also call the subroutine with a statement like X = USR(O), but on return from the subroutine the BASIC interpreter will abort with a TYPE MISMATCH error. (However, it's not good programming style to call this subroutine as if it were a function which returns a value.) Line 18 also deletes the loader by issuing the NEW command, so be sure to SAVE a copy before you RUN the program.

To use VIC Variable Lister, first LOAD and RUN the loader (Program 1), then LOAD and RUN the program for which you wish the variables listed. After the program has run, give the SYS to start Variable Lister. If you need to check the value of variables at some point before the end of the program, simply include a SYSxxxx statement at the desired point in your program, where xxxx is the proper SYS address for your memory configuration.

Commodore 64 And Atari Version Notes To Variable Lister

Kevin Martin, Editorial Programmer

The 64 version of "Variable Lister" works just like the VIC version, except that it is stored in the free memory above address $C000, safe from BASIC. Install the lister by loading and running Program 2. You can initiate the listing of variable values by typing SYS 50000. As an aid in program debugging, you can also include SYS 50000 as a statement in your program wherever you want to see the current values of all the variables.

If you have typed in the "MLX" machine language editor program from a previous issue of COMPUTE!, you can use it to make a copy of the machine language portion of Variable Lister. LOAD and RUN Program 2, then LOAD and RUN MLX. Specify 49991 as the starting address and 50824 as the ending address, then use the MLX Save option to store the machine language on tape or disk. The program thus created can be reloaded without erasing a BASIC program already in memory by typing:

LOAD "filename",1,1 for tape
LOAD "filename",8,1 for disk.

As before, the variable listing is initiated with SYS 50000.

The Atari version of Variable Lister is a combination of two programs from The Atari BASIC Sourcebook (COMPUTE! Books). The programs are found on pages 123 and 125–26.

Before typing this program in, be sure to type NEW. Type this program in and, instead of SAVEing it, LIST the program to disk (LIST"D:VARLIST") or to cassette (LIST"C:"). Then type NEW in again and LOAD the program for which you want to list variables. Finally, use ENTER to add the Variable Lister subroutine form disk (ENTER"D:VARLIST") or cassette (ENTER"C:"). Then type GOTO 32700 to obtain the listing of the variables.