Classic Computer Magazine Archive CREATIVE COMPUTING VOL. 10, NO. 1 / JANUARY 1984 / PAGE 191

The first one; print statement formatter for Apple. M.J. Parrott.

The First One

Print Statement Formatter For Apple

A look at a typical Basic program will convince you that, usually, between one quarter and one third of it is concerned with outputting text to the screen. This text is of two kinds; the first takes just a few lines of program and screen and occurs scattered throughout the length of the program. The second kind of text tends to occur in large chunks and is typically printed toward the beginning where it lists instructions or gives information.

This second kind of text is frustrating to incorporate into the program. It is usually added after the main body has been written and debugged and it is a real chore to get it concise (to save memory) and to get it correctly formatted for the screen. How many times have you typed in PRINT statements only to discover on running the program that you have words wrapping around on the screen or that the last word on a line goes right to the margin and forces an extra carriage return resulting in an untidy display? I have many times said and therefore thought that a utility which allowed you to type in text without worrying about wrap around and which then formatted this into lines of Basic, correctly formatted for the screen would be a great boon.

Searching around for such a utility to use on my Apple II proved a disappointment; I couldn't find one. So I wrote my own. I find it extremely useful and timesaving and hope that you will too. I called it the First One.

Using The Program

After BRUNning it from disk, type (RETURN) from Basic or CTRL-Y from the monitor to initiate the program. The screen clears ready for you to type your text. Any character that is visible on the screen may be typed; a double quote, however, appears on the screen as a single quote or apostrophe. Control characters (including the RETURN key) are, in general, ignored by the program, but a few enable the incorporation of extra commands into the Basic program which will be generated. These control characters are


The First One will translate about six screenfuls of text into lines of Basic, but does not incorporate lines to halt the printing of the text at appropriate points to give you time to read it. I did this deliberately, as the exact choice of statement to do this depends to some extent on the remainder of the program.

It is no effort to go through the lines adding the necessary appropriate line as required. Once the lines of Basic have been formed, they can be treated exactly as if they had been typed in by you, the user. I usually use RENUMBER on the lines to match them to and subsequently merge them with my main program.

The whole process of typing the text, renumbering the lines and merging them with the host program is very much quicker than writing lines of PRINT statements directly. The lines of Basic as written by the First One also are more compact than those written directly due to the incorporation of ASCII character 13 into the string rather than ending the string whenever the righthand margin of the screen is reached.

How It Works

I have presented the program The First One both as a source listing and as a hexadecimal dump. It was assembled to begin at $4000 and is in essentially three parts. The first sets up the ampersand and CTRL-Y vectors to point to the second part, then returns to the caller. The second clears a buffer starting at $5000 to zero bytes, clears the screen, and accepts keystrokes. These are checked to see if they are printable characters or are commands and if so are displayed on the screen and are put into the buffer. If the buffer is filled or on receipt of the exit command (CTRL-E), the third part of the program begins operation where the lines of Basic are formed beginning at $801 as normal.

This part of the program resets the pointers to the buffer (BUF) and to the area of memory used to assemble the Basic (BAS) and looks at the next byte to see if it is text or a command or a zero value, which indicates that the end of the text had been reached. If it is text, 40 characters from the buffer are taken and scanned forward, looking for commands other than a carriage return. If none are found, the 40 characters are scanned backward looking for the first space or carriage return to find a convenient point at which to break the line of text as it will appear on the screen.

When found, the characters in front of this point are converted to the corresponding ASCII code with the high bit off and are stored in the appropriate area of memory for the Basic. The BUF pointer is reset and the process is repeated.

A count of the number of characters appearing in a line of Basic is kept, and when it is more than 240 or when a command is found, the line of text (in the Basic) is finished off with the appropriate PRINT token, with the line number, with the double quotes which delimit the string, and with the link addresses. The process then returns to the start and begins again.

Finally, when the end is reached, a jump is made to the Applesoft routine (RUN, $D566) where the Basic program is run. If the last character in the buffer is a command such as CTRL-I, CTRL-F or, perhaps more likely, a CTRL-N, the final line of Basic will be a PRINT statement with nothing following. This can easily be deleted when the Basic is listed.

Other than RUN the First One uses only a few routines in the Apple monitor, most of which can probably be duplicated on other 6502 machines. These are (addresses and affected registers are in parentheses) CLS ($FC 58, A, Y) which clears the screen and sets the cursor to the upper lefthand corner, GET ($FD35, A, X, Y) which takes a keystroke ASCII value into the accumulator, PRT ($FDED) which then prints the ASCII value of the accumulator on to the next possible position of the screen, BACK ($FC10, A) which moves the cursor back one place on the screen, and BEL ($FBE4,A,Y) which rings a bell.

Therefore, if you feel brave enough to translate the First One to another machine, it should be quite easy, providing the various pointers and Basic tokens are changed to the appropriate values.