Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: does anyone know assembly code for JVM?

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool does anyone know assembly code for JVM?

    I have to write assembly level code

    It basically uses stacks sorta

    It's not in java, and normally I wouldn't ask if I weren't so desperate, and also if javascript questions can be answered here, it seemed ok to ask.

    Anyway,

    I know that

    ILOAD a

    loads the variable a

    And

    BIPUSH 0
    ISTORE a

    sets a to 0

    I know
    DUP clones the top of the stack and pushes the clone to the top of the stack

    I even found a code for Adding it appears and echoing

    // add.jas
    //
    // mic1 microarchitecture simulator 
    // Copyright (C) 1999, Prentice-Hall, Inc. 
    //
    // This program is free software; you can redistribute it and/or modify 
    // it under the terms of the GNU General Public License as published by 
    // the Free Software Foundation; either version 2 of the License, or 
    // (at your option) any later version. 
    //
    // This program is distributed in the hope that it will be useful, but 
    // WITHOUT ANY WARRANTY; without even the implied warranty of 
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    // Public License for more details. 
    //
    // You should have received a copy of the GNU General Public License along with 
    // this program; if not, write to: 
    //
    //   Free Software Foundation, Inc. 
    //   59 Temple Place - Suite 330 
    //   Boston, MA 02111-1307, USA. 
    //
    // A copy of the GPL is available online the GNU web site: 
    //
    //   [url=http://www.gnu.org/copyleft/gpl.html]The GNU General Public License v3.0 - GNU Project - Free Software Foundation (FSF)[/url]
     
     
    //  add.jas
    //
    //  Author
    //    Dan Stone
    //
    //  Description
    //    Program to input hex 2 numbers, add them, and print the result.
    //    Illustrates how to invoke methods.
    //
    //  Usage
    //    To compile:  java ijvmasm add.jas
    //      This will create add.ijvm
    //
    //    To run:      java mic1sim mic1ijvm.mic1 add.ijvm
    //      Click the Run button
    //      Enter the first number (up to 8 digit hex, using upper-case "A"-"F")
    //      Press return
    //      Enter the second number and press return
    //      The result will be displayed, with leading zeros (always prints 8 digits)
    //      Repeat as often as desired
    //      Click on the Stop button
     
    .constant
    OBJREF 0x40			// needed for method invokation - see S.C.O. chapter 4
    .end-constant
     
     
    .main				// start of program
     
    .var				// local variables for main program
    a
    b
    total
    .end-var
     
    start:	BIPUSH 0x0		// initialize var a and b
    	DUP
    	ISTORE a
    	ISTORE b
    	BIPUSH 0x20		// print " "
    	OUT
    	LDC_W OBJREF		// prepare for method call
    	INVOKEVIRTUAL getnum
    	ISTORE a		// store return value in a
    	BIPUSH 0x2b		// print "+"
    	OUT
    	LDC_W OBJREF
    	INVOKEVIRTUAL getnum
    	ISTORE b		// store return value in b
    	BIPUSH 0x3d		// print "========"
    	DUP
    	DUP
    	DUP
    	DUP
    	DUP
    	DUP
    	DUP
    	OUT
    	OUT
    	OUT
    	OUT
    	OUT
    	OUT
    	OUT
    	OUT
    	BIPUSH 0xa
    	OUT
    	ILOAD a
    	ILOAD b
    	IADD
    	ISTORE total		// add a and b, store in total
    	LDC_W OBJREF		// push OBJREF
    	ILOAD total		// push total, parameter for method print
    	INVOKEVIRTUAL print
    	GOTO start		// start over
    .end-main
     
    .method getnum()
    .var
    a
    .end-var
     
    	BIPUSH 0x0		// initialize a
            ISTORE a
    geta:	IN			// read key press
    	DUP			// duplicate key for comparison
    	BIPUSH 0xa		// if key = cr,
    	IF_ICMPEQ return	//   return
    	DUP
    	BIPUSH 0x30		// if key < "0"
    	ISUB			//
    	IFLT geta4		//   goto geta4 (key is not a hex digit)
        	DUP
    	BIPUSH 0x3a		// else if key < ":"
    	ISUB			//
    	IFLT geta2		//   goto geta2 (key is numeric character - "0"-"9")
    	DUP
    	BIPUSH 0x41		// else if key < "A"
    	ISUB			//
    	IFLT geta4		//   goto geta4 (key is not a hex digit)
    	DUP
    	BIPUSH 0x46		// else if key > "F"
    	SWAP			//  
    	ISUB			//
    	IFLT geta4		//   goto geta4 (key is not a hex digit)
    	DUP			// else (key is letter - "A"-"F")
    	OUT			//   print key
    	BIPUSH 0x37		//   convert key from character to number
    	ISUB			//
    	GOTO geta3		//   goto geta3
    geta2:  DUP
    	OUT			// print key (numeric character)
    	BIPUSH 0x30		// convert key from character to number
    	ISUB
    geta3:  ILOAD a			// shift a left 8 bits
    	DUP
    	IADD
    	DUP
    	IADD
    	DUP
    	IADD
    	DUP
    	IADD
    	IADD			// add key to a
    	ISTORE a
    	GOTO geta		// get next key
     
    geta4:	POP			// pop invalid character
    	GOTO geta		// get next key
     
    return: OUT			// print cr
    	ILOAD a			// load a as return value
    	IRETURN			// return
    .end-method
     
    .method print( total ) 		// print converts a number into a string of
    				//   characters and prints them.  All of the characters
    				//   are pushed onto the stack, least significant
    				//   digit first, then popped off and printed.
    .var
    place
    index
    .end-var
     
    print: 	BIPUSH 0x9		// there are 8 nibbles in each integer--setting
    				//   this as nine pushes 10 characters onto the
     				//   stack, thus a total of ten printed digits,
    				//   but setting this less does not remove the
    				//   two leading zeros, just removes significant
    				//   digits
    	ISTORE index
    	BIPUSH 0x1		// comparison bit
    	ISTORE place
    print1:	BIPUSH 0x0
    	ILOAD index		// index = index - 1
    	BIPUSH 0x1
    	ISUB
    	DUP
    	IFEQ pall		// if index = 0  goto pall
    	ISTORE index
    	ILOAD total		// else
    	ILOAD place		//
    	IAND			//   if 1st bit of current nibble is zero (total & place)
    	IFEQ print2		//     goto print2
    	BIPUSH 0x1		//   else set first bit of character
    	IADD
    print2: ILOAD place		//   place = place << 1 
    	DUP
    	IADD
    	ISTORE place
    	ILOAD total
    	ILOAD place
    	IAND			//   if 2nd bit of current nibble is zero (total & place)
    	IFEQ print3		//     goto print3
    	BIPUSH 0x2		//   else set second bit of character
    	IADD	
    print3: ILOAD place		//   place = place << 1
    	DUP
    	IADD
    	ISTORE place
    	ILOAD total
    	ILOAD place
    	IAND			//   if 3rd bit of current nibble is zero (total & place)
    	IFEQ print4		//     goto print4
    	BIPUSH 0x4		//   else set second bit of character
    	IADD	
    print4: ILOAD place		//   place = place << 1
    	DUP
    	IADD
    	ISTORE place
    	ILOAD total
    	ILOAD place
    	IAND			//   if 4th bit of current nibble is zero (total & place)
    	IFEQ print5		//     goto print5
    	BIPUSH 0x8		//   else set second bit of character
    	IADD	
    print5: ILOAD place		//   place = place << 1
    	DUP
    	IADD
    	ISTORE place
    	GOTO print1
     
    pall:   POP			// Pop off leading 0's
    	POP
    	BIPUSH 0x9
    	ISTORE index
    pall1:	ILOAD index		// index = index - 1
    	BIPUSH 0x1
    	ISUB
    	DUP
    	IFEQ return		// if index = 0  return
    	ISTORE index
    	DUP
    	BIPUSH 0xa		// else if character < 0xa goto pall1
    	ISUB
    	IFLT pall2
    	BIPUSH 0x37		// else convert character to "A"-"F"
    	IADD
    	OUT			// print character
    	GOTO pall1		// goto pall (prepare & print next character)
    pall2:	BIPUSH 0x30		// convert character to "0"-"9"
    	IADD
    	OUT			// print character
    	GOTO pall1		// goto pall1 (prepare & print next character)
    return:	BIPUSH 0xa		// print cr
    	OUT
    	IRETURN			// no return value
    .end-method

    And
    // 
    //   echo.jas
    //
    //   Author
    //     Dan Stone
    //
    //   Description
    //     Sample assembly program that reads a key press and echoes the result to 
    //     standard output.  
    //
     
     
    .main
     
    L1:   IN           // request character input from memory
          DUP          // duplicate top of stack (inputed char) for comparing
          BIPUSH 0x0   // push 0x0 for comparison
          IF_ICMPEQ L2 // if no characters are available for input, loop
          OUT          // else, print character
          GOTO L1      // loop back to beginning of program
     
     
    L2:   POP          // No key has been pushed, so clear the stack,
          GOTO L1      // and start over
    .end-main

    They want me to write microinstructions for code for

    X + Y - Z

    They said:

    [1] (35 points) Write an IJVM assembly program that accepts three integer values (say, X, Y, and Z)
    from the console and then print the result of the following computation on the console screen:
    X + Y – Z
    Hints: You can refer to “add.jas” and “echo.jas” sample code. You may do the computation just
    ONCE.

    I can easily do it for regular java but assembly is a bit confusing.

    I know I'm probably asking too much but I figured I'd ask anyway.



    Hey, it created a link to a Wikipedia entry on IJVM! Cool!

    Well, that might help explain things more! Way to go JPF setup!!!

    Well, so much for the link.

    Oh well...creating a new link....

    IJVM - Wikipedia, the free encyclopedia
    Last edited by javapenguin; April 4th, 2011 at 08:39 PM. Reason: Wow!


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default does anyone know assembly code for JVM?

    BTW, these codes are what make the JVM, at least the IJVM, work.

    The JVM is what makes java work so this isn't really that off topic, if at all!




  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: I might be asking too much but does anyone know assembly code for JVM?

    Working directly with Java byte-code is a little... odd. Few people know how Java byte-code works. I'm moving this Other Programming Languages for this reason.

    When working with assembly, you have to remember to code very verbosely. I don't have any experience with working with Java byte-code, but when working with other assembly languages one of the most important parts is learning the instructions set. Read through the documentation (pay attention to the main points of that instruction, as well as any "fringe" behaviors of that instruction), and see if you can put together something which would accomplish what you want. It looks like the two samples provided can add and print out numbers.

    a few hints:
    x+y-z = a-z
    where a =x+y
    If you can find an instruction which subtracts two numbers, this would be fairly simple. Otherwise, look for a negation operator, or in the most extreme case, you'll need to write a two's compliment routine which computes the negation for you.

    Once you have the result, print it out using the echo routine.

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    javapenguin (April 6th, 2011)