Assembly Language for Beginners

(Jeff_L) #1

4.1. JAVA


}
}


Let’s compile it:


javac ret.java


...and decompile it using the standard Java utility:


javap -c -verbose ret.class


And we get:


Listing 4.1: JDK 1.7 (excerpt)
public static int main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=1, locals=1, args_size=1
0: iconst_0
1: ireturn

The Java developers decided that 0 is one of the busiest constants in programming, so there is a separate
short one-byteiconst_0instruction which pushes 0


(^5). There are alsoiconst_1(which pushes 1),iconst_2, etc., up toiconst_5.
There is alsoiconst_m1which pushes -1.
The stack is used in JVM for passing data to called functions and also for return values. Soiconst_0
pushes 0 into the stack.ireturnreturns an integer value (iin name meansinteger) from theTOS^6.
Let’s rewrite our example slightly, now we return 1234:
public class ret
{
public static int main(String[] args)
{
return 1234;
}
}
...we get:
Listing 4.2: JDK 1.7 (excerpt)
public static int main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=1, locals=1, args_size=1
0: sipush 1234
3: ireturn
sipush(short integer) pushes 1234 into the stack.shortin name implies a 16-bit value is to be pushed.
The number 1234 indeed fits well in a 16-bit value.
What about larger values?
public class ret
{
public static int main(String[] args)
{
return 12345678;
}
}
(^5) Just like in MIPS, where a separate register for zero constant exists:1.5.5 on page 26.
(^6) Top of Stack

Free download pdf