; File: SomeFunctions.j ; Author: Carl E. Bredlau ; Purpose: Defining a simple class with class methods ; A void show(int) static method is included for debugging ; ------------------------------------------------------------------------- ; Use semicolon for comments ; Define the class: .class public SomeFunctions ; ... extends .super java/lang/Object ;---------------------------------------------------- ; standard initializer. Just copy it. .method public ()V .limit stack 1 aload_0 invokespecial java/lang/Object/()V return .end method ;---------------------------------------------------- ; Defining methods ; public static int distSq(int x, int y) ; returns x*x + y*y ; two parms, both integer ; | returns an integer .method public static distSq(II)I ; set limits used by this method .limit locals 2 .limit stack 3 ; Parameters: ; 0 - x ; 1 - y ; Stack iload_0 ; x dup ; x x imul ; x*x iload_1 ; y x*x dup ; y y x*x imul ; y*y x*x iadd ; y*y + x*x ireturn .end method ;---------------------------------------------------- ; public static int millionX(int x) ; returns 1000000*x .method public static millionX(I)I ; set limits used by this method .limit locals 1 .limit stack 2 ; Parameters: ; 0 - x ldc 1000000 ; Jasmime will create the constant with ; a million. ; The instruction will have the correct index ; to this constant ; Stack ; 1000000 iload_0 ; x 1000000 imul ; 1000000*x ireturn .end method ;---------------------------------------------------- .method public static distSq(III)I ; set limits used by this method .limit locals 3 .limit stack 3 ; Parameters: ; 0 - x ; 1 - y ; 2 - z ; First compute the sum of the first two squares... ; Stack iload_0 ; x iload_1 ; y x ; Call static method: see the type of parms and return value. ; This will generate a constant the method definition. ; The instruction will contain the index of this constant. ; name of class name of method ; | | parms ; | | | return invokestatic SomeFunctions/distSq(II)I ; x*x + y*y ; (Debugging) Checking top of the stack: dup invokestatic SomeFunctions/show(I)V iload_2 ; z (x*x + y*y) dup ; z z (x*x + y*y) imul ; z*z (x*x + y*y) ; (Debugging) Checking top two elements of the stack: dup invokestatic SomeFunctions/show(I)V ; top element swap ; swap the top two elements dup invokestatic SomeFunctions/show(I)V ; second element swap ; them back iadd ; z*z + x*x + y*y ireturn .end method ;---------------------------------------------------- ; public static int dist(int x, int y) ; returns abs(x - y) .method public static dist(II)I ; set limits used by this method .limit locals 2 .limit stack 2 ; Parameters: ; 0 - x ; 1 - y ; x - y ; Stack iload_0 ; x iload_1 ; y x isub ; y - x ; abs is defined in the class java.lang.Math ; see how the packages get turned into a path invokestatic java/lang/Math/abs(I)I ; ; |x - y| ireturn .end method ;---------------------------------------------------- ; public static int sgn(int x); ; returns 1, if x > 0 ; 0, if x = 0 ; -1, if x < 0 .method public static sgn(I)I .limit locals 1 .limit stack 3 ; Parameters: ; 0 - x iload_0 ; x dup ; x x keep x around for 2nd test ; Labels must be on separate lines pos: ifle zero ; x is x > 0? pop ; yes. remove x iconst_1 ; 1 goto endif zero: ifne neg ; no. is x = 0? iconst_0 ; 0 yes goto endif neg: iconst_m1 ; -1 no endif: ; All paths here will have an integer on the stack ireturn .end method ;---------------------------------------------------- ; public static int abs(int x); ; returns |x| ; if (x < 0) ; x = -x ; return x .method public static abs(I)I .limit locals 1 .limit stack 2 ; Parameters: ; 0 - x iload_0 ; x dup ; x x ; Labels must be on separate lines ifge done ; x is x < 0? ineg ; -x yes. x = -x done: ; All paths here will have an integer on the stack ireturn .end method ;---------------------------------------------------- ; public static void show(int x) ; System.out.println(">>> " + x); ; Hide the dirty details for now... ; To be explained later .method public static show(I)V ; set limits used by this method .limit locals 1 .limit stack 3 getstatic java/lang/System/out Ljava/io/PrintStream; ; get out object dup ; keep a reference ldc ">>> " invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V ; System.out.print(">>>") iload_0 ; x invokevirtual java/io/PrintStream/println(I)V ; System.out.println(x) return .end method