Income o Easymakingmoneysecret Income o Easymakingmoneysecret t Attitudes Extra o Attitudes searchasearch

Tag xsearchc Extra tn1 a Attitudes Easymakingmoneysecret n1l Tag ssearchachsearchsa1che Easymakingmoneysecret I Www cme Www is Easymakingmoneysecret osearch Tag usearchr Extra ut Tag nsearch clsearchs Www isearch searcho Income Easymakingmoneysecret Tag e Tag ysearchpsearchodsearchci1ee Easymakingmoneysecret tsearchr Attitudes rsearchs Easymakingmoneysecret .searchLsearchc Trading isearchy Trading Income o Easymakingmoneysecret e Www 60 Income F Income rsearchh Trading wordssearcha Income esearchdsearchf Easymakingmoneysecret n Income dsearchi Www Tag achsearchn Extra Income ang Extra agsearch. Esearche Easymakingmoneysecret y Www ef Easymakingmoneysecret nsearchti Easymakingmoneysecret n searchvsearchnu1l Easymakingmoneysecret yca Income lssearcha1com Attitudes intio Www Trading f searchh Attitudes s p1imi Www i Easymakingmoneysecret essearch a Attitudes dsearchd Tag ess1m searchea Www searchor1.

The primitives define a virtual Forth machine. To port Forth to a new system, only the primitives are rewritten. While some Forths run under DOS or Windows, in an embedded application the machine-code definitions of the primitives are the operating system.

Forth passes parameters onto a stack. Before a word is executed, the necessary parameters must be present on the stack. After execution, the results, if any, are left on the stack.

This is precisely what happens in most modern computer languages, but the stack is usually hidden. In Forth, the programmer is aware of what is on the stack and can manipulate it directly. For example, the primitive Forth word SWAP exchanges the top two elements of the stack. Most languages save pending operations. When you write C = A + B, the compiler puts the "equals" and "plus" operations on its pending list until it gets to the end of the expression. Then, it rewrites it as "Fetch A, Fetch B, Add, Store C."

Forth cuts out the middle step. In Forth, you write the same operation as A @ B @ + C !. The @ and ! are Forth¡¯s shorthand for the "fetch" and "store" operations. The + , oddly enough, represents addition.

Luckily, only a handful of Forth words are this cryptic. Most Forths accept names up to 31 characters long, and most standard words describe their function. Good Forth is self-commenting, so make your words as self-descriptive as possible. At run time, any numbers you type are placed on top of the parameter stack. You debug a word by typing its input parameters, then the word. It executes immediately as if Forth were an interpreter, allowing you to check the results on the stack.

A stack element typically has 32 bits (some Forths use 16) and is untyped. It might represent a signed or unsigned integer, an address, a single-precision floating-point number, or a Boolean flag. You need to keep track.

Forth¡¯s philosophy is to permit, not to impede. If you have a good reason to add a Boolean to an address, Forth won¡¯t stop you. For that matter, there¡¯s nothing in Forth that prevents you from putting the wrong number of items on the stack. Forth is fast and efficient, but you have to keep your eyes open.

Creating a New Definition

Perhaps the most important word in Forth is the colon, which switches the compiler from run mode to compile mode and creates a new dictionary definition. The first word in the source after a colon is the name of the word to be defined. The definition follows the name. Logically enough, the definition is terminated by a semi-colon. This compiles a return instruction and switches the compiler back to run mode.

Thus, a complete Forth definition might look like this:

: MAGNITUDE (X Y¡ªvector magnitude) DUP * SWAP DUP * + SQRT ;

The expression in parentheses is the stack picture. It reminds the programmer what the word¡¯s input and output parameters are. DUP (duplicate) generates a second copy of the top element on the stack, * is a single-precision multiplication, and SQRT takes the square root of a number.

As an example of Forth¡¯s flexibility, suppose you had a hankering for C¡¯s ++ operation. Forth¡¯s nearest equivalent is +! which adds a specified number to a variable. If you define : ++ 1 SWAP +! ; then ALPHA ++ adds one to the variable ALPHA. What Forth does not allow, but C does, is writing this as ALPHA++. Since Forth does not parse expressions, it would read ALPHA++ as an undefined word.

