\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}

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

{\bf ASSUME THROUGHOUT THAT WE ARE DEALING WITH ORDINARY PCS RUNNING
LINUX.  By the way, instructions like {\tt pushl w}, where {\tt w} is a
label in the .data section, are OK.}

{\bf 1.}  Consider the assembly language presentation of the compiled
code for {\bf NumNode.java} on p.17 of our PLN on the JVM.

\begin{itemize}

\item [(a)] (5) Give the JVM machine code in hex for the {\bf if\_cmpge}
instruction which starts at offset 21 of {\bf Insert()}.

\item [(b)] (5) Give the line number in the source code on p.16 whose compiled
code includes this same {\bf if\_cmpge} instruction.

\item [(c)] (10) Suppose we are currently just about to execute the {\bf
putfield} instruction in offset 6 of {\bf NumNode()}.  Draw the
contents---in terms of variables in the source code on p.16---of the
operand stack at that point (with the stack growing downward in your
picture).

\end{itemize}

{\bf 2.} (10) Say an interrupt occurs during the execution of some
instruction.  During Step D of that instruction, how many bytes are read
from memory?  How many bytes are written?

{\bf 3.}  For each of the following instructions, state how many bytes
of memory are read, and how many are written.  Do not count instruction
fetches.

\begin{itemize}

\item [(a)] (10) {\bf addl \$8, (\%eax)}

\item [(b)] (10) {\bf inb \$50, \%al}
\end{itemize}

{\bf 4.} (5) Consider the threads example from discussion section.  Suppose
we execute the program as

\begin{Verbatim}[fontsize=\relsize{-2}]
a.out /dev/pts/3 /dev/pts/2 /dev/pts/4 /dev/pts/6
\end{Verbatim}

and run {\bf ps} (with whatever options are needed for threads).  How many
threads will show up?

{\bf 5.} (15) Write OS code that launches a \underline{new} user process
(as opposed to giving an already-existing process a turn).  Assume that
the OS has stored the addresses of the processes' stack and
first-executed instruction at the OS' variables {\bf stackstart} and
{\bf progstart}, respecitively, and that the initial value of EFLAGS is
in {\bf initeflags}.  Assume also that the values in the registers EAX,
EBX and so on will be initialized by the process itself.  You may have
at most six instructions, hopefully fewer.

{\bf 6.} (10) Consider our very first assembly language example program,
in Section 4 of our PLN on Linux assembly language.  This code
calculated the sum of a four-element array.  I've revised the code, so
that it accomplishes this same goal but in a different way.  Here is the
core of the output from {\bf as -a}:

\begin{Verbatim}[fontsize=\relsize{-2}]
                   _start:
0000 B8040000            movl $4, %eax  
     00
                                        
0005 BB000000            movl $0, %ebx  
     00
000a 031D0000      top:  addl x, %ebx
     0000
0010 80050C00            _______, top+_____
     000004
0017 48                  decl %eax  
0018 75F0                jnz top  
001a 891D1000      done: movl %ebx, sum  
\end{Verbatim}

Fill in the blanks.

{\bf 7.} Consider a page fault ISR on an Intel machine, written in
assembly language.  

\begin{itemize}

\item [(a)] (10) At what address will the ISR start?  Use the c()
notation in your answer, e.g.  c(EBX)+8888*c(2000).

\item [(b)] (10) Suppose for some reason we want the ISR to check whether
the instruction that triggered the interrupt was a JNZ.  We'll put 1 in
EAX if that is the case, and put 0 there otherwise.  Fill in the blank:

\begin{Verbatim}[fontsize=\relsize{-2}]
     movl $0, %eax
     ______________________________
     jnz q
     movl $1, %eax
 q:
\end{Verbatim}

\end{itemize}

{\bf Solutions:}

{\bf 1.a} 0xa2000f

{\bf 1.b} 25

{\bf 1.c}

\begin{Verbatim}[fontsize=\relsize{-2}]
     this
     V
\end{Verbatim}

{\bf 2.}  12 bytes written, none read

{\bf 3.a}  4 bytes read, 4 bytes written

{\bf 3.b}  no bytes read or written

{\bf 4.}  5 including {\bf main()}

{\bf 5.}

\begin{Verbatim}[fontsize=\relsize{-2}]
    movl stackstart, %esp
    pushl initeflags
    pushl $0
    pushl progstart
    iret
\end{Verbatim}

{\bf 6.}  The program literally modifies itself.

\begin{Verbatim}[fontsize=\relsize{-2}]
    addb $4, top+2
\end{Verbatim}

{\bf 7.a}  

\begin{Verbatim}[fontsize=\relsize{-2}]
c(c(IDT) + 8 * 0xe)
\end{Verbatim}

\begin{Verbatim}[fontsize=\relsize{-2}]
     cmpb (%cr2), $0x75
\end{Verbatim}

\end{document}
