text
stringlengths
0
99.6k
Page-119
The footnote on this page contains a typo in my copy. The correct definition is:*ln2
: R% 50 */ 1+ 2/ ;
*ln2
Problems
Problem 1
In the answer to this problem, Brodie means NEGATE , not MINUS .
You might also notice that Blazin' Forth does not return a -17 like Brodie says it should. This is due to the 83-standards use of floored division. In floored division, numbers are always truncated to the next lower value. To get the same result as Brodie in 83-Forth systems, use ABS and then NEGATE.
*ln2
*cn1;Chapter 6*cn0
*ln2
Forth-83 uses a new loop structure which is very fast, but differs in a few points from the older loops used in older Forths. Sorry about this, but there will be a little more information here than previously.
*ln2
Page-130
*ln2
Earlier (in the notes to chapter 5) we said that "I" works differently in '83 Forths. Well, originally, "I" was meant to be used as it is here, to access the loop index. Since in those days, the loop index was stored on the top of the return stack, you could also use "I" to copy that value, even if you weren't in a loop, which was not a purpose for which it was originally intended. So that's the difference, I now only returns the loop index, while R@ copies the top of the return stack to the parameter stack. (For the curious: In Blazin' Forth, I returns the sum of the top two return stack items. It may seem strange, but it works just fine!)
The terminating condition for loops is also different than in earlier Forths. Don't be fooled by the fact that most of the examples in this chapter work the same - the exit condition for the loops are different, and if you ignore this fact, you are going to get bit by it.
In '83 loops, the loop is finished when the Index crosses the boundary between the LIMIT and LIMIT - 1 . A couple of examples:
If the Index is 0 , and the LIMIT is 10 , then the loop will terminate when the Index is incremented from 9 to 10 (crosses the boundary).
If the Index is 0 , and the Limit is -10 , then the loop will terminate when the index is incremented from -11 to -10 or decremented from -10 to -11 . This means, if you are using a DO..LOOP, that your loop will take the long way around...0 1 2 3 4 ...32767 -32768 .....-15 -14 -13 -12 -11 . If you are using +LOOP with a negative step value, then your loop will look like this: 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 (boundary crossed, and looped terminated).
*ln2
One odd side effect of this new loop structure is that anytime the limit and the index are equal, the loop will execute 65,535 times. So, for example, the word STARS :
: STARS 0 DO ASCII * EMIT LOOP ;
If used like this:
0 STARS
will EMIT an awful lot of stars. Blazin' Forth contains a word to handle cases like this - ?DO . ?DO acts just like DO , except when the loop arguments are equal. If the arguments are equal, then the loop won't be entered at all - FORTH will jump to the word following LOOP or +LOOP . In addition to keeping out nasty surprises (like 0 STAR) ?DO can also shorten and speed up your code. For example, we often find we don't want to perform a loop when the LIMIT is zero (as in STARS , above.) The traditional way to do this is:
: STARS ?DUP IF 0 DO ASCII * EMIT LOOP THEN ;
But you can now do it this way:
: STARS 0 ?DO IF ASCII * EMIT LOOP ;
You can think of ?DO as having a built in IF .
*fp0
Page 135
The definition given by Brodie:
: TEST 100 10 DO I . -1 +LOOP ;
will execute quite a few more times than one (in '83 Forth). Remember, '83 loops stop when the boundary is crossed between the LIMIT and LIMIT-1 . This crossing can be in either direction, either down ( 101 100 (crossed to 99, so exit) ) or up ( 98 99 (crossed to 100, so exit)) . Since TEST counts down, it will proceed all the way around the number circle, starting with 10, and then going negative, until it eventually goes 101 100 (as in our first example, above). Just though I would mention this.
*ln2
Page-140 LEAVE
*ln2
In '83 Forth, LEAVE leaves immediately, not at the next execution of LOOP or +LOOP. Brodies examples will work the same for both '83 and earlier Forths, but problems can arise if you aren't aware of the difference. For example:
*ln2
: FOO 0 DO I DUP 100 = IF LEAVE THEN . LOOP ;
FOO is a word that will exit when the loop counter reaches 100 . Notice, however, that the actual behaviour of FOO will differ between '83 and earlier standards. In earlier Forths, FOO's execution would be this:
When the Counter reaches 100, LEAVE will set the index equal to the limit.
" . " will be executed, printing 100 on the terminal screen, and leaving the stack empty.
LOOP will execute, and discover its time to stop (since LEAVE changed the INDEX).
In '83 Forth, this is what happens.
When the Counter reaches 100, LEAVE will jump to the word past LOOP , which in this case is the end of the definition. 100 will not be printed, but left on the stack. Quite different behaviours result, as you can see. Just remember that the 83 LEAVE leaps , and you'll be OK .
By the way, Blazin' Forth has a handy word ?LEAVE . ?LEAVE will cause an exit from the loop if the flag on top of the stack is true, otherwise, the loop continues undisturbed. This can shorten your code quite a bit. Here's an example:
: BAR 0 DO I 100 = IF LEAVE THEN LOOP ;
This word just leaves when the index reaches 100. Using ?LEAVE would make things a little nicer:
: BAR 0 DO I 100 = ?LEAVE LOOP ;
Works just the same. Just remember that you have to leave a flag on the stack for ?LEAVE to eat.
*ln2
*cn1;Chapter 7*cn0
*ln2
Section II
Page 161
The Forth-83 word for U* is UM* . It works the same as U* , but has been renamed.
The Forth-83 word for U/MOD is UM/MOD . It works the same.
*ln2
/LOOP is not included. It is no longer necessary in '83 systems. (For the curious: /LOOP was essentially a kludge to fix a problem with the older loops. Just use +LOOP instead.)
*ln2
Page 164
*ln2
'83 systems will only accept the period as punctuation in numbers. However, in Blazin' Forth, you may change this. Here are the steps to follow to get Blazin' Forth to accept the same characters those given by Brodie: