Example of a Program Written in the JVM Assembly Language
The following program computes the perimeter and area of a rectangle from coordinates of two points, which are opposite vertices of the rectangle.
; Rectangle: Program that computes the perimeter and area of a rectangle
; from coordinates of two points (X1,Y1) and (X2,Y2).
; Author: Richard St-Denis, Universite de Sherbrooke, 2013.
.source rectangle.mjv
.class public rectangle
.super java/lang/Object
;*******************************************************
.method static public ()V
.limit locals 0
.limit stack 2
return
.end method
;*******************************************************
.method public ()V
.limit locals 1
.limit stack 2
aload_0
invokenonvirtual java/lang/Object/()V
return
.end method
;*******************************************************
.method public static main([Ljava/lang/String;)V
.limit locals 1
.limit stack 0
invokestatic rectangle/main()V
return
.end method
;*******************************************************
.method static public main()V
.limit locals 6
.limit stack 4
.var 0 is 'n' I from L1 to L6 ; point number
.var 1 is 'b' I from L1 to L6 ; base
.var 2 is 'h' I from L1 to L6 ; height
.var 3 is 'a' I from L1 to L6 ; area
.var 4 is 'p' I from L1 to L6 ; perimeter
.var 5 is 'points' [I from L1 to L6 ; coordinates of the two points
L1:
iconst_0 ; initialize the variables
istore 0
iconst_0
istore 1
iconst_0
istore 2
iconst_0
istore 3
iconst_0
istore 4
aconst_null
astore 5
ldc 4 ; points = new int[4]
multianewarray [I 1
astore 5
L2:
iload 0
ldc 2
if_icmpeq L3
; display the prompt message for X coordinate
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc "Enter the X coordinate of point "
invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V
getstatic java/lang/System/out Ljava/io/PrintStream;
iload 0
ldc 1
iadd ; n + 1
invokevirtual java/io/PrintStream/print(I)V
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc ": "
invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V
aload 5 ; read the X coordinate of a point
ldc 2
iload 0
imul ; 2 * n
invokestatic mjio/scanf/readI()I
iastore
; display the prompt message for Y coordinate
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc "Enter the Y coordinate of point "
invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V
getstatic java/lang/System/out Ljava/io/PrintStream;
iload 0
ldc 1
iadd ; n + 1
invokevirtual java/io/PrintStream/print(I)V
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc ": "
invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V
aload 5 ; read the Y coordinate of a point
ldc 2
iload 0
imul
ldc 1
iadd ; 2 * n + 1
invokestatic mjio/scanf/readI()I
iastore
iload 0
ldc 1
iadd
istore 0 ; n = n + 1
goto L2
L3:
aload 5 ; X1 - X2
ldc 0
iaload
aload 5
ldc 2
iaload
isub
istore 1
aload 5 ; Y1 - Y2
ldc 1
iaload
aload 5
ldc 3
iaload
isub
istore 2
iload 1
ldc 0
if_icmpge L4
iload 1 ; |X1 - X2|
ineg
istore 1
L4:
iload 2
ldc 0
if_icmpge L5
iload 2 ; |Y1 - Y2|
ineg
istore 2
L5:
iload 1 ; calculate the area
iload 2
imul
istore 3
iload 1 ; calculate the perimeter
iload 1
iadd
iload 2
iadd
iload 2
iadd
istore 4
; display the results
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc "The perimeter is: "
invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V
getstatic java/lang/System/out Ljava/io/PrintStream;
iload 4
invokevirtual java/io/PrintStream/print(I)V
getstatic java/lang/System/out Ljava/io/PrintStream;
invokevirtual java/io/PrintStream/println()V
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc "The surface is: "
invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V
getstatic java/lang/System/out Ljava/io/PrintStream;
iload 3
invokevirtual java/io/PrintStream/print(I)V
getstatic java/lang/System/out Ljava/io/PrintStream;
invokevirtual java/io/PrintStream/println()V
L6:
return
.end method
Under Solaris, the following commands allow for assembling and execution of the program.
echo "Assembling ..."
java -jar /opt/jasmin-2.2/jasmin.jar rectangle.j
echo "Execution ..."
java rectangle
The directory mjio contains the file scanf.class derived from the file scanf.java