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

Sums Backwards

Introduction

Ask most people to add two and three on a calculator and they press 2, then +, then 3. SuperForth wants 2 3 +. It looks backwards, and for about a day it feels backwards.

It is not a quirk. It falls straight out of the stack, and once you see why, it stops looking backwards and starts looking like the only sensible way to do it.

Why The Sign Comes Last

+ is a word, like every other word. When SuperForth reaches it, it runs it, there and then. It cannot wait to see what comes next on the line, so whatever + is going to add has to be on the stack already.

So you put the numbers on first, and then you say what to do with them.

+ takes the top two numbers off the stack, adds them, and puts the answer back. Two numbers went in; one came out. Anything underneath is left alone.

The Four Signs

WordDoes
+adds the top two numbers
-takes the top number away from the one beneath it
*multiplies the top two numbers
/divides the one beneath by the top number

For + and * the order does not matter. For - and / it does, and the rule is the same for both: type it in the order you would say it. "Twenty take away three" is 20 3 -. "Twenty divided by three" is 20 3 /.

In MAME, a reminder from S3, because you need them now: + is SHIFT and ;, * is SHIFT and the ' key, and - is SHIFT and =. The key with - printed on it gives an underscore, and 20 3 _ gets you _?.

Whole Numbers Only

SuperForth's numbers are whole numbers. 20 3 / is 6, not 6.666. The remainder is thrown away - unless you ask for it:

WordLeaves
/how many times it goes: 20 3 / leaves 6
MODwhat is left over: 20 3 MOD leaves 2
/MODboth: 20 3 /MOD leaves 2 and then 6, with the 6 on top

The Code

2 3 + .
20 3 - .
6 7 * .
20 3 / .
20 3 MOD .
2 3 + 4 * .

Starting from

A fresh boot, with SuperForth just started.

What you should see

2 3 + .
5 ok
20 3 - .
17 ok
6 7 * .
42 ok
20 3 / .
6 ok
20 3 MOD .
2 ok
2 3 + 4 * .
20 ok

Six sums typed at SuperForth, each answered on the line below

No Brackets, Ever

Look at the last line again: 2 3 + 4 * . printed 20.

Follow the stack. 2 3 puts two numbers on. + replaces them with 5. 4 goes on top of the 5. * replaces both with 20. That is (2 + 3) x 4, and you never needed a bracket, because the order you typed things in is the order they happened in.

If you wanted 2 + (3 x 4) instead, you would do the multiplication first: 2 3 4 * + . prints 14. The * takes the 3 and the 4, which are on top, and the + then finds 2 and 12 waiting.

There is no precedence to remember and nothing to get wrong about which sign binds tighter. What you lose is being able to glance at a sum and see what it says. Turn STON on and step through a line one word at a time until it stops being a puzzle.

How Big Is A Number?

A SuperForth number runs from -32768 to 32767. That is all the room it has.

Go past either end and nothing warns you. The number simply wraps round to the other end, like a car's mileage going past all the nines:

32767 1 + .
-32768 ok

This is the mistake you will not see coming. A sum that ought to be 86,400 prints something plausible and wrong, and SuperForth says ok. Keep an eye on the size of your answers. Anything that might pass thirty thousand needs a different tool, and S26 has one.

A number with a - in front of it is negative: -5 is a number, and it prints as -5. When you divide a negative number the answer is rounded towards zero, so -20 3 / is -6, and the remainder, -2, keeps the sign of the number you divided.

A Few Small Words

WordDoesExample
1+adds one to the top number5 1+ leaves 6
2+adds two5 2+ leaves 7
MAXkeeps the bigger of the top two3 9 MAX leaves 9
MINkeeps the smaller3 9 MIN leaves 3
ABSdrops a minus sign if there is one-7 ABS leaves 7
MINUSchanges the sign5 MINUS leaves -5

1+ is one word with no space in it. 1 + is two words and does the same thing; the single word is just less to type.

MINUS is not subtraction. It turns a number into its negative. Subtraction is -.

Change One Thing

  • Turn the subtraction round: 3 20 - . What do you get, and what does it tell you about which number - takes away from which?
  • Type 20 3 /MOD . . - two dots. Which comes out first, the 6 or the 2? Why that way round?
  • Type 32767 1 + . and then -32768 1 - . What do the two results tell you about the ends of the range?
  • Type 7 0 / . Dividing by nothing has no sensible answer. Does SuperForth refuse? What does that tell you about how much checking it does on your behalf?

Exercises

5.1 Add 10, 20 and 30 using two + words, with all three numbers typed before either +.

5.2 How many minutes are there in a week?

5.3 How many seconds are there in a day? Work it out on paper first, then ask SuperForth. Explain the answer it gives you.

5.4 Find the average of 10, 20 and 60.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
_?In MAME, you pressed the key marked -. That gives an underscore. Minus is SHIFT and =.
A subtraction or division gives the answer for the sum the other way roundThe numbers are in the wrong order. Type them in the order you would say them.
A big sum gives a small or negative answer, and okIt went past 32767 and wrapped. Nothing warns you.
ok, but the stack is empty and your number has goneA sign found only one number waiting. It does not complain: it uses whatever lay underneath, and the answer is lost. Usually a number is missing from the line.

Summary

The numbers go on the stack first and the sign comes after, because a word can only work on what is already there. + - * / each take two numbers and leave one; MOD and /MOD get you the remainder. Longer sums need no brackets, only the right order. Numbers are whole, run from -32768 to 32767, and wrap silently at the ends.

Next

S6, Moving Things On The Stack - what to do when the number you want is not on top, or when you need the same number twice.

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.