<p>A stack-based language necessarily contains at least &quot;push&quot; and &quot;pop&quot; instructions. However, these typically aren't the only ways to manipulate the stack. For example, several languages have an instruction to swap the two values at the top of the stack. <a href="https://github.com/bbrk24/Trilangle" rel="noreferrer">Trilangle</a> has an instruction to look arbitrarily far down the stack, but I don't know how common that is.</p>
<p>What are some of the most common stack manipulation operations, and what benefits do they provide?</p>

<language-design><stack>
What operations are common in stack-based languages?
CC BY-SA 4.0
2023-07-01T11:52:27.767Z
Related: [Satoshi Nakamoto](https://en.wikipedia.org/wiki/Satoshi_Nakamoto) (the creator of Bitcoin) [was a Forth programmer](https://www.youtube.com/watch?v=i7Vz6r6p1o4&t=35m9s)! (at 35 min 09 secs). [Forth](https://en.wikipedia.org/wiki/Forth_%28programming_language%29) (a stack-based language).
Peter Mortensen
<p>Stack manipulation instructions in various bytecode formats:</p>
<ul>
<li>Java Virtual Machine (JVM): pop, pop2, dup, dup_x1, dup_x2, dup2, dup2_x1, dup2_x2, swap (<a href="https://en.wikipedia.org/wiki/List_of_Java_bytecode_instructions" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/List_of_Java_bytecode_instructions</a>, <a href="https://docs.oracle.com/javase/specs/jvms/se12/html/jvms-6.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/specs/jvms/se12/html/jvms-6.html</a>)</li>
<li>Python: COPY(i), SWAP(i) (<a href="https://docs.python.org/3.11/library/dis.html" rel="nofollow noreferrer">https://docs.python.org/3.11/library/dis.html</a>), ROT_TWO, ROT_THREE, ROT_FOUR, DUP_TOP, DUP_TOP_TWO (<a href="https://docs.python.org/3.10/library/dis.html" rel="nofollow noreferrer">https://docs.python.org/3.10/library/dis.html</a>), POP_TOP (both)</li>
<li>Common Intermediate Language (CIL, related to .NET): dup, pop (<a href="https://en.wikipedia.org/wiki/List_of_CIL_instructions" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/List_of_CIL_instructions</a>)</li>
</ul>

CC BY-SA 4.0
2023-05-17T14:48:11.170Z
2023-05-17T14:48:11.170Z
Solomon Ucko
<p><em>Data for this post comes from <a href="https://github.com/Vyxal/Vyxal/discussions/1728#discussioncomment-5769518" rel="noreferrer">the Vyxal element corpus</a> and the <a href="https://github.com/Adriandmen/05AB1E/issues/182" rel="noreferrer">05AB1E element corpus</a>, both of which show the most commonly used built-ins from over 1500 answers in each language on the code golf stack exchange. It also comes from a list of things languages like Forth and Factor have</em></p>
<p>Here's a list of some things that are helpful for a stack language. Their stack effects are given as <code>stack before -&gt; stack after</code>:</p>
<ul>
<li>Pop (<code>a -&gt; </code>) and push (<code> -&gt; a</code>) (as mentioned in the question)</li>
<li>Duplicate (dup) <code>a -&gt; a a</code></li>
<li>Over <code>a b -&gt; a b a</code></li>
<li>Swap <code>a b -&gt; b a</code> (also mentioned in the question)</li>
<li>Wrap into list <code>a b c ... -&gt; [a, b, c, ...]</code></li>
<li>Rotate left <code>a b c ... -&gt; b c ... a</code></li>
<li>Rotate right <code>a b c ... -&gt; ... a b c</code></li>
<li>Rotate <code>a b c -&gt; c a b</code></li>
<li>triplicate (duplicate then duplicate again) <code>a -&gt; a a a</code></li>
<li>n-licate (duplicate n times) <code>a -&gt; a a ... a</code></li>
<li>nip (alternatively, swap-pop) <code>a b -&gt; b</code> and <code>a b c -&gt; a c</code></li>
<li>2dup <code>a b -&gt; a b a b</code></li>
<li>n-over (over but the nth item from the top) <code>a b c ... -&gt; a b c ... stack[len(stack) - n]</code> (thanks to @mousetail for the suggestion) (mentioned in question too)</li>
<li>push stack length <code>a b c ... -&gt; a b c ... len(stack)</code></li>
<li>dump <code>x, [a, b, c] -&gt; x a b c</code></li>
</ul>

CC BY-SA 4.0
2023-05-17T11:35:00.850Z
2023-05-20T03:31:05.147Z
2023-05-20T03:31:05.147Z
lyxal
CC BY-SA 4.0
2023-05-17T11:36:55.110Z
Does nip truncate the whole thing (i.e. `a ... b c` -> `c`) or is it equivalent to swap-pop?
Bbrk24
CC BY-SA 4.0
2023-05-17T11:40:38.403Z
the [factor documentation](https://docs.factorcode.org/content/word-nip%2Ckernel.html) for nip suggests swap-pop
lyxal
CC BY-SA 4.0
2023-05-17T12:10:17.497Z
N-over is also really useful, copy the N-th element to the front
mousetail 'he-him'
CC BY-SA 4.0
2023-05-17T14:14:06.153Z
N-over appears to be the same thing as the “indexed read” operator I mentioned in the question, with the difference being that Trilangle’s operator pulls the value of `n` off the top of the stack.
Bbrk24
CC BY-SA 4.0
2023-05-17T14:32:22.347Z
"Whole stack" operations like "wrap into list" are common in esoteric and recreational golfing languages, but some of the more "serious" stack-based languages like Factor and Cat tend to avoid those since they're pretty difficult to do static analysis against.
Silvio Mayolo
# What operations are common in stack-based languages?
Posted by: username_0 on 2023-05-17
A stack-based language necessarily contains at least "push" and "pop" instructions. However, these typically aren't the only ways to manipulate the stack. For example, several languages have an instruction to swap the two values at the top of the stack. [Trilangle](https://github.com/bbrk24/Trilangle) has an instruction to look arbitrarily far down the stack, but I don't know how common that is.

What are some of the most common stack manipulation operations, and what benefits do they provide?
**Tags:** language-design stack
> **Comments:**
> **username_1**: Related: [Satoshi Nakamoto](https://en.wikipedia.org/wiki/Satoshi_Nakamoto) (the creator of Bitcoin) [was a Forth programmer](https://www.youtube.com/watch?v=i7Vz6r6p1o4&t=35m9s)! (at 35 min 09 secs). [Forth](https://en.wikipedia.org/wiki/Forth_%28programming_language%29) (a stack-based language).  
---
## 2 answers
### Answer by username_3 on 2023-05-17. Score: 11 | Accepted answer
*Data for this post comes from [the Vyxal element corpus](https://github.com/Vyxal/Vyxal/discussions/1728#discussioncomment-5769518) and the [05AB1E element corpus](https://github.com/Adriandmen/05AB1E/issues/182), both of which show the most commonly used built-ins from over 1500 answers in each language on the code golf stack exchange. It also comes from a list of things languages like Forth and Factor have*

Here's a list of some things that are helpful for a stack language. Their stack effects are given as `stack before -> stack after`:

* Pop (`a ->` ) and push ( `-> a`) (as mentioned in the question)
* Duplicate (dup) `a -> a a`
* Over `a b -> a b a`
* Swap `a b -> b a` (also mentioned in the question)
* Wrap into list `a b c ... -> [a, b, c, ...]`
* Rotate left `a b c ... -> b c ... a`
* Rotate right `a b c ... -> ... a b c`
* Rotate `a b c -> c a b`
* triplicate (duplicate then duplicate again) `a -> a a a`
* n-licate (duplicate n times) `a -> a a ... a`
* nip (alternatively, swap-pop) `a b -> b` and `a b c -> a c`
* 2dup `a b -> a b a b`
* n-over (over but the nth item from the top) `a b c ... -> a b c ... stack[len(stack) - n]` (thanks to @mousetail for the suggestion) (mentioned in question too)
* push stack length `a b c ... -> a b c ... len(stack)`
* dump `x, [a, b, c] -> x a b c`
> **Comments:**
> **username_0** (2023-05-17): Does nip truncate the whole thing (i.e. `a ... b c` -> `c`) or is it equivalent to swap-pop?  
> **username_3** (2023-05-17): the [factor documentation](https://docs.factorcode.org/content/word-nip%2Ckernel.html) for nip suggests swap-pop  
> **username_4** (2023-05-17): N-over is also really useful, copy the N-th element to the front  
> **username_0** (2023-05-17): N-over appears to be the same thing as the “indexed read” operator I mentioned in the question, with the difference being that Trilangle’s operator pulls the value of `n` off the top of the stack.  
> **username_5** (2023-05-17): "Whole stack" operations like "wrap into list" are common in esoteric and recreational golfing languages, but some of the more "serious" stack-based languages like Factor and Cat tend to avoid those since they're pretty difficult to do static analysis against.  
---
### Answer by username_2 on 2023-05-17. Score: 4
Stack manipulation instructions in various bytecode formats:

* Java Virtual Machine (JVM): pop, pop2, dup, dup\_x1, dup\_x2, dup2, dup2\_x1, dup2\_x2, swap (<https://en.wikipedia.org/wiki/List_of_Java_bytecode_instructions>, <https://docs.oracle.com/javase/specs/jvms/se12/html/jvms-6.html>)
* Python: COPY(i), SWAP(i) (<https://docs.python.org/3.11/library/dis.html>), ROT\_TWO, ROT\_THREE, ROT\_FOUR, DUP\_TOP, DUP\_TOP\_TWO (<https://docs.python.org/3.10/library/dis.html>), POP\_TOP (both)
* Common Intermediate Language (CIL, related to .NET): dup, pop (<https://en.wikipedia.org/wiki/List_of_CIL_instructions>)
---
