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

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 2 Part 035  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5. ╓───────────────────────╖
  6. ║ Answer to problem 2.3 ║
  7. ╙───────────────────────╜
  8. 1) It is standard practice for Forth words to be passed some of their
  9. parameters on the stack by the user or the calling word.  In this case
  10. we would place then number to count up to on the stack like so:
  11.  
  12. 100 COUNT_UP <enter>
  13.  
  14. In this case the loop limit would be 100 and the initial loop value
  15. would be 0.  The loop limit is provided by the user or the calling word
  16. and the initial loop value is hard coded into the word definition.
  17.  
  18. 2) This is easy....  Start a new line!
  19.  
  20. 3) I is a Forth word that can be used within a DO ( or ?DO ) ... LOOP
  21.    to fetch the current value of the loop counter to the top of the
  22.    parameter stack.
  23.  
  24. 4) The program counts up because the Loop counter is incremented by 1
  25.    at the word LOOP, if the loop limit has been reached the execution
  26.    continues just after the word LOOP otherwise execution resumes at the
  27.    first word after the ?DO .
  28.  
  29. 5) If the stack was empty it would use whatever garbage was buried one
  30.    level above the top of the stack.  If you are unlucky this could be a
  31.    very large number like 50000 and you could be waiting for a long time
  32.    to recover from your mistake.
  33.  
  34. 6) Leaving out the . would result in all the numbers being left on the
  35.    parameter stack.  If you get enough numbers on the parameter stack
  36.    you will CRASH the Forth system.
  37.  
  38. 7) The program will only count up to n-1  ( not n ) . This is because
  39.    the check for loop completion is done at the word LOOP after the loop
  40.    counter is incremented.  If the loop limit has been reached the loop
  41.    will be terminated and will not be executed for the case I = n.
  42.  
  43. ╓─────────────╖
  44. ║ Problem 2.4 ║
  45. ╙─────────────╜
  46. a) Modify the COUNT_UP program so that it counts up to " n " .
  47.  
  48. b) Use HELP and VIEW to find out as much as you can about the word +LOOP
  49.    When you are finished try replacing the word LOOP by " 2 +LOOP"
  50.    What happens?
  51.  
  52. c) Make the word COUNT_UP_BY ( n inc -- )  that will count up to n by
  53.    inc. Note: This is not easy so try it and upload you solution!
  54.  
  55. d) Make words COUNT_DOWN and COUNT_DOWN_BY that work like the UP words
  56.    except that they count down from " n ". Carefully check you words
  57.    with various values of n and inc.  Upload your solutions with samples
  58.    of the output (capture it with a pop up editor like side-kick or use
  59.    the PD program called BACKSCRL.ARC available from BCFB.)
  60.  
  61.  
  62. ┌────────────────────────────────────┐
  63. │  Please move to Lesson 2 Part 040  │
  64. └────────────────────────────────────┘
  65.