21 February 1994. Greetings to Forthers, Would be Forthers and Should be Forthers everywhere. Updated Information on Real Time Forth Version 5. The zipped files RTF5PPCL.ZIP and RTF5PPS.ZIP each contains the text of the book and a small number of example files. They differ only in the format within the file. RTF5PPCL.ZIP contains the book in PCL format ready to feed to a HP Laserjet 2 (or higher) or equivalent. RTF5PPS.ZIP contains the book in Post Script format. Neither contains the book in simple ASCII format as it uses special fonts, diagrams etc. In both files the book is presented in four parts, each of less than 100 pages. This is done so that you can print it on printers that do not handle running out of paper gracefully. To print to an HP laserjet 2 or higher or clone, copy each file to your printer with a command line such as: COPY RTFV5A.PCL /B LPT1 to send it to a Laserjet on LPT1 (add a path to the front of the file name if needed and adapt as needed if your printer is on another port). Note the switch /B, without it you won't get too much useable!!!! To print on a Post Script printer just follow your normal procedure. Please note I do not have a Post Script printer, and while I have ghost scripted one section to check, I hope all prints OK. I work in PCL, this version was produced by printing the document to a file through a generic post script printer driver! Then sit back and let the printer print about 250 pages in all! I have used small font size to keep the number of pages as low as practicable but you are still going to be waiting for a while. The book is formatted for A4 paper, which is hard to get in the US. It is however laid out so that it will print on 8.5" by 11" paper too. If your printer asks you to load A4 paper and you want to use US paper, lie and tell it you have loaded A4 when you really have loaded 8.5" by 11". If the printer accepts your lie, it will print fine. If your printer is a very smart PCL printer (like an HP4M) and it knows that you have not loaded what it asked for, you can edit the file using a hex editor to replace the PCL escape sequence for A4 paper with one for US Letter paper. Do a global search and replace on the character sequence &l26a and change it to &l02a. Then you can print it. Thanks to Dave Marsh for providing this fix. Sorry I can't help anyone on how to fool a Post Script printer at all! If you don't have access to a Laserjet, or can't for some reason print such as large document, printed copies will be available at nominal cost from a number of sources in time. As I write this in February 1994, Mountain View Press Box 429, Route 2, La Honda, CA 94020 USA is the only known one. I hope other sources, especially outside the USA, will also appear. The book is shareware but copyright to me, but you are welcome to use it for your own use (see note in the book about voluntary donations to my sanity restoration fund if you like it and what to do if you want to print off class copies). I have teaching materials to go with the book that I would be glad to supply to other educators teaching Forth, just contact me. To help you decide if you want to print it, the contents pages of the book follow. Tim Hendtlass Physics Department, Swinburne University of Technology, PO Box 218, Hawthorne, Victoria 3122, AUSTRALIA Phone +61 3 819 8863 Fax +61 3 819 0856 Email tim@brain.physics.swin.oz.au PREAMBLE 1 An explanatory note for people who know another computer language 3 CHAPTER 1: An overview of Forth 5 The Stack 5 A first step 5 A few comments 6 Defining things to be done later. 7 CHAPTER 2: The data stack. 9 Keeping track of the stack. 9 Shuffling and cloning the stack 10 Standard Words from the Required Word Set. 10 Words from the double word set. 11 Non-Standard Words 12 Exercises on manipulating the stack. 12 CHAPTER 3: Arithmetic, Logic and Comparisons 15 A note about order. 15 A note about integer division. 15 Arithmetic. 16 Logic 17 Comparisons. 17 Exercises. 18 CHAPTER 4: Basic Control Structures 19 Standard Control Structures. 19 The IF THEN ELSE construct. 19 The DO loop. 20 Associated words. 20 BEGIN.. UNTIL and BEGIN..WHILE..REPEAT. 21 Non-standard control structures. 22 CASE 22 EXEC: 23 Exercises. 23 CHAPTER 5: Moving Data Around 25 Moving data between memory and the stack. 25 Moving data between the stack and the dictionary. 25 Moving data between the stack and the user. 26 Words that output numbers and text to the screen 26 Standard words that obtain input from the keyboard 27 Moving data between the outside world and the stack. 27 Words that provide direct access to input and output ports 28 Coordinating input and output. 28 Exercises. 31 CHAPTER 6: A first programming example - signal filtering 33 CHAPTER 7: Entering and Compiling your Program 37 Installing FPC 37 Starting FPC 37 Opening a file 37 Editing a file 38 Creating a new file 38 A handy hint 39 Loading and Testing 39 Inspecting the source of Forth words 40 Decompiling words 41 Listing the available Forth words 41 SED - The sequential editor 42 Overview 42 Key functions 42 Moving the cursor 43 Deleting characters 43 Copying and moving text 43 Searching for and replacing text 43 Miscellaneous 44 Expanded descriptions. 44 F6 andF8 and their variants 47 Control key template 48 Keypad Template 48 Function Key Template 49 CHAPTER 8 It didn't work - now what? 51 The Debugger 51 SEEing into Forth definitions 53 CHAPTER 9: Basic number and text handling 55 Numeric conversion. 55 Setting the number base. 55 Converting a number into ASCII. 55 Converting ASCII into a number. 56 Moving Strings Around. 57 Text Output and Input. 57 Text Output 57 Text Input 58 CHAPTER 10: Maths - who needs it? 59 Single precision integer arithmetic. 60 Double precision integer arithmetic. 61 32 bit fixed point arithmetic. 64 32 bit floating point arithmetic. 66 Forth or Assembly code? 67 48 bit floating point arithmetic, SFLOAT 68 Control and Defining Words 68 Stack Words 69 Maths Words 69 Logical Test Words 70 Predefined Numbers 70 Words that Input and Output from the Floating Point Stack 70 Relative Performance. 71 CHAPTER 11: Deferred words 75 The difference between ' and ['] 76 CHAPTER 12: A conundrum of ciphers 79 Why ciphers? 79 A digression into ciphers. 79 Implementing a cipher with arithmetic. 81 Implementing a cipher with an array. 82 Implementing a cipher with a new defining word. 84 CHAPTER 13: The DOS interface and file handling 89 The interface to DOS 89 Making DOS system calls. 89 Interfacing to DOS commands. 89 Manipulating Files in FPC 90 Working directly with files. 90 Creating and clearing a handle. 91 Words for using a file described by a handle. 91 Working within a file 93 Manipulating the handle stack 93 Handle Fields 93 An example of LINEREAD usage 93 Block words - present but not used by FPC 94 CHAPTER 14: Vocabularies 95 The vocabulary order control words. 96 Formatted printing using vocabularies. 97 Exercises. 104 CHAPTER 15: CREATE, DOES> and a glimpse inside 107 More on definitions. 107 Producing a defining word. 108 A 1-of list of clauses. 109 An Example of a 1-of list of clauses 112 ;CODE and ;USES 112 Problem 113 CHAPTER 16: Multi-tasking 115 A list of multi-tasking control words 115 An example of multi-tasking. 116 Exercise. 119 CHAPTER 17: Timing 121 TIMER - measuring the execution time. 121 DOWN-COUNTER - making it happen at the right time. 122 An example 123 CHAPTER 18: PASM, the FPC assembler 125 Prefix or postfix? 125 PASM glossary 126 Syntax comparison 127 Addressing modes 128 Register Mode 128 Immediate Mode 128 Direct Mode 129 Index Mode 129 Implied Mode and Segment Override 129 Macros in PASM 130 Local labels 130 Inline code 131 CHAPTER 19: Mixing Forth with assembly language 133 Assembly code in a Forth colon definition. 133 An example using INLINE and END-INLINE 134 Forth code in an assembly definition. 135 >H, H>, HDOES and HRET in detail 136 CHAPTER 20: Interrupts and Forth 139 Forth and ISRs written entirely in assembler. 141 An interrupt driven counter. 144 Writing ISRs in Forth rather than assembler. 146 An example of a high level ISR. 149 Lean, mean, interruptable interrupts and DOS. 150 Extra Information for IBM PC Users. 151 Interrupt Exercises. 152 CHAPTER 21: Input output, revisited 154 Synchronised slow data transfer. 154 Synchronised fast data transfer. 155 CHAPTER 22: Interfacing with basic PC input/output resources 159 Interfacing to the parallel (printer) port. 159 Interfacing to the serial ports. 161 An example. 164 Moving data very fast - direct memory access. 166 CHAPTER 23: An example with the lot to go 169 The internal design. 169 A few points of detail. 170 The listing. 171 CHAPTER 24: Turnkey, Meta and Target Compiling 175 Why do any of the above? 175 Making a turnkey program. 176 Meta Compiling 176 Target Compiling. 181 Comparative performance. 183 APPENDIX 1: The internal organization of FPC 185 Header Space 185 List Space 186 Code Space 187 APPENDIX 2: Answers to problems 188 APPENDIX 3: An ASCII list of useful Forth words 190 Notation. 190 The list. 190 APPENDIX 4: Forth Words sorted by function 221 Symbol definitions used in this appendix 221 Category Titles 222 APPENDIX 5: A starter set of words 233