Writing Simple JVM Methods (integer operations)

Honors Computer Systems Organization (Prof. Grishman)

Basic facts

Basic JVM instructions

stack operations
ldc    constant          ;  push constant onto stack
pop                      ;  pop (and discard) top of stack
dup                      ;  duplicate top of stack
iload   var              ;  push local variable onto stack
istore  var              ;  pop top of stack into local variable
arithmetic operations
iadd                     ;  add top two elements of stack, leave result on stack
irem                     ;  integer remainder
goto   label             ;  unconditional jump
ifeq   label             ;  jump if equal to zero, etc.
ifne   label
iflt   label
ifle   label
ifgt   label
ifge   label
ireturn                  ;  return from method
Labels, followed by a colon, appear on a separate line preceding the instructio being labeled..

Jasmin Directives

.class public classname  ;  first line of class file
.super superclass       ;  second line of class file
                         ;  typical superclass is "java/lang/Object"
.method public static methodname (II)I
                         ;  gives name and type information for method
                         ;  (II)I = two integer arguments, integer result
.limit stack n           ;  maximum size of stack in method
.limit locals n          ;  max number of local variables used by method
.end method
Warning:  last line of Jasmin file must be followed by an end-of-line (enter)!

Running a Jasmin method

Create a Java class file with minimal main method ("Driver.java")
public class Driver {
  public static void main (String args[]){
    int result = Jasminclass.myfunc (3, 5);
    System.out.println ("myfunc returned " + result);
Create a Jasmin class file ("Jasminclass.j"):
.class public Jasminclass
.super java/lang/Object
.method public static myfunc(II)I
.limit stack 2
.limit locals 2
        iload   0
        iload   1
.end method
Assemble the Jasmin file ("jasmin Jasminclass.j"), producing Jasminclass.class
Compile the Java file ("javac Driver.java"), producing Driver.class
Run the class with the main method ("java Driver"), producing the output
myfunc returned 8

DOS environment

In order for this to work, you need to set up your DOS environment .