Skip to main content

Stack Manipulation

Here 0 <= i,j,k <= 15 if not stated otherwise.

TVM Instructions Content List

OpcodeFift syntaxStackDescriptionGas
Please enter a search query
No results found

Basic stack manipulation primitives

xxxxxxx
Opcode
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Fift syntax
xxxxxxxxxxxxxxxxx
Stack
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Description
xxxx
Gas
00NOP-Does nothing.18
01SWAPx y - y xSame as s1 XCHG0.18
0is[i] XCHG0Interchanges s0 with s[i], 1 <= i <= 15.18
10ijs[i] s[j] XCHGInterchanges s[i] with s[j], 1 <= i < j <= 15.26
11iis0 [ii] s() XCHGInterchanges s0 with s[ii], 0 <= ii <= 255.26
1is1 s[i] XCHGInterchanges s1 with s[i], 2 <= i <= 15.18
2is[i] PUSHPushes a copy of the old s[i] into the stack.18
20DUPx - x xSame as s0 PUSH.18
21OVERx y - x y xSame as s1 PUSH.18
3is[i] POPPops the old s0 value into the old s[i]. Equivalent to s[i] XCHG0 DROP18
30DROPx -Same as s0 POP, discards the top-of-stack value.18
31NIPx y - ySame as s1 POP.18

Complex stack manipulation

xxxxxxx
Opcode
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Fift syntax
xxxxxxxxxxxxxxxxx
Stack
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Description
xxxx
Gas
4ijks[i] s[j] s[k] XCHG3Equivalent to s2 s[i] XCHG s1 s[j] XCHG s[k] XCHG0.26
50ijs[i] s[j] XCHG2Equivalent to s1 s[i] XCHG s[j] XCHG0.26
51ijs[i] s[j] XCPUEquivalent to s[i] XCHG0 s[j] PUSH.26
52ijs[i] s[j-1] PUXCEquivalent to s[i] PUSH SWAP s[j] XCHG0.26
53ijs[i] s[j] PUSH2Equivalent to s[i] PUSH s[j+1] PUSH.26
540ijks[i] s[j] s[k] XCHG3_lLong form of XCHG3.34
541ijks[i] s[j] s[k] XC2PUEquivalent to s[i] s[j] XCHG2 s[k] PUSH.34
542ijks[i] s[j] s[k-1] XCPUXCEquivalent to s1 s[i] XCHG s[j] s[k-1] PUXC.34
543ijks[i] s[j] s[k] XCPU2Equivalent to s[i] XCHG0 s[j] s[k] PUSH2.34
544ijks[i] s[j-1] s[k-1] PUXC2Equivalent to s[i] PUSH s2 XCHG0 s[j] s[k] XCHG2.34
545ijks[i] s[j-1] s[k-1] PUXCPUEquivalent to s[i] s[j-1] PUXC s[k] PUSH.34
546ijks[i] s[j-1] s[k-2] PU2XCEquivalent to s[i] PUSH SWAP s[j] s[k-1] PUXC.34
547ijks[i] s[j] s[k] PUSH3Equivalent to s[i] PUSH s[j+1] s[k+1] PUSH2.34
55ij[i+1] [j+1] BLKSWAPPermutes two blocks s[j+i+1] … s[j+1] and s[j] … s0.
0 <= i,j <= 15
Equivalent to [i+1] [j+1] REVERSE [j+1] 0 REVERSE [i+j+2] 0 REVERSE.
26
5513ROT2
2ROT
a b c d e f - c d e f a bRotates the three topmost pairs of stack entries.26
550i[i+1] ROLLRotates the top i+1 stack entries.
Equivalent to 1 [i+1] BLKSWAP.
26
55i0[i+1] -ROLL
[i+1] ROLLREV
Rotates the top i+1 stack entries in the other direction.
Equivalent to [i+1] 1 BLKSWAP.
26
56ii[ii] s() PUSHPushes a copy of the old s[ii] into the stack.
0 <= ii <= 255
26
57ii[ii] s() POPPops the old s0 value into the old s[ii].
0 <= ii <= 255
26
58ROTa b c - b c aEquivalent to 1 2 BLKSWAP or to s2 s1 XCHG2.18
59ROTREV
-ROT
a b c - c a bEquivalent to 2 1 BLKSWAP or to s2 s2 XCHG2.18
5ASWAP2
2SWAP
a b c d - c d a bEquivalent to 2 2 BLKSWAP or to s3 s2 XCHG2.18
5BDROP2
2DROP
a b - Equivalent to DROP DROP.18
5CDUP2
2DUP
a b - a b a bEquivalent to s1 s0 PUSH2.18
5DOVER2
2OVER
a b c d - a b c d a bEquivalent to s3 s2 PUSH2.18
5Eij[i+2] [j] REVERSEReverses the order of s[j+i+1] … s[j].26
5F0i[i] BLKDROPEquivalent to DROP performed i times.26
5Fij[i] [j] BLKPUSHEquivalent to PUSH s(j) performed i times.
1 <= i <= 15, 0 <= j <= 15.
26
60PICK
PUSHX
Pops integer i from the stack, then performs s[i] PUSH.18
61ROLLXPops integer i from the stack, then performs 1 [i] BLKSWAP.18
62-ROLLX
ROLLREVX
Pops integer i from the stack, then performs [i] 1 BLKSWAP.18
63BLKSWXPops integers i,j from the stack, then performs [i] [j] BLKSWAP.18
64REVXPops integers i,j from the stack, then performs [i] [j] REVERSE.18
65DROPXPops integer i from the stack, then performs [i] BLKDROP.18
66TUCKa b - b a bEquivalent to SWAP OVER or to s1 s1 XCPU.18
67XCHGXPops integer i from the stack, then performs s[i] XCHG.18
68DEPTH- depthPushes the current depth of the stack.18
69CHKDEPTHi -Pops integer i from the stack, then checks whether there are at least i elements, generating a stack underflow exception otherwise.18/58
6AONLYTOPXPops integer i from the stack, then removes all but the top i elements.18
6BONLYXPops integer i from the stack, then leaves only the bottom i elements. Approximately equivalent to DEPTH SWAP SUB DROPX.18
6Cij[i] [j] BLKDROP2Drops i stack elements under the top j elements.
1 <= i <= 15, 0 <= j <= 15
Equivalent to [i+j] 0 REVERSE [i] BLKDROP [j] 0 REVERSE.
26

TVM Instructions Content List