Varsort 1

by C.L.Stone


Issue 11

Sep/Oct 84

Next Article >>

<< Prev Article



This is the first of two Variable utility programs. The second VARSORT2 which allows you to add descriptions to the variables and which can be accessed from this program will be published next issue. Whilst written for disk users, the program can be easily converted for cassette.

When writing a program which uses quite a lot of variables, it is very easy to forget which variables you have already used. It is possible to obtain a list of these from the computer itself but they will be in the order of entering. This program is 'ENTERed' from disc and tacks itself on to the end of your program (its line numbers run from 32000 upwards). When 'ENTERed' type 'GOTO 32000' and the program will clear the screen and print, in three columns, all the variables you have used so far. You will then be asked to press any key for a 'sort'. The sort section is the one used in a program published in Page 6 some time ago although in this version you can watch its progress on the screen. When completed, the sorted list of variables is displayed on screen, again in three columns. You are then asked if you want to print them or put them to disk - the filename is fixed. Finally you have the option to call 'VARSORT2' or finish. A word of warning - whichever option you choose will erase your original program from memory - so SAVE IT FIRST !!!.

The program runs as follows:-


Line 32000 dimensions the strings and array used. The array is cleared.

Line 32010 sets the mode to zero: sets the colours: clears the cursor.

Line 32020 prints the heading asking you to wait for the variables to be fetched.

Line 32030 sets a TRAP: OPENs a channel for input from keyboard.

Line 32040 sets a loop 'XX' which will run from the address contained within memory locations 130 and 131 to that in 132 and 133 LESS 47. The first is the beginning of the variable list - the last is the end minus 1. Why 47 then? The variables used in this program occupy 46 bytes and, as we don't want them listed we stop the list just before they are reached.

(A word of explanation - the variables are listed nose to tail with no gaps in between. What makes it easy to pick out each separate variable is the way in which the last character is stored - as an INVERSE character. The ATASCII number for an Inverse Character is 128 greater than that for a normal one).

Line 32050 looks at the value contained in 'XX', if less than 128 then its value is passed on to 'XXX': GOSUB's line 32100 and on returning GOTO's 32070.

Line 32060 is only reached when a PEEK at 'XX' is greater than 128 and when this is so then 'XXX' is made equal to 'XX' but less 128: GOSUB's 32100 and, on return, is immediately GOSUBbed again to line 32110, for it is obvious that a complete variable name has been obtained. 

Line 32070 sends you back for another pass through the loop until finally the list is exhausted whereupon the program GOSUB's to line 32440 (the screen print-out).

Line 32080 prints the request to press any key for the sort to begin and then waits for your input.

Line 32090 clears the screen and GOTO's line 32150.

Line 32100 adds the individual characters of the variable (in 'XXX') to a string 'XX$'. 

Line 32110 is brought into use when the complete variable is contained in 'XX$', the string is then checked for length, if not 10 characters then it has a dot added until it is. As the final string will contain all the variables, each sub string must be of a fixed length so that we can extract it! 

Line 32120 adds 'XX$' to that final 'long' string, 'XAR$' and 'XX$' are then cleared ready for the next variable.

Line 32130 is the error line which the TRAP, when sprung, sends the program to.

Line 32140 sends you back to line 32010 to have another try.


Line 32150 is the first line of the sort section. The bell is rung, warning you of the start: 'XX' is used again, this time to represent the length of each variable sub string. A loop is started - 'XZX1 ' - running from 1 to the length of 'XAR$' divided by 'XX', this of course gives you the number of variables in the string: 'XX1' is equalled to the pass through the loop 'XZX1' times 'XX': 'XROW' is equalled to the number of variables in the string. 

Line 32160 Another loop 'XZX2' is started running from 1 to the number of variables: 'XX2' is equalled to 'XZX2' times 'XX'. This second loop completes a whole cycle whilst the first loop is stationary on one item at a time. This is used by the line 32180.

Line 32170 prints the loop positions as they are executed.

Line 32180 compares the variable in the first loop (using 'XX1' to split it from the long string) with the variables as they appear in the second. When the first variable is 'less' (alphabetically speaking!) than that in the second loop then 'XROW' (initially representing the total number of variables) is reduced by one.

Line 32190 goes back for another pass through loop 'XZX2' until this is completed. 

