ebook img

FORTH theory & practice PDF

144 Pages·1982·5.32 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview FORTH theory & practice

FORTH Theory & Practice Richard DeGrandis-Harrison yfCORNSSFT Copyright © Acornsoft Limited 1982 All Rights Reserved No part of this book may be reproduced by any means without the prior permission of the copyright holder. The only exceptions are as provided for by the Copyright (photocopying) Act or for the purposes of review or in order for the software herein to be entered into a computer for the sole use of the owner of this book. SECOND EDITION ISBN 0 907876 05 6 Published by: Acornsoft Limited 4a Market Hill Cambridge CB2 3NJ England ATOM FORTH on cassette and System FORTH on disc are available from Acornsoft. FORTH THEORY AND PRACTICE CONTENTS 1 About this Manual 1 2 About FORTH 3 3 Starting FORTH 7 4 Stacks of Arithmetic 9 5 FORTH Definitions 19 6 Conditionals and Loops 35 7 The Ins and Outs of FORTH 45 8 Tape Interface and Editor 55 9 Graphics 65 10 What <BUILDS and DOES> Does 69 11 Further Examples 77 12 Error Messages 89 Glossary of FORTH Words 91 Appendix A Two 1s-Complement Arithmetic 117 Appendix B System FORTH 119 Appendix C Dictionary Entry Structure 121 Appendix D Memory Allocation 125 Appendix E Further Reading 129 Index 131 To Annette Acknowledgments This version of FORTH is based on the public domain publications provided through the courtesy of the FORTH Interest Group, P.0. Box 1105, San Carlos, CA. 94070 The greatest common divisor routine of Section 6.4 is by R.L. Smith. The random number routine of Section 8.2.2 is by J.E. Rickenbacker. These routines appeared in FORTH DIMENSIONS Vol.2, pages 167 and 34 respectively. The factorial calculations of Section 11.2.1 were provided by David Johnson-Davies of Acornsoft. I am grateful for much helpful advice, particularly on the Acorn operating systems, given by David Johnson-Davies, and Joe Dunn of Acorn Computers. I also owe a special debt of gratitude to Chris Jordan of Acornsoft who has had the unenviable task of ensuring that this manual makes sense. He has made many improvements to the text and has, I think, done an excellent job. 1 About this Manual Although this manual is written to explain the operation of FORTH for the Acorn ATOM, most of the contents are applicable to any version of the FORTH language. The implementation, like most others for home computer use, is based on the model produced by the FORTH Interest Group. Individual addresses of registers, subroutines etc. and the details of the memory map will, of course, be different on different machines, and the machine code sections are necessarily concerned with the 6502 microprocessor. In the descriptions of FORTH code, only the tape interface (Chapter 8) and the graphics (Chapter 9) are likely to be significantly different in other versions. In this manual all FORTH words are written in upper-case letters, exactly as they are typed in and appear on the display. Since FORTH may use any character that can be typed on the keyboard, there may occasionally be confusion between a FORTH word and punctuation marks. In any cases where such confusion may arise, FORTH words are placed in angle brackets, e.g. <.> , <,> and <."> . In examples which contain text, both typed at the keyboard and produced by the computer, the underlined sections represent the computer's output, for example: 2 3 + . 5 OK All keyboard input must be terminated by pressing the RETURN key, and this will not normally be shown explicitly. All numbers appearing in the text will, unless otherwise stated, be given in decimal base. 1 2 2 About FORTH FORTH was invented around 1969 by Charles H. Moore. It was originally created as a convenient means of controlling equipment by computer. Most high level languages that can be used on mini and micro computers (e.g. BASIC) are too slow for such control and the only other alternative is to use machine code routines. These, however, are very tedious to write and enter. FORTH solves many of these problems by allowing fast-executing programs to be written in a high-level language. It also has the very great advantage on small systems of using very little memory for program storage. One further advantage, which will become more apparent as you use the language, is that FORTH encourages the writing of well-structured programs. The speed of FORTH is largely due to the fact that it is a compiled language, so that the stored program is in a form very close to machine code. Unlike most other compiled languages, however, FORTH is interactive, which means that each new word can be tested as soon as it has been entered. If it does not do what you want it can be changed immediately until you are satisfied. Perhaps the most powerful feature of FORTH is that it is an extensible language. When you define a new word in FORTH, it becomes an integral part of the language and can be used to produce further definitions, in exactly the same way as the words resident in the basic system. This allows the production of short, neat solutions to complex problems. You may be beginning to realise, from what has been said so far, that writing programs in FORTH is very different from writing in languages like BASIC. A FORTH program consists of a series of definitions of actions, each represented by a ’word1. These words are then combined in further definitions until the required action of the whole program is represented by a single word. The program can then be executed by typing this single word at the keyboard. The procedure for writing a program in FORTH begins with a specification of the overall action of the program. This is then broken down into a sequence of small tasks and these, if necessary, are further divided into simpler tasks. Eventually the tasks are reduced to the point where each is very easy to write in FORTH code. The program is then written, starting with these simple routines and building back up to the full program. Testing can be carried out at each stage, greatly reducing the chance of errors in the final program. As an example we can consider the task of controlling a domestic washing machine. The whole program might be represented by the word WASHING which could be defined as: : WASHING WASH RINSE DRY ; 3 The words WASH, RINSE and DRY could themselves be defined as: : WASH FILL HEAT SOAP AGITATE SPIN ; : RINSE FILL AGITATE SPIN ; : DRY SPIN SPIN ; At the next lower level the words FILL and HEAT, for example, could be written: : FILL TAP ON BEGIN ?FULL UNTIL TAP OFF ; and : HEAT HEATER ON BEGIN PTEMPERATURE UNTIL HEATER OFF ; Coding could then begin with the definitions of the actions of the words TAP, HEATER, ON, OFF, ?FULL etc. The action of each word would be checked, with a simulation of the machinery and sensors of the washing machine, until the program is completed by the definition of WASHING. This example illustrates that, in FORTH, the problem to be solved at any stage is simple and well-defined. Note also that many of the words appear several times; once a word is defined it may be used in a number of different situations, greatly easing the programming load. FORTH is an example of threaded code. The words in a FORTH program can be imagined to be strung together like beads on a thread. From one word the thread loops to pass through all the words in its definition and, if necessary, further loops include the words of lower level definitions. Ultimately the thread returns to the highest level word of the sequence. FORTH is actually implemented as indirect threaded code, where each 'bead1 on the thread is not the routine itself but the address of the routine. In the dictionary, therefore, each word consists of a list of the addresses of the words out of which it is built. So far there have been many references to 'words' in FORTH, so it is about time to define what can be used as a FORTH word. A word is defined as any combination of characters, separated by one or more spaces from another word. Any character that can be typed on the keyboard, including non-printing characters and control codes, is allowed. The only characters that can not be used in a FORTH word are a space, which is reserved as a delimiter to separate successive words, and a null (ASCII zero), which is used to mark the end of input text. In ATOM FORTH a word may be of any length up to a maximum of 31 characters. 4 The following are examples of valid words: FORTH it +1 EMPTY-BUFFERS The following are not valid: EMPTY BUFFERS (includes a space) THIS-WORD-CAN1T-BE-USED-IN-FORTH (32 characters) 6

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.