CS201J: Engineering Software, Fall 2003
|
Section Notes: Friday 7 November 2003
The following code declares an array of integers (foo), creates a new array of integers, and assigns values to array elements 0 through 2, and then prints out the contents of array element 2. Running java FooArray on the command line produces the result 3. Taste-testing Byte Codes
The line numbers indicated to the left of the code correspond with the assembly code below to help you figure out what's going out at each step.
14 public class FooArray { 15 public static void main(String[] args) { 16 17 int[] foo; 18 foo = new int[4]; 19 20 foo[0] = 1; 21 foo[1] = 2; 22 foo[2] = 3; 23 24 System.out.println(foo[2]); 25 } 26 27 }The following assembly code was produced by running the command J:\CS201J\JASMIN~1>D-Java -o jasmin FooArray.class. D-java is a class file disassembler that has an option to output java byte codes in Jasmin format (-o jasmin).The Jasmin assembly file output for FooArray.class looks like the code below:
; ; Output created by D-Java (mailto:umsilve1@cc.umanitoba.ca) ; ;Classfile version: ; Major: 45 ; Minor: 3 //Standard Initializer: .source FooArray.java .class public synchronized FooArray .super java/lang/Object ; >> METHOD 1 << //Sets up the stack for the array: .method public <init>()V .limit stack 1 .limit locals 1 .line 14 .var 0 is this LFooArray; from Label1 to Label2 //The variable at location 0 contains the call to FooArray Label1: aload_0 //Pushes the variable at location 0, call to FooArray, onto the stack invokenonvirtual java/lang/Object/<init>()V return Label2: .end method ; >> METHOD 2 << //Creates a new stack frame and sets it up for main: .method public static main([Ljava/lang/String;)V .limit stack 3 .limit locals 2 .line 18 .var 0 is args [Ljava/lang/String; from Label1 to Label3 //The variable at location 0 in the main stack frame contains the //parameters to String[] in line 15 Label1: iconst_4 //push the constants 0 through 4 onto the stack- represents the //indexes in the declared array. newarray int //allocates a single-dimensional array of integers astore_1 //pops the reference to the array off of the stack and stores it //in variable 1. .line 20 .var 1 is foo [I from Label2 to Label3 Label2: aload_1 //pushes the variable at location 1 (the array) onto the stack iconst_0 //pushes 0 onto the stack iconst_1 //pushes 1 onto the stack iastore //takes 1 and stores it as the first index (0) in the foo array //of integers. .line 21 aload_1 //pushes the variable at location 1 (the array) onto the stack iconst_1 //pushes 1 onto the stack iconst_2 //pushes 2 onto the stack iastore //takes 2 and stores it as the second index (1) in the foo array //of integers. .line 22 aload_1 //pushes the variable at location 1 (the array) onto the stack iconst_2 //pushes 2 onto the stack iconst_3 //pushes 3 onto the stack iastore //takes 3 and stores it as the third index (2) in the foo array //of integers .line 24 getstatic java/lang/System/out Ljava/io/PrintStream; //"Ljava/io/PrintStream;" is a reference to a PrintStream object; getstatic // pops the reference from the stack, retrieves the value of //java/lang/System/out, and pushes it onto the stack aload_1 //pushes the variable at location 1 (the array) onto the stack iconst_2 //pushes 2 onto the stack iaload //retrieves the value of index 2 (which is 3), which was just //pushed onto the stack, and places it on the stack invokevirtual java/io/PrintStream/println(I)V //invokes the java method to print the contents at the top of the // stack, the value 3. .line 25 return Label3: .end methodFor further descriptions of java byte code instructions in Jasmin syntax, visit http://mrl.nyu.edu/~meyer/jasmin/.
University of Virginia Department of Computer Science CS 201J: Engineering Software |
Sponsored by the National Science Foundation |
cs201j-staff@cs.virginia.edu |