Line 32200 comes into effect when loop 'XZX2' has completed its cycle, checking each of its items against the single item of loop 'XZX1'. 'XROW now holds the actual position which the loop 'XZX1' item will hold in the new string. This is now placed in the array 'XROW', the position within the array being determined by the pass through loop 'XZX1'.

Line 32210 now returns for the next pass through loop 'XZX1' (just one item before going through loop 'XZX2' again). 'XROW' is automatically reinitialized to the total number of variables before the loop 'XZX2' starts. This continues until the whole of loop 'XZX1' has been compared with the items shown by loop 'XZX2', and the 'XROW' array contains the new positions.

Line 32220 starts a loop 'XZX1' running as before. 'XX1' is set as before.

Line 32230 starts loop 'XZX2' as before. 'XX2' is set as before.

Line 32240 prints the state of the loops to screen (alongside the other).

Line 32250 checks the first loop 'XZX1 ' and if the pass is equal to 'XROW' array as defined by the pass through the loop 'XZX2' then the new string has the variable shown in that pass through 'XZX2' transferred to the new string in the position which 'XZX1' and 'XROW (XZX1)' agree upon. 

Line 32260 goes back for another loop through 'XZX2' and line 32270 through 'XZX1'. 

Line 32280 When all is finished and the new string 'XAR1$' contains the sorted list then this line sets about restoring the original string. 'XAR$' is first emptied, then 'XAR$' is made equal to 'XAR1$', finally 'XAR1$' is emptied.

Line 32290 prints the fact that the sort has been completed and line 32300 pauses for a while, clears the screen and GOSUB's line 32440 for a print out




Line 32320 asks you if you wish to print the list - waits for an answer Y or N. An ' N' would send the program to line 32380.

Line 32330 sets a TRAP in case the printer is not switched on: does an exploratory LPRINT, which if OK then passes on to line 32350. 

Line 32340 is the error routine which prints the message to switch the printer on. 

Line 32345 sends you back to line 32330 so creating a loop, this will continue (ringing the bell in the process) until the printer is switched on.

Line 32350 sets the printer to the Condensed Print and 8 lines to the inch mode. This should be changed to suit your own printer.

Line 32360 sets 'XX' to equal 10: starts a loop 'XZX1' running from 1 to the number of variables as before: sets 'XX1' as before: prints to paper the variables out of XAR$ as defined by 'XX': goes back for another pass: clears the printer settings. 

Line 32380 asks if you wish to record to Disc. An 'N' would send you to line 32420. The variables would be filed under the name D:XXX.DAT and can be used by the program VARSORT2. 

Line 32390 sets a TRAP to close the channel when finished: OPENs a channel to write data to the above file.

Line 32400 prints the data to disc. 

Line 32410 closes the channel.

Line 32420 asks you if you wish to call the program VARSORT2. A 'Y' would cause VARSORT2 to be run, so erasing both your own program and VARSORT1 in the process. The moral being SAVE IT!!

Line 32430 An' N' would close the keyboard input channel and issue a NEW instruction, with the same effect as above with regard to your program.




Line 32440 sets 'XX' to equal 10: 'XXXLIN' (line counter) to 5: 'XZX1' (screen print position) to zero.

Line 32450 starts a loop 'XX2' as before: 'XX1 ' is set as before: at a position defined by 'XZX1' and 'XXXLIN' the variables are printed: 'XXXLIN' has 1 added to it.

Line 32460 'XXXLIN' is checked, if 19 and 'XZX2' equals 1 the 'XXXLIN' is reset to 5: 'XZX1 ' to 14: 'XZX2' to 2: the program then GOTO's line 32490.

Line 32470 checks 'XXXLIN', if 19 and and 'XZX2' equals 2 then 'XXXLIN' is reset to 5: 'XZX1' to 28: 'XZX2' to 3: and goto's line 32490.

Line 32480 checks 'XXXLIN', if 19 and 'XZX2' equals 3 then 'XXXLIN' is reset to 5: 'XZX1' to zero: 'XZX2' to 1: and GOTO's line 32500.

Line 32490 sends you back for another pass through the loop. When the loop is finished the program RETURNS to a position immediately after the original GOSUB.

Line 32500 asks you to press a key for the next page and then waits for an input.

Line 32510 uses a loop 'XXX' to print a line of blanks from lines 19 to 5: returns to line 32490.

Even if you do not type it in I hope that the line by line explanation will help you to understand the structure of the program and will help you to write your own utilities. VARSORT2 will follow in the next issue.


AtariLister - requires Java