
\documentclass[twocolumn]{article}

\setlength{\oddsidemargin}{-0.5in}
\setlength{\evensidemargin}{-0.5in}
\setlength{\topmargin}{0.0in}
\setlength{\headheight}{0in}
\setlength{\headsep}{0in}
\setlength{\textwidth}{7.0in}
\setlength{\textheight}{9.5in}
\setlength{\parindent}{0in}
\setlength{\parskip}{0.05in}
\setlength{\columnseprule}{0.3pt}
\usepackage{fancyvrb}
\usepackage{relsize}
\usepackage{hyperref}

\begin{document}

Name: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

Directions: {\bf Work only on this sheet} (on both sides, if needed); do
not turn in any supplementary sheets of paper. There is actually plenty
of room for your answers, as long as you organize yourself BEFORE
starting writing.

{\bf 1.} (30) Following is a table of analogies between the C/Intel/Linux
world and the JVM world.  Fill in the blanks!  (The lengths of the
blanks are not meaningful.)

\begin{tabular}{|r|r|}
\hline
C/Intel/Linux & JVM \\ \hline 
\hline
\hline
\ & {\bf Integer.parseInt()} \\ \hline 
\ & {\bf invokestatic, invokevirtual} \\ \hline 
{\bf pushl \$1} & \  \\ \hline 
{\bf .o} or ELF & \ \\ \hline 
EBP & \  \\ \hline 
ESP & \  \\ \hline 
\end{tabular}

{\bf 2.} (20) Consider the function {\bf gy()} on p.235.  Fill in the
blanks:  The reason that {\bf x} is in slot 1 instead of slot
\_\_\_\_\_\_\_\_\_\_\_\_\_\_ is that the Java keyword 
\_\_\_\_\_\_\_\_\_\_\_\_\_\_ is not present in line 1.

{\bf 3.} (50) Consider this Java code to find Fibonacci numbers.  (These
are 1,1,2,3,5,8,13,21,..., each one being the sum of the previous two.)

\begin{Verbatim}[fontsize=\relsize{-2}]
public class Fib {

   public static void main(String[] clargs)
   {  int i,n,fib[];

      n = Integer.parseInt(clargs[0]);
      fib = new int[n];
      fib[0] = fib[1] = 1;
      genfibs(n,fib);
      for (i = 0; i < n; i++)
         System.out.println(fib[i]);
   }

   public static int genfibs(int k, int fbs[])
   {  int i;
      for (i = 2; i < k; i++)
         fbs[i] = fbs[i-1] + fbs[i-2];
      return 0;
   }
}
\end{Verbatim}

Below is part of the output from running this through {\bf javap -c}.
Fill in the blanks.

\begin{Verbatim}[fontsize=\relsize{-2}]
public static void main(java.lang.String[]);
  Code:
   0:   aload_0
   1:   iconst_0
   2:   aaload
   3:   invokestatic    #2; //Method java/lang/Integer.parseInt...
   6:   istore_2
   7:   iload_2
   8:   newarray int
   10:                               // BLANK
   11:  aload_3
   12:  iconst_0
   13:  aload_3
   14:  iconst_1
   15:  iconst_1
   16:  dup_x2
   17:  iastore
   18:  iastore
   19:                               // BLANK
   20:  aload_3
   21:  invokestatic    #3; //Method genfibs:(I[I)I
   24:  pop
   25:  iconst_0
   26:  istore_1
   27:  iload_1
   28:  iload_2
   29:  if_icmpge       47
   32:  getstatic       #4; //Field java/lang/System.out:Ljava/io/PrintStream;
   35:  aload_3
   36:  iload_1
   37:  iaload
   38:  invokevirtual   #5; //Method java/io/PrintStream.println:(I)V
   41:  iinc    1, 1
   44:  goto    27
   47:  return
public static int genfibs(int, int[]);
  Code:
   0:   iconst_2
   1:   istore_2
   2:                               // BLANK
   3:   iload_0
   4:   if_icmpge                   // BLANK
   7:   aload_1
   8:   iload_2
   9:   aload_1
   10:  iload_2
   11:  iconst_1
   12:  isub
   13:  iaload
   14:  aload_1
   15:  iload_2
   16:  iconst_2
   17:  isub
   18:  iaload
   19:  iadd
   20:  iastore
   21:  iinc    2, 1
   24:  goto                       // BLANK
   27:  iconst_0
   28:  ireturn
\end{Verbatim}

{\bf Solutions:}

{\bf 1.}

\begin{tabular}{|r|r|}
\hline
C/Intel/Linux & JVM \\ \hline 
\hline
\hline
{\bf atoi()} & {\bf Integer.parseInt()} \\ \hline 
CALL & {\bf invokestatic, invokevirtual} \\ \hline 
{\bf pushl \$1} & {\bf iconst\_0}  \\ \hline 
{\bf .o} or ELF & {\bf .java} \\ \hline 
ESP & optop  \\ \hline 
EBP & Frame Data  \\ \hline 
\end{tabular}

{\bf 2.} 0, {\bf static}

{\bf 3.}

Key points:

\begin{itemize}

\item {\bf fib} is in slot 3, so result of {\bf newarray} must go there

\item for the call to {\bf genfibs()}, we must push {\bf n} and then
(the address of) {\bf fibs} onto the stack, and {\bf n} is in slot 2

\item to execute {\bf if\_cmpge} we must first push its two operands,
{\bf i} and {\bf k}

\item the compare operation is there to test whether we've finished the
loop, and if so, we jump down to the {\bf return 0}, in offsets 27 and
28 in {\bf genfibs()}

\item similarly, offset 24 in {\bf genfibs()} is the bottom of the loop;
if we're not done, we jump to the top, offset 2

\end{itemize}

\begin{Verbatim}[fontsize=\relsize{-2}]
Compiled from "Fib.java"
public class Fib extends java.lang.Object{
public Fib();
  Code:
   0:	aload_0
   1:	invokespecial	#1; //Method java/lang/Object."<init>":()V
   4:	return

public static void main(java.lang.String[]);
  Code:
   0:	aload_0
   1:	iconst_0
   2:	aaload
   3:	invokestatic	#2; //Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
   6:	istore_2
   7:	iload_2
   8:	newarray int
   10:	astore_3
   11:	aload_3
   12:	iconst_0
   13:	aload_3
   14:	iconst_1
   15:	iconst_1
   16:	dup_x2
   17:	iastore
   18:	iastore
   19:	iload_2
   20:	aload_3
   21:	invokestatic	#3; //Method genfibs:(I[I)I
   24:	pop
   25:	iconst_0
   26:	istore_1
   27:	iload_1
   28:	iload_2
   29:	if_icmpge	47
   32:	getstatic	#4; //Field java/lang/System.out:Ljava/io/PrintStream;
   35:	aload_3
   36:	iload_1
   37:	iaload
   38:	invokevirtual	#5; //Method java/io/PrintStream.println:(I)V
   41:	iinc	1, 1
   44:	goto	27
   47:	return

public static int genfibs(int, int[]);
  Code:
   0:	iconst_2
   1:	istore_2
   2:	iload_2
   3:	iload_0
   4:	if_icmpge	27
   7:	aload_1
   8:	iload_2
   9:	aload_1
   10:	iload_2
   11:	iconst_1
   12:	isub
   13:	iaload
   14:	aload_1
   15:	iload_2
   16:	iconst_2
   17:	isub
   18:	iaload
   19:	iadd
   20:	iastore
   21:	iinc	2, 1
   24:	goto	2
   27:	iconst_0
   28:	ireturn

}

\end{Verbatim}

\end{document}

