5.  PROGRAMMING IN cmFORTH

 

 

 

5.1.    cmForth Source Code

The best programming examples and documentation are still the cmForth itself.  The complete source code and associated documentation in the form of shadow screens are shown in the following Listing 1.  This version of cmForth is derived from the December 1987 version of cmForth, written by Chuck Moore for his ForthKit 4.  Mr. Thor-Bjorn Bladh of Lund, Sweden converted it for RTX2000.  The source code from Mr. Bladh has no comments nor documentation.  Mr. Jay Melvin restored the source listing to the style of the original cmForth with stack comment.  Dr. C. H. Ting restored the shadow screen comments with appropriate adjustment.

The source screens can be grouped into the following subsections:

Screen 1.           Optimized compiler loader.  It is generally needed to do normal   programming.

Screen 2.           Meta compiler.  Loaded by Screen 3.

Screen 3.           Target compiler.  Recompile cmForth itself.

Screens 4-7.        Optimized compiler.  Loaded by Screen 1.

Screens 8-11.       cmForth kernel.

Screens 12-22.     cmForth text interpreter.

Screens 23-24.     Reset and interpreter word set.

Screens 25-30.     cmForth compiler.

 

¡@

5.2.    Utilities

cmForth reflects much of the personality of its principal author, Chuck Moore.  Mr. Moore invented Forth and has never obliged himself to follow standards other than his own.  Many of the words in cmForth behave differently from the same words defined in popular standards like figForth, Forth-79, and Forth-83.  Adjusting completely to cmForth is often painful for many Forth users who have been conditioned to these standards.  In this utility package, many cmForth words are redefined to follow the standard behavior, like COUNT, TYPE, and DUMP.  Several words are added to ease programming and debugging, like .S, WORDS, LIST, and some line editing commands.

These utility words are presented in Listing 2.  Many of them were taken from 'Footsteps in an Empty Valley' for NC4000.

 

¡@

5.3.    Double and Quad Math Words

cmForth was designed exclusively for a 16 bit machine.  There are a few words dealing with double integers like 2DUP and 2DROP, but no double integer math and logic words.  This package extend the math and logic function to 32 bit double integers and 64 bit quad integers.  They are optimized for RTX2000, taking advantage of the powerful instruction set supported by RTX.  This package greatly extends the dynamic range and the precision of numbers that RTX can operate on.

This package could be used as a basis to construct a floating point math package for RTX2000.  The source code is presented in Listing 3.  Most complicated words are extensively commented.

 

¡@

5.4.    Multiple Precision Math Words

This is a major programming effort to use RTX to process very large integer numbers, up to 400 bytes which is equivalent to about 1100 decimal digits.  One of the applications of large integer numbers is the public key encryption and decryption systems.  Elementary functions provided in this package are addition, subtraction, multiplication, division, square, square root, exponantiation, exponantiation-modulus, and GCD.

This package is derived from Dr. S. Y. Tang's multiple precision math files he contributed to F-PC, a Forth programming system optimized for IBM-PC/XT/AT machines by Tom Zimmer and Dr. Robert L. Smith.

Because the code in this package is closely customized to run under the RTX cmForth, detailed comments are included to explain specific techniques employed in implementing the algorithms.  Source code and comments are shown in Listing 4.

 

Listing 1.  Source and Documentation of RTX cmForth.

Listing 2.  Utilities for RTX cmForth.

Listing 3.  Double and Quad Integer Word Set

Listing 4.  Multiple Precision Math Package.

¡@