102.

prev next contents
jsr

jump to subroutine

Jasmin Syntax
     jsr <label> 
<label> is a label name. To define the location of the label, use the <label> name followed by a colon:
 <label>: 
<label> then becomes associated the address of the following instruction. Labels can only be assigned one location in a method. On the other hand, a single <label> can be the target of multiple branch instructions.

Stack

Before

After
...
return-address

...
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 
Bytecode

Type

Description
u1
jsr opcode = 0xA8 (168)
s2
branchoffset
See Also

jsr_w, ret, goto, goto_w

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.


prev next contents
Java Virtual Machine, by Jon Meyer and Troy Downing, O'Reilly Associates


Java Virtual Machine
Java Virtual Machine (Java Series)
ISBN: 1565921941
EAN: 2147483647
Year: 1996
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net