figFORTH REFUSES TO DIE

 

PREFACE FOR THE SECOND EDITION

 

 

The first edition of Systems Guide to figForth was published almost ten years ago.  Forth has made significant changes, and perhaps some improvements since.  The figForth Model was supposedly replaced by Forth-78, Forth-79, and Forth-83 Standards, at least that is the impressions Forth experts wanted us to believe.  FigForth should have been laid to rest long, long ago.  In it place, there are many better, larger, and more comprehensive Forth implementations available from many vendors and also in the public domain.  Why would anybody want to use this old fashioned figForth?

 

The fact remains that figForth is still very popular.  Among all the publications sold by the Forth Interest Group over the years, Bill Ragsdale's figForth Installation Manual has always been the most demanded document.  I have also decided many times to quit reprinting this book, but Roy Martens of the Mountain View Press just keeps on ordering them.  There must be some magic behind figForth, keeping it alive despite its age, the lack of support, and its deficiency in tools for serious program development.

 

However, there are many advantages in figForth which make it useful to many programmers and new students of Forth.  Here is a list:

 

1.  Consistency.  It is a single model implemented uniformly on many microprocessors.  Many

    implementations are readily available in assembly listings.

2.  Well factored kernel.  Less than 50 words are machine dependent.  All other words are

    defined in high level.  It is easily portable to other microprocessors and operating systems.

3.  The source code are in regular assembly, which can be understood by most programmers.

    Most other Forth systems are generated through meta compilation, and the source code

    are only available in Forth.  The Forth source listing is difficult for Forth programmer

    to understand, let alone non-Forth programmers.

4.  Simplicity.  It provides only the functionality to be self-supporting.  Lacking of

    extensive and complicated utility makes it easier to study and comprehend.

 

Generally, it is impossible to study and understand a commercial Forth system.  The source code is not available in most cases.  Where the source code is provided, it is too complicated to be dissected and to be put back together again.  For people who are curious about how Forth really ticks, figForth is the only Forth system that an average person can understand in a couple of weeks.  It is also easy to port figForth to a virgin computer un-adulterated by a prior Forth implementation.  This I found ten years ago, and I think it is still true today.  We have no valid alternative in teaching and learning Forth besides this old figForth.

 

The original manuscript of this book was written using the ED line editor under the RT-11 operating system on a DEC LSI-11 microcomputer.  That was the desk top publishing system of the day.  The text was first entered in upper case only, because the old ADM terminal I used did not have lower case characters.  It was painfully converted to lower case and eventually printed out using a letter quality printer.  However, lines were too long to fit on a page.  The printing was done using 1/10" characters with 1/16" spacings, and the characters tended to rub elbows with one another.  Lines were doubly spaced so that it gave the reader the illusion of a thick manual with substance.  The result was an eye-sore, whether I admitted it or not.

 

In spite of the poor condition of this book, I often ran into people who complimented me for it.  Many of them mentioned that it lead them to the understand of the figForth Model and the appreciation of Forth as a programming language.  Now, since I have much better tools for desk top publishing, it is time to give this book a facelift so that it does some justice to the figForth Model and its faithful users.

 

Only cosmetic touches are made in this revision.  As I re-visited the chapters, I found most of the discussions are still useful for people wanted to fully understand Forth.  After ten years, we have many more books on Forth.  However, none of them tried to present Forth in the bottom-up and inside-out fashion this book did.  Besides, the discussions on syntax of Forth, the detailed implementation of Forth editor and assemblers are not covered in available Forth literature.

 

I like to thank Bill Ragsdale, Kim Harris, Bob Smith, John James, John Cassidy, and the members of the Forth Implementation Team to provide the figForth Model and implementations and their efforts in my enlightenment.  I am also indebted to Kevin Appert in helping me move the manuscript from the RT-11 system into MS-DOS so that I had free access to the texts again.

 

                                            C. H. Ting

 

San Mateo, California

April 1989


 

 

PREFACE TO THE FIRST EDITION

 

 

 

Forth was developed by Charles Moore in the 1960's.  It took the final form as we now know it in 1969, when Mr.  Moore was at the National Radio Astronomy Observatory, Charlottesville, Va.  It was created out of his dissatisfaction with available programming tools, especially for instrumentation control and automation.  Distribution of his work to other observatories has made Forth the standard language for observatory automation.  Mr.  Moore and several associates formed FORTH, Inc.  in 1973 for the purpose of licensing and support of the Forth operating system and programming language, and to supply application software to meet customers' unique requirements. 

 

