Reverse Engineering for Beginners

(avery) #1

CHAPTER 54. JAVA CHAPTER 54. JAVA


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
14: invokevirtual #3 // Method java/io/PrintStream.println:(I)V
17: iinc 1, 1
20: goto 2
23: return

The incomingreferenceto the array is in the zeroth slot. Thea.lengthexpression in the source code is converted to
anarraylengthinstruction: it takes areferenceto the array and leaves the array size atTOS. ialoadat offset 13 is used
to load array elements, it requires to arrayreferencebe present in the stack (prepared byaload_0at 11), and also an index
(prepared byiload_1at offset 12).


Needless to say, instructions prefixed withamay be mistakenly comprehended asarrayinstructions. It’s not correct. These
instructions works withreferencesto objects. And arrays and strings are objects too.


54.13.2Summing elements of array.


Another example:


public class ArraySum
{
public static int f (int[] a)
{
int sum=0;
for (int i=0; i<a.length; i++)
sum=sum+a[i];
return sum;
}
}


public static int f(int[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=3, locals=3, args_size=1
0: iconst_0
1: istore_1
2: iconst_0
3: istore_2
4: iload_2
5: aload_0
6: arraylength
7: if_icmpge 22
10: iload_1
11: aload_0
12: iload_2
13: iaload
14: iadd
15: istore_1
16: iinc 2, 1
19: goto 4
22: iload_1
23: ireturn

LVAslot 0 contains areferenceto the input array. LVAslot 1 contains the local variablesum.


54.13.3The only argument of themain()function is an array too


We’ll be using the only argument of themain()function, which is an array of strings:

Free download pdf