C H A P T E R 4 |
Using Forth Tools |
This chapter introduces the Forth programming language as it is implemented in OpenBoot. Even if you are familiar with Forth, work through the examples shown in this chapter; they provide specific, OpenBoot-related information.
The version of Forth contained in OpenBoot is based on ANS Forth. Appendix I ," lists the complete set of available commands.
Forth has a very simple command structure. Forth commands, also called Forth words , consist of any combination of characters that can be printed. For example, letters, digits, or punctuation marks. Examples of legitimate words are shown below:
Forth words must be separated from one another by one or more spaces (blanks). Characters that are normally treated as "punctuation" in some other programming languages do not separate Forth words. In fact, many of those "punctuation" characters are Forth words!
Pressing Return at the end of any command line executes the typed commands. (In all the examples shown, a Return at the end of the line is assumed.)
A command line can have more than one word. Multiple words on a line are executed one at a time, from left to right, in the order in which they were typed. For example:
In OpenBoot, uppercase and lowercase letters are equivalent in Forth word names. Therefore, testa , TESTA , and TesTa all invoke the same command. However, words are conventionally written in lowercase.
Some commands generate large amounts of output (for example, dump or words ). You can interrupt such a command by pressing any key except q . (If you press q , the output is aborted, not suspended.) Once a command is interrupted, output is suspended and the following message appears:
Press the space bar ( <space> ) to continue, press Return ( <cr> ) to output one more line and pause again, or type q to abort the command. When a command generates more than one page of output, the system automatically displays this prompt at the end of each page.
The terms shown in describe the data types used by Forth.
Enter a number by typing its value, for example, 55 or -123. Forth accepts only integers (whole numbers); it does not understand fractional values (e.g., 2/3). A period at the end of a number signifies a double number. Periods or commas embedded in a number are ignored, so 5.77 is understood as 577. By convention, such punctuation usually appears every four digits. Use one or more spaces to separate a number from a word or from another number.
Unless otherwise specified, OpenBoot performs integer arithmetic on data items that are one cell in size, and creates results that are one cell in size.
Although OpenBoot implementations are encouraged to use base 16 (hexadecimal) by default, they are not required to do so. Consequently, you must establish a specific number base if your code depends on a given base for proper operation. You can change the number base with the commands decimal and hex to cause all subsequent numeric input and output to be performed in base 10 or 16, respectively.
For example, to operate in decimal, type:
To change to hexadecimal, type:
To identify the current number base, you can use:
The 16 on the display shows that you are operating in hexadecimal. If 10 showed on the display, it would mean that you are in decimal base. The .d command displays a number in base 10, regardless of the current number base.
The Forth stack is a last-in, first-out buffer used for temporarily holding numeric information. Think of it as a stack of books: the last one you put on the top of the stack is the first one you take off. Understanding the stack is essential to using Forth .
To put a number on the stack, simply type its value.
The contents of the stack are normally invisible. However, properly visualizing the current stack contents is important for achieving the desired result. To show the stack contents with every ok prompt, type:
The topmost stack item is always shown as the last item in the list, immediately before the ok prompt. In the above example, the topmost stack item is 8 .
If showstack has been previously executed, noshowstack will remove the stack display prior to each prompt.
Nearly all words that require numeric parameters fetch those parameters from the top of the stack. Any values returned are generally left on top of the stack, where they can be viewed or consumed by another command. For example, the Forth word + removes two numbers from the stack, adds them together, and leaves the result on the stack. In the example below, all arithmetic is in hexadecimal.
Once the two values are added together, the result is put onto the top of the stack. The Forth word . removes the top stack item and displays that value on the screen. For example:
To aid understanding, conventional coding style requires that a stack diagram of the form ( -- ) appear on the first line of every definition of a Forth word. The stack diagram specifies what the execution of the word does to the stack.
Entries to the left of -- represent those stack items that the word removes from the stack and uses during its operation. The right-most of these items is on top of the stack, with any preceding items beneath it. In other words, arguments are pushed onto the stack in left to right order, leaving the most recent one (the right-most one in the diagram) on the top.
Entries to the right of -- represent those stack items that the word leaves on the stack after it finishes execution. Again, the right-most item is on top of the stack, with any preceding items beneath it.
For example, a stack diagram for the word + is:
Therefore, + removes two numbers ( nu1 and nu2 ) from the stack and leaves their sum ( sum ) on the stack. As a second example, a stack diagram for the word . is:
The word . removes the number on the top of the stack (nu) and displays it.
Words that have no effect on the contents of the stack (such as showstack or decimal ), have a ( -- ) stack diagram.
Occasionally, a word will require another word or other text immediately following it on the command line. The word see , used in the form:
Stack items are generally written using descriptive names to help clarify correct usage. See for stack item abbreviations used in this manual.
Stack manipulation commands (described in ) allow you to add, delete, and reorder items on the stack.
clear | ||
depth | ||
drop | ||
2drop | ||
3drop | ||
dup | ||
2dup | ||
3dup | ||
?dup | ||
nip | ||
over | ||
2over | ||
pick | ||
>r | ||
r> | ||
r@ | ||
roll | ||
rot | ||
-rot | ||
2rot | ||
swap | ||
2swap | ||
tuck |
A typical use of stack manipulation might be to display the top stack item while preserving all stack items, as shown in this example:
Forth provides an easy way to create new command words from sequences of existing words. shows the Forth words used to create such new words.
This kind of word is called a colon definition , named after the word that is used to create them. For example, suppose you want to create a new word, add4 , that will add any four numbers together and display the result. You could create the definition as follows:
The ; (semicolon) marks the end of the definition that defines add4 to have the behavior ( + + + . ). The three addition operators ( + ) reduce the four stack items to a single sum on the stack; then . removes and displays that result. An example follows.
Definitions are forgotten if a machine reset takes place. To keep useful definitions, put them into the script or save them in a text file on a host system. This text file can then be loaded as needed. (See Chapter 5 for more information on loading files.)
When you type a definition from the User Interface, the ok prompt becomes a ] (right square bracket) prompt after you type the : (colon) and before you type the ; (semicolon). For example, you could type the definition for add4 like this:
The above use of ] while inside a multi-line definition is a characteristic of Sun's implementation.
The stack diagram shows proper use of a word, so include a stack diagram with every definition you create, even if the stack effect is nil ( -- ) . Use generous stack comments in complex definitions to trace the flow of execution. For example, when creating add4 , you could define it as:
Or you could define it as follows:
Note - The "(" is a Forth word meaning ignore the text up to the ")". Like any other Forth word, the "(" must have one or more spaces after it. |
The commands listed in perform single-precision arithmetic.
The commands listed in perform double number arithmetic.
d+ | ||
d- | ||
fm/mod | ||
m* | ||
s>d | ||
sm/rem | ||
um* | ||
um/mod |
The commands listed in perform data type conversion.
bljoin | ||
bwjoin | ||
lbflip | ||
lbsplit | ||
lwflip | ||
lwsplit | ||
wbflip | ||
wbsplit | ||
wljoin |
The data type conversion commands listed in are available only on 64-bit OpenBoot implementations.
The commands listed in perform address arithmetic.
aligned |
Increase n1 if necessary to yield a variable aligned address. |
|
/c | ||
/c* | ||
ca+ | ||
ca1+ | ||
cell+ | ||
cells | ||
char+ | ||
chars | ||
/l | ||
/l* | ||
la+ | ||
la1+ | ||
/n | ||
/n* | ||
na+ | ||
na1+ | ||
/w | ||
/w* | ||
wa+ | ||
wa1+ |
The address arithmetic commands listed in are available only on 64-bit OpenBoot implementations.
/x | ||
/x* | ||
xa+ | ||
xa1+ |
The User Interface provides interactive commands for examining and setting memory. With it, you can:
Memory operators let you read from and write to any memory location. All memory addresses shown in the examples that follow are virtual addresses.
A variety of 8-bit, 16-bit, and 32-bit (and in some systems, 64-bit) operations are provided. In general, a c (character) prefix indicates an 8-bit (one byte) operation; a w (word) prefix indicates a 16-bit (doublet) operation; an l (longword) prefix indicates a 32-bit (quadlet) operation; and an x prefix indicates a 64-bit (octlet) operation.
waddr , qaddr , and oaddr indicate addresses with alignment restrictions. For example, qaddr indicates 32-bit (4 byte) alignment; on many systems such an address must be a multiple of 4, as shown in the following example:
Forth, as implemented in OpenBoot, adheres closely to the ANS Forth Standard. If you explicitly want a 16-bit fetch, a 32-bit fetch or (on some systems) a 64-bit fetch, use w@ , l@ or x@ instead of @ . Other memory and device register access commands also follow this convention.
lists commands used to access memory.
The memory access commands listed in are available only on 64-bit OpenBoot implementations.
The dump command is particularly useful. It displays a region of memory as both bytes and ASCII values. The example below displays the contents of 20 bytes of memory starting at virtual address 10000.
Some implementations support variants of dump that display memory as 16-, 32- and 64-bit values. You can use sifting dump (see Searching the Dictionary ) to find out if your system has such variants.
If you try to access an invalid memory location (with @ , for example), the operation may abort and display an error message, such as Data Access Exception or Bus Error .
lists memory mapping commands.
alloc-mem | ||
free-mem |
The following screen is an example of the use of alloc-mem and free-mem .
alloc-mem allocates 4000 bytes of memory, and the starting address ( ef7a48 ) of the reserved area is displayed.
Device registers cannot be reliably accessed using the virtual memory access operators discussed in the last section. There are special operators for accessing device registers, and these operators require that the machine be properly set up prior to their use. For a detailed explanation of this topic, see Writing FCode 3.x Programs .
The dictionary contains all the available Forth words. Forth defining words create new Forth words.
Defining words require two stack diagrams. The first diagram shows the stack effect when the new word is created. The second (or "Execution:") diagram shows the stack effect when that word is later executed.
lists the defining words that you can use to create new Forth words.
If a Forth command is created with the same name as an existing command, the new command will be created normally. Depending on the implementation, a warning message "new-name isn't unique" may be displayed. Previous uses of that command name will be unaffected. Subsequent uses of that command name will use the latest definition of that command name. (To correct the original definition such that all uses of the command name get the corrected behavior, make the correction with patch . (See Using patch and (patch) )
value lets you create a name for a numerical value that can be changed. Later execution of that name leaves the assigned value on the stack. The following example creates a word named foo with an initial value of 22, and then calls foo to use its value in an arithmetic operation.
The value can be changed with the word to . For example:
Words created with value are convenient, because you do not have to use @ to retrieve their values.
The defining word variable creates a name with an associated one-cell memory location. Later execution of that name leaves the address of the memory on the stack. @ and ! are used to read or write to that address. For example:
The defining word defer creates a word whose behavior can be changed later, by creating a slot which can be loaded with different behaviors at different times. For example:
The dictionary contains all the available Forth words. lists some useful tools you can use to search the dictionary. Please note that some of these tools work only with methods or commands while others work with all types of words (including, for example, variables and values).
Before you can understand the operation of the dictionary searching tools, you need to understand how words become visible . If there is an active package at the time a word is defined, the new word becomes a method of the active package, and is visible only when that package is the active package. The commands dev and find-device can be used to select or change the active package. The command device-end deselects the currently active package leaving no active package.
If there is no active package at the time a word is defined, the word is globally visible (i.e. not specific to a particular package and always available).
The dictionary searching commands first search through the words of the active package, if there is one, and then through the globally visible words.
Note - The Forth commands only and also will affect which words are visible. |
.calls can be used to locate all of the Forth commands that use a specified word in their definition. .calls takes an execution token from the stack and searches the entire dictionary to produce a listing of the names and addresses of every Forth command which uses that execution token. For example:
displays a "pretty-printed" listing of the source for thisword (without the comments, of course). For example:
For more details on the use of see , refer to Using the Forth Language Decompiler .
sifting takes a string from the input stream and searches vocabularies in the dictionary search order to find every command name that contains the specified string as shown in the following screen.
words displays all the visible word names in the dictionary, starting with the most recent definition. If a node is currently selected (as with dev ), the list produced by words is limited to the words in that selected node.
The commands listed in control the compilation of data into the dictionary.
The dictionary compilation commands listed in are available only on 64-bit OpenBoot implementations.
x, |
Compile an octlet, o , into the dictionary (doublet-aligned). |
Basic commands to display stack values are shown in .
. | ||
.r | ||
.s | ||
showstack | ||
noshowstack |
Turn off automatic display of the stack before each ok prompt |
|
u. | ||
u.r |
The .s command displays the entire stack contents without disturbing them. It can usually be used safely for debugging purposes. (This is the function that showstack performs automatically.)
You can print numbers in a specific number base or change the operating number base using the commands in .
.d | ||
.h | ||
base | ||
decimal | ||
d# number | ||
hex | ||
h# number |
The d# and h# commands are useful when you want to input a number in a specific base without explicitly changing the current base. For example:
The .d and .h commands act like " . " but display the value in decimal or hexadecimal, respectively, regardless of the current base setting. For example:
This section describes text and character input and output commands.
lists commands to control text input.
Comments are used with Forth source code (generally in a text file) to describe the function of the code. The ( (open parenthesis) is the Forth word that begins a comment. Any character up to the closing parenthesis ) is ignored by the Forth interpreter. Stack diagrams are one example of comments using ( .
Note - Remember to follow the( with a space, so that it is recognized as a Forth word. |
\ (backslash) indicates a comment terminated by the end of the line of text.
key waits for a key to be pressed, then returns the ASCII value of that key on the stack.
ascii , used in the form ascii x , returns on the stack the numerical code of the character x .
key? looks at the keyboard to see whether the user has recently typed any key. It returns a flag on the stack: true if a key has been pressed and false otherwise. See Conditional Flags for a discussion on the use of flags.
lists general-purpose text display commands.
cr sends a carriage-return/linefeed sequence to the console output device. For example:
emit displays the letter whose ASCII value is on the stack.
shows commands used to manipulate text strings.
Some string commands specify an address (the location in memory where the characters reside) and a length (the number of characters in the string). Other commands use a packed string or pstr , which is a location in memory containing a byte for the length, immediately followed by the characters. The stack diagram for the command indicates which form is used. For example, count converts a packed string to an address-length string.
The command ." is used in the form: ." string " . It outputs text immediately when it is encountered by the interpreter. A " (double quotation mark) marks the end of the text string. For example:
When " is used outside a colon definition, only two interpreted strings of up to 80 characters each can be assembled concurrently. This limitation does not apply in colon definitions.
Normally, OpenBoot uses a keyboard for command input, and a frame buffer with a connected display screen for display output. (Server systems may use an ASCII terminal connected to a serial port. For more information on how to connect a terminal to your system, see your system's installation manual.) You can redirect the input, the output, or both, to a serial port. This may be useful, for example, when debugging a frame buffer.
lists commands you can use to redirect input and output.
The commands input and output temporarily change the current devices for input and output. The change takes place as soon as you enter a command; you do not have to reset your system. A system reset or power cycle causes the input and output devices to revert to the default settings specified in the NVRAM configuration variables input-device and output-device . These variables can be modified, if needed (see Chapter 3 ).
input must be preceded by one of the following: keyboard , ttya , ttyb , or device-specifier text string. For example, if input is currently accepted from the keyboard, and you want to make a change so that input is accepted from a terminal connected to the serial port ttya , type:
At this point, the keyboard becomes nonfunctional (except perhaps for Stop-A ), but any text entered from the terminal connected to ttya is processed as input. All commands are executed as usual.
To resume using the keyboard as the input device, use the terminal keyboard to type:
Similarly, output must be preceded by one of the following: screen , ttya , or ttyb or device-specifier . For example, if you want to send output to a serial port instead of the normal display screen, type:
The screen does not show the answering ok prompt, but the terminal connected to the serial port shows the ok prompt and all further output as well.
io is used in the same way, except that it changes both the input and output to the specified place. For example:
Generally, the argument to input , output , and io is a device-specifier , which can be either a device path name or a device alias. The device must be specified as a Forth string, using double quotation marks ( " ), as shown in the two examples below:
In the preceding examples, keyboard , screen , ttya , and ttyb are predefined Forth words that put their corresponding device alias string on the stack.
OpenBoot implements a command line editor (similar to EMACS, a common text editor), some optional extensions and an optional history mechanism for the User Interface. You use these tools to re-execute previous commands without retyping them, to edit the current command line to fix typing errors, or to recall and change previous commands.
lists line-editing commands available at the ok prompt.
The command line history extension saves previously-typed commands in an EMACS-like command history ring that contains at least 8 entries. Commands may be recalled by moving either forward or backward around the ring. Once recalled, a command may be edited and/or resubmitted (by typing the Return key). The command line history extension keys are:
Selects and displays the previous command in the command history ring. |
|
Selects and displays the next command in the command history ring. |
|
The command completion extension enables the system to complete long Forth word names by searching the dictionary for one or more matches based on the already-typed portion of a word. When you type a portion of a word followed by the command completion keystroke, Control-Space, the system behaves as follows:
If the system finds exactly one matching word, the remainder of the word is automatically displayed.
If the system finds several possible matches, the system displays all of the characters that are common to all of the possibilities.
If the system cannot find a match for the already-typed characters, the system deletes characters from the right until there is at least one match for the remaining characters.
The command completion extension keys are:
Forth conditionals use flags to indicate true/false values. A flag can be generated in several ways, based on testing criteria. The flag can then be displayed from the stack with the word ".", or it can be used as input to a conditional control command. Control commands can cause one behavior if a flag is true and another behavior if the flag is false. Thus, execution can be altered based on the result of a test.
A 0 value indicates that the flag value is false . A -1 or any other nonzero number indicates that the flag value is true .
lists commands that perform relational tests, and leave a true or false flag result on the stack.
< | ||
<= | ||
<> | ||
= | ||
> | ||
>= | ||
0< | ||
0<= | ||
0<> | ||
0= | ||
0> | ||
0>= | ||
between | ||
false | ||
true | ||
u< | ||
u<= | ||
u> | ||
u>= | ||
within |
> takes two numbers from the stack, and returns true ( -1 ) on the stack if the first number was greater than the second number, or returns false ( 0 ) otherwise. An example follows:
0= takes one item from the stack, and returns true if that item was 0 or returns false otherwise. This word inverts any flag to its opposite value.
The following sections describe words used in a Forth program to control the flow of execution.
The commands if , else and then provide a simple control structure.
The commands listed in control the flow of conditional execution.
The format for using these commands is:
The if command consumes a flag from the stack. If the flag is true (nonzero), the commands following the if are performed. Otherwise, the commands (if any) following the else are performed.
Note - The ] prompt reminds you that you are part way through creating a new colon definition. It reverts to ok after you finish the definition with a semicolon. |
A high-level
case
command is provided for selecting alternatives with multiple possibilities. This command is easier to read than deeply-nested
if...then
commands.
lists the conditional case commands.
case | ||
endcase | ||
endof | ||
of |
Here is a simple example of a case command:
A begin loop executes the same commands repeatedly until a certain condition is satisfied. Such a loop is also called a conditional loop.
lists commands to control the execution of conditional loops.
In both cases, the commands in the loop are executed repeatedly until the proper flag value causes the loop to be terminated. Then execution continues normally with the command following the closing command word ( until or repeat ).
In the begin...until case, until removes a flag from the top of the stack and inspects it. If the flag is false , execution continues just after the begin , and the loop repeats. If the flag is true , the loop is exited.
In the begin...while...repeat case, while removes a flag from the top of the stack and inspects it. If the flag is true , the loop continues by executing the commands just after the while . The repeat command automatically sends control back to begin to continue the loop. If the flag is false when while is encountered, the loop is exited immediately; control goes to the first command after the closing repeat .
An easy mnemonic for either of these loops is: If true, fall through.
The loop starts by fetching a byte from location 4000 and displaying the value. Then, the key? command is called, which leaves a true on the stack if the user has pressed any key, and false otherwise. This flag is consumed by until and, if the value is false , then the loop continues. Once a key is pressed, the next call to key? returns true , and the loop terminates.
Unlike many versions of Forth, the User Interface allows the interactive use of loops and conditionals -- that is, without first creating a definition.
A do loop (also called a counted loop) is used when the number of iterations of the loop can be calculated in advance. A do loop normally exits just before the specified ending value is reached.
lists commands to control the execution of counted loops.
The following screen shows several examples of how loops are used.
contains descriptions of additional program execution control commands.
abort | ||
abort " ccc " | ||
eval | ||
execute | ||
exit |
Return from the current word. (Cannot be used in counted loops.) |
|
quit |
abort causes immediate termination and returns control to the keyboard. abort" is similar to abort but is different in two respects. abort" removes a flag from the stack and only aborts if the flag is true . Also, abort" prints any desired message when the abort takes place.
eval takes a string from the stack (specified as an address and a length). The characters in that string are then interpreted as if they were entered from the keyboard. If a Forth text file has been loaded into memory (see "Invalid Cross-Reference" ), then eval can be used to compile the definitions contained in the file.