FORTH LANGUAGE Charles Moore created Forth in the 1960s and 1970s to - - PDF document

forth language
SMART_READER_LITE
LIVE PREVIEW

FORTH LANGUAGE Charles Moore created Forth in the 1960s and 1970s to - - PDF document

ARINDAM ADHIKARY, PID: 904567450 , ROLL NO: MIT/01/02 aadhikar@vt.edu, aadhikary.mit1@spjimr.ernet.in MOHMMAD BIN QASIM FAISAL, PID: 904585976 ROLL NO: MIT/01/33 mfaisal@vt.edu, faisal.mit1@spjimr.ernet.in FORTH LANGUAGE Charles Moore created


slide-1
SLIDE 1

ARINDAM ADHIKARY, PID: 904567450 , ROLL NO: MIT/01/02 aadhikar@vt.edu, aadhikary.mit1@spjimr.ernet.in MOHMMAD BIN QASIM FAISAL, PID: 904585976 ROLL NO: MIT/01/33 mfaisal@vt.edu, faisal.mit1@spjimr.ernet.in

FORTH LANGUAGE

Charles Moore created Forth in the 1960s and 1970s to give computers real-time control

  • ver astronomical equipment. . The object in developing this new "programming tool"

was to overcome the need for an engineer to learn a large number of methods for controlling compilers (linkers, assemblers and directives plus high-level languages). A number of Forth's features (such as its interactive style) make it a useful language for AI programming, and devoted adherents have developed Forth-based expert systems and neural networks. The first program to be called Forth was written in about 1970. The first complete implementation was used in 1971 on a DEC PDP-11 for the National Radio Astronomy Observatory's 11-meter radio telescope in Arizona. This system was responsible for pointing and tracking the telescope, collecting data and recording it on magnetic tape, supporting an interactive graphics terminal on which an astronomer could analyze previously recorded data. The system was so useful that astronomers from all

  • ver the world began asking for copies. The success of this application enabled Moore

and Elizabeth ("Bess") Rather in 1973 to form "FORTH, Inc.", to explore commercial uses of the language. A version was developed, in 1977, for the newly introduced 8 Bit microprocessors called "micro FORTH". Their “MiniFORTH” product for minicomputers complemented this.

Dictionary:

The dictionary contains all the executable routines (or words) that make up a Forth

  • system. System routines are entries predefined in the dictionary that become available

when the system is booted. The basic form of the most common type of word definition is: <name> <words to be executed> ; The dictionary is a linked list of variable-length entries, each of which is a Forth word and its definition. In most implementations, the dictionary grows toward high memory; the discussion in this section will assume it does. Each dictionary entry points to the entry that logically precedes it The address of the next available cell at the end of the dictionary is put on the stack by the word HERE.

slide-2
SLIDE 2

The ANS Forth term for one of these chains is word list. A word list is a subset of the dictionary containing words for some special purpose. There usually are several word lists present in a system and these are normally available to all users on a re-entrant basis. The essential structure of dictionary entries is the same for all words, and is diagrammed in the Figure . The link cell contains the location of the preceding entry. This speeds up searches, which start at the recent end of the dictionary and work backwards to the older end. Some systems are case sensitive and others are not; see your product documentation for

  • details. To avoid problems and to maximize the transportability of code, the names of the

words provided in a standard system are defined in all upper-case letters and should always be referred to in all upper-case letters when using them in subsequent definitions.

slide-3
SLIDE 3

Although the order of the fields in a dictionary entry is arranged in each implementation to optimize each machine's dictionary search, Figure 3 shows a general model. In addition, usually there are several control bits to control the type and use of the

  • definition. Since the longest name field in most systems has 31 characters, requiring only

five bits to express a count, the control bits are often found in the byte containing the

  • count. The most important control bit is called the precedence bit. A word whose

precedence bit is set executes at compile time. The precedence bit is set by the word

  • IMMEDIATE. The precedence bit is used for a few special words, such as compiler

