Déjà vu
The first part of is based on the first page of a CSCI 235 lab, so it may be a tad familiar.
Decoding a simple Java class
Download a tar-gzip of Collatz program written in Java into your csci431 directory. Extract it and then compile and run it with the following commands. (Isn’t the inconsistency odd.)
javac edu/unca/csci235/Collatz.java java edu.unca.csci235.Collatz
Run a few commands using javap, the Java disassembler, to view and save the compiled Java bytecode.
javap edu.unca.csci235.Collatz javap -c edu.unca.csci235.Collatz | more javap -c edu.unca.csci235.Collatz > Collatz.java-bytecode
Take a look at A Java Programmer’s Guide to Byte Code or the Wikipedia page on the JVM of the rather nice Wikibook on the Java Byte Code or some the many official specifications.
Open up your Collatz.java-bytecode
and go to the collatz routine. (It will be around address 262.)
Add some ”comments“ to your code describing what is happen.
It should start with something like this:
static int collatz(int);
Code:
0: iconst_0 [[ 0 Push 0
1: istore_1 [[ Store in slot 1
2: iload_0 [[ n Load from slot 0
3: iconst_1 [[ n 1 Push 1
You also need to create a table that associates variable names with positions within a stack frame. Here’s a big start.
static int collatz(int);
Stack:
0: n
1: count
....
Code:
Go through and annotate the entire Collatz
method.
When you are done with that continue to the
fill_collatz_table method and
see how it calls collatz.
Does my phone do this?
I doubt it. Take a look at ART and Dalvik. This is much faster.
An ugly example
Start with an ugly nested Java class. Compile the Java class and notice all the .class files that where generated. Run javap -c on all of these .class files.
Start with the main class and examine it carefully before examining the bytecode of the other three. First examine a brief bytecode listing and then look at a verbose bytecode listing.
javap -c Prob3_6.class > Prob3_6.bytecode javap -c -verbose Prob3_6.class > Prob3_6.verbose.bytecode
Now move on to the other three internal classes, but this time you must use some single quotes to satisfy the shell. (We will return this in Chapter 14 when we look at scripting languages.) For example:
javap -c 'Prob3_6$B.class' > 'Prob3_6$B.bytecode' javap -c -verbose 'Prob3_6$B.class' > 'Prob3_6$B.verbose.bytecode' javap -c 'Prob3_6$B$A.class' > 'Prob3_6$B$A.bytecode' javap -c -verbose 'Prob3_6$B$A.class' > 'Prob3_6$B$A.verbose.bytecode' javap -c 'Prob3_6$B$R.class' > 'Prob3_6$B$R.bytecode' javap -c -verbose 'Prob3_6$B$R.class' > 'Prob3_6$B$R.verbose.bytecode'