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

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 6 Part 040  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.                ┌──────────────────────────────┐
  6.                │   Moving Strings in memory.  │
  7.                └──────────────────────────────┘
  8.  
  9. CMOVE n bytes from memory at addr-from to memory at addr-to.  Left-most
  10. or low memory bytes are moved first. ( ie  Move starts at beginning of
  11. string.)
  12.  
  13. CMOVE    ( addr-from addr-to n -- )
  14.  
  15. If strings overlap only use CMOVE when addr-from > addr-to, which means
  16. you are moving the string down to lower memory.  Overlapping string
  17. example below:
  18.  
  19. CREATE DIGITS ," 0123456789**********" <enter> ok
  20. DIGITS COUNT TYPE <enter> 0123456789********** ok
  21. \ Shift 123456789*  down one byte over laying the 0
  22. DIGITS 2+ DIGITS 1+ 10 CMOVE  <enter> ok
  23. DIGITS COUNT TYPE <enter> 123456789*********** ok
  24.  
  25. The above overlapping string move is successful.
  26.  
  27. CREATE DIGITS1 ," 0123456789**********" <enter> ok
  28. DIGITS1 COUNT TYPE <enter> 0123456789********** ok
  29. \ Shift 0123456789 up one byte over laying the 1
  30. DIGITS1 1+ DIGITS 2+ 10 CMOVE  <enter> ok
  31. DIGITS1 COUNT TYPE <enter> 00000000000********* ok
  32.  
  33. The above overlapping string move fails because addr-from < addr-to and
  34. CMOVE starts moving bytes from low memory.  In this case the solution is
  35. to use CMOVE>
  36.  
  37. CMOVE> n bytes from addr-from to addr-to. Right-most or high memory
  38. bytes are moved first. ( ie Move starts at end or top of string.)
  39.  
  40. CMOVE>   ( addr-from addr-to n -- )
  41.  
  42. If strings overlap use CMOVE> when addr-from < addr-to then use CMOVE>
  43. as the string is being moved up to higher memory. Overlapping string
  44. example below.
  45.  
  46. CREATE LETTERS ," ABCDEFGHIJ**********" <enter> ok
  47. LETTERS COUNT TYPE <enter> ABCDEFGHIJ********** ok
  48. \ Shift string ABCDEFGHIGJ to higher memory overlaying B
  49. LETTERS 1+ LETTERS 2+ 10 CMOVE> <enter> ok
  50. LETTERS COUNT TYPE <enter> AABCDEFGHIJ********* ok
  51.  
  52. The above move is successful
  53.  
  54. CREATE LETTERS ," ABCDEFGHIJ**********"  ok
  55. LETTERS COUNT TYPE ABCDEFGHIJ********** ok
  56. \ Shift string BCDEFGHIGJ* to lower memory overlaying J
  57. LETTERS 2+ LETTERS 1+ 10 CMOVE>  ok
  58. LETTERS COUNT TYPE ******************** ok
  59.  
  60. The above move is unsuccessful CMOVE should have been used.
  61.  
  62. If you find all the above shifting an moving confusing you could use
  63. Forth's smart MOVE which checks for the overlapping case and makes the
  64. correct application of CMOVE or CMOVE>  This decision is made at run
  65. time so if the strings don't overlap or you can think like a computer
  66. your programs will run faster using CMOVE and CMOVE>
  67.  
  68. \ MOVE ( addr-from addr-to n -- )
  69. \ The high level definition of MOVE is given below.
  70. : MOVE   ( addr-from addr-to n -- )
  71.        -ROT 2DUP U<
  72.        IF    ROT CMOVE>
  73.        ELSE  ROT CMOVE  THEN  ;
  74.  
  75. ╓─────────────╖
  76. ║ Problem 6.5 ║
  77. ╙─────────────╜
  78. Verify that the above definition of MOVE correctly checks for overlap
  79. and makes the correct application of CMOVE or CMOVE> .
  80. Write high level Forth definitions of the words CMOVE and CMOVE> using
  81. a)  The  DO .... LOOP   programming structure.
  82. b)  The  BEGIN  .... UNTIL programming structure.
  83. Which versions execute fastest?
  84.  
  85.                   ┌──────────────────┐
  86.                   │  Packing Strings │
  87.                   └──────────────────┘
  88.  
  89. Forth's standard counted string representation is called a packed
  90. string. The word EXPECT which we used for string input does not provide
  91. us with a counted string directly. In a number of the examples we worked
  92. around this by providing an extra byte for the string count at the
  93. beginning of the buffer provided to EXPECT.  In many applications it is
  94. preferred that all input take place through one buffer and then have the
  95. application program move the inputted string to its final destination,
  96. storing it in Forth's standard packed format.  The word CPACK presented
  97. in part 5 of this lesson will do the required job.
  98.  
  99. ┌─────────────────────────────────────┐
  100. │   Please Move to lesson 6 Part 050  │
  101. └─────────────────────────────────────┘
  102.