home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l6p020
< prev
next >
Wrap
Text File
|
1990-07-15
|
5KB
|
110 lines
╔════════════════════════════════════════════════════╗
║ Lesson 6 Part 020 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
┌───────────────────────────────────────────────┐
│ Introduction to Forth's String Operators. │
└───────────────────────────────────────────────┘
A counted string in memory is |05|48|45|4C|4C|4F| <-hex
preceded by character count. |05| H| E| L| L| O|
Compile a counted {text} string into dictionary.
," {text}" ( -- ) USE OUTSIDE DEFINITION ONLY!!!
To access a string which we compile into the dictionary we must lay down
a dictionary header first using CREATE as follows:
CREATE NAME$ ," George Smith" <enter> ok
HEX NAME$ U. <enter> 7508 ok <--- code segment offset of string.
NAME$ C@ . <enter> C ok <--- hex value of string count.
NAME$ 16 DUMP <enter> <--- I am using a different version of DUMP
08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 89ABCDEF01234567
7508 0C 47 65 6F 72 67 65 20 53 6D 69 74 68 E9 41 8D George SmithiA
The useful Forth word COUNT takes an address, addr, as the stack input
and provides addr+1 and the byte stored at addr as stack outputs. This
makes COUNT useful for fetching the length of a string and pointing to
the first character in the sting. COUNT can also be used to fetch
successive characters of a string.
\ COUNT ( addr -- addr+1 n) Fetch count at addr and increment addr.
\ High level definition of COUNT , in F-PC COUNT is a CODE definition.
: COUNT ( addr -- addr+1 n)
DUP 1+ SWAP C@ ;
Try using COUNT with our string NAME$ as shown below.
NAME$ COUNT .S <enter> [2] 7509 C ok
. <enter> C ok
COUNT EMIT <enter> G ok
COUNT EMIT <enter> e ok
COUNT EMIT <enter> o ok
COUNT EMIT <enter> r ok
COUNT EMIT <enter> g ok
COUNT EMIT <enter> e ok
The first application of COUNT could be used to fetch the number of
characters in a string. If the character count was then used to control
a loop which repeatedly executed the phrase COUNT EMIT we would have a
string display word! This is exactly how the Forth word TYPE would be
defined. Try the definition below, it is not the same as the one in
F-PC which is highly optimized but it works just fine.
\ Given address addr and character count n type the string.
\ TYPE ( addr n -- ) Type n characters of string at addr.
\ High level definition of TYPE ,
: TYPE
0 ?DO COUNT EMIT LOOP DROP ;
\ Below is an example of executing TYPE ...
NAME$ COUNT TYPE <enter> George Smith ok
╓─────────────╖
║ Problem 6.1 ║
╙─────────────╜
A common mistake that made by the Forth novice is to execute TYPE with
invalid stack inputs. If we accidentally executed NAME$ TYPE describe
what would happen. What would TYPE use for an address? What would TYPE
use for the character count? All Forth strings have a one byte
character count which means that the longest string possible is 255
characters. One of Forths strengths is that you can add your own
compiler and run time protection. Write a new version of TYPE called
SAFE-TYPE that Aborts and provides and error message if an attempt is
made to execute it with a character count which is greater that 255.
You might like to investigate the use of the Forth word ABORT" and use
it in your definition.
╓──────────────╖
║ Problem 6.2 ║
╙──────────────╜
VIEW F-PC's definition of TYPE and suggest at least 3 reasons for
its increased complexity.
To compile a counted string within a Forth word definition we simply
enclose the text string, {text} in double quotes as shown below
: NOTMYNAME ( -- addr count ) " George Smith" ;
Later when NOTMYNAME is executed the address and count of the string
will be left on the stack as shown in the stack picture below:
" {text}" ( -- addr count ) ONLY USE WITHIN A WORD DEFINITION!
Compile a counted string into a word definition. When word is later
executed the address and count are returned. In F-PC a string defined
within a Forth word definition will get compiled into the "list" segment
This should be contrasted with the use of CREATE STRING$ ," {text}"
which compiles the string into the code segment.
." {text}" can also be used within a Forth word definition. We used the
." " word pair in Lesson 1. The difference between ." {text" and
" {text}" is that the latter returns the string address and count and
must be followed by TYPE if you wish to display the string. You would
probably use " {text}" in a word that required some string processing
before the display operation.
┌───────────────────────────────────┐
│ Please Move to Lesson 6 Part 030 │
└───────────────────────────────────┘