Forth Lesson 3

From OLPC
Revision as of 19:48, 8 June 2007 by 71.130.102.141 (talk)
Jump to navigation Jump to 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

Arithmetic and Logical Operators

We have already seen " ", which pops two numbers, adds them, and pushes the result. Here are some more numeric operators with their stack diagrams:

         ( n1 n2 -- n1 n2 )
 -       ( n1 n2 -- n1-n2 )
 *       ( n1 n2 -- n1*n2 )   \ Signed multiplication
 /       ( n1 n2 -- n1/n2 )   \ Signed division
 mod     ( n1 n2 -- n1%n2 )
 /mod    ( n1 n2 -- n1%n2 n1/n2 )
 and     ( n1 n2 -- n1