pbForth + sum ! By Josh Jennings Ratnakar Kamath
History Developed by Charles Moore in the 60’s First Forth system released in early 70’s Early applications controlled radio telescopes and Satellites Multitasking and real-time support on single CPU Too many “flavors” leads to push for standard Forth-83 standard adopted – each vendor still has peculiarities ANSI Committee formed to standardize again Draft available online pbForth is developed in late 1998
Fundamental principles Forth programming is unlike any other language Moore’s Principles – 1. Keep it Simple – 2. Do not Speculate – 3. Do it Yourself What makes Forth unique – according to Leo Brodie – 1. Implicit Calls – 2. Implicit Data Passing – 3. Direct Access to Memory Forth is an interpreter and a compiler
Stack Based Language “ Forth is a programming language that uses two stacks and a dictionary of words that a programmer adds to in writing a program. “ Data stack Return stack
Syntax Separated by spaces Case sensitive Free format Line ends with a carriage return You are responsible for memory management
Stacks Again Parameter/data stack and return stack LIFO ok 1 2 dup ok
Dictionary Forth has a dictionary of words Dictionary can be extended Anything can be used to define a word : and ; are used to extend the dictionary : art ; : name (put_your_definition_here) ;
Basic Math “Reverse Polish Notation” or “postfix” operators Forth has 16 bit fixed point math (signed and unsigned) – and some 32 bit math You can add floating point – ???
Managing the stack Parameters are passed through the stack – DUP ( n1 –- n1 n1 ) (dupe) – DROP ( n1 n2 –- n1 ) (drop) – SWAP ( n1 n2 –- n2 n1 ) (swap) – OVER ( n1 n2 –- n1 n2 n1 ) (over) – ROT ( n1 n2 n3–- n2 n3 n1 ) (rote) – 2DUP ( n1 n2 –- n1 n2 n1 n2 ) (two- dupe) – 2DROP ( n1 n2 –- ) (two-drop) – 2SWAP ( n1 n2 n3 n4 –- n3 n4 n1 n2 ) (two-swap) – 2OVER ( n1 n2 n3 n4 –- n1 n2 n3 n4 n1 n2 ) (two-over)
Comparisons False is 0, everything else is true 1 2 > Forth returns true with all bits set. = ( n1 n2 –- f ) (equal) < ( n1 n2 –- f ) (less-than) > ( n1 n2 –- f ) (greater-than) 0= ( n1 –- f ) (zero-equal) 0< ( n1 –- f ) (zero-less) Logical expressions like and, or, xor and invert are present.
Conditional Execution and Loops Can use conditional statements inside a definition only flag IF do_if_true ELSE do_if_false THEN Loop again can be used only inside a definition limit index DO do_stuff_here LOOP Begin … again Begin … until Begin … while … repeat
Variables and fetch ! Store variable foo 46 foo ! 2362 constant BAR Bar. Variable and constant add words to the dictionary and allocate space for the values, they are defining words like : ;
Arrays CREATE RAY 32 CELLS ALLOT Creates an array of 32 cells 34 ray 12 cells + ! ray 12 cells A string is array of characters S” hello”. (Shows the starting address) Type (displays string)
RCX specific Words PbForth is a replacement firmware for the RCX It has a dictionary of pre-defined words for controlling the RCX. For ex. Power, display, motor, sensor, sound and timer.
Motor Control Words Syntax: MOTOR_SET ( power dir idx-- ) Power 0 – 7 Dir 1 – 4 Idx 0 – MOTOR_SET Turns the motor on forward.
A program : FORWARD (MOVE ROBOT FORWARD) MOTOR_SET(MOTOR A FULL POWER FORWARD) MOTOR_SET ;(MOTOR C FULL POWER FORWARD) : BACKWARD (BACK ROBOT UP, WHILE TURNING) MOTOR_SET (MOTOR A LOW POWER FORWARD) MOTOR_SET (MOTOR C FULL POWER BACKWARD) 0 0 TIMER_SET (SET FIRST TIMER TO ZERO) BEGIN (DELAY FOR 1 SECOND) 0 TIMER_GET 10 = UNTIL ; : MAIN (**MAIN PROGRAM**) SENSOR_IN (INITIALIZE SENSOR) BEGIN SENSOR_VAL 1 = IF (IF BUMPER TRIGGERED…) BACKWARD (BACK UP AND TURN) ELSE FORWARD (OTHERWISE, PROCEED FORWARD) THEN BUTTON 1 = UNTIL(REPEAT UNTIL RUN BUTTON PUSHED) MOTOR_SET (STOP MOTOR A) MOTOR_SET (STOP MOTOR C) INIT ;(RETURN RCX TO READY STATE)
Why PbForth? Simple Cross-Platform Development Environment Small Memory Footprint - Big Memory Space Interactive Software Development Compiler Runs on the RCX
Disadvantages Cryptic Reverse polish notation Fractions
References