\documentclass[10pt]{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.} This problem is about the {\bf read()} system call.

\begin{itemize}

\item [(a)] (10) During execution of {\bf read()} the CPU is in
\_\_\_\_\_\_\_\_\_\_\_\_  mode.  It got into this mode because the
function is called via the \_\_\_\_\_\_\_\_\_\_\_\_ instruction instead
of by the \_\_\_\_\_\_\_\_\_\_\_\_ instruction.

\item [(b)] (10) The OS will need to check which system call has been
made, i.e. check whether it was a call to {\bf write()}, a call to {\bf
read()}, a call to {\bf open()}, etc.  Give a single instruction from
the Intel CMP family which checks whether the call was to {\bf read()}.

\end{itemize}

{\bf 2.} Consider the following Java code and its compiled JVM machine
language:

\begin{verbatim}
public class First8 {
   public static void main(String[] CLArgs)
   {  int X[];
      X = new int[3];
      X[0] = 4; X[1] = 8; X[2] = 1;
      System.out.println(First(X,8));
   }
   public static int First(int A[], int K)
   {  int I;
      for (I = 0; ; I++)
         if (A[I] == K) break;  // name this line L
      return I;
   }
}
...
Method void main(java.lang.String[])
   0 iconst_3
   1 newarray int
   3 astore_1
...
  17 getstatic #2 <Field java.io.PrintStream out>
  20 ________ 
  21 bipush ________
  23 invokestatic #3 <Method int First(int[], int)>
  26 invokevirtual #4 <Method void println(int)>
  29 return
Method int First(int[], int)
   0 iconst_0
   1 istore_2
   2 goto 5
   5 aload_0
   6 iload_2
   7 iaload
   8 iload_1
   9 if_icmpne 15
  12 goto 21
  15 iinc 2 1
  18 goto 5
  21 ________
  22 ireturn
\end{verbatim}

\begin{itemize}

\item [(a)] (20) The compiled code for the source line named L (see
comment in source code) is in offsets \_\_\_\_\_\_\_\_ through
\_\_\_\_\_\_\_\_ of {\bf First()}.

\item [(b)] (15) Give the machine code for the instructions in offsets
8-9 of {\bf First()}.  Your answer must consist of four bytes of hex.

\item [(c)] (20) Fill in the blanks in offsets 20 and 21 of {\bf main()}
and in offset 21 of {\bf First()}.

\end{itemize}

{\bf 3.} (25) The following MIPS code, which could be part of an OS,
changes an entry in the page table to indicate that the given page is
not resident in memory.  The labels {\bf pgtbl} and {\bf pgnum} are in
the {\bf .data} segment; {\bf pgtbl} is the beginning of the page table,
and {\bf pgnum} contains the number of the page to be recorded as
nonresident.  Assume the page table structure on p.16 of our PLN unit on
OS.  Fill in the blanks (MIPS pseudoinstructions are allowed).

\begin{verbatim} 
lw $8,________ 
sll $9,$8,________ 
lw $10,________($9)
and $11,$10,0x________ 
sw $11, ________(________) 
\end{verbatim}

{\bf Solutions:}

{\bf 1.} Kernel; {\bf int}; {\bf call}

{\bf 2.}  Since {\bf read()} is system call number 3, we should use

\begin{verbatim}
cmpl $3, %eax
\end{verbatim}

{\bf 3.a}  5-12; note that offset 12 implements the {\bf break}

{\bf 3.b}  The opcode is 0x1b for {\bf iload\_1}.  For {\bf if\_cmpne}
the opcode is 0xa0 and there is a 2-byte operand for the distance to the
jump target, in this case 15-9 = 6.  So, the two instructions have
machine code 0x1ba00006.

{\bf 3.c}  In offsets 20 and 21 of {\bf main()}, we need to pass the two
arguments for the call to {\bf First()}.  The first argument, {\bf X},
is an array name, thus an address, so we need an instruction from the
{\bf aload} family, in this case {\bf aload\_1} since {\bf X} is in slot
1.  The second argument is 8, which we push in offset 21.  If offset 21
of {\bf First()}, we need to push the return value, for use by the {\bf
ireturn} in offset 22.  Since the return value is {\bf I}, in offset 2,
we need {\bf iload\_2}.

{\bf 4.}

\begin{verbatim} 
lw $8,pgnum                     # index into the table 
sll $9,$8,2                     # multiply by 4, since each entry is 4 bytes
lw $10,pgtbl(9)                 # get the page table entry
and $11,$10,0xfffff7ff          # change the residence bit to 0
sw $11, pgtbl($9)               # store new entry
\end{verbatim}

\end{document}

