Assembly Language for Beginners

(nextflipdebug2) #1
4.1. JAVA
50: ldc #12 // String September
52: aastore
53: dup
54: bipush 9
56: ldc #13 // String October
58: aastore
59: dup
60: bipush 10
62: ldc #14 // String November
64: aastore
65: dup
66: bipush 11
68: ldc #15 // String December
70: aastore
71: putstatic #2 // Field months:[Ljava/lang/String;
74: return

anewarraycreates a new array ofreferences(henceaprefix).

The object’s type is defined in theanewarray’s operand, it is the
“java/lang/String” string.


Thebipush 12beforeanewarraysets the array’s size.

We see here a new instruction for us:dup.

It’s a standard instruction in stack computers (including the Forth programming language) which just
duplicates the value atTOS.

By the way, FPU 80x87 is also a stack computer and it has similar instruction –FDUP.

It is used here to duplicate areferenceto an array, because theaastoreinstruction pops thereference
to array from the stack, but subsequentaastorewill need it again.

The Java compiler concluded that it’s better to generate adupinstead of generating agetstaticinstruc-
tion before each array store operation (i.e., 11 times).

aastoreputs areference(to string) into the array at an index which is taken fromTOS.

Finally,putstaticputsreferenceto the newly created array into the second field of our object, i.e.,
monthsfield.

Variadic functions

Variadic functions actually use arrays:

public static void f(int... values)
{
for (int i=0; i<values.length; i++)
System.out.println(values[i]);
}

public static void main(String[] args)
{
f (1,2,3,4,5);
}

public static void f(int...);
flags: ACC_PUBLIC, ACC_STATIC, ACC_VARARGS
Code:
stack=3, locals=2, args_size=1
0: iconst_0
1: istore_1
2: iload_1
3: aload_0
4: arraylength
5: if_icmpge 23
8: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
11: aload_0
12: iload_1
13: iaload
Free download pdf