home *** CD-ROM | disk | FTP | other *** search
- ╔════════════════════════════════════════════════════╗
- ║ Lesson 6 Part 040 F-PC 3.5 Tutorial by Jack Brown ║
- ╚════════════════════════════════════════════════════╝
-
- ┌──────────────────────────────┐
- │ Moving Strings in memory. │
- └──────────────────────────────┘
-
- CMOVE n bytes from memory at addr-from to memory at addr-to. Left-most
- or low memory bytes are moved first. ( ie Move starts at beginning of
- string.)
-
- CMOVE ( addr-from addr-to n -- )
-
- If strings overlap only use CMOVE when addr-from > addr-to, which means
- you are moving the string down to lower memory. Overlapping string
- example below:
-
- CREATE DIGITS ," 0123456789**********" <enter> ok
- DIGITS COUNT TYPE <enter> 0123456789********** ok
- \ Shift 123456789* down one byte over laying the 0
- DIGITS 2+ DIGITS 1+ 10 CMOVE <enter> ok
- DIGITS COUNT TYPE <enter> 123456789*********** ok
-
- The above overlapping string move is successful.
-
- CREATE DIGITS1 ," 0123456789**********" <enter> ok
- DIGITS1 COUNT TYPE <enter> 0123456789********** ok
- \ Shift 0123456789 up one byte over laying the 1
- DIGITS1 1+ DIGITS 2+ 10 CMOVE <enter> ok
- DIGITS1 COUNT TYPE <enter> 00000000000********* ok
-
- The above overlapping string move fails because addr-from < addr-to and
- CMOVE starts moving bytes from low memory. In this case the solution is
- to use CMOVE>
-
- CMOVE> n bytes from addr-from to addr-to. Right-most or high memory
- bytes are moved first. ( ie Move starts at end or top of string.)
-
- CMOVE> ( addr-from addr-to n -- )
-
- If strings overlap use CMOVE> when addr-from < addr-to then use CMOVE>
- as the string is being moved up to higher memory. Overlapping string
- example below.
-
- CREATE LETTERS ," ABCDEFGHIJ**********" <enter> ok
- LETTERS COUNT TYPE <enter> ABCDEFGHIJ********** ok
- \ Shift string ABCDEFGHIGJ to higher memory overlaying B
- LETTERS 1+ LETTERS 2+ 10 CMOVE> <enter> ok
- LETTERS COUNT TYPE <enter> AABCDEFGHIJ********* ok
-
- The above move is successful
-
- CREATE LETTERS ," ABCDEFGHIJ**********" ok
- LETTERS COUNT TYPE ABCDEFGHIJ********** ok
- \ Shift string BCDEFGHIGJ* to lower memory overlaying J
- LETTERS 2+ LETTERS 1+ 10 CMOVE> ok
- LETTERS COUNT TYPE ******************** ok
-
- The above move is unsuccessful CMOVE should have been used.
-
- If you find all the above shifting an moving confusing you could use
- Forth's smart MOVE which checks for the overlapping case and makes the
- correct application of CMOVE or CMOVE> This decision is made at run
- time so if the strings don't overlap or you can think like a computer
- your programs will run faster using CMOVE and CMOVE>
-
- \ MOVE ( addr-from addr-to n -- )
- \ The high level definition of MOVE is given below.
- : MOVE ( addr-from addr-to n -- )
- -ROT 2DUP U<
- IF ROT CMOVE>
- ELSE ROT CMOVE THEN ;
-
- ╓─────────────╖
- ║ Problem 6.5 ║
- ╙─────────────╜
- Verify that the above definition of MOVE correctly checks for overlap
- and makes the correct application of CMOVE or CMOVE> .
- Write high level Forth definitions of the words CMOVE and CMOVE> using
- a) The DO .... LOOP programming structure.
- b) The BEGIN .... UNTIL programming structure.
- Which versions execute fastest?
-
- ┌──────────────────┐
- │ Packing Strings │
- └──────────────────┘
-
- Forth's standard counted string representation is called a packed
- string. The word EXPECT which we used for string input does not provide
- us with a counted string directly. In a number of the examples we worked
- around this by providing an extra byte for the string count at the
- beginning of the buffer provided to EXPECT. In many applications it is
- preferred that all input take place through one buffer and then have the
- application program move the inputted string to its final destination,
- storing it in Forth's standard packed format. The word CPACK presented
- in part 5 of this lesson will do the required job.
-
- ┌─────────────────────────────────────┐
- │ Please Move to lesson 6 Part 050 │
- └─────────────────────────────────────┘
-