home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / tutor / l3p020 < prev    next >
Encoding:
Text File  |  1990-07-15  |  4.8 KB  |  114 lines

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 3 Part 020  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5. We begin with a couple of problems.  If you really want to learn Forth
  6. one of the best ways is to study the programs of others and make
  7. modifications to them.  Here are two suggestions with regards to the
  8. program that prints double numbers.
  9.  
  10. ╓──────────────╖
  11. ║ Problem 3.1  ║
  12. ╙──────────────╜
  13. Modify  .SD to become .SS which prints ALL single numbers on the stack.
  14. ( F-PC's .S only prints the count and the top four items on the stack.
  15. Hints:  We have not looked at IF...ELSE...THEN yet but no matter you
  16. need only make three changes:
  17.   i)   delete one line(s) that add the trailing decimal point.
  18.   ii)  change the line with the D.   to  .
  19.   iii) change the line with 2 +LOOP  to  LOOP
  20.  
  21. ╓──────────────╖
  22. ║ Problem 3.2  ║
  23. ╙──────────────╜
  24. Make a new version of .SD that only prints the stack as double numbers
  25. if the stack contains an even number of items, if the stack contains an
  26. odd number of items then it should just use your modified version from
  27. Problem 3.1 above.
  28. Hints:  This is a lot harder...  You may want to wait till the end of
  29. this lessons at which time we will have discussed IF...ELSE...THEN but
  30. we suspect you may have already read about them in Starting Forth so give
  31. it a try if you like.
  32.  
  33.                   ┌────────────────────┐
  34.                   │   Stack Notation.  │
  35.                   └────────────────────┘
  36.  
  37. When constructing stack pictures signed double numbers are prefixed with
  38. "d" and unsigned double numbers are prefixed with "ud". Thus.. we could
  39. thus use d1 d2 d3  or da db dc  for signed double numbers and ud1 ud2
  40. ud3 or uda udb udc  for unsigned double numbers.
  41.  
  42. Here is a list of the commonly used single and double stack operators.
  43.   DROP  ( n  -- )         Drop top number on data stack.
  44.   SWAP  ( n m --  m n )   Swap top two numbers on data stack.
  45.   DUP   ( n --  n n )     Duplicate top number on data stack.
  46.   OVER  ( n m -- n m n )  Make copy of second item to top of stack
  47.   ROT   ( a b c -- b c a) Rotate third item to the top of stack.
  48.  -ROT   ( a b c -- c a b) Rotate in opposite direction.
  49.   PICK  ( ? n -- ? nth)   Copy nth item to top of stack (0 based)
  50.   ROLL  ( ? n -- ? nth)   Rotate nth item to top (0 based).
  51.   NIP   ( n m -- m )      Discard second item on data stack.
  52.   TUCK  ( n m -- m n m)   Push copy of top under second item.
  53.   3DUP  ( a b c -- a b c a b c)  Make copy of top 3 items.
  54.   2DROP ( dn -- )           Drop double number from top.
  55.      or ( a b -- )
  56.   2SWAP ( dn dm -- dm dn)   Swap top two double numbers.
  57.      or ( a b c d -- c d a b)
  58.   2DUP  ( dn -- dn dn)      Make another copy of top double number.
  59.      or ( a b -- a b a b )
  60.   2OVER ( dn dm -- dn dm dn) Copy second double number to top.
  61.      or ( a b c d -- a b c d a b )
  62.  
  63. ╓──────────────╖
  64. ║ Problem 3.3  ║
  65. ╙──────────────╜
  66. Write high level Forth definitions for the following words.
  67. a) 2SWAP   ( hint: use  ROLL twice )
  68. b) 2DROP
  69. c) 2DUP    ( hint: use PICK twice )
  70. d) 2OVER   ( hint: use PICK twice )
  71. e) 2NIP    ( d1 d2 -- d2)
  72. f) 2TUCK   ( d1 d2 -- d2 d1 d2 )
  73. g) 2ROT    ( d1 d2 d3 -- d2 d3 d1 )
  74. h) -2ROT   ( d1 d2 d3 -- d3 d1 d2 )
  75. Use your .SD word to verify the correct operation of your words.
  76.  
  77. ╓──────────────╖
  78. ║ Problem 3.4  ║
  79. ╙──────────────╜
  80. ( Floored Division Review )
  81. Floored symmetric division.  Note that q and r must satisfy
  82. the equations:   m/n  = q  +  r/n    or  m = nq + r
  83.   /     ( m n   q )     Leave q , the floor of real quotient.
  84.   MOD   ( m n   r )     Leave r , remainder (satisfying above).
  85.   /MOD  ( m n   r q )   Leave remainder r and quotient q .
  86. Complete the following table WITHOUT using your computer!
  87.         m    n      r     q       Check:  n * q   +  r
  88.        13    5                            5 *
  89.       -11    5                            5 *
  90.        -2    5                            5 *
  91.        13   -5                           -5 *
  92.       -11   -5                           -5 *
  93.        -2   -5                           -5 *
  94.  
  95. ╓──────────────╖
  96. ║ Problem 3.5  ║
  97. ╙──────────────╜
  98. ( Easy words )
  99. Here are the stack comments for some of the easy words
  100. of Lesson 2 Part 130
  101.   1+    ( n -- n+1 )     Increment top stack item by 1.
  102.   2+    ( n -- n+2 )     Increment top stack item by 2.
  103.   1-    ( n -- n-1 )     Decrement top stack item by 1.
  104.   2-    ( n -- n-2 )     Decrement top stack item by 2.
  105.   2*    ( n -- 2n  )     Multiply  top stack item by 2.
  106.   2/    ( n -- n/2 )     Divide    top stack item by 2.
  107.  
  108. Write high level Forth definitions of these words and their double
  109. number analogs D1+ through D2/
  110.  
  111. ┌─────────────────────────────────────┐
  112. │  Please move to Lesson 3 Part 030   │
  113. └─────────────────────────────────────┘
  114.