\documentclass[11pt]{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}

\begin{document}

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

\textbf{Directions: 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.
In order to get full credit, SHOW YOUR WORK.}

{\bf 1.} Look at Section 4 (``Another Example'') of our PLN unit on the JVM. 

\begin{itemize}

\item [(a)] (15) The assembly code compiled from line 26 in the source code on
p.9 is shown in offsets \_\_\_\_\_\_\_\_ through \_\_\_\_\_\_\_\_ of
{\bf Min()} on pp.10-11.

\item [(b)] (20) Suppose we change line 17 in the source code to

\begin{verbatim}
public void Min(int Q)
\end{verbatim}

and we delete line 31.  No other changes are made.\footnote{With a
change in the call in line 14.  The method wouldn't make much sense
then, but just suppose we make the indicated changes.}  Then the
instructions in offsets 21 and 59 of {\bf Min()} would change to
\_\_\_\_\_\_\_\_ and \_\_\_\_\_\_\_\_, respectively.  Moreover, the
instruction in offset 59 would move to offset \_\_\_\_\_\_\_\_.  

\end{itemize}

{\bf 2.} (15) The machine language for the subroutine {\bf addone()} in
our PLN unit on RISC will occupy a total of \_\_\_\_\_\_\_\_ bytes.
(Count only the subroutine, not the calling code in {\bf main()}.)

{\bf 3.} (15) Assume the page table structure on p.16 of the PLN unit on OS,
and assume there is no TLB.  Consider accesses to the page table when a
page fault occurs, during the period just after the fault is discovered
to the time that the instruction causing the fault resumes execution.
During this time, \_\_\_\_\_\_\_\_ bytes will be read by the hardware,
\_\_\_\_\_\_\_\_ bytes will be written by the hardware, \_\_\_\_\_\_\_\_
bytes will be read by the OS, and \_\_\_\_\_\_\_\_ bytes will be written
by the OS.  Fill in the blanks with numbers, and again, remember we
considering only accesses to the page table.

{\bf 4.} Consider the following program and the GDB session which
executes it:

\begin{Verbatim}[fontsize=\relsize{-2}]
.data
nb:  .long 6
us:  .string "ECS50\n"
.text
.globl _start
_start:
      movl ________, %eax
      movl $1, %ebx
      movl $us, %ecx
      movl nb, ________ 
      int $0x80
      movl $1, %eax
      int $0x80
\end{Verbatim}

\begin{Verbatim}[fontsize=\relsize{-2}]
Breakpoint 1, _start () at write.s:8
8             movl $1, %ebx
(gdb) p/x $eip
$1 = 0x8048079
(gdb) p/x $esp
$2 = 0xbfffda70
(gdb) p/x &nb
$3 = 0x8049094
(gdb) c
Continuing.
ECS50
Breakpoint 2, _start () at write.s:12
12            movl $1, %eax
(gdb) p/x $eip
$4 = 0x804808a
\end{Verbatim}

\begin{itemize}

\item [(a)] (10) Fill in the blanks in the source code.

\item [(b)] (10) Show the hex value of c(ESP), i.e., the
location of the top of the stack, at the instant the first
{\bf int} instruction finishes execution, just before the next Step A
begins for the next instruction.

\end{itemize}

{\bf 5.} (15) Suppose {\bf R} is a class variable, i.e. is declared {\bf
static}, and {\bf S} is a local variable in slot 2.  Show JVM assembly
code which could be generated by the compiler for the statement

\begin{verbatim}
S = R*R;
\end{verbatim}

Your answer must consist of four or fewer instructions.  For full
credit, you must minimize the total number of bytes occupied by the
compiled code you show. 

{\bf Solutions:}

{\bf 1.a}  25-30; note that offset 30 implements the {\bf break}

{\bf 1.b}  The change in heading for {\bf Min()} means two extra items
in its Local Variables Area:  {\bf this} and {\bf Q}, the former coming
from the fact that {\bf Min()} is now non-static.  That means that
offset 21 of {\bf Min()} will change to {\bf iload\_3}.  Also, since
{\bf Min()} now has type {\bf void}, offset 59 changes to {\bf return},
and we no longer have the instruction in offset 58, which had been the
preparation for {\bf ireturn}.

But there is more.  Consider offset 2.  We'd like to change it to {\bf
istore\_4}, but there is no such instruction.  Instead, it must be
changed to {\bf istore 4}; {\bf istore} is a 2-byte instruction (the 4
here goes in the second byte), where we formerly had only one byte.  We
need to make similar changes at offsets 9, 15, 24, 25, 26, 42, 43, 47
and 48.  So, the instruction which had been in offset 59 moves to offset
59-1+10 = 68.  (And, as mentioned earlier, that instruction changes to
{\bf return}.)

{\bf 2.}  There are 4 instruction, 4 bytes each, so 16 bytes in all.

{\bf 3.}  The hardware does nothing to the page table during this time;
it's all the OS.  So, the first two answers are 0.  A number of
different answers were allowed for the other two blanks, given the
multiple interpretations.

{\bf 4.a}  \$4, \%edx

{\bf 4.b}  The {\bf int} pushes three items onto the stack, thus 12
bytes.  ESP then becomes 0xbfffda70-12 = 0xbfffda64.

{\bf 5.}  The straightforward compilation (and in fact the one chosen by
{\bf javac}) would be

\begin{verbatim}
getstatic #____
getstatic #____
imul
istore_2
\end{verbatim}

But a shorter one (one byte less) is

\begin{verbatim}
getstatic #____
dup
imul
istore_2
\end{verbatim}



\end{document}

