Classic Computer Magazine Archive COMPUTE! ISSUE 79 / DECEMBER 1986 / PAGE 69

Word Count for SpeedScript

Jonathan Bell

"Word Count" is a resident utility that works with the latest versions of SpeedScript, COMPUTE!ís popular Commodore 64 word processor. Since Word Count adds a new command to SpeedScript, it's available whenever you use the word processor. Although the code is written in machine language, you can take advantage of the new command without understanding machine language.

In many writing situations, it's useful to know how many words you've written. For a writer's contest, for instance, you may be limited to 2500 words or less. For a term paper, on the other hand, you may need to write a minimum number of words on a certain topic, "Word Count" adds a new command to the latest and most powerful versions (3.0 and higher) of COMPUTE!'s Commodore 64 word processor, SpeedScript. (The program will not work with versions prior to 3.0.) The new command is available at all times from within the word processor and it doesn't take any memory away from Speed-Script's text space. Word Count is also compatible with other SpeedScript enhancements such as "Preview-80" (COMPUTE!ís GAZETTE, November, 1985) and "Commodore 64 SpeedScript Fontmaker" (COMPUTE!, January, 1986).

Installing The Program

The program accompanying this article is a BASIC loader that creates an enhanced version of SpeedScript which contains Word Count. Type in and save the program on disk or tape. If you're using tape, change the 8 at the end of line 120 to a 1. Follow these steps exactly as shown to install Word Count in SpeedScript:

1. Load a copy of SpeedScript (version 3.0 or higher) and run it as usual.

2. Select the border and text colors you want.

3. Exit SpeedScript by tapping the RESTORE key and responding to the prompt with Y.

4. Type POKE 44,40: POKE 10240, 0: NEW and press RETURN.

5. Load the Word Count loader program from disk or tape.

6. Insert the disk or tape on which you want to store the enhanced version of SpeedScript.

7. Run the BASIC loader program and respond to the prompts as indicated on the screen.

The loader program ends by saving the enhanced version of SpeedScript under a new filename. Be careful that you don't overwrite your copy of the original version of SpeedScript. You may need the original again if you made any mistakes in typing in the BASIC loader. When the save is finished, turn the computer off and on, then load and run the new version of SpeedScript. Load any document into Speed-Script, then press CTRL-W (hold down the CTRL key and press W). The number of words in the document appears in the command line at the top of the screen.

The new word count command is fastóit takes only about two and a half seconds to tally all the words in the largest document SpeedScript can hold (over 43,000 bytes). The count is very close to the actual number of words. However, no counting routine can be perfect. Word Count is occasionally fooled by hyphenated words, dashes with a space on either side, embedded printer commands followed by a space, or cases where no space appears after a comma or other punctuation.

After you confirm that the word count feature works properly, you can delete the BASIC loader program. To make copies of the enhanced version of SpeedScript, simply load SpeedScript and save it to a new disk or tape as usual.

Inside Word Count

You can use the new word count feature without understanding how it works. However, machine language programmers may be interested in its basic operation. Unlike some other SpeedScript enhancements, Word Count doesn't steal any memory from the word processor's text space. Its ML code resides in the 143-byte free area (locations 9329-9472) between the end of SpeedScript and the beginning of the document. Word Count uses 126 of those free bytes.

If you have a copy of the book SpeedScript: The Word Processor for the Commodore 64 and VIC-20, refer to the routine labeled CONTROL on page 101. Word Count wedges into that routine, diverting the normal program flow if an unidentified CTRL combination is used. The new code checks to see if you pressed CTRL-W. If not, nothing is done and control returns to SpeedScript as usual. If CTRL-W was pressed, Word Count counts the words in the document by counting the number of blank spaces which are preceded by anything other than a space. When Word Count is done counting, it runs part of the display free memory routine (see page 104 of the book). At this point it returns control to SpeedScript.

The same technique can be used by machine language programmers to create other SpeedScript utilities that rely on unimplemented CTRL key combinations. Typing POKE 2854,114: POKE 2855,36 diverts control to location 9330 ($2472) whenever SpeedScript detects an unknown CTRL combination. The accumulator (A register) holds the ASCII value of the CTRL combination. Your code at $2472 should compare that value to the CTRL combination you are looking for. If there's a match, branch to the rest of your program. If no match appears, execute JMP $0A69 to reenter the main loop of SpeedScript.

If you install a new routine with this wedge technique, make sure that the routine ends with the instruction JMP $0A69 to return control to the proper place. One last hint: Before you write any new routines, check the SpeedScript source code to see whether it already contains a routine you can use. One reason Word Count can do its job in only 126 bytes is that it uses part of an existing routine to perform part of its work.

Word Count For SpeedScript

For Instructions on entering this program, please refer to "COMPUTEI's Guide to Typing In Programs" In this issue of COMPUTEI.





GS 50 POKE2854, 114 : POKE 2855, 36 : POKE9070,0




HJ 90 PRINT "{2 DOWN}OK.{DOWN}" : IF A$="N"THEN 120

RD 100 IF A$="T" THEN POKE 4904,234:POKE 4905,169 : POKE 4906,84:GOTO 120

KM 110 IF A$="D" THEN POKE 4904,234:POKE 4905,169 : POKE 4906,68

PE 120 POKE43,1 : POKE44,8:POKE45,240 : POKE46,36:S AVE"SPEEDSCRIPT3/C",8

FX 130 DATA 201,23,240,3,76,105,10,169,0,133,25,133,26,173,8,32,133,3,173,9,32

QJ 140 DATA 133,4,173,23,32,133,5,173,24,32,133,6,56,165,3,229,5,133,28,165,4

DH 150 DATA 229,6,5,28,240,28,160,0,177,3,201,32,208,10,197,27,240,6,230,25,208

CQ 160 DATA 2,230,26,133,27,230,3,208,217,230,4,208,213,169,170,133,3,169,17 7,133

AA 170 DATA 4,169,145,133,5,169,179,133,6,56,169,0,229,25,133,28,169,0,229,26,5,28

GD 180 DATA 240,6,230,25,208,2,230,26,32,78,10,165,26,166,25,32,1,30,76,105,10,0