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

Moving Things On The Stack

Introduction

A word takes what it needs from the top of the stack. That is fine until the number you want is second from the top, or you need the same number twice and using it once uses it up.

Forth's answer is a handful of words that do no arithmetic at all. They just move the plates around. There are five that matter, and you will use them in almost everything you write.

Writing Down What A Word Does

From here on the course describes words the way Forth programmers do, with a little picture of the stack before and after:

+   ( n1 n2 -- sum )

Read it as: before the word runs, the stack has n1 and n2 on it, n2 on top; after, it has their sum instead. The -- is the word running. The top of the stack is always on the right, just as .S shows it. Anything further down the stack is not mentioned, because it is not touched.

The Five

WordPictureIn words
DUP( n -- n n )copy the top number
DROP( n -- )throw the top number away
SWAP( n1 n2 -- n2 n1 )exchange the top two
OVER( n1 n2 -- n1 n2 n1 )copy the second one down, onto the top
ROT( n1 n2 n3 -- n2 n3 n1 )pull the third one down up to the top

DUP is the one you will use most. Every word that uses a number uses it up, so if you want it afterwards, you DUP it first.

The Code

11 22 33 DUP .S SP!
11 22 33 DROP .S SP!
11 22 33 SWAP .S SP!
11 22 33 OVER .S SP!
11 22 33 ROT .S SP!

Every line starts from the same three numbers, does one thing to them, shows the result with .S, and clears the stack with SP! ready for the next.

Starting from

A fresh boot, with SuperForth just started.

What you should see

11 22 33 DUP .S SP!

11 22 33 33 ok
11 22 33 DROP .S SP!

11 22 ok
11 22 33 SWAP .S SP!

11 33 22 ok
11 22 33 OVER .S SP!

11 22 33 22 ok
11 22 33 ROT .S SP!

22 33 11 ok

Five lines, each applying one stack word to 11 22 33 and showing the result

Check each result against its picture in the table. 11, 22 and 33 were chosen so that you can always tell which number went where.

Putting Them To Work

Squaring a number. To multiply a number by itself you need it twice:

7 DUP * .

prints 49. DUP makes the second 7; * uses them both.

Fixing the order. Suppose the numbers arrived the wrong way round for a subtraction - 3 then 20, when you wanted twenty take away three:

3 20 SWAP - .

prints 17.

Getting rid of a leftover. A number you no longer need is in the way of the ones underneath it. DROP it.

The Same Again, In Pairs

WordPicture
2DUP( n1 n2 -- n1 n2 n1 n2 )
2DROP( n1 n2 -- )
2SWAP( n1 n2 n3 n4 -- n3 n4 n1 n2 )

And two oddities:

-DUP copies the top number only if it is not zero: ( n -- n n ), but ( 0 -- 0 ). It looks pointless now and earns its keep in S14.

PICK copies a number from as far down as you say. 3 PICK copies the third number down onto the top, and 1 PICK is the same as DUP.

Keep The Stack Shallow

Five words are enough to arrange any three numbers any way you like, and that is about where you should stop. If you find yourself doing ROT SWAP ROT to reach something four deep, the fix is not a cleverer shuffle. It is to break the job into smaller words, each dealing with two or three numbers - which is what S7 is for.

Change One Thing

  • Change ROT to ROT ROT in the last line. Predict the stack first. What would a third ROT do?
  • Try 11 22 33 SWAP DROP .S. Which of the three numbers did that pair of words remove?
  • Try 11 22 OVER OVER .S. You have met a single word that does the same. Which?
  • Type SP! DUP and then .S. SuperForth says ok to the DUP. Should it have? What is that number on the stack?

Exercises

6.1 Print the square of 12 using DUP.

6.2 Print the cube of 3 - three times three times three - typing the 3 only once.

6.3 Start with 1 2 3 on the stack and rearrange it to 3 2 1 using two of this section's words. Check with .S.

6.4 Start with 1 2 on the stack and get to 1 2 1 2 without using 2DUP.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
A strange large number on the stack that you never typedA stack word ran with too little on the stack. DUP, SWAP, OVER and ROT do not check: they say ok and use whatever happens to be lying underneath. SP! and start the line again.
DROP? Empty StackSuperForth does notice when the stack ends up with less than nothing on it, which is what DROP on an empty stack does.
The right numbers, in the wrong orderCount again from the right-hand end of the .S display. The top is on the right, and ROT pulls the third one up, not the bottom one.

Summary

DUP copies the top number, DROP discards it, SWAP exchanges the top two, OVER copies the second, and ROT brings the third to the top. The ( before -- after ) picture says what any word does to the stack. These words do not check that the numbers they need are there; SuperForth complains only when the stack ends up below empty.

Next

S7, A New Word - teaching SuperForth a word of your own, so that DUP * becomes SQUARE and you never have to think about it again.

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.