directives, but it is zero for most words. The cells (if any) after the code field address are called the parameter field, which is of variable length. CONSTANTs and VARIABLEs keep their data in the first cell of the parameter field. Other definitions may keep several values. Glossary HERE ( -- addr ) Push the address of the next available dictionary location onto the stack. ALLOT ( n -- ) Increment the dictionary address pointer by n number of bytes. References CODE definitions, Section 4.1 Code field addresses, Section 2.7.4 Creating dictionary entries, Section 2.7.1 Word lists, Section 3.6 Control Structure: Data Stack: Every Forth system contains at least one data stack. In a multitasked system, each task may have its own data stack. The stack is a cell-wide, push-down LIFO (last-in, first-out) list; its purpose is to contain numeric operands for Forth commands. Commands commonly expect their input parameters on this stack and leave their output results there. The stack's size is indefinite. This command line first puts the numbers 25 and 10 on the implied stack; the "*" command multiplies the two numbers on the top of the stack and replaces them with their product; then the number 50 is placed on the stack, and the "+" command adds it to the previous product; finally, the "." command prints the result to the user's terminal. Even the language's structural features are stack-based. For example: : FLOOR5 ( n -- n' ) DUP 5 < IF DROP 5 ELSE 1 – THEN; This code defines a new word (again, 'word' is the term used for a subroutine) called "FLOOR5" using the following commands: "DUP" simply duplicates the number on the stack; "<" compares the two numbers on the stack and replaces them with a true-or-false value; "IF" takes a true-or-false value and chooses to execute commands immediately after it or to skip to the "ELSE"; "DROP" discards the value on the stack; and "THEN"

slide-4
SLIDE 4

