The Forth Language CSC 507 – Roy Ford November 22, 2005.

Slides:



Advertisements
Similar presentations
PbForth + sum ! By Josh Jennings Ratnakar Kamath.
Advertisements

1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 2: Data types and addressing modes dr.ir. A.C. Verschueren.
INSTRUCTION SET ARCHITECTURES
Programming Languages and Paradigms The C Programming Language.
World’s first Forth compiler for the.NET platform Valer BOCAN, PhD.
The Assembly Language Level
Kernighan/Ritchie: Kelley/Pohl:
Chapter 10.
Forth Lecture L7.1. A Brief History of Programming Languages
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Programming Languages From FORTRAN to WHYP. A Brief History of Programming Languages
EECC250 - Shaaban #1 Lec # 5 Winter Stacks A stack is a First In Last Out (FILO) buffer containing a number of data items usually implemented.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
A Programming Language for the FC16 Forth Core
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Lecture 18 Last Lecture Today’s Topic Instruction formats
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Forth A stack language.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Module R3 Process Scheduling. Module R3 involves the creation of a simple “Round Robin” dispatcher. The successful completion of this module will require.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
I Power Higher Computing Software Development High Level Language Constructs.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
CHP-3 STACKS.
Higher Computing Software Development -So Far- 5/10/10.
Ada, Scheme, R Emory Wingard. Ada History Department of Defense in search of high level language around Requirements drafted for the language.
Chapter 8 String Operations. 8.1 Using String Instructions.
Data Structures Using C++ 2E
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Overview of Instruction Set Architectures
Stacks Chapter 5.
A bit of C programming Lecture 3 Uli Raich.
CS216: Program and Data Representation
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C Basics.
JavaScript: Functions.
Stacks Chapter 4.
Chapter 10 The Stack.
Introduction to Python
Chapter 9 :: Subroutines and Control Abstraction
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Arrays, For loop While loop Do while loop
Introduction to Python
Chapter 8 Central Processing Unit
Lecture 18 Arrays and Pointer Arithmetic
Forth A stack language.
Overloading functions
Course Overview PART I: overview material PART II: inside a compiler
Chapter 9: Pointers and String
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Where is all the knowledge we lost with information? T. S. Eliot
CSc 453 Interpreters & Interpretation
This is an introduction to JavaScript using the examples found at the CIS17 website. In previous examples I specified language = Javascript, instead of.
Introduction to Python
5.3 Implementing a Stack Chapter 5 – The Stack.
LINEAR DATA STRUCTURES
SPL – PS2 C++ Memory Handling.
Lecture 3 - Instruction Set - Al
Presentation transcript:

The Forth Language CSC 507 – Roy Ford November 22, 2005

Components of Language Stack Stores intermediate results as 32 bit integers Dictionary Stores all command words for the language Interpreter Interpretively executes commands found in the dictionary using the stack to store calculations

Language History Charles Moore Started to work on Forth around 1970 First official Forth was FIG (Forth Interest Group) Forth in late 70’s Forth 79 was the basis for the first commercial implementations Forth 83 tried to fix Forth 79, but caused inconsistency ANS Forth defined in 1994 to consolidate Forth 79 and 83, and allow for things like 32 bit integers My testing was with Win32Forth, an extension of ANS Forth with OOP, floating point, file I/O, etc

Simple Forth Example The order of operations in Forth is Postfix (or Reverse Polish) Notation 5 4 + . 9 ok . 5 4 + Stack Ptr Stack Ptr Stack Ptr Stack Ptr 5 4 9 5 Other operations include + - * / mod 1+ 1-

Stack Operations As this is a stack machine, there are a few operations that focus on manipulating the stack DUP – duplicates the top of the stack SWAP – swaps the top 2 elements DROP – drops the top element of stack ROT – Rotate the top 3 numbers

