|
44 THE 444 44 44 44 44 TH 44 44 44 44 44 44 44 44 44 44 44 44 444444444444444 COURSE 44 44
by Richard E. Haskell Dept. of Computer Science and Engineering Oakland University Rochester, Michigan 48309
PREFACE
What is FORTH? . . . . And why should you want to learn it? Forth is a programming language that ... ...was invented by Charles Moore in the early 70's; ...is extensible; ...keeps all definitions in a dictionary; ...is extremely compact; ...is recursive; ...can be programmed in RAM, PROM or ROM; ...is structured; ...uses a stack and postfix notation; ...is extremely modular; ...is interactive; ...is easy to debug; ...allows easy machine access; ...is fast (and contains an assembler); ...is transportable; ...can be understood in its entirety; ...has been implemented in Forth engines that execute Forth directly;
...has been implemented in public domain software
for nearly every ...is addictive; ...is unlike any other language.
This set of 11 lessons called The Forth Course has been designed to make it easy for you to learn Forth. The material has been developed over several years of teaching Forth as part of a senior/graduate course in the design of embedded software computer systems at Oakland University in Rochester, Michigan. The lessons in this course have been laid out in page-size modules (such as the first page of this preface) that can be made into viewgraphs for use in lectures. In addition, the lessons are written as F-PC .SEQ files which can be FLOADed. This will cause all Forth words defined in the lesson to be compiled and ready to use. This means that you will not need to type in all of the examples. The course assumes that you know some other programming language (such as C, C++, Fortran or Basic). It is helpful, but not essential, to know 8088/8086 assembly language. The 8086 Tutor monitor that accompanies these lessons is a useful tool for learning 8086 assembly language. The book
Introduction to Computer Engineering -- Logic Design and the 8086 Microprocessor by Richard E. Haskell Prentice-Hall, 1993, ISBN 0-13-489436-7
describes the use of the 8086 Tutor monitor and will teach you what you need to know about assembly language programming. F-PC 3.60 is the Forth that these lessons assume you are using. This is a large, full-featured, public domain Forth that is due largely to the efforts of Tom Zimmer. You will find that this version of Forth includes many features that you expect in a good language -- a good editor and good debugging tools -- and some features that you may not expect -- like an integrated hypertext system. There will be many features included with the system that you may never use and others that you will want to learn about when you need them. F-PC comes with a large amount of on-line help and a complete user's manual that you can print. You can download the entire FPC V3.6 system from
http://www.forth.org/compilers.html
An executable version of F-PC, is included with these lessons as the file F.EXE. To run it from a DOS prompt, just type F. ¡@ A version of Forth, called WHYP, that can be used to program 68HC12/11 microcontrollers is described in the book ¡@ Design of Embedded Systems Using 68HC12/11 Microcontrollers by Richard E. Haskell Prentice Hall, 2000, ISBN 0-13-083208-1 ¡@
TABLE OF CONTENTS
Lesson 1 INTRODUCING FORTH 1.1 INTRODUCING FORTH ............................................................................ 1-2 1.2 FORTH ARITHMETIC ............................................................................... 1-3 1.3 FORTH ARITHMETIC OPERATORS ........................................................ 1-4 1.4 STACK MANIPULATION WORDS ........................................................... 1-5 1.5 MORE FORTH WORDS ............................................................................. 1-8 1.6 COLON DEFINITIONS .............................................................................. 1-10 1.7 EXERCISES ............................................................................................... 1-12
Lesson 2 USING F-PC 2.1 USING SED TO EDIT FILES ..................................................................... 2-2 2.2 LOADING AND RUNNING YOUR PROGRAMS ..................................... 2-3 2.3 DEBUGGING YOUR PROGRAMS ........................................................... 2-4 2.4 EXERCISES ............................................................................................... 2-6
Lesson 3 HOW FORTH WORKS 3.1 VARIABLES ............................................................................................. 3-2 3.2 MORE ABOUT VARIABLES -- FETCH AND STORE ............................. 3-3 3.3 CONSTANTS ............................................................................................ 3-5 3.4 THE FORTH COLON WORD : 3-6 3.5 ARRAYS .................................................................................................. 3-7 3.6 THE RETURN STACK.............................................................................. 3-8 3.7 CODE WORDS ......................................................................................... 3-9 3.8 THE FORTH DICTIONARY .................................................................... 3-10 3.9 TABLES ................................................................................................... 3-11 3.10 CHARACTER OR BYTE DATA ............................................................. 3-12 3.11 FINDING DICTIONARY ADDRESSES .................................................. 3-13 3.12 THE NAME FIELD .................................................................................. 3-14 3.13 OPERATION OF THE F-PC INNER INTERPRETER ............................... 3-15 3.14 EXERCISES .............................................................................................. 3-17
Lesson 4 FORTH DECISIONS 4.1 BRANCHING INSTRUCTIONS AND LOOPS ........................................... 4-2 4.2 CONDITIONAL WORDS ............................................................................ 4-3 4.3 FORTH LOGICAL OPERATORS ................................................................ 4-4 4.4 THE IF STATEMENT ................................................................................. 4-5 4.5 THE DO LOOP .......................................................................................... 4-6 4.6 THE UNTIL LOOP ..................................................................................... 4-10 4.7 THE WHILE LOOP ...................................................................................... 4-11 4.8 EXERCISES ................................................................................................ 4-12
Lesson 5 NUMBERS 5.1 DOUBLE NUMBERS ................................................................................. 5-2 5.2 DOUBLE COMPARISON OPERATORS ..................................................... 5-4 5.3 MULTIPLICATION AND DIVISION .......................................................... 5-5 5.4 FLOORED DIVISION .................................................................................. 5-6 5.5 16-BIT OPERATORS.................................................................................... 5-9 5.6 DOUBLE NUMBER MULTIPLICATION .................................................... 5-11 5.7 EXERCISES .................................................................................................. 5-12
Lesson 6 STRINGS 6.1 STRING INPUT ............................................................................................ 6-2 6.2 ASCII - BINARY CONVERSIONS ............................................................... 6-4 6.3 NUMBER OUTPUT CONVERSIONS ........................................................... 6-5 6.4 SCREEN OUTPUT ........................................................................................ 6-7
Lesson 7 CODE WORDS AND DOS I/O 7.1 CODE WORDS ............................................................................................ 7-2 7.2 CODE CONDITIONALS ............................................................................... 7-5 7.3 LONG MEMORY WORDS ........................................................................... 7-6 7.4 DOS WORDS ................................................................................................ 7-7 7.5 BASIC FILE I/O ............................................................................................. 7-9 7.6 READING NUMBERS AND STRINGS ......................................................... 7-14 7.7 WRITING NUMBERS AND STRINGS ......................................................... 7-20
Lesson 8 DEFINING WORDS 8.1 CREATE...DOES> ........................................................................................ 8-2 8.2 A SIMPLE JUMP TABLE .............................................................................. 8-4 8.3 JUMP TABLE WITH ARBITRARY STACK VALUES ................................. 8-6 8.4 JUMP TABLE WITH FORTH WORDS ......................................................... 8-8 8.5 POP-UP MENUS .......................................................................................... 8-10 8.6 EXERCISES .................................................................................................. 8-18
Lesson 9 COMPILER WORDS 9.1 COMPILING VS. INTERPRETING ............................................................ 9-2 9.2 COMPILE AND [COMPILE] ....................................................................... 9-5 9.3 LITERALS ................................................................................................... 9-6 9.4 CONDITIONAL COMPILER WORDS ........................................................ 9-8 BEGIN...WHILE...REPEAT ............................................................................ 9-9 IF...ELSE...THEN ........................................................................................... 9-10 BEGIN...AGAIN ............................................................................................. 9-11 BEGIN...UNTIL ............................................................................................ 9-12 DO...LOOP ..................................................................................................... 9-13 9.5 EXERCISES ................................................................................................. 9-14
Lesson 10 FORTH DATA STRUCTURES 10.1 ARRAYS ..................................................................................................... 10-2 10.2 LINKED LISTS ........................................................................................... 10-5 10.3 RECORDS ................................................................................................... 10-13
Lesson 11 TERMINAL PROGRAM USING INTERRUPTS 11.1 8086/8088 INTERRUPTS ......................................................................... 11-2 11.2 THE 8250 ACE CHIP .............................................................................. 11-3 11.3 A QUEUE DATA STRUCTURE ................................................................ 11-5 11.4 SENDING CHARACTERS TO THE SCREEN AND/OR TO DISK ...................................................................................... 11-8 11.5 DOWNLOADING FILES ........................................................................... 11-10 11.6 MAIN TERMINAL PROGRAM ................................................................... 11-12 ¡@ ¡@ |