SuperForth for the Tatung Einstein, showing a SuperForth 1.12 session defining a word
← Back to Courses
module
13

True And False

Introduction

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.

Flags

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:

WordPictureTrue 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 .

Turning A Flag Over

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=.

Combining Flags

WordLeaves 1 when
ANDboth flags are 1
OReither flag is 1
XORexactly 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.

The Code

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

Three comparisons, then TEEN? tried on 15 and 25

A Warning About AND And OR

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.

Change One Thing

  • Type 15 TEEN? 0= . What does the 0= do to the answer?
  • Make TEEN? answer true for 12 and 20 as well. There are two ways: change the numbers, or change the words. Try both.
  • Type 6 3 AND . and 6 3 OR . Write 6 and 3 out in binary and explain both answers.
  • Type 5 5 < . and 5 5 = . Now build "less than or equal" out of > and one other word from this section.

Exercises

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.

When It Goes Wrong

SymptomCause
The answer is the opposite of what you expectedThe numbers are the wrong way round. 3 5 < asks whether 3 is less than 5.
AND gives a number that is neither 0 nor 1One of the things you combined was not a flag.
The number you were testing has goneComparisons use up what they compare. DUP first if you need it again.

Summary

< > = 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.

Next

S14, Making A Choice - doing one thing or another depending on a flag.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.