Lab 9 Java Bytecode & The Jasmin Assembler

Slides:



Advertisements
Similar presentations
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Advertisements

Virtual Machines Matthew Dwyer 324E Nichols Hall
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Code Generation Introduction. Compiler (scalac, gcc) Compiler (scalac, gcc) machine code (e.g. x86, arm, JVM) efficient to execute i=0 while (i < 10)
IT253: Computer Organization Lecture 6: Assembly Language and MIPS: Programming Tonga Institute of Higher Education.
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
CS 153: Concepts of Compiler Design November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Advice Weaving in AspectJ Alex Gontmakher. Outline Possible implementation approaches Quick JVM primer AJC implementation Performance Evaluation.
1 Java Virtual Machine Professor Yihjia Tsai Tamkang University.
IPT Readings on Instrumentation, Profiling, and Tracing Seminar presentation by Alessandra Gorla University of Lugano December 7, 2006.
Procedure calls (1) The fact: Most programming languages support the concept of procedures (methods). Each method has its own local variables that are.
Consider With x = 10 we may proceed as (10-1) = 9 (10-7) = 3 (9*3) = 27 (10-11) = -1 27/(-1) = -27 Writing intermediates on paper.
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
1 Programming Languages Translation  Lecture Objectives:  Be able to list and explain five features of the Java programming language.  Be able to explain.
1 Languages and Compilers (SProg og Oversættere) Lecture 9 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
Compiler design Computer Science Rensselaer Polytechnic Lecture 1.
Code Generation Introduction. Compiler (scalac, gcc) Compiler (scalac, gcc) machine code (e.g. x86, arm, JVM) efficient to execute i=0 while (i < 10)
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
CSC 8505 Compiler Construction IR Example: Java Bytecode (looking inside class files)
Java Programming Robert Chatley William Lee
Application Security Tom Chothia Computer Security, Lecture 14.
1 Intro to Computer Science I Chapter 1 Introduction to Computation Algorithms, Processors, and Programs.
UNDER THE HOOD: THE JAVA VIRTUAL MACHINE Lecture 24 – CS2110 – Fall 2009.
CSCI 273: Processing An Introduction. Programming Languages –An abstract "human understandable" language for telling the computer what to do –The abstract.
The Java Virtual Machine 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Krakatoa: Decompilation in Java “Does Bytecode Reveal Source?” Todd A. Proebsting Scott A. Watterson The University of Arizona Presented by Karl von Randow.
Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.
Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and.
FRST JAVA PROGRAM. Getting Started with Java Programming A Simple Java Application Compiling Programs Executing Applications.
1 Introduction to JVM Based on material produced by Bill Venners.
A Java Compiler Overview. October 21, 2003Shane A. Brewer2 Who Am I? Shane A. Brewer Masters Graduate.
Syntax Directed Translation Compiler Design Lecture (03/16//98) Computer Science Rensselaer Polytechnic.
CS591x A very brief introduction to Java. Java Developed by Sun Microsystems was intended a language for embedded applications became a general purpose.
CSCI Processing CSCI Introduction to Algorithm Design An Introduction.
4-Nov-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic 1: The Java Environment Maj Joel.
CS Software Studio Java Lab 1 Meng-Ting Wang PLLAB, Computer Science Department, National Tsing-Hua University.
More on MIPS programs n SPIM does not support everything supported by a general MIPS assembler. For example, –.end doesn’t work Use j $ra –.macro doesn’t.
Code generation exercises. Function body Transform the following code into java bytecode: def middle(small: Int, big: Int): Int = { val mid = small +
Chap. 10, Intermediate Representations J. H. Wang Dec. 14, 2015.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 18: Code Safety and Virtual Machines
CS 153: Concepts of Compiler Design November 4 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Java FilesOops - Mistake Java lingoSyntax
CS 153: Concepts of Compiler Design November 18 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.
Code generation exercises. Function body Transform the following code into java bytecode: def middle(small: Int, big: Int): Int = { val mid = small +
20-Feb-16 javap. Bytecode Java is compiled into bytecode, which is intermediate between Java and a “real” assembly language To implement Java, it is only.
3/5/2002e-business and Information Systems1 Java Java Java Virtual Machine (JVM) Java Application Program Interface (API) HW Kernel API Application Programs.
UNDER THE HOOD: THE JAVA VIRTUAL MACHINE II CS2110 Fall 200 Lecture 25 1.
Review on Program Challenge CSc3210 Yuan Long.
Recap: Printing Trees into Bytecodes To evaluate e 1 *e 2 interpreter –evaluates e 1 –evaluates e 2 –combines the result using * Compiler for e 1 *e 2.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
RealTimeSystems Lab Jong-Koo, Lim
CS 432: Compiler Construction Lecture 15
Java and C II CSE 351 Spring 2017 Instructor: Ruth Anderson
The Java Virtual Machine (JVM)
CS216: Program and Data Representation
MIC-1 SIMULATOR Programming in IJVM
CS 153: Concepts of Compiler Design November 2 Class Meeting
Compiling, Assembling and Executing Java using Java
Java Virtual Machine (JVM)
Introduction to Algorithm Design
Lecture 19: 0xCAFEBABE (Java Byte Codes) CS201j: Engineering Software
Byte Code Verification
Course Overview PART I: overview material PART II: inside a compiler
Compiler Design 21. Intermediate Code Generation
CS 153: Concepts of Compiler Design November 6 Class Meeting
CMPE 152: Compiler Design April 11 Class Meeting
Java and C II CSE 351 Spring 2017 Instructor: Ruth Anderson
Compiler Design 21. Intermediate Code Generation
CMPE 152: Compiler Design April 16 Class Meeting
Presentation transcript:

Lab 9 Java Bytecode & The Jasmin Assembler

Java Bytecode javac compiles your java source code into an intermediate set of instruction called the bytecode. JVM (Java Virtual Machine) is a machine that interfaces with the hardware to run the byte code instructions.

Java Bytecode JVM can be used as an interface to other programming languages that translates to the byte code instruction set But, it is hard to translate to a class file which is a binary file

Jasmin Jasmin is a Java Assembler that can translate Bytecode instructions to a binary class You can download jasmin from http://sourceforge.net/projects/jasmin/files/jasmin/jasmin-2.4/jasmin-2.4.zip/download. $ sudo mv jasmin.jar /usr/local/lib $ export CLASSPATH="/usr/local/lib/jasmin.jar:$CLASSPATH“

Using Jasmin Write your program using the bytecode instructions Assemble the code using jasmin to generate class files Execute the class files using java

Jasmin Code Structure Comment Area Header Area Field Area Method Area

Jasmin Code Structure Comment Area Header Area ; Classfile version: ; Major: 49 ; Minor: 0 .source MyApp.java .class public MyApp .super java/lang/Object

Jasmin Code Structure Field Area Method Area .field public static x I .method public <init>()V aload_0 invokenonvirtual java/lang/Object/<init>()V return .end method .method public static main([Ljava/lang/String;)V .limit stack 10 .limit locals 10

Example Our task is to write the following program in bytecode instructions, public class simple { public static int main(String[] args){ int x = 0; x = System.in.read(); x = x + 3; System.out.println(x); return 0; }

Example Create the file simple.j and write the instructions. .class public simple .super java/lang/Object .method public <init>()V aload_0 invokespecial java/lang/Object/<init>()V return .end method

Example .method public static main([Ljava/lang/String;)V .limit stack 5 .limit locals 100 ldc 0 istore 1 ; initialize x to zero and store it ; in local variable 1 ; the read function starts at this point istore 50 ; storage for a dummy integer for ; reading it by read

Example .method public static main([Ljava/lang/String;)V .limit stack 5 .limit locals 100 ldc 0 istore 1 ; initialize x to zero and store it ; in local variable 1 ; the read function starts at this point istore 50 ; storage for a dummy integer for ; reading it by read

Example 1 2 .. 50 51 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 1 2 .. 50 51 99 Stack Locals

Example 49 1 2 .. 50 51 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 49 1 2 .. 50 51 99 Stack Locals

Example 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 1 2 .. 50 51 49 99 Stack Locals

Example 49 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 49 1 2 .. 50 51 49 99 Stack Locals

Example Ascii for LF 10 49 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 Ascii for LF 10 49 1 2 .. 50 51 49 99 Stack Locals

Example 39 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 39 1 2 .. 50 51 49 99 Stack Locals

Example 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 1 2 .. 50 51 49 99 Stack Locals

Example 49 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 49 1 2 .. 50 51 49 99 Stack Locals

Example 32 49 1 2 .. 50 51 49 99 Ascii for Space Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 32 49 1 2 .. 50 51 49 99 Ascii for Space Stack Locals

Example 17 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 17 1 2 .. 50 51 49 99 Stack Locals

Example 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 1 2 .. 50 51 49 99 Stack Locals

Example 49 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 49 1 2 .. 50 51 49 99 Stack Locals

Example 48 49 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 48 49 1 2 .. 50 51 49 99 Stack Locals

Example 1 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 1 1 2 .. 50 51 49 99 Stack Locals

Example 10 1 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 10 1 1 2 .. 50 51 49 99 Stack Locals

Example 10 1 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 10 1 1 2 .. 50 51 49 99 Stack Locals

Example 1 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 1 1 2 .. 50 51 49 99 Stack Locals

Example 1 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 1 1 2 .. 50 51 49 99 Stack Locals

Example 1 2 .. 50 51 49 99 Stack Locals Label1: getstatic java/lang/System/in Ljava/io/InputStream; invokevirtual java/io/InputStream/read()I istore 51 iload 51 ldc 10 isub ifeq Label2 ldc 32 ldc 48 iload 50 imul iadd istore 50 goto Label1 1 2 .. 50 51 49 99 Stack Locals

Example Label2: ; now our dummy integer contains the ; integer read from the keyboard iload 50 ; read function ends here istore 1 ; store this value in x iload 1 ldc 3 iadd istore 1 ; x=x+3 getstatic java/lang/System/out Ljava/io/PrintStream; swap invokevirtual java/io/PrintStream/println(I)V ; print x return ; return from main .end method

Example Use jasmin to translate the simple.j file to the simple.class binary file $ java -cp $CLASSPATH jasmin.Main simple.j Or, $ java –jar jasmin.jar simple.j Execute the class file $ java simple

Activity Write the equivalent bytecode instructions for the following class and use jasmin to generate the class file. public class simple { public static int main(String[] args){ int x = 0; int y = 5; x = System.in.read(); x = x * y - 2; System.out.println(x); return 0; }