\documentclass{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.}  (25) Suppose c(ESP) = 5000, c(5000) = 240 and c(240) = 8800.
Consider the execution (excluding the instruction fetch) of

\begin{Verbatim}[fontsize=\relsize{-2}]
addl $8, (%esp)
\end{Verbatim}

\begin{itemize}

\item [(a)] What values will go onto the address bus during the
execution of this instruction?

\item [(a)] What values will go onto the data bus during the
execution of this instruction?

\end{itemize}

{\bf 2.}  (25) Fill in the blanks:  The IDT register is written to by the
\_\_\_\_\_\_\_\_\_\_ at \_\_\_\_\_\_\_\_\_\_ time, and is read by the
hardware at the times that \_\_\_\_\_\_\_\_\_\_ occur.

{\bf 3.}  (25) Suppose {\bf x()} calls {\bf y()}, which in turn calls {\bf
z()}, with {\bf x()} and {\bf y()} being written in C, and with {\bf
z()} in assembly language.  Suppose the declaration of {\bf x()} starts
with

\begin{Verbatim}[fontsize=\relsize{-2}]
int x(int a, int b, int c)
\end{Verbatim}

Give {\bf at most four lines} of assembly code, to be placed at the very
beginning of {\bf z()}, which will copy the third parameter of {\bf x()}
(formally named {\bf c} above, but not referrable to as such from within
{\bf z()}, for reasons of generality), to the EDX register.  You may use
any registers as ``scratch'' space except ESP and EBP.  

{\bf 4.} (25) The code below is made up, but could be part of Linux's
context switch code, preceding the real code shown on p.10 of our PLN
unit on OS.  It would choose the process to be given the next turn, and
would point EBX and ECX to the proper values.  Assume that:  each
process' TSS is 812 bytes long; the current state of the process, 'R' or
'S' for Run or Sleep, is stored 240 bytes from the beginning of the TSS;
the process number is stored in the word 88 bytes from the beginning of
the TSS; the variable {\bf currturn} in the {\bf .data} segment gives
the address of the TSS of the process whose turn it is currently (at the
time {\bf u}'s turn ends on p.9, {\bf currturn} points to {\bf u}'s
TSS, and the code below will ultimately set {\bf currturn} to {\bf v}'s
TSS).  The TSSs are assumed to form an array, i.e.  they are stored
contiguously.

\begin{Verbatim}[fontsize=\relsize{-2}]
   # saving of register values on stack, 
   # code not shown
   movl __________, %ecx
   movl %ecx, %ebx
top:
   # check if at end of array; 
   # if so, go to array start (code not shown)
   addl __________, %ebx
   cmpb 'R', __________(%ebx)
   jz got_one
   jmp top
got_one:
   movl __________, __________
   # restoring of register values from stack, not shown
\end{Verbatim}

{\bf Solutions:}

{\bf 1.a}  5000, 5000

{\bf 1.b}  240, 248

{\bf 2.}  OS, boot, interrupts

{\bf 3.}  Note that EBP still has the value placed into it by {\bf y()}.

\begin{Verbatim}[fontsize=\relsize{-2}]
movl (%ebp), %ebx
movl 8(%ebx), %ebx
\end{Verbatim}

Due to an error in the PLN, the value 16 was accepted in place of 8.

{\bf 4.}

\begin{Verbatim}[fontsize=\relsize{-2}]
   # saving of register values on stack, 
   # code not shown
   movl currturn, %ecx
   movl %ecx, %ebx
top:
   # check if at end of array; 
   # if so, go to array start (code not shown)
   addl $812, %ebx
   cmpb 'R', 240(%ebx)
   jz got_one
   jmp top
got_one:
   movl %ebx, currturn 
   # restoring of register values from stack, not shown
\end{Verbatim}

\end{document}