Forth Interest Group was formed in 1978 by a group of Forth programmer in Northern California.  It is a non-profit organization.  Its purpose is to encourage the use of Forth language by the interchange of ideas through seminars and publications.  It organized a Forth Implementation Team in 1978 to develop Forth operating systems for popular microprocessors from a common language model, now known as figForth.  In early 1979, the Forth Implementation Team published six assembly listings of figForth for 8080, 6800, 6502, PDP-11, 9900, and PACE at $10.00 each.  The quality and availability of these listings, which are placed in the public domain, made figForth the most popular dialect in Forth. 

 

Most of the published materials on Forth are manuals which teach how to use a particular Forth implementation on a particular computer.  Very few deal with the inner mechanisms on how the Forth system operates which is essential to the understanding and effective utilization of the Forth language.  My intention here is to describe how the Forth system does all these wonderful things no other language can.  With a deeper understanding of the inner mechanism, a user can have a better appreciation of many unique features which make Forth such a powerful programming tool. 

 

Among other things, documentation on Forth is very difficult to read and to comprehend because Forth definitions are short and their numbers are many.  The definitions are very hard to arrange in a logical order to promote better or easier understanding.  For example, the glossary is arranged alphabetically, which is great for reference purposes.  If you know which definition you are looking for, you can find it very conveniently in the glossary, but how a definition is related to others and how it is to be used are not easy to find.  The source codes, coded in Forth , are also difficult to comprehend because the definitions are ordered from bottom up, i.e., low level definitions must precede the higher level definitions using the low level definitions.  I will not mention the problems in reading codes written with postfix notations.  These are problems for which Forth is often criticized.  A book on the systems aspect in the figForth Model can help programmers to climb the learning curve and ease somewhat the growing pain in learning this very strange language. 

 

In this book I will attempt to explain the operation of figForth system in a systematic fashion.  The top level Forth definitions related to the system operations are treated in logical sequences.  Most of these definitions are defined in terms of other predefined Forth definitions; therefore, it is required that the reader has some basic knowledge of the elements contained in the Forth language, such as the dictionary, the data stack, and the return stack.  However, Forth language is structured and modular, so that the logical contents of a definition are not difficult to grasp if the functions of all the low level definitions involved are clearly stated. 

 

Because of the modular structures inherent in the Forth language, the definition of a Forth word itself is a fine vehicle to convey its function.  In fact, the definition can be used in lieu of a flow chart.  In our discussions, Forth definition are laid out in a vertical format.  The component definitions are written in a column on the left hand side of a page, and the comments and explanations are in columns toward the right hand side.  When a group of words of very close relationship (a phrase) appears, it is often displayed in one line to save space. 

 

Many Forth words are defined in machine codes.  They are called code definitions or primitive definitions and they form the body of what is called the "virtual Forth machine ".  These definitions are used to convert a particular CPU into a Forth computer.  The detailed contents of these words cannot be discussed without resorting to the assembly language of the host CPU, and we shall avoid their discussion as much as possible.  In the cases where it is absolutely necessary to use them in order to clarify how the system functions, the figForth PDP-11 codes will be used because the PDP-11 instruction set is very close to what is required optimally to implement a virtual Forth computer. 

 

The detailed definitions of Forth words will strictly adhere to those defined in the figForth model as presented in the figForth Installation Manual.  This model is the most complete and consistent documentation defining a Forth language system which has been implemented on a host of microcomputers.  The Forth operating system written in Forth provides the best examples for the serious students to learn the Forth language.  Most of the programming tools provided by the Forth system were developed to code the Forth system itself.  By going through the Forth system carefully, a Forth user can learn most programming techniques supported by the Forth language for his own use. 

 

In Chapter 1, I try to lay down the formal definition of Forth as a programming language.  It was completed only very recently, after all other chapters were done.  Some terms used in Chapter 1 are not quite consistent with those used in the later chapters.  The terms 'word', 'definition', and 'instruction' are used interchangeably in later chapters are differentiated in Chapter 1.  Chapter 2 is an overview of the figForth operating system. 

 

