Leo Brodie's inability to express even the most complex technical concepts without adding a twist of humor comes from an early love of comedy. He specialized in playwriting at UCLA and has had several comedies produced there and in local theater. He has also written freelance magazine articles and has worked as a copywriter for an add agency. When a company he was working for installed a computer, he became inspired to try designing a microprocessor-based toy. Although he never got the toy running, he learned a lot about computers and programming. He now works at Forth, Inc. as a technical and marketing writer, where he can play on the computers as the muse determines without having to be a fanatical computer jockey, and is allowed to write books such as this.
Leo's other interests include singing, driving classic Volvos, and dancing to 50's music.
The Forth community can celebrate a significant event with the publication of Starting Forth. A greater effort, talent, and commitment have gone into this book than into any previous introductory manual. I, particularly, am pleased at this evidence of the growing popularity of Forth, the language.
I developed Forth over the period of some years as an interface between me and the computers I programmed. The traditional languages were not providing the power, ease, or flexibility that I wanted. I disregarded much conventional wisdom in order to include exactly the capabilities needed by a productive programmer. The most important of these is the ability to add whatever capabilities later become necessary.
The first time I combined the ideas I had been developing into a single entity, I was working on an IBM 1130, a "third-generation" computer. The result seemed so powerful that I considered it a "fourth-generation computer language." I would have called it FOURTH, except that the 1130 permitted only five-character identifiers. So FOURTH became FORTH, a nicer play on words anyway.
One principle that guided the evolution of Forth, and continues to guide its application, is bluntly: Keep It Simple. A simple solution has elegance. It is the result of exacting effort to understand the real problem and is recognized by its compelling sense of rightness. I stress this point because it contradicts the conventional view that power increases with complexity. Simplicity provides confidence, reliability, compactness, and speed.
Starting Forth was written and illustrated by Leo Brodie, a remarkably capable person whose insight and imagination will become apparent. This book is an original and detailed prescription for learning. It deftly guides the novice over the thresholds of understanding that all Forth programmers must cross.
Although I am the only person who has never had to learn Forth, I do know that its study is a formidable one. As with a human language, the usage of many words must be memorized. For beginners, Leo's droll comments and superbly cast characters appear to make this study easy and enjoyable. For those like myself who already know Forth, a quick reading provides a delightful trip and fresh views of familiar terrain. But I hope this book is not so easy and enjoyable that it seems trivial. Be warned that there is heavy content here and that you can learn much about computers and compilers as well as about programming.
Forth provides a natural means of communication between man and the smart machines he is surrounding himself with. This requires that it share characteristics of human languages, including compactness, versatility, and extensibility. I cannot imagine a better language for writing programs, expressing algorithms, or understanding computers. As you read this book, I hope that you may come to agree.
Charles H. Moore
Welcome to Starting Forth, your introduction to an exciting and powerful computer language called Forth.
If you're a beginner who wants to learn more about computers, Forth is a great way to learn. Forth is more fun to write programs with than any language that I know of. (See the "Introduction for Beginners", and check Wikipedia)
If you are a seasoned professional who wants to learn Forth, this book is just what you need. Forth is a very different approach to computers, so different that everyone, from newcomers to old hands, learns Forth best from the ground up. If you're adept at other computer languages, put them out of your mind for now, and remember only what you know about computers. (See the "Introduction for Professionals.")
Since many people with different backgrounds are interested in Forth, I've arranged this book so that you'll only have to read what you need to know, with footnotes addressed to different kinds of readers. The first half of Chap. 7 provides a background to computer arithmetic for beginners only.
This book explains how to write simple applications in Forth. It includes all standard Forth words that you need to write a high-level single-task application. This word set is an extremely powerful one, including everything from simple math operators to compiler-controlling words. (ANS Forth standard online)
Excluded from this book are all commands that are related to the assembler, target compiler and other specialized utilities. These commands are available on some versions of Forth such as eForth and most commercial implementations. (Forth vendors)
I've chosen examples that will actually work on a Forth system with a terminal and a disk. Don't infer from this that Forth is limited to batch or string-handling tasks, since there is really no limit to Forth's usefulness.
Here are some features of this book that will make it easy to use:
All commands are listed twice: first, in the section in which the word is introduced, and second, in the summary at the end of that chapter.
Each chapter also has a review of terms and a set of exercise problems, with answers.
Several "Handy Hints" have been included to reveal procedural tips or optional routines that are useful for learners but that don't merit an explanation as to how or why they work.
A personal note: Forth is a very unusual language. It violates many cardinal rules of programming. My first reaction to Forth was extremely sceptical, but as I tried to develop complicated applications I began to see its beauty and power. You owe it to yourself to keep an open mind while reading about some of its peculiarities. I'll warn you now: few programmers who learn Forth ever go back to other languages.
Good luck, and enjoy learning!
Leo Brodie
| |
---|---|
Copyright | The original print edition of Starting FORTH is copyrighted by FORTH, Inc. and all rights are reserved. Permission has been granted to Marcel Hendrix to make this on-line version of the work available and to make certain modifications to the original printed work. Further reproduction (except for the printing of single copies for personal use), modification, distribution, posting on other web sites, or unauthorized use of either the printed or on-line version of Starting FORTH by any party other than Marcel Hendrix is expressly forbidden without prior written permission from: |
More information | FORTH, Inc. 5155 W. Rosecrans Blvd. #1018 Hawthorne, CA 90250 USA +1 (310) 978-9454 [fax] +1 (310) 491-3356 [phone] |
| |
I'd like to thank the following people who helped to make this book possible:
For consultation on Forth technique and style: Dean Sanderson, Michael LaManna, James Dewey,
Edward K. Conklin, and Elizabeth D. Rather, all of FORTH Inc.; for providing insights into the
art of teaching Forth and for writing several of the problems in this book: Kim Harris of the
Forth Interest Group; for proofreading, editorial suggestions, and enormous amounts of work
formatting the pages: Carolyn A. Rosenberg; for help with typing and other necessities: Sue
Linstrot, Carolyn Lubisich, Kevin Weaver, Kris Cramer, and Stephanie Brown Brodie; for help
with the graphics: Winnie Shows, Natasha Elbert, Barbara Roberts, and John Dotson of Sunrise
Printery (Redondo Beach, CA); for technical assistance: Bill Patterson and Gary Friendlander;
for constructive criticism, much patience and love: Stephanie Brown Brodie; and for inventing
Forth: Charles H. Moore.
Starting Forth, First Edition is from 1981. These web pages were designed in 2003, when it became apparent that SF would never be re-issued by the copyright holder. A small supply of about 500 books was all that was left.
When you can get hold of the original, do so.
In this transcript Forth code has been ANSified. The samples should run on, at least, iForth and SwiftForth. Where necessary, statements that were valid in 1981 have been exchanged with statements more appropriate for 2003 (when this tribute was written).
Starting Forth is full of very difficult to reproduce graphics. These enormously enhance the text's mnemonic value, and are invaluable for a first-time Forth user. I have therefore added "substitute" graphic elements, roughly at the same spot where they are in the original. The original graphics are, of course, much better.
In this transcript I have assumed a 32-bit, byte-addressing Forth, with 8-bit characters. The address returned by WORD is assumed to be HERE. This allows the common trick of ALLOTing length of str CHARS after using WORD in order to compile string str to memory. Multitasking issues are ignored (e.g. no >TYPE, just TYPE). Division is symmetric, not floored, and two's complement is assumed throughout. Most Forths should not have problems with this. Chapter 7 exploits extended uses of number conversion. Some Forths are broken in this respect, but iForth and SwiftForth do support these neat features.
At first when beginners hear the term "computer language," they wonder, "What kind of language could a computer possibly speak? It must be awfully hard for people to understand. It probably looks like:
976#!@NX714&+if it looks like anything at all."
Actually a computer language should not be difficult to understand. Its purpose is simply to serve as a convenient compromise for communication between a person and a computer.
Consider the marionette. You can make a marionette "walk" simply by working the wooden control, without even touching the strings. You could say that rocking the control means "walking" in the language of the marionette. The puppeteer guides the marionette in a way that the marionette can understand and that the puppeteer can easily master.
Computers are machines just like the marionette. They must be told exactly what to do, in specific language. And so we need a language which possesses two seemingly opposite traits:
On the one hand, it must be precise in its meaning to the computer, conveying all the information that the computer needs to know to perform the operation. On the other hand, it must be simple and easy-to-use by the programmer.
Many languages have been developed since the birth of computers: Fortran is the elder statesman of the field; COBOL is still the standard language for data processing; BASIC was designed as a beginner's language along the road toward languages like Fortran and COBOL; C and Java are the general purpose application languages of the 90's. This book is about a very different kind of language: Forth. Forth's popularity has kept constant over the past several years, and its popularity is shared among programmers in all fields.
All the languages mentioned above, including Forth, are called "high-level" languages. It's important for beginners to recognize the difference between a high-level language and the computer it runs on. A high-level language looks the same to a programmer regardless of which make or model of computer it's running on. But each make or model has its own internal language, or "machine language." To explain what a machine language is, let's return to the marionette.
Imagine that there is no wooden control and that the puppeteer has to deal directly with the strings. Each string corresponds to exactly one part of the marionette's body. The harmonious combinations of movements of the individual strings could be called the marionette's "machine language."
Now tie the strings to a control. The control is like a high-level language. With a simple turn of the wrist, the puppeteer can move many strings simultaneously.
So it is with a high-level computer language, where the simple and familiar symbol "+" causes many internal functions to be performed in the process of addition.
Here's a clever thing about a computer: it can be programmed to translate high-level symbols (such as "+") into the computer's own machine language. Then it can proceed to carry out the machine instructions. A high-level language is a computer program that translates humanly understandable words and symbols into the machine language of the particular make and model of computer.
What's the difference between Forth and other high-level languages? To put it very briefly: it has to do with the compromise between man and computer. A language should be designed for the convenience of its human users, but at the same time for compatibility with the operation of the computer.
Forth is unique among languages because its solution to this problem is unique. This book will explain how.
Forth enjoyed a rising tide of popularity up to around 1994, (ANS and ISO Forth standards), perhaps most visibly among enthusiasts and hobbyists. After 1996 or so Forth's popularity has stayed relatively constant. But this development is only a new wrinkle in the history of Forth. Forth has been in use from 1972 on, in critical scientific and industrial applications. In fact, if you use a mini- or microcomputer professionally, chances are that Forth can run your application--more efficiently than the language you're presently using.
Now you'll probably ask rhetorically, "If Forth is so efficient, how come I'm not using it?" The answer is that you, like most people, don't know what Forth is.
To really get an understanding of Forth, you should read this book and, if possible, find a Forth system and try it for yourself. For those of you who are still at the bookstore browsing, however, this section will answer two questions: "What is Forth?" and "What is it good for?"
Forth is many things:
As a language, Forth begins with a powerful set of standard commands, then provides the mechanics by which you can define your own commands. The structural process of building definitions upon previous definitions is Forth's equivalent of high-level coding. Alternatively, words may be defined directly in assembler mnemonics, using Forth's assembler. All commands are interpreted by the same interpreter and compiled by the same compiler, giving the language extreme flexibility.
The highest level of your code will resemble an English-language description of your application. Forth has been called a "meta-application language"--a language that you can use to create problem-oriented languages.
As an operating system, Forth does everything that traditional operating systems do, including interpretation, compilation, assembling, virtual memory handling, I/O, text editing, etc.
But because the Forth operating system is much simpler than its traditional counterparts due to Forth's design, it runs much more quickly, much more conveniently, and in much less memory.
What is Forth good for? Forth offers a simple means to maximize a processor's efficiency. For example:
Forth is fast. High-level Forth executes as fast as other high-level languages and between 20 to 75% slower than equivalent assembly-language programs, while time-critical code may be written in assembler to run at full processor speed. Without a traditional operating system, Forth eliminates redundancy and needless run-time error checking.
Forth compiled code is compact. Forth applications require less memory than their equivalent assembly-language programs and consume less power (important for hand-helds and portable gadgets!) Written in Forth, the entire operating system and its standard word set reside in less than 8K bytes. Support for a target application may require less than 1K bytes.
Forth is transportable. It has been implemented on just about every mini- and microcomputer known to the industry. Most microcontrollers and DSPs, even tiny ones, also have a Forth implementation.
Forth has been known to cut program development time by a factor of ten for equivalent assembly-language programming and by a factor of two for equivalent high-level programming in C or Java. Productivity increases because Forth epitomizes "structured programming" and because it is interactive and modular.
Here are a few samples of Forth in the real world (FORTH, Inc., MPE):
There's a catch we must admit. It is that Forth makes you responsible for your computer's efficiency. To draw an analogy: a manual transmission is tougher to master than an automatic, yet for many drivers it offers improved control over the vehicle.
Similarly, Forth is tougher to master than traditional high-level languages, which essentially resemble one another (i.e., after learning one, it is not difficult to learn another). Once mastered, however, Forth gives you the capability to minimize CPU time and memory space, as well as an organizing philosophy by which you can dramatically reduce project development time.
And remember, all of Forth's elements enjoy the same protocol, including operating system (sometimes), compiler, interpreters, text editor, virtual memory, assembler, and multiprogrammer. The learning curve for Forth is much shorter than that for all these separate elements added together.
If all of this sounds exciting to you, turn the page and start Forth.