Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement SWAP in Forth

Tags:

forth

I saw that in an interview with Chuck Moore, he says:

The words that manipulate that stack are DUP, DROP and OVER period. There's no, well SWAP is very convenient and you want it, but it isn't a machine instruction.

So I tried to implement SWAP in terms of only DUP, DROP and OVER, but couldn't figure out how to do it, without increasing the stack at least.

How is that done, really?

like image 235
sashoalm Avatar asked Nov 22 '13 09:11

sashoalm


People also ask

How do you implement a swap function?

Alternatively, one can implement a swap function using only addition and subtraction operations. We operate on passed pointers in the function, thus, modifying the argument values directly. In the main function, there is an if condition before the swap function is called to avoid invocation when the operands are equal.

Why is there an IF condition before the swap function?

In the main function, there is an if condition before the swap function is called to avoid invocation when the operands are equal. The most tricky and slightly complicated implementation of the swap function is where the bitwise XOR operation is used. Note that this version does not need a third variable like the previous example.

Is there a swap function in C with arguments?

There is no C standard library function that provides the feature like C++ has std::swap function. In this article, we implement swap functions for integral values; namely, most of them take long int type arguments, but one can always define multiple prototypes for different types and ensure generic features using macro expansions.

What is the return value of swap () function?

Return Value: The function does not return anything, it swaps the values of the two variables. Programs below illustrate the swap () function:


Video Answer


3 Answers

You are right, it seems hard or impossible with just dup, drop, and over.

I would guess the i21 probably also has some kind return stack manipulation, so this would work:

: swap   over 2>r drop 2r> ;

Edit: On the GA144, which also doesn't have a native swap, it's implemented as:

over push over or or pop

Push and pop refer to the return stack, or is actually xor. See http://www.colorforth.com/inst.htm

like image 68
Lars Brinkhoff Avatar answered Oct 12 '22 01:10

Lars Brinkhoff


In Standard Forth it is

: swap ( a b -- b a ) >r >r 2r> ;

or

: swap ( a b -- b a ) 0 rot nip ; 

or

: swap ( a b -- b a ) 0 rot + ;

or

: swap ( a b -- b a ) 0 rot or ;
like image 41
Marcel Hendrix Avatar answered Oct 12 '22 01:10

Marcel Hendrix


This remark of Charles Moore can easily be misunderstood, because it is in the context of his Forth processors. A SWAP is not a machine instruction for a hardware Forth processor. In general in Forth some definitions are in terms of other definitions, but this ends with certain so called primitives. In a Forth processor those are implemented in hardware, but in all Forth implementations on e.g. host systems or single board computers they are implemented by a sequence of machine instruction, e.g. for Intel :

CODE SWAP pop, ax pop, bx push, ax push, bx END-CODE

He also uses the term "convenient", because SWAP is often avoidable. It is a situation where you need to handle two data items, but they are not in the order you want them. SWAP means a mental burden because you must imagine the stack content changed. One can often keep the stack straight by using an auxiliary stack to temporarily hold an item you don't need right now. Or if you need an item twice OVER is preferable. Or a word can be defined differently, with its parameters in a different order.

Going out of your way to implement SWAP in terms of 4 FORTH words instead of 4 machine instructions is clearly counterproductive, because those FORTH words each must have been implemented by a couple of machine instructions themselves.

like image 3
Albert van der Horst Avatar answered Oct 12 '22 02:10

Albert van der Horst