Forth A stack language.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

INSTRUCTION SET ARCHITECTURES
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Ch. 8 Functions.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
True or false A variable of type char can hold the value 301. ( F )
TK 2633 Microprocessor & Interfacing Lecture 3: Introduction to 8085 Assembly Language Programming (2) 1 Prepared By: Associate Prof. Dr Masri Ayob.
Program Design and Development
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Professor Jennifer Rexford COS 217
String Escape Sequences
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Lecture 18 Last Lecture Today’s Topic Instruction formats
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
Machine Instruction Characteristics
Pascal Programming Strings, Arithmetic operators and output formatting National Certificate – Unit 4 Carl Smith.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
Built-in Data Structures in Python An Introduction.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Chapter 10 Instruction Sets: Characteristics and Functions Felipe Navarro Luis Gomez Collin Brown.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Functions. Motivation What is a function? A function is a self-contained unit of program code designed to accomplish a particular task. We already used.
What is a program? A sequence of steps
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Computer Architecture. Instruction Set “The collection of different instructions that the processor can execute it”. Usually represented by assembly codes,
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Bill Tucker Austin Community College COSC 1315
Information and Computer Sciences University of Hawaii, Manoa
Format of Assembly language
Building Java Programs
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
The Forth Language CSC 507 – Roy Ford November 22, 2005.
Primitive Data, Variables, Loops (Maybe)
The Selection Structure
MATLAB: Structures and File I/O
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Building Java Programs
Shift & Rotate Instructions)
CS 100: Roadmap to Computing
Forth A stack language.
Building Java Programs Chapter 2
Building Java Programs
Introduction to Computer Science
Building Java Programs Chapter 2
CS 100: Roadmap to Computing
COMPUTING.
Presentation transcript:

Forth A stack language

Free implementations For our purposes, pforth seems to be best http://www.softsynth.com/pforth/ Another alternative is SwiftForth http://www.forth.com/swiftforth/dl.html Both are ANS compliant

Forth syntax A word consists of a sequence of printable (non-whitespace) characters A program consists of a sequence of words, separated by whitespace Any questions?

Basic execution Forth uses a dictionary to hold function definitions, and a stack to hold data Words are evaluated left to right, top to bottom, in the usual order Forth is case insensitive Most documentation uses words in all caps, but lowercase is also ok If a word is in the dictionary, then the function is looked up and executed else Forth converts the word to a number, if it can, and places it on the stack

Defining and calling functions A function is defined with : functionName ( old -- new , words ) body ; The function expects to find its parameters on the stack The function places its return values on the stack The ( old -- new , words ) tells how the stack is changed For example, addition might be documented as ( n1 n2 -- n3 , adds top two items ) A function is called by mentioning its name Parameters, if any, are taken from the stack Return values, if any, are placed on the stack Example: : add3 ( n1 n2 n3 -- n4 , add 3 items) + + ; ok 1 2 3 4 5 add3 .s 1 2 12 <-Top ok

Displaying the stack . ( N -- , print number on top of stack ) Removes (“pops”) the top number from the stack and prints it 2 2 + . 4 ok .s ( -- , print the entire stack ) This does not modify the stack contents 1 2 3 4 5 .s 1 2 3 4 5 <-Top ok + . 9 ok .s 1 2 3 <-Top ok The top of the stack is on the right The above examples are from SwiftForth, which prints <- Top Most other Forth implementations just take this for granted

Manipulating the stack 0sp ( clears the stack ) (pforth only) 0sp 0sp ? .s <-Top ok dup ( n -- n n , duplicate top of stack ) 1 2 3 4 5 dup .s 1 2 3 4 5 5 <-Top ok swap ( a b -- b a , swap top two items on stack ) 1 2 3 4 5 swap .s 1 2 3 5 4 <-Top ok over ( a b -- a b a , copy second item on stack ) 1 2 3 4 5 over .s 1 2 3 4 5 4 <-Top ok pick ( ... v3 v2 v1 v0 N -- ... v3 v2 v1 v0 vN ) Makes a copy of the Nth stack item; 0 PICK is equivalent to DUP

More stack manipulations DROP ( a -- , remove top item from the stack ) ROT ( a b c -- b c a , ROTate third item to top ) ?DUP ( n -- n n | 0 , duplicate only if non-zero, '|' means OR ) -ROT ( a b c -- c a b , rotate top to third position ) 2SWAP ( a b c d -- c d a b , swap pairs ) 2OVER ( a b c d -- a b c d a b , leapfrog pair ) 2DUP ( a b -- a b a b , duplicate pair ) 2DROP ( a b -- , remove pair ) NIP ( a b -- b , remove second item from stack ) TUCK ( a b -- b a b , copy top item to third position )

Arithmetic Arithmetic is integer + - * / mod are add, subtract, multiply, integer divide, modulus, all ( n1 n2 -- n3 ) Some “shorthand” operators are 1+ 1- 2+ 2- 2* 2/ 10 2/ . 5 ok /MOD ( n1 n2 -- rem quot , remainder and quotient of n1/n2 ) MIN ( n1 n2 -- n3 , minimum of n1 and n2 ) MAX ( n1 n2 -- n3 , maximum of n1 and n2 ) ABS ( n -- abs(n) , absolute value of n ) NEGATE ( n -- -n , negate value, faster then -1 * ) LSHIFT ( n c -- n<<c , left shift of n ) RSHIFT ( n c -- n>>c , logical right shift of n ) ARSHIFT ( n c -- n>>c ) , arithmetic right shift of n )