Program Structure

Forth is highly structured. There is a way to compile a GOTO if you really want to, but normally you use the words IF, ELSE, THEN, BEGIN, UNTIL, WHILE, REPEAT, DO, and LOOP to control the flow of the program. These words compile conditional jumps into a definition.

Forth¡¯s IF checks the top of the stack for a flag left by one of Forth¡¯s many comparison words. To execute option 1 if the top two numbers on the stack are equal and option 2 if they are not, Forth¡¯s syntax is:

= IF do-option-1 ELSE do-option-2 THEN.

(I use ENDIF in my programs because I feel that THEN is an illogical throw-back to BASIC. Forth condones such personal idiosyncrasies, even if your bosses and co-workers may not.)

Constants, Variables, and Strings

A number in the source is compiled as a literal. A named constant stores a value at compile time and puts that value on the stack when it is invoked. Naming a variable compiles a storage space. Using a variable puts that address on the stack, ready for a fetch or store operation. A Forth string is just a variable whose first byte specifies its length.

Since a variable supplies its address, it can be manipulated before being used. For example, if you were using a Forth that had no ARRAY structure, you could define one. Forth can specify new types of defining words. Alternatively, you could fake it. BETA 7 + C@ fetches the eighth byte in the array that starts at the address of the variable BETA.

One deficiency of Forth source is that it may be unclear whether a word represents a variable or a function. Some follow the convention of hyphenating variable names but splitting function names with periods. Since good Forth code can be quite English-like, it is handy to distinguish code from comments visually without needing to parse a line. Thus, many commonly use upper case for code and lower case for comments.

Hardware for Forth

Forth has been implemented on just about every microprocessor which has ever existed but some chips are more suitable than others. Obviously, the closer the chip architecture comes to the Forth virtual machine outlined in Figure 2, the better Forth runs. Forth needs two stacks so it runs faster on a chip that supports more than one. Since Forth needs few registers, a chip with many is just a lot of wasted silicon.

Figure 2¡ª

The Forth virtual machine has a Harvard architecture. Hardware implementations frequently keep
the top stack element in a separate register.

Minimal Forths do arithmetic on 16- or 32-bit integers, so Forth runs slowly on eight-bit chips. Historically, Motorola microprocessors have been more suited to Forth than Intel ones. The MC6809 and the MC680X0 series were ideal 8-bit chips for Forth.

Since the Forth virtual machine is relatively simple, it can be implemented on a gate array. A pioneering effort by Charles Moore, the inventor of Forth, led Harris to introduce their RTX 2000 in 1989. This 10-MIPS single-chip Forth engine used a Harvard architecture with its parameter and return stacks on the chip. Unfortunately, it was not a commercial success and is now used only in niche markets such as processors on satellites.

Using Forth

Commercial and public-domain versions of Forth exist at all levels. For an embedded application on an 8- or 16-bit processor, it may be most convenient to write Forth programs on a PC and then transfer the finished code to the target system. While the development system may use the full facilities of DOS or Windows, there is no need for the finished product to contain more than a small run-time package and the program itself. Even the dictionary is only needed when compiling and debugging the program. It can be omitted from the final code.

Because Forth programs tend to compile to about 10 bytes per line, a 2000-line program plus a 4-KB run-time file easily fits in a 32-KB PROM. If the target system supports a serial port and executes from RAM, I prefer to compile and debug on the target. Even though this means finding memory space for the dictionary and the compiler, it helps immensely in testing the hardware. I¡¯ve resolved many hardware bugs by programming a short Forth loop to wiggle a bit so I could follow a signal through the suspect area with an oscilloscope.

A commercial Forth comes with an initial dictionary that contains the Forth primitives and the words needed to implement the compiler. Many Forths have a built-in source editor, but you can use any editor you find convenient. You will probably be supplied with libraries of the operating system calls needed to develop Forth programs under OSs like Windows. For an embedded application using a single-card PC, a DOS function library is useful.

