Classic Computer Magazine Archive COMPUTE! ISSUE 42 / NOVEMBER 1983 / PAGE 283

Variable Lister

E. A. Cottrell

This utility lists all your program variables in order, including variable type (simple, array). It's a helpful tool, especially for checking long programs and writing documentation. For the VIC and 64.

There are two types of variables, simple and array, and three categories in each type, floating point numeric, integer numeric, and string. All of these variables are stored in the VIC and 64 immediately after the BASIC program.

The simple variables are stored below the arrays starting at the address pointed to by memory locations 45 and 46 (see box). Each of these simple variables occupies seven bytes of memory. The first two bytes contain the first two characters (in ASCII) of the name of the variable, with coding to indicate which type of variable it is. This coding is accomplished by adding 128 to both characters if it is an integer variable and by adding 128 to the second character if it is a string variable. No coding indicates a floating point variable. The remaining bytes in numeric variables contain the value of the variable. In the case of string variables, the remaining bytes contain the length of the string and the location at the top of memory which contains the first character of the string.

Arrays are quite different in that the length of the variable is determined by the number of elements in the array. The information which must be stored for an array variable includes the name of the variable, which is coded the same as for a simple variable, a pointer to the location of the next variable, the number of dimensions in the array, and the number of elements in the array.

In addition, the value of each element in the case of numeric arrays, or the pointer to the string and its length for string arrays, must be stored. As you can see, array variables can eat up a lot of memory in a hurry. It is best to use the lowest possible number of elements in your arrays. If you do not specify the size of an array, the computer will set it at ten elements. If you need less than ten, you will save a minimum of five bytes per element if you establish the size of the array with a DIMension statement. Although a simple integer variable takes up the same amount of memory as a simple floating point variable, three bytes per element can be saved if you use integer instead of floating point variables in arrays.

LOADing The Lister

"Variable Lister" is a machine language (ML) program which is loaded by POKEs using a BASIC program, thus eliminating the need for an assembler. The ML is automatically loaded into the top of memory and protected from your BASIC program. Before you RUN the program, be sure to SAVE a copy since it self-destructs after it is run. When the machine language is loaded, the loader program will give you the location to SYS to when you want to list your variables. For example, with 16K of expansion memory plugged into your VIC, you would type SYS 24320 in order to list your variables. The program will then list the simple variables in the order of appearance in the program, with indicators of their type. Next the array variables will be listed with proper indicators.

Variable Lister is especially useful when you write programs with many variables and have to find new names. It is also valuable for documenting programs when they are completed.

The variables are listed across the screen to prevent them from scrolling out of view. If you have a printer, the following changes may be made to give you a listing which may be easier to read.

160 IF PA < > 33632 THEN PRINT "DATA ERROR " : END

Address Pointers

Now and then you'll see a reference to "pointers" within the computer's memory. These are two-byte long numbers, usually located in the first 256 memory cells of the computer, which hold an important address.

Things change while a program is running or being written. For example, if you add a line to a BASIC program, you've expanded the amount of space that the program is taking up in RAM memory. Obviously, when you go to SAVE the program, the computer has to know where the BASIC program ends. So, it keeps track of the "current top of BASIC program" in a pointer. This pointer is located (in the VIC and 64) in addresses 45 and 46. The number held in cell 46 is multiplied by 256 and then added to the number in cell 45. To see at which address in RAM memory your current BASIC program ends, you can type: ? PEEK (45) + PEEK (46) * 256.

There are a number of other pointers as well, including "limit of memory," "start of arrays," "string storage," and "start of BASIC." The locations of these pointers are listed in memory maps for each computer which have been published in COMPUTE! and in various COMPUTE! Books. They are also frequently available from user groups. There are some interesting things you can do by manipulating these pointers with POKEs. For one thing, you could fool the computer into reserving space for programs in odd places, or even partitioning memory so that two independent BASIC programs could run simultaneously. In any event, pointers hold information essential to the computer, and their values can be accessed using the formula above.

260 DATA 32, 210, 255, 169, 13, 32, 210
420 DATA 41, 32, 210, 255, 169, 13, 32

To send the list to your printer, simply OPEN a file to your printer:

OPEN1, 4 : CMD1 : SYSXXXXX

The BASIC program for which you wish to list variables must be RUN before you give the SYS to start the Lister. This is because the variables are not set up in memory until a program is RUN. If you're a VIC owner, this program works well in conjunction with "VIC Searcher" (compute!, February 1983). First, list the variables with Variable Lister, then find the lines on which they appear with the Searcher. Both of these programs may be loaded together. Remember that if you use the Searcher program, remove line 0 and RUN the program before using the Lister.

Variable Lister

120 ME = PEEK(55) + 256 * PEEK(56)
130 VS = ME - 256 : PA = 0
140 POKE 56, PEEK(56) -1
150 FORI = VS TO VS + 240 : READ A : POKE I, A : PA = PA + A : NEXT
160 IF PA <> 33670 THEN PRINT "DATA ERROR" : END
170 PRINT "SYS" VS "TO START" : NEW
180 DATA 165, 45, 197, 47, 240, 93, 133
190 DATA 253, 165, 46, 133, 254, 160, 0
200 DATA 169, 0, 141, 61, 3, 177, 253
210 DATA 41, 128, 208, 60, 177, 253, 41
220 DATA 127, 32, 210, 255, 200, 173, 61
230 DATA 3, 201, 0, 208, 6, 177, 253
240 DATA 41, 128, 208, 46, 177, 253, 41
250 DATA 127, 32, 210, 255, 173, 61, 3
260 DATA 32, 210, 255, 169, 32, 32, 210
270 DATA 255,152, 24, 105, 6, 144, 5
280 DATA 164, 254, 200, 132, 254, 168, 101
290 DATA 253, 197, 47, 240, 17, 208, 186
300 DATA 96, 169, 37, 141, 61, 3, 208
310 DATA 189, 169, 36, 141, 61, 3, 208
320 DATA 203, 165, 49, 197, 47, 240, 114
330 DATA 165, 47, 133, 253, 165, 48, 133
340 DATA 254, 160, 0, 169, 0, 141, 61
350 DATA 3, 177, 253, 240, 216, 41, 128
360 DATA 208, 77, 177, 253, 41, 127, 32
370 DATA 210, 255, 200, 173, 61, 3, 201
380 DATA 0, 208, 6, 177, 253, 41, 128
390 DATA 208, 63, 177, 253, 41, 127, 32
400 DATA 210, 255, 173, 61, 3, 32, 210
410 DATA 255, 169, 40, 32, 210, 255, 169
420 DATA 41, 32, 210, 255, 169, 32, 32
430 DATA 210, 255, 200, 177, 253, 24, 101
440 DATA 253, 197, 49, 240, 39, 177, 253
450 DATA 24, 101, 253, 170, 200, 177, 253
460 DATA 101, 254, 133, 254, 134, 253, 208
470 DATA 165, 96, 169, 37, 141, 61, 3
480 DATA 208, 172, 169, 36, 141, 61, 3
490 DATA 208, 186, 165, 48, 197, 50, 208
500 DATA 136, 96, 200, 234, 177, 253, 101
510 DATA 254, 197, 50, 240, 224, 16, 222
520 DATA 136, 208, 202