BASIC listing Need to refer to a program while writing some documentation? Jonathan Harston has the solution. There are times when you want to look at a program on a disk, but you have another program in memory and don't want to go through through saving it, reloading the other program, listing it and reloading the first back again. Or, you could be in the middle of running a program or in an application such as a word processor. What you need is PrList. The listing on the next page creates a machine code program - PrList - that sits on your disk and will list a BASIC program file from disk to your screen. And you can do it from anywhere providing you can enter star commands. Type in the listing and check it with Get it Right! When run it generates the code. You then save it by copying the command line given at the end of the program. The command is called by entering: *PRLIST If there is no BASIC ROM, the file cannot be found or it is not BASIC appropriate errors are generated. Once the program has found a correct file, it displays it from the start in exactly the same way as from BASIC with LISTO 0 - the LISTO setting is ignored. The listing continues until either it reaches the end or the Escape key is pressed. You can hold down Shift+Control to halt the listing as you go along. How it works The first part of the program sets up the position of some variable locations and then goes back to assemble on top of them. This is safe as this part of the program is only executed once, and the variables are all initialised during execution. This helps to squeeze the length down to 512 bytes to fit it into the cassette filing system buffers. At the start of execution, the program checks to see if a BASIC ROM is present and the current ROM number is saved. Location &24B is accessed by *FX187 and containes the ROM number of the slot containing BASIC - or &FF if there's none. Next, the file is looked for. OSARGS 1,0 returns the address of the filename, which is then opened and the first byte is checked to be 13. If it is not 13, it's not BASIC. This is not the most rigorous of tests - it will let through a text file with the first line of zero length - but it will stop most non-BASIC files. The version number of the BASIC ROM at address &8008 is then looked at to locate the token table. This is used because BASIC programs are compressed in memory. PRINT is stored as the byte &F1 rather than as individual characters. The program now gets to the main loop. The quotes flags is zeroed to indicate that we are not inside a string. The line number is fetched in two bytes and is printed out with flag set to 32 for leading spaces. The line length is fetched and discarded. An inner loop is now entered to print out the characters of the line. First, the Escape flag is checked, and if it is set, the program tidies up and exits, otherwise a character is fetched. If the quote flag is set, a jump is made to print the character. If we are not inside a string, and the character is 128 or higher, it must be a keyword token and a jump is made to print it out. If the character is a quote then the flag is flipped. The character is then printed and - if it was not 13 - a jump is made back to do another character. If it was 13 a new line is started. Next comes the exit routine used when Escape is pressed or the end is reached. The file is closed, the previous ROM paged in, and the program finishes. Following this is the routine to decode the token bytes. If the byte is 141, a jump is made to decode the line number that follows it, otherwise a call to the token printing routine is carried out, and then a jump back to do the next character on the line. Character 141 indicates a line number after a GOTO, GOSUB or RESTORE. This is stored in three bytes, and the routine fetches them and decodes them. It then prints out the number with no leading spaces, before jumping back to deal with the next character on the line. The routine to print out a token uses the address found in the setup part of the program at the beginnin. This points to the start of the table. This holds the text along with the value of the token. The routine searches through the table for a match and then prints the characters. Finally, there is a general purpose 16-bit decimal number printout routine. This works by repeatedly trying to subtract decreasing powers of 10 from the original value and printing out the number of times is was successful before going on to the next digit. {*} This extremely simple but very useful routine will be a valuable aid in program development and writing. Table talk If you are using the Tube don't worry about the fact that the program resides in the I/O processor. This is because that's where the ROMs are, and the routine only accesses the filing system and printout routines. It doesn't interact with the current language at all. In fact, you can be within a different language altogether, or a word processor for example. The program works perfectly on a network because it's been designed to find out what environment it is in and behave accordingly, instead of assuming it to be the same as the one it was assembled on. This is sensible programming practice. There is a question mark in the table of addresses - this is for BASIC with byte &8008 set to 3. If you know the correct address you can add it at this point. Micro User, June 1990.