In the rest of the book, each chapter will dwell on a particular topic in the figForth system.  The more important definitions at the highest level, which the user will use most often are discussed first to give an overall view of the tasks involved.  The low level definitions or utility definitions used by the high level definitions are then discussed in detail to complete the entire picture.  Descriptive comments are given for the low level definitions when they appear in a high level definition before they are completely defined.  Therefore, it will be helpful to re-read a chapter so that the knowledge gained by studying the utility definitions can further illuminate the high level definition outlining the task involved. 

 

Special thanks are due to William F. Ragsdale, who authored the figForth Installation Manual and guides the Forth Interest Group from its inception, to John S. James, who developed the PDP-11 figForth and the PDP-11 Assembler, and to John Cassady, who developed the 8080 figForth and the 8080 Assembler.  Thanks are also due to Robert Downs, Anson Averrell, Alice Ferrish and Albert Ting, who kindly gave me long lists of corrections and made many helpful suggestions on the manuscript. 

 

 

                                            C. H. Ting

San Mateo, Ca. 

May, 1981. 

 

 


 

 

SYSTEMS GUIDE TO figFORTH

 

 

 

CONTENTS

 

 

 

figFORTH REFUSES TO DIE, PREFACE TO THE SECOND EDITION          i

 

PREFACE TO THE FIRST EDITION                              iii

 

CONTENTS                                               v

 

FIGURES                                         viii

 

TABLES                                          viii

 

1.  LANGUAGE DEFINITION OF FORTH   ..................  1

    1.1.    Programming Language    ................................  1

    1.2.    Words  ............................................................   2

    1.3.    Standard Instructions   ....................................   3

    1.4.    User Instructions   ..........................................   5

    1.5.    Structures and Colon Instructions   .................   6

    1.6.    Code Instructions   .........................................   7

    1.7.    Constants, Variables, and Vocabulary  ...........   8

    1.8.    Create Defining Instructions  ..........................   8

    1.9.    Conclusion  ....................................................   10

 

2.  THE figFORTH MODEL  .........................................   11

    2.1 Forth as an Operating System   ...........................    11

    2.2.    Memory Map   .................................................   13

    2.3.    Instruction Set   ................................................   13

    2.4.    System Constants and User Variables   ............   17

    2.5.    Simple Colon Definitions   ...............................   18

 

3.  THE TEXT INTERPRETER   .....................................   21

    COLD   .......................................................................   21

    ABORT, QUIT   ..........................................................   23

    INTERPRET   ..............................................................   25

    X   ...............................................................................   26

 

4.  THE ADDRESS INTERPRETER   ..............................   27

    NEXT, EXECUTE   .....................................................   28

    DOCOL, ;S   ...............................................................    29

    PUSH, POP    ...............................................................   29

    PUT, LIT    ...................................................................   30

 

5.  COMPILER   ................................................................   31

    [ , ]     ............................................................................   31

    CREATE, CODE   .........................................................   32

    : , ;    ..............................................................................   34

 

6.  ERROR HANDLING   ..................................................   37

    ?ERROR, ERROR, (ABORT)    .....................................   37

    MESSAGE, ?COMP, ?EXEC, ?PAIRS   .........................   39

    ?CSP, ?LOADING, ?STACK   .......................................   40

 

