       ͻ
        Lesson 2 Part 035  F-PC 3.5 Tutorial by Jack Brown 
       ͼ

ķ
 Answer to problem 2.3 
Ľ
1) It is standard practice for Forth words to be passed some of their
parameters on the stack by the user or the calling word.  In this case
we would place then number to count up to on the stack like so:

100 COUNT_UP <enter>

In this case the loop limit would be 100 and the initial loop value
would be 0.  The loop limit is provided by the user or the calling word
and the initial loop value is hard coded into the word definition.

2) This is easy....  Start a new line!

3) I is a Forth word that can be used within a DO ( or ?DO ) ... LOOP
   to fetch the current value of the loop counter to the top of the
   parameter stack.

4) The program counts up because the Loop counter is incremented by 1
   at the word LOOP, if the loop limit has been reached the execution
   continues just after the word LOOP otherwise execution resumes at the
   first word after the ?DO .

5) If the stack was empty it would use whatever garbage was buried one
   level above the top of the stack.  If you are unlucky this could be a
   very large number like 50000 and you could be waiting for a long time
   to recover from your mistake.

6) Leaving out the . would result in all the numbers being left on the
   parameter stack.  If you get enough numbers on the parameter stack
   you will CRASH the Forth system.

7) The program will only count up to n-1  ( not n ) . This is because
   the check for loop completion is done at the word LOOP after the loop
   counter is incremented.  If the loop limit has been reached the loop
   will be terminated and will not be executed for the case I = n.

ķ
 Problem 2.4 
Ľ
a) Modify the COUNT_UP program so that it counts up to " n " .

b) Use HELP and VIEW to find out as much as you can about the word +LOOP
   When you are finished try replacing the word LOOP by " 2 +LOOP"
   What happens?

c) Make the word COUNT_UP_BY ( n inc -- )  that will count up to n by
   inc. Note: This is not easy so try it and upload you solution!

d) Make words COUNT_DOWN and COUNT_DOWN_BY that work like the UP words
   except that they count down from " n ". Carefully check you words
   with various values of n and inc.  Upload your solutions with samples
   of the output (capture it with a pop up editor like side-kick or use
   the PD program called BACKSCRL.ARC available from BCFB.)


Ŀ
  Please move to Lesson 2 Part 040  

