OP_JUMP
Definition
This opcode jumps to a location defined by the current location minus the argument to this script (counted in bytes). However, False or a bignum/scriptnum of 0 continues to the next instruction, not re-executing the current one.
(REQ1.0) Jumping to exactly the location after the last instruction (that is, to the end of the script code, but not beyond) is a valid way to end the script (all other validation checks, such as clean stack, are still checked for the script to be valid).
Note that since the offset is specified in bytes, it is possible to jump into an area of the script that is also pushed bytes. For example: "PUSH 010000000000h, JUMP", jumps backwards 1 byte, executing the last byte of the push as code (which pushes 0 to the stack), then executes JUMP again, passing thru because the stack top is false. Script authors might also use "0 IF code ENDIF" to create isolated code sections that can only be JUMPED into.
Error conditions
If the relative location is outside the bounds of the current script code, the script MUST fail. (REQ1.1)
If the maximum ops or sig ops are exceeded, the script MUST abort and fail at that moment (that is, abort out of infinite loops) (REQ1.2)
If the relative location is a byte buffer of length greater than 8 (an out-of-range scriptnum), the script MUST fail. (REQ1.3)
Syntax and Stack
relative location OP_JUMP => item?
- relative location: a backwards offset specified as a number of bytes, relative to the beginning of this instruction. If the relative location field is False (the empty stack item) or a form of 0 (a scriptnum or bignum of 0) then continue with the next instruction (do not jump). A positive relative jump moves towards the beginning of the script. A negative one moves towards the end.
Example Uses
Note that locations in these scripts are denoted by "
Basic countdown loop
PUSH count top: do some code 1SUB DUP IF PUSH top - jumpat # this is evaled at compile time else PUSH FALSE endif jumpat: IFJUMP
Loop 4 times
PUSH FALSE # when ifjump pops this we'll stop looping PUSH top - jumpat # this is evaled at compile time DUP # Make 4 copies of the jump target DUP2 top: # since the stack is false, top, top, top, top, we'll execute the loop 4 times. do some code jumpat: IFJUMP
Function call
0 IF # skip executing this code right now, so we can define functions
funcDefinition:
ELSE
call that function
push continue - funcEnd # first push where we want to resume execution push arg1 # Push the function args push arg2 push funcDefinition - jumpat # Push the function's address jumpat: IFJUMP # call the function continue:
ENDIF
Use stack data to define the loop contents using OP_EXEC
top: PUSH