Skip to content

Stack manipulation

Miden VM stack is a push-down stack of field elements. The stack has a maximum depth of \(2^{32}\), but only the top \(16\) elements are directly accessible via the instructions listed below.

In addition to the typical stack manipulation instructions such as drop, dup, swap etc., Miden assembly provides several conditional instructions which can be used to manipulate the stack based on some condition - e.g., conditional swap cswap or conditional drop cdrop.

Stack manipulation

Instruction Stack_input Stack_output Notes
drop
- (1 cycle)
[a, … ] [ … ] Deletes the top stack item.
dropw
- (4 cycles)
[A, … ] [ … ] Deletes a word (4 elements) from the top of the stack.
padw
- (4 cycles)
[ … ] [0, 0, 0, 0, … ] Pushes four \(0\) values onto the stack.
Note: simple pad is not provided because push.0 does the same thing.
dup.n
- (1-3 cycles)
[ …, a, … ] [a, …, a, … ] Pushes a copy of the \(n\)th stack item onto the stack. dup and dup.0 are the same instruction. Valid for \(n \in \{0, ..., 15\}\)
dupw.n
- (4 cycles)
[ …, A, … ] [A, …, A, … ] Pushes a copy of the \(n\)th stack word onto the stack. dupw and dupw.0 are the same instruction. Valid for \(n \in \{0, 1, 2, 3\}\)
swap.n
- (1-6 cycles)
[a, …, b, … ] [b, …, a, … ] Swaps the top stack item with the \(n\)th stack item. swap and swap.1 are the same instruction. Valid for \(n \in \{1, ..., 15\}\)
swapw.n
- (1 cycle)
[A, …, B, … ] [B, …, A, … ] Swaps the top stack word with the \(n\)th stack word. swapw and swapw.1 are the same instruction. Valid for \(n \in \{1, 2, 3\}\)
swapdw
- (1 cycle)
[D, C, B, A, … ] [B, A, D, C … ] Swaps words on the top of the stack. The 1st with the 3rd, and the 2nd with the 4th.
movup.n
- (1-4 cycles)
[ …, a, … ] [a, … ] Moves the \(n\)th stack item to the top of the stack. Valid for \(n \in \{2, ..., 15\}\)
movupw.n
- (2-3 cycles)
[ …, A, … ] [A, … ] Moves the \(n\)th stack word to the top of the stack. Valid for \(n \in \{2, 3\}\)
movdn.n
- (1-4 cycles)
[a, … ] [ …, a, … ] Moves the top stack item to the \(n\)th position of the stack. Valid for \(n \in \{2, ..., 15\}\)
movdnw.n
- (2-3 cycles)
[A, … ] [ …, A, … ] Moves the top stack word to the \(n\)th word position of the stack. Valid for \(n \in \{2, 3\}\)

Conditional manipulation

Instruction Stack_input Stack_output Notes
cswap
- (1 cycle)
[c, b, a, … ] [e, d, … ] \(d = \begin{cases} a, & \text{if}\ c = 0 \\ b, & \text{if}\ c = 1\ \end{cases}\)
\(e = \begin{cases} b, & \text{if}\ c = 0 \\ a, & \text{if}\ c = 1\ \end{cases}\)
Fails if \(c > 1\)
cswapw
- (1 cycle)
[c, B, A, … ] [E, D, … ] \(D = \begin{cases} A, & \text{if}\ c = 0 \\ B, & \text{if}\ c = 1\ \end{cases}\)
\(E = \begin{cases} B, & \text{if}\ c = 0 \\ A, & \text{if}\ c = 1\ \end{cases}\)
Fails if \(c > 1\)
cdrop
- (2 cycles)
[c, b, a, … ] [d, … ] \(d = \begin{cases} a, & \text{if}\ c = 0 \\ b, & \text{if}\ c = 1\ \end{cases}\)
Fails if \(c > 1\)
cdropw
- (5 cycles)
[c, B, A, … ] [D, … ] \(D = \begin{cases} A, & \text{if}\ c = 0 \\ B, & \text{if}\ c = 1\ \end{cases}\)
Fails if \(c > 1\)

Last update: January 17, 2024
Authors: avenbreaks