home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / tutor / l6p020 < prev    next >
Text File  |  1990-07-15  |  5KB  |  110 lines

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 6 Part 020  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.         ┌───────────────────────────────────────────────┐
  6.         │   Introduction to Forth's String Operators.   │
  7.         └───────────────────────────────────────────────┘
  8.  
  9.  
  10. A counted string in memory is   |05|48|45|4C|4C|4F|   <-hex
  11. preceded by character count.    |05| H| E| L| L| O|
  12.  
  13. Compile a counted {text} string into dictionary.
  14. ," {text}"  ( -- )  USE OUTSIDE DEFINITION ONLY!!!
  15.  
  16. To access a string which we compile into the dictionary we must lay down
  17. a dictionary header first using  CREATE as follows:
  18.  
  19. CREATE NAME$ ," George Smith" <enter>  ok
  20. HEX NAME$ U. <enter> 7508 ok    <---  code segment offset of string.
  21. NAME$ C@ . <enter>  C ok        <---  hex value of string count.
  22. NAME$ 16 DUMP <enter>     <---  I am using a different version of DUMP
  23.       08 09 0A 0B 0C 0D 0E 0F  10 11 12 13 14 15 16 17  89ABCDEF01234567
  24. 7508  0C 47 65 6F 72 67 65 20  53 6D 69 74 68 E9 41 8D   George SmithiA
  25.  
  26. The useful Forth word COUNT takes an address,  addr, as the stack input
  27. and provides addr+1 and the byte stored at addr as stack outputs. This
  28. makes COUNT useful for fetching the length of a string and pointing to
  29. the first character in the sting. COUNT can also be used to fetch
  30. successive characters of a string.
  31.  
  32. \ COUNT ( addr -- addr+1 n)  Fetch count at addr and increment addr.
  33. \ High level definition of COUNT , in F-PC  COUNT is a CODE  definition.
  34. : COUNT ( addr -- addr+1 n)
  35.          DUP 1+ SWAP C@ ;
  36.  
  37. Try using COUNT with our string NAME$ as shown below.
  38. NAME$ COUNT .S  <enter> [2]   7509       C  ok
  39. . <enter> C  ok
  40. COUNT EMIT <enter> G ok
  41. COUNT EMIT <enter> e ok
  42. COUNT EMIT <enter> o ok
  43. COUNT EMIT <enter> r ok
  44. COUNT EMIT <enter> g ok
  45. COUNT EMIT <enter> e ok
  46.  
  47. The first application of COUNT could be used to fetch the number of
  48. characters in a string.  If the character count was then used to control
  49. a loop which repeatedly executed the phrase  COUNT EMIT  we would have a
  50. string display word!  This is exactly how the Forth word TYPE would be
  51. defined.  Try the definition below, it is not the same as the one in
  52. F-PC which is highly optimized but it works just fine.
  53.  
  54. \ Given address addr and character count n type the string.
  55. \ TYPE  ( addr n -- )     Type n characters of string at addr.
  56. \ High level definition of TYPE ,
  57. : TYPE
  58.       0 ?DO  COUNT EMIT LOOP DROP ;
  59.  
  60. \ Below is an example of executing TYPE ...
  61. NAME$ COUNT TYPE <enter> George Smith ok
  62.  
  63. ╓─────────────╖
  64. ║ Problem 6.1 ║
  65. ╙─────────────╜
  66. A common mistake that made by the Forth novice is to execute TYPE with
  67. invalid stack inputs.  If we accidentally executed  NAME$ TYPE  describe
  68. what would happen.  What would TYPE use for an address? What would TYPE
  69. use for the character count?  All Forth strings have a one byte
  70. character count which means that the longest string possible is 255
  71. characters.  One of Forths strengths is that you can add your own
  72. compiler and run time protection.  Write a new version of TYPE called
  73. SAFE-TYPE that Aborts and provides and error message if an attempt is
  74. made to execute it with a character count which is greater that 255.
  75. You might like to investigate the use of the Forth word  ABORT" and use
  76. it in your definition.
  77.  
  78. ╓──────────────╖
  79. ║ Problem 6.2  ║
  80. ╙──────────────╜
  81. VIEW F-PC's definition of TYPE and suggest at least 3 reasons for
  82. its increased complexity.
  83.  
  84. To compile a counted string within a Forth word definition we simply
  85. enclose the text string, {text} in double quotes as shown below
  86.  
  87. : NOTMYNAME  ( -- addr count ) " George Smith" ;
  88.  
  89. Later when NOTMYNAME is executed the address and count of the string
  90. will be left on the stack as shown in the stack picture below:
  91.  
  92.  " {text}" ( -- addr count )  ONLY USE WITHIN A WORD DEFINITION!
  93.  
  94. Compile a counted string into a word definition.  When word is later
  95. executed the address and count are returned. In F-PC a string defined
  96. within a Forth word definition will get compiled into the "list" segment
  97. This should be contrasted with the use of  CREATE STRING$ ," {text}"
  98. which compiles the string into the code segment.
  99.  
  100. ." {text}" can also be used within a Forth word definition. We used the
  101. ."  " word pair in Lesson 1.  The difference between  ." {text" and
  102. " {text}" is that  the latter returns the string address and count and
  103. must be followed by TYPE if you wish to display the string.  You would
  104. probably use " {text}" in a word that required some string processing
  105. before the display operation.
  106.  
  107. ┌───────────────────────────────────┐
  108. │  Please Move to Lesson 6 Part 030 │
  109. └───────────────────────────────────┘
  110.