Output ." ( -- , print string up to next " ) EMIT ( n -- , display character with ASCII value n ) 111 108 dup 101 72 emit emit emit emit emit Hello ok CHAR ( <char> -- char , get ASCII value of a character ) Unusual: Uses the following text, not the stack! char A . 65 ok ." ( -- , print string up to next " ) ." Hello from pforth!" Hello from pforth! ok Works in SwiftForth when in a function, but not by itself in REPL (?) SPACE ( -- , output a space ) SPACES ( n -- , output n spaces ) CR ( -- , start new line , carriage return )

Strings ." will output a string, up to a " ." hello" hello ok Stack<10> S" will put a string on the stack Format: Character count on top, machine address below that s" Hello from Forth!" ok Stack<10> 140255546192593 17 TYPE will remove a print a string from the stack type Hello from Forth! ok Stack<10>

Strings, character by character Given the machine address of a string, C@ will return the first character CHAR+ will advance to the next character s" ABCDE" ok Stack<10> 140354489823953 5 drop dup ok Stack<10> 140354489823953 140354489823953 c@ emit A ok Stack<10> 140354489823953 char+ dup c@ emit B ok Stack<10> 140354489823954 char+ char+ char+ dup c@ emit E ok Stack<10> 140354489823957

Input KEY ( -- char , input character ) : testkey ( -- ) ." Hit a key: " key cr ." That was " . cr ; ok testkey Hit a key: A That was 65

Loading from a file In file sample.fth, in same directory as sf: \ Sample Forth Code \ Author: Phil Burk ANEW \ forget what was loaded before : SQUARE ( n -- n*n , square number ) DUP * ; : TEST.SQUARE ( -- ) CR ." 7 squared = " 7 SQUARE . CR ; In SwiftForth: include sample.fth ok test.square 7 squared = 49 ok Very similar in pforth forget square – forgets everything from square on down (pforth only)

More about loading FORGET definition is supposed to forget the named definition and everything that follows This works in pforth, but I don’t think it does in SwiftForth ANEW should forget what was loaded before INCLUDE? filename will only load code if it isn’t already in the dictionary

Constants and variables Constants can be defined as value CONSTANT name 42 constant theAnswer ok theAnswer . 42 ok Variables can be defined with VARIABLE name VARIABLE ( <name> -- , define a 4 byte memory storage location ) @ ( address -- value , FETCH value FROM address in memory ) ! ( value address -- , STORE value TO address in memory ) variable ans ok 42 ans ! ok ans ? 42 ok ans @ . 42

Logic true and false are constants -1 and 0 Comparisons are < <= = >= > 0= 0> 0< 2 3 < . -1 ok 2 3 > . 0 ok 2 3 <= . -1 ok 2 3 >= . 0 ok Logical operators are and or not true -1 and . -1 ok true true and not . 0 ok

IF-ELSE-THEN statements Forth has an “if” statement : showIf ( flag -- , print logical value ) if ." true" else ." false" then ; ok true showIf true ok false showIf false ok 55 showIf true ok The “else” part is optional It helps to think of then as “end if

BEGIN … END Loops BEGIN … END will loop until a given condition is true 5 begin 1- dup . dup not until 4 3 2 1 0 ok Stack<10> 0 0 begin dup . 1+ dup 3 = until 0 1 2 ok Stack<10> 3

DO … LOOP Loop end start DO … LOOP will loop a fixed number of times 5 1 do ." hi! " loop hi! hi! hi! hi! ok Stack<10> You can access the loop counter with I 5 1 do ." hi#" i 48 + emit loop hi#1 hi#2 hi#3 hi#4 ok Stack<10> 70 65 do i . i emit space loop 65 A 66 B 67 C 68 D 69 E ok You can leave the loop with LEAVE 50 1 do ." hi " i 5 > if leave then loop hi hi hi hi hi hi ok Stack<10>

BEGIN … WHILE … REPEAT Loop BEGIN test WHILE body REPEAT acts like a while loop in more traditional languages 2 begin dup 1000 < while dup . 2* repeat 2 4 8 16 32 64 128 256 512 ok Stack<10> 1024

“Case” statements On file sample.fth: : day-of-week ( n -- str , return day of week, Sunday = 1 ) case 1 of s" Sunday" endof 2 of s" Monday" endof 3 of s" Tuesday" endof 4 of s" Wednesday" endof 5 of s" Thursday" endof 6 of s" Friday" endof 7 of s" Saturday" endof endcase ; include sample.fth Include sample.fth include added 768 bytes,39236 left. ok Stack<10> 4 day-of-week type Wednesday ok Stack<10>

References Most of this lecture based on (or shamelessly stolen from) the excellent Forth Tutorial by Phil Burk: http://www.softsynth.com/pforth/pf_tut.php

The End The first time I combined the ideas I had been developing into a single entity, I was working on an IBM 1130, a “third-generation” computer. The result seemed so powerful that I considered it a “fourth generation computer language.” I would have called it Fourth, except that the 1130 permitted only five-character identifiers. So Fourth became Forth, a nicer play on words anyway. --Charles “Chuck” Moore