CS 220 - Lab 1: Peeling the Onion -- Introduction to the Java Virtual
Machine
Due September 15, 2003
General Lab Goals
This course introduces the basic ideas of computer architecture by examining
several layers of language below the programming language layer with which
you are familiar. We look at the relation of each layer to programs
written in high-level languages like Java. The first layer is the
"assembly language" layer, which is a symbolic representation of the underlying
machine on which the program is running. There are many different assembly
languages, one for each different machine architecture -- Intel, Sun, Motorola,
and the so-called Java Virtual Machine (JVM for short).
While assembly language programming is not usually a preferred programming
style, it is sometimes useful for writing efficient programs. Knowledge
of assembly language also provides a window through which the architecture
of modern computers can be studied. In this course, we use the JVM assembly
language to provide insight into the principles of computer organization
and design. These principles help us develop a better understanding about
what is and what is not practically computable.
So this lab serves as an introduction to the JVM assembly language in the
iMac Java environment. In addition, it asks you to complete a series
of written exercises based on your reading of assigned sections of Chapters
1 and 2 and appendix A of your text.
Part 1 - Run a Java Program
In the Mac lab, connect to the server Collaboration and open
the Computer Science -> csci220 folder. You'll need to log
in to access this server, using your Bowdoin login and password.
Inside the csci220 folder, there's a folder called testJVM,
which you should drag (copy) to the desktop.
Now open this folder and take a look at the text of the program testJVM.java:
// A simple test program
class testJVM extends basic
{ public static void main(String param[]) throws Exception
{ input in = new input();
output out = new output();
out.writeln("Enter an integer");
int n = in.readint();
for (int i=n; i>0; i--)
out.writeln(i);
out.writeln("Welcome to Java!");
}
}
Compile and run this program in a separate terminal window, using the
following steps.
1. Open a UNIX terminal window on the desktop (Applications
-> Terminal Alias).
2. Inside this window, type cd desktop/testJVM to redirect
attention to the testJVM folder.
3. Compile the programs in this folder by typing javac text.java
and javac testJVM.java
4. Run the program by typing java testJVM
Part 2 - Disassembly: Examining the JVM Code
Now obtain the underlying JVM code (called "disassembly") by typing
the UNIX command "javap -c testJVM". A listing will appear that contains
the following information:
Method void main(java.lang.String[])
0 new #2 <Class input>
3 dup
4 invokespecial #3 <Method input()>
7 astore_1
8 new #4 <Class output>
11 dup
12 invokespecial #5 <Method output()>
15 astore_2
16 aload_2
17 ldc #6 <String "Enter an integer">
19 invokevirtual #7 <Method void writeln(java.lang.String)>
22 aload_1
23 invokevirtual #8 <Method int readint()>
26 istore_3
27 iload_3
28 istore 4
30 goto 42
33 aload_2
34 iload 4
36 invokevirtual #9 <Method void writeln(int)>
39 iinc 4 -1
42 iload 4
44 ifgt 33
47 aload_2
48 ldc #10 <String "Welcome to Java!">
50 invokevirtual #7 <Method void writeln(java.lang.String)>
53 return
Yipes! This is the JVM code that corresponds to the simple Java
program above.
It has three columns. The first column identifies the memory address
of each JVM instruction (counting in decimal). The second
column displays the symbolic JVM machine instructions that correspond to
the original Java program after being translated by the compiler (this
is called "assembly" language. The third column gives some commentary
(enclosed in <>) about method calls and strings that occur in the
original program. Some of this you may be able to figure out, but most of
it will be unintelligible (don't worry, we'll dissect this later!)
Hang onto this disassembly listing. There are questions in the next
section that will ask you about some of it. To copy this listing
into the testJVM folder, type the instruction javap -c testJVM >
testJVM.code. You may now review or print this listing by opening
it in any text editor (e.g., TextEdit).
You may save this file to your personal workspace by dragging it from the
folder student -> testJVM to the folder csci220 -> Users
-> <your login>.
Part 3 - Questions
After reading Chapter 1 in your text, answer the following questions
on page 37:
#1, 5, and 8
After reading the assigned sections of Chapter 2, answer the following
questions on page 113:
#1, 3, 7, 12-14, 18, 20, 28, 34
After reading Appendix A, answer the following questions on page 641:
#1, 2, 6, 7, 9, 14
Returning to the disassembly listing you obtained in Part 2, answer the
following questions about it:
- What is the memory address of the JVM assembly language instruction
"return" in that listing?
- Looking at the comments in column 4 of this listing, can you find
the JVM machine language instructions that correspond to the Java statement
int n = in.readint; in the original program? What are
they?
- How many memory locations in the JVM listing are required to represent
all the statements in the original Java program?
Lab 1 Deliverables:
When you finish this lab, notice that the Mac Desktop has a folder called
csci220 -> Drop Box, a place where you may submit completed programs
during the semester. There's also a csci220 -> Users folder,
which students in the class can be used for for saving lab work throughout
the semester. Each user's folder is password protected, and can be
accessed by your Bowdoin e-mail password.
Be sure to drag the csci220 icon to the Trash each time you
finish a lab session, so that your work will not be accidentally accessed
or destroyed by someone who uses that machine after you leave the lab.
To complete this lab, hand in your written (or typed) answers to the questions
in Part 3 by 5:00pm on the due date. If your answers are typed, you
may drag your typed document, whose name identifies you (for example, atucker.lab1),
to the Drop Box. If you prefer handing in hard copy, leave
it in the CS220 mailbox outside my office (Searles 220).