
Every decision a program makes comes down to a question with a yes or no answer. Is the score over a hundred? Has a key been pressed? Are there any lives left?
SuperForth asks such questions with ordinary words that take numbers from the stack - and the answer they leave is an ordinary number too.
3 5 < .
prints 1. Three is less than five, so < leaves 1, meaning true. Ask 5 3 < . and you get 0, meaning false.
A number used this way is called a flag, and in stack pictures it is written f:
| Word | Picture | True when |
|---|---|---|
< | ( n1 n2 -- f ) | n1 is less than n2 |
> | ( n1 n2 -- f ) | n1 is greater than n2 |
= | ( n1 n2 -- f ) | they are equal |
0= | ( n -- f ) | n is zero |
0< | ( n -- f ) | n is negative |
As with - and /, read them in the order you type them: 3 5 < is "three, five, less than?" - is three less than five.
In MAME, < is SHIFT and , and > is SHIFT and .
0= has a second use. It turns 0 into 1, and anything else into 0 - so it reverses a flag. "Not equal" is = 0=. "Not less than" is < 0=.
| Word | Leaves 1 when |
|---|---|
AND | both flags are 1 |
OR | either flag is 1 |
XOR | exactly one of them is 1 |
So "between 13 and 19" is "more than 12 and less than 20":
: TEEN? DUP 12 > SWAP 20 < AND ;
The DUP is there because the number is needed twice and each comparison uses it up. It is a custom, not a rule, to end a yes-or-no word's name with a question mark.
3 5 < .
5 3 < .
5 5 = .
: TEEN? DUP 12 > SWAP 20 < AND ;
15 TEEN? .
25 TEEN? .
Starting from
A fresh boot, with SuperForth just started.
What you should see
3 5 < .
1 ok
5 3 < .
0 ok
5 5 = .
1 ok
: TEEN? DUP 12 > SWAP 20 < AND ;
ok
15 TEEN? .
1 ok
25 TEEN? .
0 ok

AND, OR and XOR do not really know about flags. They work on the separate binary digits of whatever two numbers you give them. With 0 and 1 that comes out exactly as the table above says, which is why flags are 0 and 1. With other numbers it does not:
6 3 AND .
prints 2, not 1. So only combine things that really are flags - the results of comparisons - and never raw numbers that merely happen to be non-zero.
15 TEEN? 0= . What does the 0= do to the answer?TEEN? answer true for 12 and 20 as well. There are two ways: change the numbers, or change the words. Try both.6 3 AND . and 6 3 OR . Write 6 and 3 out in binary and explain both answers.5 5 < . and 5 5 = . Now build "less than or equal" out of > and one other word from this section.13.1 Define EVEN? ( n -- f ), true when n is even. MOD will help.
13.2 Define POSITIVE? ( n -- f ), true for numbers above zero. Try it on 5, on -5 and on 0.
13.3 Define BIG? ( n -- f ), true when n is more than 1000 or less than -1000. There is a way that needs only one comparison.
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
| The answer is the opposite of what you expected | The numbers are the wrong way round. 3 5 < asks whether 3 is less than 5. |
AND gives a number that is neither 0 nor 1 | One of the things you combined was not a flag. |
| The number you were testing has gone | Comparisons use up what they compare. DUP first if you need it again. |
< > = 0= 0< take numbers and leave a flag: 1 for true, 0 for false. 0= reverses a flag. AND, OR and XOR combine flags, and misbehave on anything that is not one. A word that leaves a flag is a question you can ask again by name.
S14, Making A Choice - doing one thing or another depending on a flag.