home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l4p070
< prev
next >
Wrap
Text File
|
1990-07-15
|
5KB
|
109 lines
╔════════════════════════════════════════════════════╗
║ Lesson 4 Part 070 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
┌──────────────────────────┐
│ Useful Memory Operators │
└──────────────────────────┘
We continue with the introduction of some new memory operators that can
be useful with variables.
Useful Memory Operators
Note: cell = 2 bytes = 16 bits = 1 word
+! ( n adr -- ) Add n to the value found at address adr
ON ( adr -- ) Set cell at adr to true or -1.
OFF ( adr -- ) Set cell at addr to false or 0.
Examples:
VARIABLE RAIN RAIN ? <enter> 0 ok
5 RAIN +! RAIN ? <enter> 5 ok
5 RAIN +! RAIN ? <enter> 10 ok
: DRIP 1 RAIN +! ; <enter> ok \ Compare this to the previous DRIP
DRIP RAIN ? <enter> 11 ok
DRIP RAIN ? <enter> 12 ok
RAIN OFF RAIN ? <enter> 0 ok
RAIN ON RAIN ? <enter> -1 ok
RAIN @ U. <enter> 65535 ok
RAIN OFF RAIN ? <enter> 0 ok
DRIP DRIP DRIP RAIN ? <enter> 3 ok
╓──────────────╖
║ Problem 4.10 ║
╙──────────────╜
Here are high level Forth definitions of ON and OFF:
: ON ( adr -- ) 2 255 FILL ; \ Set cell at adr to value true or -1.
: OFF ( adr -- ) 2 ERASE ; \ Set cell at adr to value false or 0.
a) Write high level Forth definitions of ON and OFF without using FILL
and ERASE.
b) Write high level Forth definitions for +! and ? .
c) Rewrite your words WARMER and COOLER of problem 4.3 usin +!
╔════════════════════════════════════════════════════════════════╗
║* next up is CREATE the most important Forth word so far *║
╚════════════════════════════════════════════════════════════════╝
CREATE <name> ( -- ) Creates a dictionary entry named <name>
When executed, <name> leaves the address
<name> ( -- adr) of the first memory cell which follows
the word name. No memory is allocated.
Here are some friends of CREATE . CREATE wouldn't be of much use
if he didn't have friends.
ALLOT ( n -- ) Allocate n bytes of memory in the
dictionary.
, ( n -- ) Allocate 16 bits ( 2 bytes ) of memory
initializing it to the value n.
C, ( n -- ) Allocate 8 bits ( 1 byte ) of memory
initializing it to low 8 bits of n.
And now some examples are in order. Make sure that you duplicate
these on your own machine and make up variations of them to test
you understanding.
HEX <enter> ok \ HEX mode
HERE . <enter> 7038 ok \ Your here values may be different but
\ the changes/increments will be the same.
CREATE DATA <enter> ok \ Create dictionary header in header segment.
HERE . <enter> 703B ok \ Three bytes of machine code generated.
10 ALLOT <enter> ok \ Allot 10 bytes of storage in the code seg.
HERE . <enter> 704B ok \ Verify space allotted.
DATA 10 DUMP <enter> \ Initial values are random
+---------+-------------------------------------------------+
| SEG:OFF | 8 9 A B C D E F 0 1 2 3 4 5 6 7 |
+---------+-------------------------------------------------+
|31DE:7038| 05 41 4C 4C 4F 54 20 20 84 41 4E 45 D7 79 17 66 |
+---------+-------------------------------------------------+
ok
DATA 10 ERASE <enter> ok \ Initial storage area to zeros.
DATA 10 DUMP <enter> \ Verify that storage area is zeroed.
+---------+-------------------------------------------------+
| SEG:OFF | 8 9 A B C D E F 0 1 2 3 4 5 6 7 |
+---------+-------------------------------------------------+
|31DE:7038| 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
+---------+-------------------------------------------------+
ok
1234 DATA ! <enter> ok \ Store 1234 at offset 0.
5678 DATA 2 + ! <enter> ok \ Store 5678 at offset 2.
DATA 10 DUMP <enter> \ Verify numbers were stored
+---------+-------------------------------------------------+
| SEG:OFF | 8 9 A B C D E F 0 1 2 3 4 5 6 7 |
+---------+-------------------------------------------------+
|31DE:7038| 34 12 78 56 00 00 00 00 00 00 00 00 00 00 00 00 |
+---------+-------------------------------------------------+
Note 16 bit values are stored in lo-hi or byte reversed order.
Why is the word CREATE so important when it seems only to make an
empty entry in the dictionary? It is because we can use it to
build other words. The definition of VARIABLE is just:
: VARIABLE CREATE 2 ALLOT ; \ Try this definition and see if it works
┌────────────────────────────────────┐
│ Please move to Lesson 4 Part 080 │
└────────────────────────────────────┘