A First Program

This first section of the tutorial helps you to program a computer. If you've programmed a computer before, then you may prefer to choose a different option from the Introduction.

Computers can do the most amazing tasks, but only if you give them detailed, 100% accurate instructions. We think Forth is a simple and natural way to do this.

We have used this page to add to WebForth some basic words needed to build a set of traffic lights -
RED RED-&-YELLOW GREEN YELLOW - the 4 phases of the light sequence (as used in the UK).

Type in one of these four words and then press the Enter key to see what the word does. WebForth responds with a final OK, to show that it completed the word with no problems. (Always press the Enter key to end a line - won't mention this again.)

(If you click with the mouse anywhere outside the WebForth window, you will disconnect WebForth from the keyboard and it will no longer echo the keys you type. All browsers operate like this. To restore the keyboard to WebForth, just click with the mouse anywhere within the WebForth window.)

Notice we write RED-&-YELLOW, not RED & YELLOW, to keep it as a single word. As in English text, spaces act to separate words.

Now build this sequence into an umbrella word - PHASES. We do this by using : (colon) before and ; (semi-colon) after as in

   : PHASES  RED RED-&-YELLOW GREEN YELLOW ; 

(WebForth will respond to instructions it doesn't understand with a ? instead of OK - usually because the instruction was not 100% accurate. Just press Enter and type it in correctly.)

Now you've built the word PHASES, you can use it just as you did the words RED and GREEN. Try it out now by entering

   PHASES

and see that it carries out the sequence you expect.

To complete our traffic light, the sequence must repeat until it is switched off. Do this by building TRAFFIC-LIGHT as a new word which repeats PHASES again and again using

   : TRAFFIC-LIGHT  LAMP-POST BEGIN PHASES AGAIN ; 

BEGIN and AGAIN are standard words in Forth. Now try this out by entering

   TRAFFIC-LIGHT

Watch it repeat indefinitely then stop it by pressing the key Esc.

Congratulations
- you've just programmed a computer for the first time!

The full program

If you want to see how RED, GREEN and the other words were built, take a look at their definitions. Such text is known as the "source" of the program.

Passing Numbers Around

With a few exceptions (like :), all words are independent, communicating with each other by leaving numbers on the Stack. The Stack is a simple container for holding numbers and works like an in-tray, so the most recent number is always on the top.

When you enter 2 2 +, you instruct WebForth to

  • put 2 onto the Stack
  • put another 2 onto the Stack
  • run the addition word "+"
Try it now.
Like all Forth words, "+" takes any numbers it needs from the Stack. (If there aren't any numbers waiting, we get the ? response.) When "+" runs, it removes the top two numbers from the Stack, calculates the result and pushes that onto the Stack instead. WebForth responds with "OK" and waits for more instructions.

If you now enter the full-stop word ".", WebForth removes the result from the Stack and prints it on the screen.

Now let's take a closer look at the Stack. The word .S means print stack and lists all the numbers on the stack from the bottom to the top.

Enter 56789 43210 .S + .S CR . to try this out.

You can empty the Stack with the word ABORT.

Giving Numbers a Name

These traffic lights run at a rate determined by the value held at a location called %WAIT. (We reserved this location and gave it a name with the VARIABLE command, as in

  VARIABLE %WAIT

We also set it to an initial value of 100 with the store word ! as in

  100 %WAIT !

Confirm this now by reading and printing the value with the ? command by entering

  %WAIT ?

Now change the percentage wait from 100% down to 10% to make the lights run ten times as fast. Do this by entering

  10 %WAIT !
and see the results with the command

  TRAFFIC-LIGHT

How does the store word "!" work? %WAIT puts the location on the Stack and then "!" takes first the location and then the number from the Stack before storing the number at the location.

As a reminder of this sequence, the Forth documents describe "!" as

  ! ( N L -- )

This "stack comment" ( N L -- ) simply declares that
"! takes location L from the Stack, then number N and puts nothing back".


This ends Section 1 of the Starter Kit - for Section 2, click here.