7.  TERMINAL INPUT AND OUTPUT   ...........................   41

    EXPECT   ......................................................................   41

    QUERY, WORD   ..........................................................   43

    TYPE, COUNT    ...........................................................   44

    -TRAILING, (."). ."   ......................................................   46

    ID., .LINE   ....................................................................   47

    (LINE), LIST   ................................................................   48

 

8.  NUMERIC CONVERSIONS   ........................................   49

    HEX, OCTAL, DECIMAL, (NUMBER)   ........................   49

    NUMBER, <#    ...............................................................   50

    HOLD, #, #S, SIGN   .......................................................   52

    #>, CR, SPACE, SPACES, D.R   ......................................   53

    D. , .R,  .  , ?   ..................................................................   54

    DUMP   ...........................................................................    55

 

9.  DICTIONARY   ................................................................  57

    HERE, ALLOT, ','    ..........................................................   57

    'C,' , -FIND    .....................................................................   58

    VOCABULARY, DEFINITIONS   .....................................   59

    TRAVERSE, LFA, CFA, NFA, PFA, LATEST   .................   60

    ' , FORGET   .......................................................................   61

    VLIST   ...............................................................................   62

 

10.  VIRTUAL MEMORY   .....................................................   63

    BLOCK   .............................................................................   65

    +BUF, BUFFER, R/W   ........................................................   67

    UPDATE, EMPTY-BUFFERS, DR0, DR1  ...........................   68

    FLUSH, LOAD   ...................................................................   69

    -->   .......................................................................................   70

 

11.  DEFINING WORDS  ..........................................................   71

    ;CODE   .................................................................................   72

    (;CODE), <BUILDS, DOES>   ................................................   73

    CONSTANT, VARIABLE   ....................................................   74

    USER   ....................................................................................   75

 

12.  CONTROL STRUCTURES   ................................................   77

    COMPILE   .............................................................................   77

    [COMPILE], BRANCH, 0BRANCH   .....................................   78

    IF, ENDIF    ............................................................................   79

    ELSE, BEGIN, BACK , UNTIL   .............................................   80

    AGAIN, WHILE, REPEAT   ....................................................   81

    DO, (DO), I   ...........................................................................   82

LEAVE,  LOOP,  (LOOP), +LOOP   ............................................   83

    (+LOOP)   ................................................................................   84

 

13.  EDITOR   ...............................................................................   85

    13.1.   Line Editor   ...................................................................   85

        TEXT, LINE , -MOVE   ........................................................   86

        H , S, D , E   ..........................................................................   87

        R , P , I , CLEAR, COPY   .....................................................   88

    13.2.   String Editor   ..................................................................   89

        MATCH   ...............................................................................   89

        -TEXT, 2DROP, 2DUP, 2SWAP, TOP , #LOCATE   ..............   90

        #LEAD, #LAG, M, T , L    ......................................................   91

        1LINE, FIND, DELETE, N   ....................................................   92

        F , B , X , TILL, C   .................................................................   93

   

14.  FORTH ASSEMBLER   ...........................................................   95

    14.1.   Three Levels of Forth Assembler  ....................................   95

    14.2.   PDP-11 Assembler   .........................................................   97

        ENTERCODE, CODE    ..........................................................   97

        IS, RTST   ................................................................................   98

        OP, 1OP    ................................................................................   99

        FIXMODE, ORMODE, ,OPERAND   .......................................  100

        B , ROP, BOP   .........................................................................   101

        2OP, SWAPOP   ........................................................................  102

        IF, , IPATCH, , ENDIF, , ELSE,   ...............................................  103

        BEGIN, , UNTIL, , REPEAT, , WHILE, , C;   .............................  104

        NEXT,   ......................................................................................   105

    14.3.   8080 Assembler   .................................................................   105

        CODE   .......................................................................................   105

        C; , LABEL , 8* , IS   ..................................................................   106

        1MI, 2MI, 3MI    ..........................................................................   107

        4MI, 5MI    ...................................................................................   108

        MOV, MVI, LXI, NOT, IF   ..........................................................   109

        ENDIF, ELSE, BEGIN, UNTIL   ...................................................   110

        AGAIN, WHILE, REPEAT   ..........................................................   111

 

 

 INDEX   .................................................................................................  113

 

 

 FIGURES

    1.      Memory Map of a Typical Forth System   ..................................  12

    2.      The Forth Loop   ........................................................................   22

    3.      Text Interpreter Loop   ................................................................  24

    4.      Structure of a Definition  .............................................................  33

    5.      Error Handling  ...........................................................................   38

    6.      EXPECT   ....................................................................................   42

    7.      WORD   .......................................................................................   45

    8.      Numeric Conversion   ..................................................................   51

    9.      Disk Buffers   ...............................................................................   64

    10.     BLOCK   ......................................................................................   66

 

 

TABLES

    1.      Language Definition of Forth   ........................................................   1

    2.      Standard Instructions   .....................................................................   4

    3.      User Instructions   ............................................................................   5

    4.      Creating New Defining Instructions   ...............................................   9

    5.      Stack Instructions   ...........................................................................   14

    6.      Input Output Instructions   ................................................................   15

    7.      Memory and Dictionary Instructions   ...............................................   15

    8.      Defining Instructions and Control Structures   ...................................   16

    9.      Miscellaneous Instructions   ...............................................................   16

    10.     System Constants   .............................................................................   17

    11.     User Variables    .................................................................................   17