JAVA BYTE CODE

JavedAhmedSamo 1,268 views 20 slides Nov 07, 2015
Slide 1
Slide 1 of 20
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20

About This Presentation

No description available for this slideshow.


Slide Content

SOFTWARE MAINTENANCE BYTECODE PROF. DR. SAFEEULLAH SOOMRO JAVED AHMED SAMO Course Title Presented to: Presented by:

JAVA BYTE CODE JVM OBJECT CODE .CLASS FILE JAVAC JIKES JAMAICA LILAC JIT PUSH POP iload_2 stack

BYTECODE & JVM Bytecode is an intermediate code that runs on JVM A Java virtual machine (JVM) is an abstract computing machine that enables a computer to run a Java program

Furthermore On Byte code

P-code  (various forms of instruction sets designed for efficient execution by a software interpreter as well as being suitable for further compilation into machine code. / Java bytecode  is the instruction set of the Java virtual machine. Each bytecode is composed by one, or in some cases two, bytes that represent the instruction (opcode), along with zero or more bytes for passing parameters. Advantages of Bytecode Bytecode is architecture independent (and writing a VM is easier than rewriting a compiler for every architecture) Just In-Time (JIT) compiling helps achieve same or better speed than traditional compiled code Introduction to Byte Code Other languages that have an intermediate representation C#-----------------MSIL Perl --------------- perlcompiler PHP --------------- bcompiler Python -------------

Jikes , compiles from Java to Java bytecode (developed by IBM, implemented in C++) Espresso, compiles from Java to Java bytecode (Java 1.0 only ) GCJ , the GNU Compiler for Java, compiles from Java to Java bytecode; it is also able to compile to native machine code and is available as part of the GNU Compiler Collection  (GCC ). Some projects provide Java assemblers to enable writing Java bytecode by hand. Assembly code may be also generated by machine, for example by a compiler targeting a Java virtual machine. Notable Java assemblers include : Jasmin , takes textual descriptions for Java classes, written in a simple assembly-like syntax using Java Virtual Machine instruction set and generates a Java class file Jamaica , a macro assembly language for the Java virtual machine. Java syntax is used for class or interface definition. Method bodies are specified using bytecode instructions . Krakatau  currently contains three tools: a decompiler and disassembler for Java classfiles and an assembler to create classfiles Lilac , an assembler and disassembler for the Java virtual machine . Some projects provide Java assemblers to enable writing Java bytecode

JVM-Types and their prefixes Byte  b   Short  s   Integer  i   (java booleans are mapped to jvm ints!) Long  l   Character  c Single float f   double float d   References  a   to Classes, Interfaces, Arrays These Prefixes used in opcodes ( i add, astore,...) Bytecode Basics

Instructions The JVM has 256 instructions for: • Arithmetic operations •Branch operations •Constant loading operations •Locals operations •Stack operations •Class operations •Method operations

Of the 256 possible byte-long  opcodes , as of 2015, 198 are in use (~77%), 54 are reserved for future use, and 3 instructions (~1%) are set aside as permanently unimplemented. Instructions fall into a number of broad groups: Load and store (e.g. aload_0, istore ) Arithmetic and logic (e.g. ladd , fcmpl ) Type conversion (e.g. i2b, d2i) Object creation and manipulation (new, putfield ) Operand stack management (e.g. swap, dup2) Control transfer (e.g. ifeq , goto ) Method invocation and return (e.g. invokespecial , areturn ) Instructions Conti… Byte  b  Short  s  Integer  i  Long  l  Character  c Single float f  double float d  References  a 

Arithmetic Operations Operands Operations Instructions Conti…

Java Bytecode Explanation Opcode Mnemonic Description nop Does nothing 1 aconst_null Push null on the stack 3 iconst_0 Push int 0 on the stack 4 iconst_1 Push int 1 on the stack 18 ldc <value> Push a one-word (4 bytes) constant onto the stack ldc “Hello” ldc 201 Constant may be an int , float or String Instructions Conti…

Other types of Instructions Control Flow (~20 instructions) if, goto, return Method Calls (4 instructions) Loading and Storing Variables (65 instructions) Creating objects (1 instruction) Using object fields (4 instructions) Arrays (3 instructions)

Javap examples public class Test1 { public int add(int a, int b) { int c= a+b; return c; } } javap -c Test1 javac -g Test1.java Javap included with Java Development Kit (JDK) … public int add( int , int ); Code: 0: iload_1 1: iload_2 2: iadd 3: istore_3 4: iload_3 5: ireturn // add var 1 and 2 // store as local var 3 // store onto stack // return int // push onto stack // push onto stack

public class Test1 { public int add(int a, int b) { int c= a+b; return c; } } … public int add(int a, int b) { int c = a + b; // 0 0:iload_1 // 1 1:iload_2 // 2 2:iadd // 3 3:istore_3 return c; // 4 4:iload_3 // 5 5:ireturn } jad -a Test1 javac -g Test1.java JAD is free, but not included with Java Development Kit (JDK) JAD examples

THANKS
Tags