jsr :
jump to subroutine : index : visitJumpInsn()
- Description
- This calls a local subroutine defined within the body of a method. It is
used to implement Java's finally clause.
jsr first pushes the address (pc + 3) onto the operand stack,
where pc is the address of this jsr instruction in the
bytecode. The address (pc + 3) is the address of instruction that
immediately follows the jsr instruction in bytecode - it is used used
by the ret instruction to return from the subroutine.
Next, execution branches to the address (pc + branchoffset),
where pc is the address of the jsr opcode in the bytecode and
branchoffset is the 16-bit signed integer parameter following the
jsr opcode in the bytecode. If you are using Jasmin,
branchoffset is computed for you from the address of <label>. - Example
; This example method uses a PrintMe subroutine to invoke the System.out.println() method.
.method usingSubroutine()V
ldc "Message 1"
jsr PrintMe ; print "Message 1"
ldc "Message 2"
jsr PrintMe ; print "Message 2"
ldc "Message 3"
jsr PrintMe ; print "Message 3"
return ; now return from usingSubroutine
; define the PrintMe subroutine ...
PrintMe:
astore_1 ; store return-address in local variable 1
; call System.out.println()
getstatic java/lang/System/out Ljava/io/PrintStream;
invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
ret 1 ; return to the return-address in local variable 1
.end method
- Notes
- 1. Addresses are measured in bytes from the start of the bytecode (i.e.
address 0 is the first byte in the bytecode of the currently executing
method).
2. Subroutines complicate the work of the class file verifier, so extensive use
of subroutines may slow down verification speeds. - See also
- jsr_w ret goto goto_w
- Stack
Before
|
After |
... |
return-address |
|
... |
- Bytecode
Type
|
Description |
u1
|
jsr
opcode = 0xA8 (168)
|
s2
|
branchoffset |