home *** CD-ROM | disk | FTP | other *** search
- ╔════════════════════════════════════════════════════╗
- ║ Lesson 2 Part 030 F-PC 3.5 Tutorial by Jack Brown ║
- ╚════════════════════════════════════════════════════╝
-
- By now you should be getting used to Forth's parameter stack. What have
- you been doing when you have made a mistake and ended up with a lot of
- numbers on the parameter stack that you would like to remove? Perhaps
- you entered:
-
- 11 22 33 DUP ROT DUP ROT <enter> ok
-
- and now you have 5 numbers on the stack (what are they?). How do you
- clear the stack so that it is empty (no numbers on the stack)?
-
- Method 1: . . . . . <enter> ( just print till stack is empty)
-
- Method 2: CLEAR <enter> CLEAR <- What?
-
- That's because there is no word CLEAR! but notice that it did clear the
- parameter stack. We could clear the parameter stack by entering any
- Forth word that has not already been defined. This is not a very
- sophisticated solution but it works. You might like method 3 better.
-
- Method 3: Make a word to clear the stack. Here is the definition.
-
- : CLEAR ( ?? -- "empty stack" ) \ Clear the parameter stack
- DEPTH 0 ?DO DROP LOOP ; ok
-
- Sample of its use:
- 11 22 33 .S [3] 11 22 33 ok
- CLEAR .S Stack Empty. ok
-
- ╓──────────────╖
- ║ Problem 2.2 ║
- ╙──────────────╜
- Why did we use ?DO in the definition of CLEAR instead of DO ?
- If you don't know the answer here is another question. What happens if
- both the loop limit and initial value for a DO loop are identical?
-
- Try this: : TEST1 0 0 DO I . LOOP ; TEST1 <enter>
- Now try this: : TEST2 0 0 ?DO I . LOOP ; TEST2 <enter>
-
- You should now have the answer to the first question asked.
-
- Now use Editor to enter the following Counting program.
- NEWFILE COUNTING <enter>
-
- \ Loop Demo program that counts from 0 up to n.
- : COUNT_UP ( n -- ) 0 ?DO CR I . LOOP ;
-
- ╓──────────────╖
- ║ Problem 2.3 ║
- ╙──────────────╜
- 1) Why does the ?DO have only one stack input?
- 2) What is the function of the word CR ?
- 3) What is the function of the word I ?
- 4) Why does the program count up?
- 5) What happens if COUNT_UP is executed without a stack input?
- 6) What would happen if the " . " was accidentally left out and
- COUNT_UP was tested with 100 as the stack input?
- 7) How high does the program count?
-
- ┌─────────────────────────────────────┐
- │ Please move to Lesson 2 Part 035 │
- └─────────────────────────────────────┘
-
-
-
-