\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) The following code searches through an array pointed to by
ECX for a word whose contents are equal to those of EAX.  Such a word
is assumed to exist, and once it is found, its address will be in EBX.
Fill in the blanks (you do not have to put an instruction at {\bf done}):

\begin{Verbatim}[fontsize=\relsize{-2},numbers=left]
      movl ____________________________________________
top:  _________________________________________________
      je done
      addl ____________________________________________
      _________________________________________________
done: 
\end{Verbatim}

{\bf 2.} (30) The function {\bf snc()} is intended as a replacement for
the library function {\bf strncpy()}.  The latter has arguments {\bf
dst}, {\bf src} and {\bf n}, and copies {\bf n} bytes at the string
pointed to by {\bf src} to the string pointed to by {\bf dst}.  However,
{\bf snc()} may do it faster, since it uses an instruction from the MOVS
family.  Fill in the blanks.

\begin{Verbatim}[fontsize=\relsize{-2},numbers=left]
snc:
   pushl %ecx
   pushl %esi
   pushl %edi
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
   popl %edi
   popl %esi
   popl %ecx
____________________________________________________
\end{Verbatim}

{\bf 3.} Consider the ISR, top of p.168.  Consider parts (a) and (b)
below to be independent of each other.

\begin{itemize}

\item [(a)] (15) State code, to be inserted after line 5, that will put 1
or 0 into EDX, depending on whether the user struck the A key or not.
(Assume that whatever registers you use have previously been saved.)

\item [(b)] (15) State code, to be inserted after line 3, that will put 1
or 0 into EDX, depending on whether the Carry Flag had been set just
before the interrupt occurred.  If you use any registers, push their old
values first.

\end{itemize}

{\bf x.} (10) Suppose {\bf m} is declared as

\begin{Verbatim}[fontsize=\relsize{-2}]
int m[8][20];
\end{Verbatim}

How many words apart in memory will be {\bf m[5][5]} and {\bf m[7][7]}?

{\bf Solutions:}

{\bf 1.}

\begin{Verbatim}[fontsize=\relsize{-2},numbers=left]
      movl %ecx, %ebx
top:  cmpl (%ebx), %eax
      je done
      addl $4, %ebx
      jmp top
done: 
\end{Verbatim}

{\bf 2.}

\begin{Verbatim}[fontsize=\relsize{-2},numbers=left]
snc:
   pushl %ecx
   pushl %esi
   pushl %edi
   movl 24(%esp), %ecx
   movl 20(%esp), %esi
   movl 16(%esp), %edi
   rep movsb
   popl %edi
   popl %esi
   popl %ecx
   ret
\end{Verbatim}

{\bf 3a.}  The key point was that the machine uses a scan code rather
than ASCII.

\begin{Verbatim}[fontsize=\relsize{-2}]
     movl $0,%edx
     cmpb $0x1e, %al
     jnz w
     movl $1,%edx
  w: addl $0,%edx  # dummy instruction
\end{Verbatim}

{\bf 3b.}

\begin{Verbatim}[fontsize=\relsize{-2}]
movl 16(%esp), %edx  # get saved EFLAGS value from stack
andl $1, %edx
\end{Verbatim}

Note that solutions based on JC/JNC instead of using the saved EFLAGS
value on the stack received only 5 points.  Given the information in our
course, you cannot be sure that the EFLAGS register is still intact
after the interrupt action, subsequent PUSH instructions, etc.

Similarly, you cannot tell from our course material whether instructions
like 

\begin{Verbatim}[fontsize=\relsize{-2}]
movl %eflags, %eax
\end{Verbatim}

are legal or not.  (They aren't.)

{\bf 4.}  C uses row-major order.  The element {\bf m[5][5]} will be 14
words from the end of row 5, thus 15 words from the beginning of row 6; 
then there will be 20 words to the beginning of row 5; then
7 more words to {\bf m[7][7]}.  That's a total distance of 42 words.

In general, the element {\bf m[i][j]} is at the word 20i+j words past
the beginning of {\bf m}.  So, just subtract, getting a distance of 42
words.

\end{document}

