Review on Program Challenge 1 - 4 CSc3210 Yuan Long.

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.
Exercise 1 Generics and Assignments. Language with Generics and Lots of Type Annotations Simple language with this syntax types:T ::= Int | Bool | T =>
Code Generation Introduction. Compiler (scalac, gcc) Compiler (scalac, gcc) machine code (e.g. x86, arm, JVM) efficient to execute i=0 while (i < 10)
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
Lab 9 Java Bytecode & The Jasmin Assembler
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Procedure calls (1) The fact: Most programming languages support the concept of procedures (methods). Each method has its own local variables that are.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
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.
5/6/99 Ashish Sabharwal1 JVM Architecture n Local storage area –Randomly accessible –Just like standard RAM –Stores variables (eg. an array) –Have to specify.
1 Languages and Compilers (SProg og Oversættere) Lecture 9 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
CSc 453 Interpreters & Interpretation Saumya Debray The University of Arizona Tucson.
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)
Compiler Construction Lecture 17 Mapping Variables to Memory.
Application Security Tom Chothia Computer Security, Lecture 14.
1 Intro to Computer Science I Chapter 1 Introduction to Computation Algorithms, Processors, and Programs.
CSCI 273: Processing An Introduction. Programming Languages –An abstract "human understandable" language for telling the computer what to do –The abstract.
Krakatoa: Decompilation in Java “Does Bytecode Reveal Source?” Todd A. Proebsting Scott A. Watterson The University of Arizona Presented by Karl von Randow.
1 October 1, October 1, 2015October 1, 2015October 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.
ITEC 352 Lecture 20 JVM Intro. Functions + Assembly Review Questions? Project due today Activation record –How is it used?
Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and.
1 Introduction to JVM Based on material produced by Bill Venners.
Code Compiled with javac static int k = 0; static boolean action(int si, boolean ob, int sm, int pr) { if (sm + 2*pr > 10 && !(si
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.
CSCI Processing CSCI Introduction to Algorithm Design An Introduction.
Code Optimization 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a.
Programming Languages
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 +
#include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3.
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
More Examples of Abstract Interpretation Register Allocation.
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.
CSCE 314 Programming Languages
Code generation exercises. Function body Transform the following code into java bytecode: def middle(small: Int, big: Int): Int = { val mid = small +
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
UNDER THE HOOD: THE JAVA VIRTUAL MACHINE II CS2110 Fall 200 Lecture 25 1.
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.
RealTimeSystems Lab Jong-Koo, Lim
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
The Java Virtual Machine (JVM)
CS216: Program and Data Representation
MIC-1 SIMULATOR Programming in IJVM
Java package classes Java package classes.
Computer Architecture and Organization Miles Murdocca and Vincent Heuring Chapter 4 – The Instruction Set Architecture.
CS 153: Concepts of Compiler Design November 2 Class Meeting
Java Virtual Machine (JVM)
Java Byte Codes (0xCAFEBABE) cs205: engineering software
CSc 453 Interpreters & Interpretation
Introduction to Algorithm Design
Lecture 19: 0xCAFEBABE (Java Byte Codes) CS201j: Engineering Software
Byte Code Verification
Compiler Design 21. Intermediate Code Generation
CS 153: Concepts of Compiler Design November 6 Class Meeting
CMPE 152: Compiler Design April 11 Class Meeting
Compiler Design 21. Intermediate Code Generation
CMPE 152: Compiler Design April 16 Class Meeting
CSc 453 Interpreters & Interpretation
Presentation transcript:

Review on Program Challenge CSc3210 Yuan Long

The purpose of program challenges Program Challenge 1: Setting up environment. Program Challenge 2: Checking bytecode file. Program Challenge 3: Basic program for addition in Jasmin. Program Challenge 4: Simple program for calculating one expression in Jasmin.

Program Challenge 1: Setting up environment Connecting with account in server ssh Compiling and running Java program javac *.java java * Compiling and running Jasmin program java –jar Path_Of_Jasmin.jar *.j java *

Program Challenge 2: Checking bytecode file Checking the hex code content in a bytecode file xxd *.class Why we use symbol “>” ? Redirect the output to a file

XXD

Program Challenge 3: Basic program for addition in Jasmin Instructions for adding two integer numbers

Program Challenge 3: Basic program for adding in Jasmin Instructions for adding two integer numbers bipush 12 ; push 12 into stack 12 stack

Program Challenge 3: Basic program for adding in Jasmin Instructions for adding two integer numbers bipush 12 bipush 5;push 5 into stack 5 12 stack

Program Challenge 3: Basic program for adding in Jasmin Instructions for adding two integer numbers bipush 12 bipush 5 iadd ;pop the top two elements form stack and push the addition result into stack 17 stack =12+5

Program Challenge 3: Basic program for adding in Jasmin Instructions for adding two integer numbers bipush 12 bipush 5 iadd 17 stack To be printed out.

Program Challenge 3: Basic program for adding in Jasmin Final program- plus.j.class public plus.super java/lang/Object.method public static main([Ljava/lang/String;)V.limit stack 3 getstatic java/lang/System/out Ljava/io/PrintStream; ldc "This is Yuan." invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V getstatic java/lang/System/out Ljava/io/PrintStream; bipush -22 bipush 33 iadd invokevirtual java/io/PrintStream/println(I)V return.end method

Program Challenge 4: Simple program for calculating expression in Jasmin To calculate an expression in Jasmin, you need to write its postfix notation first / 3 4 * 5 1 * 1 + / + Follow the order in postfix notation, For numbers, using bipush number For operator, using corresponding instruction. E.g. iadd, idiv, imul, isub

Program Challenge 4: Simple program for calculating expression in Jasmin Final codes bipush 12 bipush 5 bipush 3 isub idiv bipush 3 bipush 4 imul bipush 5 bipush 1 imul bipush 1 iadd idiv iadd

Program Challenge 4: Simple program for calculating expression in Jasmin Task / 3 10 / * * - * It is easy to write a Jasmin program for it. But for division bipush 10 bipush 3 idiv Result=3 fpush 10 fpush 3 fdiv Result=3.333 bipush 10 bipush 3 fdiv Result=3 Not precise Wrong. More precise.

Program Challenge 4: Simple program for calculating expression in Jasmin Task / 3 10 / * * - * It is easy to write a Jasmin program for it. But for division bipush 10 bipush 3 idiv Result=3 Fpush ldc 10 Fpush ldc 3 fdiv Result=3.333 bipush 10 bipush 3 fdiv Result=3 Not precise Wrong. More precise. However, there is no fpush in JVM. You can use ldc instead.

Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack

Program Challenge 5 Program counter Contains the address for instruction currently being executed. How to control the execution flow of program Set PC’s content depending on condition PC next =PC+Offset if_icmpxx branch_instruction ifxx branch_instruction Condition true  PC next =PC+ Offset_Of_branch_Instruction Condition false  PC next =PC+ X (number of bytes for current instruction) Set PC’s content directly without condition PC next =PC+Offset goto branch_instruction PCnext=PC+ Offset_Of_branch_Instruction

Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions PC=0x1a00

Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions x x Local Variable Array Stack PC+1 PC=0x1a01

Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions PC=0x1a01

Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions x x Local Variable Array Stack PC+2 PC=0x1a03

Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions PC=0x1a03

Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions x Local Variable Array Stack PC+? PC=? x<=-1?

Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions 0 Local Variable Array Stack PC+5 PC=0x1a08 x=0 x<=-1 is true

Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions PC=0x1a08

Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions 5 Local Variable Array Stack PC+3 PC=0x1a06 x=5 x<=-1 is false

Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions PC=0x1a06

Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions 0 5 Local Variable Array Stack PC+1 PC=0x1a07

Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions PC=0x1a08

Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions 0 Local Variable Array Stack PC+1 PC=0x1a08

Program Challenge 5 Purpose: simple program about flow control in Jasmin Java program E.g. if(x>-1) x=0; Assume x is stored in local variable 1, so you can use iload_1 to get the value of x and push it into stack iload_1 bipush -1 if_icmple FINISH iconst_0 istore_1 FINISH: ;Other instuctions PC=0x1a08

Program Challenge 5 goto branch_instruction iload_1 bipush -1 goto FINISH iconst_0 istore_1 FINISH: ;Other instuctions x x Local Variable Array Stack PC=0x1a03

Program Challenge 5 goto branch_instruction iload_1 bipush -1 goto FINISH iconst_0 istore_1 FINISH: ;Other instuctions x x Local Variable Array Stack PC+5 PC=0x1a08