Assembly Language for Beginners

(Jeff_L) #1

4.1. JAVA


23: ldc #5 // String ==100
25: invokevirtual #4 // Method java/io/PrintStream.print:(Ljava/lang/String;)V
28: iload_0
29: bipush 100
31: if_icmple 42
34: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
37: ldc #6 // String >100
39: invokevirtual #4 // Method java/io/PrintStream.print:(Ljava/lang/String;)V
42: iload_0
43: ifne 54
46: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
49: ldc #7 // String ==0
51: invokevirtual #4 // Method java/io/PrintStream.print:(Ljava/lang/String;)V
54: return

if_icmpgepops two values and compares them. If the second one is larger than the first, a jump to offset
14 is performed.


if_icmpneandif_icmplework just the same, but implement different conditions.


There is also aifneinstruction at offset 43.


Its name is misnomer, it would’ve be better to name itifnz(jump if the value atTOSis not zero).


And that is what it does: it jumps to offset 54 if the input value is not zero.


If zero,the execution flow proceeds to offset 46, where the “==0” string is printed.


N.B.: JVMhas no unsigned data types, so the comparison instructions operate only on signed integer
values.


4.1.9 Passing arguments


Let’s extend ourmin()/max()example:


public class minmax
{
public static int min (int a, int b)
{
if (a>b)
return b;
return a;
}


public static int max (int a, int b)
{
if (a>b)
return a;
return b;
}

public static void main(String[] args)
{
int a=123, b=456;
int max_value=max(a, b);
int min_value=min(a, b);
System.out.println(min_value);
System.out.println(max_value);
}
}


Here ismain()function code:


public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=5, args_size=1
0: bipush 123
2: istore_1
3: sipush 456
Free download pdf