Skip to main content

Stack Manipulation

Miden VM stack is a push-down stack of field elements. The stack has a maximum depth of 2322^{32}, but only the top 1616 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.

InstructionStack_inputStack_outputNotes
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 00 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 nnth stack item onto the stack. dup and dup.0 are the same instruction. Valid for n{0,...,15}n \in \{0, ..., 15\}
dupw.n
- (4 cycles)
[ ..., A, ... ][A, ..., A, ... ]Pushes a copy of the nnth stack word onto the stack. dupw and dupw.0 are the same instruction. Valid for n{0,1,2,3}n \in \{0, 1, 2, 3\}
swap.n
- (1-6 cycles)
[a, ..., b, ... ][b, ..., a, ... ]Swaps the top stack item with the nnth stack item. swap and swap.1 are the same instruction. Valid for n{1,...,15}n \in \{1, ..., 15\}
swapw.n
- (1 cycle)
[A, ..., B, ... ][B, ..., A, ... ]Swaps the top stack word with the nnth stack word. swapw and swapw.1 are the same instruction. Valid for n{1,2,3}n \in \{1, 2, 3\}
movup.n
- (1-4 cycles)
[ ..., a, ... ][a, ... ]Moves the nnth stack item to the top of the stack. Valid for n{2,...,15}n \in \{2, ..., 15\}
movupw.n
- (2-3 cycles)
[ ..., A, ... ][A, ... ]Moves the nnth stack word to the top of the stack. Valid for n{2,3}n \in \{2, 3\}
movdn.n
- (1-4 cycles)
[a, ... ][ ..., a, ... ]Moves the top stack item to the nnth position of the stack. Valid for n{2,...,15}n \in \{2, ..., 15\}
movdnw.n
- (2-3 cycles)
[A, ... ][ ..., A, ... ]Moves the top stack word to the nnth word position of the stack. Valid for n{2,3}n \in \{2, 3\}

Conditional manipulation

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