Opcodes · pila · habilitado

OP_SWAP (0x7c)

Intercambia los dos elementos superiores.

Verificado
Pila: entrada → salida
a b → b a
Desde
Script original de Bitcoin
Costo
Base instruction cost 100 plus bytes pushed (2025 VM limits).
Estado en Bitcoin
Igual en Bitcoin (BTC)

Ejemplo

OP_2 OP_10 OP_SWAP OP_SUB OP_8 OP_EQUAL
Paso Pila después (cima a la derecha) Qué ocurre
OP_2 2
OP_10 2 10
OP_SWAP 10 2 Without the swap, OP_SUB would compute 2 − 10 = −8.
OP_SUB 8 Second item minus top item: 10 − 2.
OP_8 8 8
OP_EQUAL 1

Recórrelo opcode por opcode en Bitauth IDE, el depurador de la VM de BCH en el navegador.

Dónde se usa OP_SWAP

  • Contratos CashScript compiler : The compiler emits OP_SWAP when a variable is reassigned inside a branch or loop, and when it builds OP_RETURN data.
  • Actualización May 2020: SigChecks and OP_REVERSEBYTES : Before OP_REVERSEBYTES, contracts reversed byte order with chains of OP_SPLIT, OP_SWAP and OP_CAT.

Fuentes

  1. Libauth BCH_2023_05 opcodes (bch-2023-opcodes.ts)