ends the conditional. The text in parentheses is a comment, advising that this word expects a number on the stack and will return a possibly changed number. The net result is a function that performs similarly to this function (written in the C programming language): int floor5(int v) { if (v < 5) return 5; else return v - 1; A terser Forth definition of FLOOR5 that gives the same result: : FLOOR5 ( n -- n' ) 1 - 5 MAX ; The standard Forth dictionary provides words for simple manipulation of single- and double-length operands on the stack: SWAP, DUP, DROP, 2SWAP, etc. Structure of the Compiler: The compiler itself consists of Forth words. This gives the programmer considerable control of the compiler, and a programmer can change the compiler's words for special purposes.The "compile time" flag in the name field is set for words with "compile time"

  • behavior. Most simple words execute the same code whether they are typed on a

command line, or embedded in code. When compiling these, the compiler simply places code or a threaded pointer to the word. Compile-time words are actually executed by the

  • compiler. The classic examples of compile time words are the control-structures such as

IF and WHILE. All of Forth's control structures, and almost all of its compiler are implemented as compile-time words. The assembler (see above) is a special dialect of the compiler. Structure of Code: In most Forth systems, the body of a code definition consists of either machine language,

  • r some form of threaded code. Traditionally, indirect-threaded code was used, but

direct-threaded and subroutine threaded Forths have also been popular. The fastest modern Forths use subroutine threading, insert simple words as macros, and perform peephole optimization or other optimizing strategies to make the code smaller and faster. Data Objects:

slide-5
SLIDE 5

When a word is a variable or other data object, the CF points to the runtime code associated with the defining word that created it. A defining word has a characteristic "defining behavior" (creating a dictionary entry plus possibly allocating and initializing data space) and also specifies the behavior of an instance of the class of words constructed by this defining word. Examples include:

  • VARIABLE -- Names an uninitialized, one-cell memory location. Instance

behavior of a VARIABLE returns its address on the stack.

  • CONSTANT -- Names a value (specified as an argument to CONSTANT).

Instance behavior returns the value.

  • CREATE -- Names a location; space may be allocated at this location, or it can

be set to contain a string or other initialized value. Instance behavior returns the address of the beginning of this space. Forth also provides a facility by which a programmer can define new application-specific defining words, specifying both a custom defining behavior and instance behavior. Some examples include circular buffers, named bits on an I/O port, and automatically-indexed arrays. Features of Forth language: Compiler: Almost anything that can be typed, can also be compiled and executed automatically. The compiler uses semantics almost identical to the interpreter, and is also very simple. Operating System: Classic Forth systems traditionally used no operating system and no dedicated file

  • system. Instead of storing code in files, source-code was stored in disk blocks written to

physical disk addresses. Forth systems use a single word "BLOCK" to translate the number of a 1K-sized block of disk space into the address of a buffer containing the data. The buffers are managed automatically by the Forth system. Assembler: Most Forth systems include a specialized assembler that produces executable words. Forth assemblers often use a reverse-polish syntax in which the parameters of an instruction precede the instruction. Registers may be referenced by the name used by the manufacturer, numbered (0..n, as used in the actual operation code) or named for their purpose in the Forth system: e.g. "S" for the register used as a stack pointer. Multitasking: Some Forth systems feature a multitasker. They use a special word, "PAUSE" to save the current tasks execution context, locate the next task, and restore its execution context. Interpreter:

slide-6
SLIDE 6

At the screen prompt, the user interacts directly with the Forth system, typing sequences

  • f words which are then read and executed by the Forth system. This is the central value
  • f the Forth system: Forth is a very simple way to translate text into computer behavior.

A flow diagram for the interpreting process is shown in Figure : The interpreter uses spaces (some systems accept other whitespace characters, also) to separate "words." When it finds a word, it tries to look the word up in the dictionary and

slide-7
SLIDE 7

execute the word's code. If that fails, it then tries to convert it into a number and push it

  • nto the stack. If that fails, then it prints the word, followed by an error message, and

awaits further user input. File System: Some classic commercial Forth systems have implemented contiguous disk files, using the Forth operating system's disk access, and placing the files at fixed disk block ranges. Usually the system implements records as fixed-length binary data, with an integer number of records per disk block. Quick searching is achieved by hashed access on key data. Self and cross compilation: A full-featured Forth system with all source code will compile itself. The usual method is to redefine the handful of words that place compiled bits into memory. The compiler's words therefore use specially-named versions of fetch and store that can be redirected to fetch and store to a buffer area in memory. The buffer area simulates or accesses a memory area beginning at a different address than the code buffer. Such compilers define words to access both the target computer's memory, and the host (compiling) computer's memory. Sub Programs: Words written in Forth are compiled into an executable form. The classical "indirect threaded" implementations compile lists of addresses of words to be executed in turn; many modern systems generate actual machine code (including calls to some external words and code for others expanded in place). Some systems feature sophisticated

  • ptimizing compilers. Generally speaking, a Forth program is saved as the memory

image of the compiled program with a single command (e.g., RUN) that is executed when the compiled version is loaded.The following block contains the Block of the Fourth language. It represents a portion of an application that controls a bank of eight LEDs used as indicator lamps on an instrument. It also indicates how Forth definitions can be combine to form an application environment.

slide-8
SLIDE 8

Notice the comment on the first line (line 0) indicating the nature of the code given in the rest of the block. This is usually called the index line. Most Forth systems allow one to look at the first line of each block to discover what blocks are required for a particular application. The LEDs are interfaced through a single 8 Bit port mapped to the address 40H. This location is defined as a CONSTANT on Line 1, so that it may be referred to by name; should the address change, one need only adjust the value of this constant. The word LIGHTS returns this address on the stack. The definition LIGHT takes a value on the stack and sends it to the device. The nature of this value is a bit mask, whose bits correspond directly to the individual lights. Thus, the command: 255 LIGHTS Will turn on all lights, while: 0 LIGHTS Will turn them all off. If the hardware changes, such that this is no longer the case, one

  • nly needs to change the definition of LIGHTS to suit the new hardware, while the rest
  • f the application code need not be changed.

Lines 4-6 contain a simple diagnostic of the sort one might type in from the terminal to confirm that everything is working. The variable DELAY contains a delay time in milliseconds; execution of the word DELAY returns the address of this variable. Two values of DELAY are set by the definitions SLOW and FAST, using the Forth operator! (Pronounced "store") which takes a value and an address, storing the value in the address.

slide-9
SLIDE 9

The definition COUNTS runs a loop from 0 though 255 (the loop is started by the word DO and ended by the word LOOP). The word I places the current loop index on the stack, this is then sent to the lights. The system will then wait for the period specified by

  • DELAY. The word @ (pronounced "fetch") fetches a value from an address, in this case

the address supplied by DELAY. This value is passed to MS, which waits the specified number of milliseconds. The result of executing COUNTS is that the light will count from 0 to 255 at the desired rate. To run this one would type: SLOW COUNTS or FAST COUNTS Lines 8-10 provide the capability of "naming" individual lamps. In this application they are being used as indicator lights. The word LAMP is a defining word, which takes a bit mask (representing a particular lamp) as an argument, and compiles it as a named entity. The word CREATE compiles a header into the dictionary, while the word, (comma) places the mask into the body of the definition. The word DOES> places the address of the following code into the cfa of the new word. Therefore when the new word is executed its action is to fetch the contents of the first item in the body of the new word. Lines 9 and 10 contain five uses of LAMP to name particular indicators. When one of these words, such as POWER, is executed, the mask is returned on the stack. In fact, this behavior is identical to the behavior of a Forth CONSTANT. The LAMP definition is included here for example. The ability to define such a "defining word" with the use of CREATES and DOES> is one of the most powerful features of the language, allowing

  • ne to define "intelligent" application based data structures.

Finally, on lines 13-15, we have the words that will control the light panel. LAMPS are a variable that contains the current state of the lamps. The word LAMP-ON takes a mask (supplied by one of the LAMP words) and turns the lamp on. It changes the state of that particular lamp, saving the result in LAMPS. LAMP-OFF will turn the given lamp off, also changing the LAMPS state. In the remainder of the application, the lamp names and LAMP-ON, LAMP-OFF are probably the only words that will be executed directly. The usage will be for example: POWER LAMP-ON or SAMPLING LAMP-OFF As appropriate, whenever the system indicators need to be reset. The time to compile this block of code on the system was about half a second, including time to fetch it from disk. So it is quite practical (and normal practice) for a programmer to simply type in a definition and try it immediately. In addition, one always has the capability of communicating with external devices directly. The first thing one would do when told about the lamps would be to type: HEX FF 40 OUTPUT And see if all the lamps come on. If not, the presumption is that something is amiss, since this phrase directly transmits the "all ones" mask to the device. This type of direct interaction is useful in application involving custom hardware, as it reduces hardware- debugging time.

slide-10
SLIDE 10

References:

  • International Standards Organization (1997).

Information technology - Programming languages - Forth (First ed.), ISO/IEC 15145:1997

  • American National Standards Institute (1994).

American National Standard for information systems: programming languages:

  • Forth. ANSI/X3.215-1994.
  • IEEE 1275 Technical Committee (1994).

IEEE Standard 1275-1994 - Standard for Boot (Initialization Configuration) Firmware: Core Requirements and Practices. IEEE.

  • C. L. Stephens and R. M. Rodriguez (1986).

PolyForth: An electronics engineer's programming tool. Software Engineering Journal, 1:154-158.

  • Leo Brodie (1984).

Thinking Forth. Prentice Hall International.

  • Leo Brodie (1982).

Starting Forth. Prentice Hall International, second edition.

  • Charles Moore (1980).

The evolution of Forth. Byte, 5(8): 76-84.

  • Charles Moore (1974).

Forth: A new way to program a mini-computer. Astronomy & Astrophysics. Supplement, 15:497-511.

  • Jan Lukasiewicz (1963).

Elements of Mathematical Logic, volume 31 of International Series of Monographs in Pure and Applied Mathematics. Pergamon Press Ltd., Headington Hill Hall, Oxford, England, second edition, 1963. First published in 1929.