Assembly Language for Beginners

(nextflipdebug2) #1

4.1. JAVA


public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=0, locals=1, args_size=1
0: return

This means that thereturninstruction is used to return control without returning an actual value.


Knowing all this, it’s very easy to deduce the function’s (or method’s) returning type from the last instruc-
tion.


4.1.3 Simple calculating functions


Let’s continue with a simple calculating functions.


public class calc
{
public static int half(int a)
{
return a/2;
}
}


Here’s the output when theiconst_2instruction is used:


public static int half(int);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=1, args_size=1
0: iload_0
1: iconst_2
2: idiv
3: ireturn

iload_0takes the zeroth function argument and pushes it to the stack.


iconst_2pushes 2 in the stack. After the execution of these two instructions, this is how stack looks like:


+---+
TOS ->| 2 |
+---+
| a |
+---+


idivjust takes the two values at theTOS, divides one by the other and leaves the result atTOS:


+--------+
TOS ->| result |
+--------+


ireturntakes it and returns.


Let’s proceed with double precision floating point numbers:


public class calc
{
public static double half_double(double a)
{
return a/2.0;
}
}


Listing 4.7: Constant pool

...
#2 = Double 2.0d
...

Free download pdf