Forth Lesson 3

From OLPC
Revision as of 16:07, 16 December 2008 by 64.34.172.97 (talk) (FIELD_OTHER)
Jump to: navigation, search
Mitch Bradley's Forth and
Open Firmware Lessons:

Review

In the previous lesson we learned that:

  • Forth words are described by stack diagrams like: ( argn .. arg0 -- resultm .. result0 )
  • Comments are either ( this is a comment ) or \ the rest of the line is a comment
  • Both "(" and "\" must be followed by whitespace
  • Comments work by executing a word that then explicitly calls the input parser

FIELD_MESSAGE_montrbocpa

Using Forth as a Calculator

Armed with those operators, plus the "." (pop and display) word that we have already seen, we can use Forth as an integer calculator. But since everything is stack based, we have to use Reverse Polish instead of algebraic syntax. Instead of writing down an algebraic expression with parentheses to control the grouping, we have to think about what to do first, and then do it.

So, suppose that we want to calculate ( 5 + ( 4 * 7 ) ). The multiplication has to be done first, before we can add the 5. We could perform that calculation in Forth with:

 ok 4 7 * 5 + .

Breaking this down into the individual steps,

  1. push 4 on the stack
  2. push 7 on the stack
  3. pop two items from the stack, multiply them, and push the result (0x1c) back on the stack
  4. push 5 on the stack
  5. pop two items from the stack, add them, and push the result (0x21) back on the stack
  6. pop the stack and display it

The same calculation can be written in a different way

 ok 5 4 7 * + .

In this version, we push all three numbers onto the stack at once, then perform the arithmetic - first the multiplication of 4 and 7, and finally the addition of 5 to the product.

Most Forth texts go to great lengths to explain how to do calculations in Reverse Polish, but I'm writing this for computer professionals and will assume that you either know this already or can figure it out. The key is that you have to think in terms of doing steps in order instead of writing a picture of the algebraic expression and letting the compiler sort it out.

Stack Operators

Sometimes you need to copy or rearrange the stack to bring items to the correct position for further use. Four basic stack operators get the most use.

 dup  ( a -- a a )
 drop ( a -- )
 swap ( a b -- b a )
 over ( a b -- a b a )

Stack operators will be examined in more detail in a later lesson. Until then, here is a list of all the Forth stack operators.

Fractions and Floating Point Numbers

Some (perhaps most) Forth systems have a set of words to operate on floating point numbers. But floating point is not terribly useful for systems programming, so Open Firmware normally doesn't include floating point support (there is no reason it couldn't have it; it's just that it's rarely worth the trouble).

So if you need to do fractions, you will have to scale the integers. For example, if you need to calculate a fractional number to two decimal places, you could just premultiply the dividend by 100. There are various simple tricks like this that will let you avoid floating point for a wide variety of simple calculations.

The double-number words that I alluded to earlier have some fairly useful operators for scaling and unscaling without overflow, but I will leave that topic to a later lesson.

For now, just try out some calculations with the words you already know. Keep in mind that the default number base for Open Firmware is hexadecimal, so if you want do work in decimal you will first need to type "decimal" to switch the base.

Thus endeth the lesson

Next Lesson