home *** CD-ROM | disk | FTP | other *** search
- ╔════════════════════════════════════════════════════╗
- ║ Lesson 3 Part 020 F-PC 3.5 Tutorial by Jack Brown ║
- ╚════════════════════════════════════════════════════╝
-
- We begin with a couple of problems. If you really want to learn Forth
- one of the best ways is to study the programs of others and make
- modifications to them. Here are two suggestions with regards to the
- program that prints double numbers.
-
- ╓──────────────╖
- ║ Problem 3.1 ║
- ╙──────────────╜
- Modify .SD to become .SS which prints ALL single numbers on the stack.
- ( F-PC's .S only prints the count and the top four items on the stack.
- Hints: We have not looked at IF...ELSE...THEN yet but no matter you
- need only make three changes:
- i) delete one line(s) that add the trailing decimal point.
- ii) change the line with the D. to .
- iii) change the line with 2 +LOOP to LOOP
-
- ╓──────────────╖
- ║ Problem 3.2 ║
- ╙──────────────╜
- Make a new version of .SD that only prints the stack as double numbers
- if the stack contains an even number of items, if the stack contains an
- odd number of items then it should just use your modified version from
- Problem 3.1 above.
- Hints: This is a lot harder... You may want to wait till the end of
- this lessons at which time we will have discussed IF...ELSE...THEN but
- we suspect you may have already read about them in Starting Forth so give
- it a try if you like.
-
- ┌────────────────────┐
- │ Stack Notation. │
- └────────────────────┘
-
- When constructing stack pictures signed double numbers are prefixed with
- "d" and unsigned double numbers are prefixed with "ud". Thus.. we could
- thus use d1 d2 d3 or da db dc for signed double numbers and ud1 ud2
- ud3 or uda udb udc for unsigned double numbers.
-
- Here is a list of the commonly used single and double stack operators.
- DROP ( n -- ) Drop top number on data stack.
- SWAP ( n m -- m n ) Swap top two numbers on data stack.
- DUP ( n -- n n ) Duplicate top number on data stack.
- OVER ( n m -- n m n ) Make copy of second item to top of stack
- ROT ( a b c -- b c a) Rotate third item to the top of stack.
- -ROT ( a b c -- c a b) Rotate in opposite direction.
- PICK ( ? n -- ? nth) Copy nth item to top of stack (0 based)
- ROLL ( ? n -- ? nth) Rotate nth item to top (0 based).
- NIP ( n m -- m ) Discard second item on data stack.
- TUCK ( n m -- m n m) Push copy of top under second item.
- 3DUP ( a b c -- a b c a b c) Make copy of top 3 items.
- 2DROP ( dn -- ) Drop double number from top.
- or ( a b -- )
- 2SWAP ( dn dm -- dm dn) Swap top two double numbers.
- or ( a b c d -- c d a b)
- 2DUP ( dn -- dn dn) Make another copy of top double number.
- or ( a b -- a b a b )
- 2OVER ( dn dm -- dn dm dn) Copy second double number to top.
- or ( a b c d -- a b c d a b )
-
- ╓──────────────╖
- ║ Problem 3.3 ║
- ╙──────────────╜
- Write high level Forth definitions for the following words.
- a) 2SWAP ( hint: use ROLL twice )
- b) 2DROP
- c) 2DUP ( hint: use PICK twice )
- d) 2OVER ( hint: use PICK twice )
- e) 2NIP ( d1 d2 -- d2)
- f) 2TUCK ( d1 d2 -- d2 d1 d2 )
- g) 2ROT ( d1 d2 d3 -- d2 d3 d1 )
- h) -2ROT ( d1 d2 d3 -- d3 d1 d2 )
- Use your .SD word to verify the correct operation of your words.
-
- ╓──────────────╖
- ║ Problem 3.4 ║
- ╙──────────────╜
- ( Floored Division Review )
- Floored symmetric division. Note that q and r must satisfy
- the equations: m/n = q + r/n or m = nq + r
- / ( m n q ) Leave q , the floor of real quotient.
- MOD ( m n r ) Leave r , remainder (satisfying above).
- /MOD ( m n r q ) Leave remainder r and quotient q .
- Complete the following table WITHOUT using your computer!
- m n r q Check: n * q + r
- 13 5 5 *
- -11 5 5 *
- -2 5 5 *
- 13 -5 -5 *
- -11 -5 -5 *
- -2 -5 -5 *
-
- ╓──────────────╖
- ║ Problem 3.5 ║
- ╙──────────────╜
- ( Easy words )
- Here are the stack comments for some of the easy words
- of Lesson 2 Part 130
- 1+ ( n -- n+1 ) Increment top stack item by 1.
- 2+ ( n -- n+2 ) Increment top stack item by 2.
- 1- ( n -- n-1 ) Decrement top stack item by 1.
- 2- ( n -- n-2 ) Decrement top stack item by 2.
- 2* ( n -- 2n ) Multiply top stack item by 2.
- 2/ ( n -- n/2 ) Divide top stack item by 2.
-
- Write high level Forth definitions of these words and their double
- number analogs D1+ through D2/
-
- ┌─────────────────────────────────────┐
- │ Please move to Lesson 3 Part 030 │
- └─────────────────────────────────────┘
-