You can also get a library of Forth extensions, which you can load if your program requires them. For example, simple Forths process only integers, so floating-point arithmetic is optional. It¡¯s easy to write your own libraries. I¡¯ve been using JForth from Delta Research to write programs which analyze filters. JForth supports windows, pull-down menus, input gadgets, and slider control of parameters. However, it can¡¯t handle complex numbers. I wrote my own floating-point complex arithmetic library in 20 minutes.

In a team writing programs to control a series of related instruments, one member should be assigned to write a library of hardware-interface functions to do things like access the front-panel controls and displays in a consistent manner. Because Forth lets programmers develop idiosyncratic ways of solving problems, you must have good documentation and close coordination between team members on a large project. Companies using Forth should maintain in-house standards for Forth extensions that relate to their products and teach these to all their programmers.

What Can You Do With Forth?

The simple answer: anything. Other computer languages limit you to the set of operations the compiler authors thought you¡¯d need. Because Forth is naturally extensible, you can do what you need. If all else fails, you can easily drop into machine code and create any data structure you need.

JForth even implements C structs, which it uses to interact with the host operating system. I once needed a structure which was written to 30 named, one-dimensional arrays. It was read as a single, named, two-dimensional array. I¡¯m told this is feasible in C, but I¡¯ve never met anyone who wanted to try it.

Forth Standards

Since its invention by Charles Moore about 1970, many Forth standards and dialects have emerged. Forth encourages innovation so there has always been a tendency for Forth vendors to customize and improve it, even when ostensibly adhering to a standard. I do most of my professional Forth programming in 1979 vintage FIG-Forth on the grounds that it is already so obsolete that it won¡¯t change. Since then, there was Forth-79 and Forth-83 and now there¡¯s an ANSI standard for Forth (X3.215/1994).

How does Forth Compare with C?

Both Forth and C let you think at a higher level and spare you the slower development process of assembler. Forth¡¯s logical compilation order eliminates the need for C prototypes within a file.

All the standard program controls of C (do, if, else, while, switch, etc.) are in Forth, often with the same names. All the important logical and mathematical operators are there, too. Conditional compilation, arrays, and unions are all supported with a Forth flair. Constants replace #defines, and Forth¡¯s direct stack manipulation eliminates most need for auto variables. Forth¡¯s use of vocabularies and its ability to FORGET definitions are more powerful than C¡¯s weak scope operators. You can support your own data types with even less pain than in C++.

Forth assumes you know what you are doing. It protects you from typographical errors and incomplete structures, but the manual has only one page of compiler error codes, not a whole chapter. As someone once said, Forth can¡¯t flag syntax errors since it doesn¡¯t know what syntax you decided to use.

In C, you¡¯re more protected. But then, you also have to do things like type casting to circumvent a patronizing compiler constantly checking up on you.

Forth has these advantages over C:

  1. The development environment is much simpler. You don¡¯t need to install a whole development suite since Forth is its own development system and, in an embedded application, its own operating system. It offers an OS, source editor, and all the debug utilities you need, and they fit on one 360-KB floppy.
    As a result, you¡¯re working with a single tool set and a single user interface. Compare this with having a compiler, OS, debugger, and maybe a target monitor program, all coming from different vendors and not designed to work together.
  2. When you buy Forth, you often get the source code for the whole development environment. Try telling Borland or Microsoft that you want to make a backward compatible variant of C to do stronger type checking, fuzzy logic, or different floating-point implementation.
  3. It¡¯s often possible to develop a Forth program on the target system itself. In my present C contract, I use a Sun workstation to run Make and to compile and link a target executable. Then, on a target machine, I download the code before powering it up and testing it. If I want to make an adjustment, it takes an hour to go through the whole loop again.
    With Forth, I could type a new word right into the serial port of the target, push parameters onto the stack, then call it to see if it worked. I could easily "splice" the new word to intercept any calls to the old word.
  4. The extensibility of the compiler lets you follow any coding fad that comes along without switching languages. Forth has been object oriented, "sandbox supporting," and platform independent from the get go. Added data structures or operator overloading that would choke C++ would not be a problem in Forth.
  5. You can drop into assembly much easier than in C, and all data structures are accessible from assembler.