home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l4p080
< prev
next >
Wrap
Text File
|
1990-07-15
|
4KB
|
109 lines
╔════════════════════════════════════════════════════╗
║ Lesson 4 Part 080 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
┌─────────────────────────────────────────┐
│ Compiling Numbers into the Dictionary │
└─────────────────────────────────────────┘
We begin with the investigation of the operators , and C, These
operators are analogous to ! and C! except that they don't take an
address, instead they just store to the next available location in the
code segment.
HERE . <enter> 7048 ok \ Again you dictionary pointer values
CREATE STUFF <enter> ok \ that are returned by HERE will be
HERE . <enter> 704B ok \ different than mine but the increments
1122 , <enter> ok \ will be the same.
HERE . <enter> 704D ok
4567 , <enter> ok
HERE . <enter> 704F ok
88 C, 99 C, <enter> ok
HERE . <enter> 7051 ok
STUFF 10 DUMP <enter>
+---------+-------------------------------------------------+
| SEG:OFF | B C D E F 0 1 2 3 4 5 6 7 8 9 A |
+---------+-------------------------------------------------+
|31DE:704B| 22 11 67 45 88 99 04 44 55 4D 50 20 20 17 AC 50 |
+---------+-------------------------------------------------+
Check out the values in the dump above an see that they correspond
to the values stored in the dictionary by , and C,
Note also that , and C, have built into them 2 and 1 byte ALLOT's
respectively. In fact , and C, could be defined in high level
Forth as:
: , ( n -- ) HERE 2 ALLOT ! ;
: C, ( b -- ) HERE 1 ALLOT C! ;
┌──────────────────────────────┐
│ Building Tables and Arrays │
└──────────────────────────────┘
The family of words, CREATE ALLOT , and C, are useful for building
tables of data and arrays. Some examples follow.
Type the following into a file called MARBLE.SEQ
\ The following will be used to keep track of our growing marble
\ collection. Tables - arrays by another name.
CREATE MARBLE 0 , 0 , 0 , 0 , 0 , 0 ,
0 CONSTANT RED 2 CONSTANT BLUE 4 CONSTANT YELLOW
6 CONSTANT BLACK 8 CONSTANT WHITE 10 CONSTANT GREEN
: MARBLES ( offset -- storage.address ) MARBLE + ;
2 RED MARBLES ! 3 BLUE MARBLES ! 5 YELLOW MARBLES !
8 BLACK MARBLES ! 13 WHITE MARBLES ! 21 GREEN MARBLES !
FLOAD MARBLE <enter>
RED isn't unique \ Looks like the colors are already used
BLUE isn't unique \ in F-PC but that's ok because our definitions
YELLOW isn't unique \ take precedence.
BLACK isn't unique
WHITE isn't unique
GREEN isn't unique ok
\ Some demonstrations:
RED MARBLES ? <enter> 2 ok \ How many red marbles?
BLACK MARBLES ? <enter> 8 ok \ How many black marbles?
5 RED MARBLES +! <enter> ok \ Increment reds by 5
RED MARBLES ? <enter> 7 ok \ How many reds now?
50 BLACK MARBLES ! <enter> ok \ Store new value for black marbles.
BLACK MARBLES ? <enter> 50 ok \ How many blacks?
Play with the above table until you are sure you understand what is
going on. Use DUMP to view what is in the array: MARBLE 10 DUMP
Type the following into the file called MARBLES.SEQ This is a different
file from the one above... It was called MARBLE.SEQ
\ Tables - arrays by another name.
CREATE TABLE 0 , 0 , 0 , 0 , 0 , 0 ,
VARIABLE MODE
0 CONSTANT RED 2 CONSTANT BLUE 4 CONSTANT YELLOW
6 CONSTANT BLACK 8 CONSTANT WHITE 10 CONSTANT GREEN
: LESS -1 MODE ! ; : LESS? MODE @ -1 = ;
: SHOW 0 MODE ! ; : SHOW? MODE @ 0= ;
: MORE 1 MODE ! ; : MORE? MODE @ 1 = ;
: ONLY 2 MODE ! ; ONLY
: MARBLES ( {n} color -- )
TABLE + DEPTH 1 = IF SHOW THEN
LESS? IF SWAP NEGATE SWAP +!
ELSE SHOW? IF @ .
ELSE MORE? IF +!
ELSE !
THEN THEN THEN ONLY ;
: MARBLE MARBLES ;
\ Now watch this magic!!!
6 RED MARBLES <enter> ok SHOW RED MARBLES <enter> 6 ok
11 BLUE MARBLES <enter> ok SHOW BLUE MARBLES <enter> 11 ok
5 MORE BLUE MARBLES <enter> ok SHOW BLUE MARBLES <enter> 16 ok
2 LESS BLUE MARBLES <enter> ok BLUE MARBLES <enter> 14 ok
RED MARBLES <enter> 6 ok
ONLY 111 BLUE MARBLES <enter> ok BLUE MARBLES <enter> 111 ok
┌───────────────────────────────────┐
│ Please move to Lesson 4 Part 090 │
└───────────────────────────────────┘