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

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 4 Part 080  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.            ┌─────────────────────────────────────────┐
  6.            │   Compiling Numbers into the Dictionary │
  7.            └─────────────────────────────────────────┘
  8.  
  9. We begin with the investigation of the operators , and C,  These
  10. operators are analogous to ! and C! except that they don't take an
  11. address, instead they just store to the next available location in the
  12. code segment.
  13.  
  14. HERE . <enter> 7048  ok   \ Again you dictionary pointer values
  15. CREATE STUFF <enter> ok   \ that are returned by HERE will be
  16. HERE . <enter> 704B  ok   \ different than mine but the increments
  17. 1122 , <enter> ok         \ will be the same.
  18. HERE . <enter> 704D  ok
  19. 4567 , <enter> ok
  20. HERE . <enter> 704F  ok
  21. 88 C, 99 C,  <enter> ok
  22. HERE . <enter> 7051  ok
  23. STUFF 10 DUMP <enter>
  24. +---------+-------------------------------------------------+
  25. | SEG:OFF |  B  C  D  E  F  0  1  2  3  4  5  6  7  8  9  A |
  26. +---------+-------------------------------------------------+
  27. |31DE:704B| 22 11 67 45 88 99 04 44 55 4D 50 20 20 17 AC 50 |
  28. +---------+-------------------------------------------------+
  29. Check out the values in the dump above an see that they correspond
  30. to the values stored in the dictionary by , and C,
  31. Note also that , and C, have built into them  2 and 1 byte ALLOT's
  32. respectively.  In fact , and C, could be defined in high level
  33. Forth as:
  34.  
  35. : ,  ( n -- )  HERE 2 ALLOT !  ;
  36. : C, ( b -- )  HERE 1 ALLOT C! ;
  37.  
  38.               ┌──────────────────────────────┐
  39.               │  Building Tables and Arrays  │
  40.               └──────────────────────────────┘
  41.  
  42. The family of words,  CREATE  ALLOT ,  and  C, are useful for building
  43. tables of data and arrays.  Some examples follow.
  44.  
  45. Type the following into a file called MARBLE.SEQ
  46. \ The following will be used to keep track of our growing marble
  47. \ collection.   Tables  -  arrays by another name.
  48.  
  49. CREATE MARBLE  0 , 0 , 0 , 0 , 0 , 0 ,
  50.  
  51. 0 CONSTANT RED         2 CONSTANT BLUE     4 CONSTANT YELLOW
  52. 6 CONSTANT BLACK       8 CONSTANT WHITE   10 CONSTANT GREEN
  53.  
  54. : MARBLES  ( offset -- storage.address )  MARBLE + ;
  55.  
  56. 2 RED   MARBLES !    3 BLUE  MARBLES !     5 YELLOW MARBLES !
  57. 8 BLACK MARBLES !   13 WHITE MARBLES !    21 GREEN  MARBLES !
  58.  
  59. FLOAD MARBLE <enter>
  60. RED isn't unique    \ Looks like the colors are already used
  61. BLUE isn't unique   \ in F-PC but that's ok because our definitions
  62. YELLOW isn't unique \ take precedence.
  63. BLACK isn't unique
  64. WHITE isn't unique
  65. GREEN isn't unique  ok
  66. \ Some demonstrations:
  67. RED MARBLES ? <enter> 2  ok    \ How many red marbles?
  68. BLACK MARBLES ? <enter> 8  ok  \ How many black marbles?
  69. 5 RED MARBLES +! <enter> ok    \ Increment reds by 5
  70. RED MARBLES ? <enter> 7  ok    \ How many reds now?
  71. 50 BLACK MARBLES !  <enter> ok \ Store new value for black marbles.
  72. BLACK MARBLES ? <enter> 50  ok \ How many blacks?
  73.  
  74. Play with the above table until you are sure you understand what is
  75. going on.  Use DUMP to view what is in the array:  MARBLE 10 DUMP
  76.  
  77. Type the following into the file called MARBLES.SEQ  This is a different
  78. file from the one above... It was called MARBLE.SEQ
  79. \ Tables  -  arrays by another name.
  80. CREATE TABLE   0 , 0 , 0 , 0 , 0 , 0 ,
  81.  VARIABLE MODE
  82.  0 CONSTANT RED         2 CONSTANT BLUE     4 CONSTANT YELLOW
  83.  6 CONSTANT BLACK       8 CONSTANT WHITE   10 CONSTANT GREEN
  84. : LESS -1  MODE !  ;    : LESS?  MODE @ -1 = ;
  85. : SHOW  0  MODE !  ;    : SHOW?  MODE @  0=  ;
  86. : MORE  1  MODE !  ;    : MORE?  MODE @  1 = ;
  87. : ONLY  2  MODE !  ;      ONLY
  88. : MARBLES  ( {n} color   -- )
  89.         TABLE  +   DEPTH 1 = IF SHOW THEN
  90.         LESS? IF   SWAP NEGATE SWAP +!
  91.               ELSE SHOW? IF   @ .
  92.                          ELSE MORE? IF   +!
  93.                                     ELSE  !
  94.       THEN  THEN  THEN   ONLY ;
  95.  
  96. : MARBLE  MARBLES ;
  97.  
  98. \ Now watch this magic!!!
  99. 6 RED MARBLES <enter> ok          SHOW RED  MARBLES <enter> 6  ok
  100. 11 BLUE MARBLES  <enter> ok       SHOW BLUE MARBLES <enter> 11  ok
  101. 5 MORE BLUE MARBLES <enter> ok    SHOW BLUE MARBLES <enter> 16  ok
  102. 2 LESS BLUE MARBLES <enter> ok    BLUE MARBLES <enter> 14  ok
  103. RED MARBLES <enter> 6  ok
  104. ONLY 111 BLUE MARBLES <enter> ok  BLUE MARBLES <enter> 111  ok
  105.  
  106. ┌───────────────────────────────────┐
  107. │  Please move to Lesson 4 Part 090 │
  108. └───────────────────────────────────┘
  109.