Presentation is loading. Please wait.

Presentation is loading. Please wait.

PbForth + sum ! By Josh Jennings Ratnakar Kamath.

Similar presentations


Presentation on theme: "PbForth + sum ! By Josh Jennings Ratnakar Kamath."— Presentation transcript:

1 pbForth num1 @ num2 @ + sum ! sum @. By Josh Jennings Ratnakar Kamath

2 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

3 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

4 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

5 Syntax Separated by spaces Case sensitive Free format Line ends with a carriage return You are responsible for memory management

6 Stacks Again Parameter/data stack and return stack LIFO 1 2 3... 3 2 1 ok 1 2 dup... 2 2 1 ok

7 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 1 2.. ; : name (put_your_definition_here) ;

8 Basic Math “Reverse Polish Notation” or “postfix” operators 1 2 + Forth has 16 bit fixed point math (signed and unsigned) – and some 32 bit math You can add floating point – ???

9 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)

10 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.

11 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

12 Variables and Constants @ fetch ! Store variable foo 46 foo ! foo @. 2362 constant BAR Bar. Variable and constant add words to the dictionary and allocate space for the values, they are defining words like : ;

13 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)

14 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.

15 Motor Control Words Syntax: MOTOR_SET ( power dir idx-- ) Power 0 – 7 Dir 1 – 4 Idx 0 – 2 7 1 0 MOTOR_SET Turns the motor on forward.

16 A program : FORWARD (MOVE ROBOT FORWARD) 7 1 0 MOTOR_SET(MOTOR A FULL POWER FORWARD) 7 1 2 MOTOR_SET ;(MOTOR C FULL POWER FORWARD) : BACKWARD (BACK ROBOT UP, WHILE TURNING) 0 1 0 MOTOR_SET (MOTOR A LOW POWER FORWARD) 7 2 2 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) 7 3 0 MOTOR_SET (STOP MOTOR A) 7 3 2 MOTOR_SET (STOP MOTOR C) INIT ;(RETURN RCX TO READY STATE)

17 Why PbForth? Simple Cross-Platform Development Environment Small Memory Footprint - Big Memory Space Interactive Software Development Compiler Runs on the RCX

18 Disadvantages Cryptic Reverse polish notation Fractions

19 References www.forth.org www.hempeldesigngroup.com http://www.vorlesungen.uni-osnabrueck.de


Download ppt "PbForth + sum ! By Josh Jennings Ratnakar Kamath."

Similar presentations


Ads by Google