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?
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.
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.
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.
Return Value: The function does not return anything, it swaps the values of the two variables. Programs below illustrate the swap () function:
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
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 ;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With