You are on the archived version of the www.TheForthSource.com website. These materials are presented for informational purposes only. We express our gratitude to 1PLs Co [urgent loans in the USA] for paying for hosting.
FORTH GUIDE


CHAPTER IV


PARAMETERS


You will have noticed that some of the FORTH functions require some parameters. The value of the parameters given to the system is converted by FORTH to 16-bit binary values for storage. They are always used as 16-bit binary values.

As MVP-FORTH starts, the system is prepared to accept numerical values in decimal. For many purposes decimal notation is most satisfactory. The number system we learned in school is decimal based. We do have just ten fingers.

But as you have already seen in the output of DUMP, hexadecimal notation is more convenient for some purposes. It is a matter of visualizing the values as represented in memory. There is no group of bits which can be completely described with a single decimal digit. The binary, octal and hexadecimal number systems allow this.

The problem is to change the number base used. MVP-FORTH provides a simple solution. All numerical entries are converted to binary for storage in memory. The value of the current base is stored in the word BASE. By simply changing the value stored in the word BASE, the input and output base or radix can be changed.

To change to the hexadecimal system, simply enter HEX.

        HEX

In this mode, hexadecimal digits 0 through F can be entered. You can then convert to the decimal mode:

        DECIMAL

You have already used a period, 'dot', to cause a number to be printed. Now you can switch back and forth between number bases.

        HEX 0F DECIMAL .

The system will promptly print 15. You have converted the hexadecimal on input to a decimal value on output. Now try it the other way around:

        DECIMAL 10 HEX . DECIMAL

This time the system will print the value A . You have converted to hexadecimal.

Working with the print command, 'dot', you will find that when you give the system a value and print it, the value is no longer there. Printing a value consumes the value. There will be times when you would like to see the value which you earlier gave without consuming it. MVP-FORTH has a command to do that: 'dot-s'.

        .S

Now try some exercises. Enter some numbers and print them with both the .S command as well as the 'dot' command. You will note that if you have not entered a number and try either command you will be shown the message STACK EMPTY. You will also note that you can repeat the .S command as many times as you wish and always have the same result. However, with the 'dot' command, once you have printed all of the values previously entered, you will get the message STACK EMPTY.

Now try putting in several values:

        1 2 3 . . .

You will note that the last number entered will be the first one printed, and so on until all of the values are printed. The value farthest to the left will be a 3 followed by a 2 followed by a 1. Now try the .S command:

        1 2 3 .S

The values are printed in the reverse order from that in which you entered them. This is just like executing a series of 'dot' commands. Now some people find that they would prefer to see the numbers in the order in which they were entered rather than the reverse order. There are two MVP-FORTH commands which control the function of the .S command.

They are .SL and .SR . These commands change a flag in a word .SS . You can easily select the mode of representation you find most natural.

While you are entering values and printing them, you might explore the way the simple arithmetical operations function. Like many Indo-European languages, the operator is at the end. The operator must know what it is going to operate on before it can operate. Rather than entering a value then the operation which must be set aside to wait for the second value before executing, simply enter the values and then the operation.

Some hand held calculators do the arithmetic operations the same way. The method is called Reverse Polish Notation or RPN. There is really nothing complicated about it. World wide, probably more people think this way than the other way. The problem is that there are a number of hand held calculators which work the reverse way. Also some of our schools teach arithmetic the reverse way. This may have something to do with the aversion many people in this country have to arithmetic.

But it is really simple enough. Just experiment with your system and see how it works. You will get a message should you do it wrong. Simply try it again.

Now suppose you know you have put in some numerical values in error and wish to erase them. First you might want to examine the current values with the .S command. Then you could use a series of 'dot' commands until you get the message - STACK EMPTY.

Another way to clear the values already entered is to enter a word that is not recognized. You will immediately get the answer: NOT RECOGNIZED. You can then try the .S command and see the message: STACK EMPTY.

Now someone will wonder if all of the numbers in all of the bases are entered in the MVP-FORTH dictionary. Certainly not! Rather, if the entered word is not found in the current MVP-FORTH dictionary, the system will attempt to convert it to a number depending on the value currently stored in the variable BASE. If successful it will save the binary value. If the system is unable to do either, then it will give you the message: NOT RECOGNIZED.

A collection of functions is used to implement this ability of the system. They might be called primitives. They are included in MVP-FORTH and are available for your use. But you will probably not want to work with them until much later, if at all.

The words DECIMAL and HEX are already defined in the dictionary. The names of other bases are not. They can be added easily enough with colon definitions. Later you will learn more about colon definitions. For now just use the format indicated here.

        : OCTAL 8 BASE ! ;
        : BINARY 2 BASE ! ;

The only new word is the exclamation point ( ! ). It is pronounced 'store'. The colon definition of OCTAL enters the value of the new base ( 8 ) and then the location of the variable BASE and finally stores the one in the other. The colon definition BINARY is exactly the same except for the value to be used.

Now you have available the words to do any sort of number conversion you wish. You can experiment with a number of them.

        10 HEX .S DECIMAL .S
        OCTAL .
        BINARY .S
        DECIMAL

It is probably a good habit to always end up in the decimal number system. That way your habit will always tell you where you are.

Now suppose you have to examine the numbers entered a number of times. This calls for another colon definition:

        : #CONVERT
           HEX .S
           DECIMAL .S
           OCTAL .S
           BINARY .S
           DECIMAL ;

Now you can execute this string of commands very simply. The name chosen is a little long but it combines the idea of a number ( # ) and the English word convert. Should you have a number of these operations to do, you might just for the occasion give it a name easy to type, XX, for example. This has no mnemonic value but is easy to use.

It is very convenient to interactively enter a colon definition in this manner just for the occasion. You can then promptly forget it.

        FORGET #CONVERT

The function erases all definitions written after the word which you forget.

You now have a problem. You could gradually build up the vocabulary in your MVP- FORTH dictionary with more and more convenient utilities. You might have more than a thousand such utilities. Then you have to remember or lookup just exactly what each of those functions are.

MVP-FORTH has included only the words which have been proven most convenient and the primitives associated with them. For a major part of FORTH programming you can do very well with the included words. Once you have mastered them, you can easily write a special function using a string of characters which is easy to type, but with no mnemonic meaning and forget the function when through. This way you will always know what the function is.

Now go back and try using MVP-FORTH for simple arithmetic calculations. The common operations are + , - , *, and / . These have all of the expected functions. They operate on 16-bit integers and yield 16-bit integers. This is sufficient to cover many of the early calculations you will need to make. The numerical operations do not end here though. A number of mixed operators are available as well as double precision, 32-bit, operators. Also MVP-FORTH has a complete floating point, quad precision, and trancendental number application. You can wait until later to explore some of those operations.

Remember that the values must be entered before the operator.

        2 2 + .
        3 6 * .
        6 2 / .
        4 2 - .
        etc

You can go on from here and learn as much as you care to, about the numerical operators. Explore what happens at the boundary conditions!

Next Previous Contents