Store & Fetch Variables The variable <name> word allocates storage for an integer in the dictionary and assigns it to <name> When you use the variable name, the address of the variable is placed on the stack ! stores stack values into a variable variable year 2005 year ! (stores 2005 into year @ fetches variables and places them on the stack year @ . (displays 2005

Adding a word : <name> <body>; : defines the start of the word and ; ends the word : square dup *; When you type 2 square . 4 ok 2 is placed on the stack, duped and multiplied

Conditional Operations In Forth, true has the numeric value of -1 and false has the numeric value of 0 Conditional operators pops the top 2 values on the stack and push the true or false value onto the stack Conditional operations remove values from the stack Operators include not, <, >, = <= is written as > not

Conditional Example 5 4 < 4 < 5 4 5 5 5 4 < 4 < 5 Stack Ptr Stack Ptr Stack Ptr 4 5 5 In this example, the < word removes 5 & 4, from the stack, does the comparison 5 < 4, and as this is false pushes 0 on the stack

Control Structures If/Else/Then Do/Loop Begin/Until Begin/While/Repeat

If/Else/Then If tests the top of the stack. If TOS not equal 0, code from If to Else is executed If TOS = 0, then code from Else to Then is executed

If/Then/Else Normally, a conditional word is used before the If for the test : absolute dup 0 < if -1 * then ; Duplicates the variable on the stack (to save it), tests if less than 0 and multiplies stack by -1 if it is -1 absolute . 1 ok 1 absolute . 1 ok -10 absolute . 10 ok 10 absolute . 10 ok

Do/Loop Do loops are one of 3 looping structures in Forth 2 parameters need to be pushed on the stack for the Do loop Limit – The maximum number of iterations Index – The starting index for the loop (usually 0) Do loop copies Limit and Index to a special return stack for iteration I is a special word that pushes the value of index from the return stack back to the main stack J is like I and is used for nested Do loops Leave breaks out of a do loop (like a break in C)

Do/Loop example <limit> <index> do <forth words> loop : testdo 10 0 do i . loop ; testdo 0 1 2 3 4 5 6 7 8 9 ok Note how i increments from the index of 0 to 9 and stops when i = limit. Index can be set to any start value In Win32Forth, if index is bigger than limit, the do becomes infinite

Do/+Loop Like a Do loop, except at the end of the loop, the value on the stack is added to the index : test+loop 0 do i . 3 +loop ; ok 9 test+loop 0 3 6 ok

Begin/Until & Begin/While/Repeat begin <forth words> <condition> until Similar to a do while loop begin <condition> while <forth words> repeat Similar to a while loop

Arrays create and allot Arrays are created by extending the parameter area of the dictionary for the variable we want to create extensions to the parameter area are done with the allot word Indexing the array requires explicit pointer arithmetic

Creating an Array variable myarray 12 allot create myarray 12 allot The variable myarray is created in the dictionary A parameter field is created to store the integer variable an additional 12 bytes is added to the end of the parameter field to store the rest of the array An integer is stored as 4 bytes, so the size of this array is 16 bytes total, or 4 integers create myarray 12 allot Similar to variable myarray, but a parameter field is not defined by the create command, making an array size of 3 integers (12 bytes)

Accessing an array Like pointer arithmetic, you need to calculate the index into the array create myarray 12 alloc 100 myarray ! (100 in index 0 200 myarray 4 + ! 300 myarray 8 + !

Strings Strings are allocated just like arrays Instead of ! and @ to store and retrieve integers from the array, c! and c@ are used as they only store and retrieve 1 byte

Readability Forth is not very readable The base language is simple, but as the dictionary is expandable it can go rapidly out of hand (4000+ words in Win32Forth) The only parameter passing mechanism to forth words is the stack, as such it is difficult to decipher what parameters are being passed to a forth word without explicit documentation do loops and if/then/else structures do exist, but they are convoluted due to having to place execution parameters on the stack. Forth makes you write programs that manipulate the stack, it does not encourage the creation of variables that help to document

Writeability Forth is a writable language Charles Moore’s purpose for writing Forth was to rapidly write code Once you learn it, RPN is a natural way to do calculations By building applications up from smaller words, the program can take advantage of abstraction

Reliability Major weak point is the pointer arithmetic for arrays, but some of this can be hidden There is no type checking, because the type is integer Exception handling has been written into Win32Forth It has been used in a number of process control applications, where reliability is a must

Questions

References “The Complete Forth”, A. Winfield, Sigma Technical Press, Cheshire, UK, 1983 “The Evolution of Forth, and Unusual Language”, C. Moore, Byte, Volume 5, Number 8, August 1980, McGraw-Hill Publication “What is Forth? A Tutorial Introduction”, J. James, Byte, Volume 5, Number 8, August 1980, McGraw-Hill Publication “FORTH Extensibility, Or How to Write a Compiler in 25 Words or Less”, K. Harris, Byte, Volume 5, Number 8, August 1980, McGraw-Hill Publication http://www.forth.org/ http://www.taygeta.com/forth/dpansd.htm#D.1 http://win32forth.sourceforge.net/

Additional Material

Forth Dictionary The Forth Dictionary is a table that contains all of the defined words, variables and constants used in the execution environment The table contains The length and name of the word A Link address to the previous table entry A code pointer to the actual code that is executed A variable length parameter field

Dictionary Example Name Executable code Associated with Link word Code Parameter Name Link Code Executable code Associated with word Parameter Name Executable code Associated with word Link Code Parameter

Dictionary Entry for + + Name Ptr to Previous Link Word Code Machine code to take 2 numbers off stack, and them and place results on stack Parameter field is null

Dictionary Entry for a Variable Name Name of Variable Link Ptr to Prev Entry Code Machine code that takes the address of the parameter field and places it on the stack Parameter 32 bit storage area for variable

Dictionary Entry for : ; : square dup *; Machine code for the : command Name square Link Ptr to Prev Word Code dup Parameter 1 Parameter 2 * Parameter 3 ;

Create/Does> Create/Does creates defining words, words that are used to create other words This is very similar to a crude class, in that it defines how to create the word, and what to do when the word is executed : array create 4 * allot does> swap 4 * + ; ok 10 array p ok 4 p ? 0 ok 5 p ? 0 ok 100 4 p ! ok 4 p ? 100 ok in this example, 100, 4 and the address of p are pushed onto the stack, then the does> statements swap 4 * + are executed Note : This is a modification of “The Complete Forth”, A. Winfield, Sigma Technical Press, Cheshire, UK, 1983, page 87