Last week's look at the uses of stack manipulation words continues
DO . . . LOOP
To repeat a particular sequence of operations a definite number of times the control structure DO . . . LOOP is used. This is the Forth equivalent of Basic's FOR . . . NEXT structure hence:
10 FOR N = 0 to 10
30 NEXT N
in Basic becomes:
10 0 DO PROCESS LOOP
The DO expects two values on the stack. The top item is an initial value for the loop counter, the second item is the limiting count. These values are removed by the DO and placed on a second stack called the return stack.
Since it is often convenient to have access to the index during the operation of a DO . . . LOOP sequence, Forth provides the word I for this purpose. The action of I is to copy the value on top of the return stack onto the parameter stack, and thus when used in a DO . . . LOOP will return the current value of the loop index.
:SAMPLES (n . . .) 0 DO I SAMPLE LOOP ;
In this example the index values might be used by SAMPLE to search an area of memory, or as data to be processed. One thing to remember is that since the test against the limit is done at the end, the loop sequence will always execute at least once. So executing:
will still process one sample. To prevent this an adjustment can be made to the definition of SAMPLES, with the inclusion of an IF . . THEN; :SAMPLES (n ) ?DUP IF 0 DO I SAMPLE LOOP THEN;
The ?DUP duplicates the top item of the stack only if it is non-zero. If the value is zero it is not duplicated but is removed by the IF whereupon execution continues after the THEN. A non-zero value would be duplicated so that it is available for the DO . . . LOOP.
BEGIN . . . UNTIL
Most computer applications require, at some time, a particular set of actions to take place and to be repeated until some condition has been fulfilled. When a language provides such a structure it makes life easier. Forth provides just such a structure, its general format being:
:PROGRAM BEGIN ACTIONS DONE UNTIL;
ACTIONS perform all the actions required by the program and DONE performs some kind of test and, on the basis of the result, leaves either a true flag or false flag on the stack. UNTIL responds to the condition on the stack by either rerouting execution back to the
BEGIN, in the case of a 0 value, or by allowing execution to continue in-line to the end of the definition.
The BEGIN does nothing at the time PROGRAM executes, it merely serves the compiler by providing an address for the UNTIL to use for directing the flow. The words enclosed in the structure may be simple and few, or many and complex, so long as a condition is left for the UNTIL to use.
As with the DO . . . LOOP structure, the test for the termination of the loop is conducted at the end of the sequence so that the enclosed code will always execute once. It should, therefore, be used only when this effect is desirable or alternatively combined with some other decision making structure, as for example in a multi-pass, sort to avoid tackling sorted data:
:ORDER . . . SORTED? NOT
IF BEGIN SORT SORTED?
One special use of BEGIN . . UNTIL is where the UNTIL is preceded by O. This has the effect of repeating unconditionally and indefinitely. Since all the processors I know of are capable of an unconditional brand the word AGAIN comes as standard on most Forth systems to serve this purpose. The BEGIN AGAIN structure is often used to enclose the outermost level of an application, thus sealing the user off from the rest of the Forth system.
BEGIN . . . WHILE . . . REPEAT
In instances where a decision to stop looping needs to be made at some point in the sequence other than at the end, a structure is provided in the form of:
:PROGRAM BEGIN ACTIONS ENOUGH NO
Here the ACTIONS are always performed at least once, whilst MORE need not be.
The portion of code between the WHILE and REPEAT statement is only executed if the WHILE finds a true condition on the stack.
In that case execution continues down to the REPEAT and then branches back to the BEGIN. If the WHILE encounters a false condition, then execution is directed to re-commence after the REPEAT and the loop is terminated.
Note the difference in the logic of UNTIL and WHILE. UNTIL terminates the loop when it finds a true value on the stack whereas WHILE terminates with a false value.
The arcade game machine you see above is probably either running the Forth computer language or its own machine code.
Forth is often used for developing arcade games, controlling robots and running arcade games on dedicated machines.
Its great advantage is speed, and it's that speed that makes it more appropriate for driving a speeding pacman or lifting an on-screen plane to the sky.
Contributors: David Janda, Dick Olney, Niklos Shawl and Bryan Skinner
Design: Nigel Wingrove
Micropaedia Editor: Geof Wheelwright
Illustrations: Virginia Armstrong and John Hallet
We conclude our programming series with a last look at the high-level Pascal programming language, complete our introduction to adventure programming, glance back at program structure and take a quick peak at the CP/M business operating system.
And in two weeks time, we give you a special one-part Micropaedia special all about computer monitors, including: how to buy them, what to look for and how they work.