home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l1p060
< prev
next >
Wrap
Text File
|
1990-05-16
|
4KB
|
89 lines
╔══════════════════════════════════════════════════════╗
║ Lesson 1 Part 6.0 F-PC 3.5 Tutorial by Jack Brown ║
╚══════════════════════════════════════════════════════╝
Are you ready to make your checker board program better? You may have
noticed that there was a lot of repetition in the words ROW1, ROW2 and
BOARD. The sequence BLACK WHITE , WHITE BLACK , and ROW1 ROW2 appear
four time each. Forth, like most other languages has a method for
specifying sequences that must be repeated. In BASIC you use the
FOR... NEXT loop. In Forth it is called the DO ... LOOP. Instead of
coding the sequence BLACK WHITE four times we can replace it with the
sequence:
4 0 DO BLACK WHITE LOOP
The 0 is the initial value of the loop counter. The 4 is the maximum
value of the loop counter. The loop counter (which we don't use here)
will start with the value 0 and each time the word LOOP is encountered
it will be incremented by 1 . If the incremented value less than the
maximum loop counter value of 4 program control will jump back to the
first word after the DO ( BLACK in our case ). It may look like the
DO... LOOP above would repeat the sequence BLACK WHITE five times, 0 1 2
3 4, but remember LOOP increments and does the checking. When the loop
counter is incremented from 3 to 4 the condition for repetition is no
longer satisfied and control will be passed to the next Forth word that
is just after LOOP. Here is what the improved program will look like.
\ ANOTHER CHECKER BOARD
: BLACK SPACE SPACE ;
: WHITE 177 EMIT 177 EMIT ;
: ROW1 CR 4 0 DO BLACK WHITE LOOP ;
: ROW2 CR 4 0 DO WHITE BLACK LOOP ;
: BOARD CR 4 0 DO ROW1 ROW2 LOOP ;
: CHECKER_BOARD BOARD CR CR ;
Use the EDitor to enter the program using the file name BOARD2.SEQ.
There are a number of ways of starting the EDitor. You can just press
<ALT> N and enter the file name BOARD2 when prompted or you can just
press <ESC> and choose "New file" from the file menu.
When you enter the program see if you can add a little style to it so it
has an appearance like the first one we entered.
It should also be noted that you can get a menu of your existing open
files to edit by choosing the "Open file" item from the FILE menu or by
just pressing <ALT> O from Forth. You then choose one of your files to
modify by using the arrow keys. You are should try this feature as soon
as you get a chance.
╓─────────────╖
║ Problem 1.2 ║
╙─────────────╜
Modify the checker board program so that it has a double lined border
constructed by from the IBM extended character set. Hint: See the
word EMIT in the discussion below.
By the way, did you notice the new word that we are using? It is called
EMIT. EMIT will display the ascii character corresponding to the number
parameter the preceding it. Try typing the following:
65 EMIT 66 EMIT 67 EMIT <enter> <--- do it, what do you see?
31 EMIT 32 EMIT 33 EMIT <enter> <--- do it, what do you see?
177 EMIT gives a white blob on the screen. number parameters
128 through 255 correspond to IBM's extended character set.
Try the following while in Forth.
: CHARSET 255 0 DO CR I . I EMIT LOOP ; <enter>
CHARSET <enter>
The Forth word " I " will fetch the current value of the loop
counter and use it as the parameter for " . " and EMIT.
What do you see.
╓──────────────╖
║ Problem 1.3 ║
╙──────────────╜
Modify CHARSET so that the information is not output so quickly by
adding a "delay loop" to waste time and hence slow down the output.
Hint: : DELAY 10000 0 DO 1 DROP LOOP ;
┌────────────────────────────────────┐
│ Please move to Lesson 1 Part 7.0 │
└────────────────────────────────────┘