Lecture 13 Java Virtual Machine Instruction Set

Document Sample
Lecture 13 Java Virtual Machine Instruction Set Powered By Docstoc
					       Lecture 13
  Java Virtual Machine:
     Instruction Set
          Instructors:
       Fu-Chiung Cheng
           (鄭福炯)
      Associate Professor
Computer Science & Engineering
 Tatung Institute of Technology
                                  1
               Java Program

class SumI {
   public static void main (String[] args) {
      int count=10;
      int sum =0;
      for (int index=1;index<count;index++)
          sum=sum+index;
   } // method main
} // class SumI
                                               2
             Java ByteCode

Method void main(java.lang.String[])
 0 bipush 10    // byte push 10 to stack (0x10)
 2 istore_1     // load 10 (top of stack) to count
 3 iconst_0     // push 0 to stack
 4 istore_2     // load 0 (top of stack to sum
 5 iconst_1     // push 1 to stack
 6 istore_3     // load 1 (top of stack) to index
 7 goto 17      // go to 17
                                                3
            Java ByteCode

10 iload_2        // load sum to stack
11 iload_3        // load index to stack
12 iadd           // add
13 istore_2       // store “top of stack” to sum
14 iinc 3 1       // index ++
17 iload_3        // load index to stack
18 iload_1        // load count to stack
19 if_icmplt 10   // if index < count goto 10
22 return                                          3
import java.io.*;
class ReadFile {
   public static void main (String[] args) {
     try {
        FileInputStream in =
           new FileInputStream(args[0]);
        int c;
        while((c = in.read()) != -1)
          System.out.println(c);
        in.close();
    } catch(Exception e) {
      e.printStackTrace();
    }
   } // method main
} // class ReadFile
         Reading 3:
Chap 6: Java Virtual Machine
       Instruction set