\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.} (5) Show the hex notation for the string 101111010110.

{\bf 2.} (5) Suppose c(EBX) = 0x12aa34bb.  What is c(BH)?

{\bf 3.} This problem is about the JS instruction.

\begin{itemize}

\item [(a)] (5) The piece of hardware which this instruction checks is
the \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_.

\item [(b)] (5) In part (a), why did it not explicitly add the
specification ``on our Intel CPUs''?

\end{itemize}

{\bf  4.}:  (10) On a 32-bit machine, a {\bf short int} might be
implemented in 16 bits.  This is a decision made by the developer of the
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_, who in turn might be
more likely to do this if the developer of the
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ has provided for 16-bit
operations.

{\bf 5.} (5) Suppose in C code there is a {\bf char} array {\bf z}, and a
statement

\begin{verbatim}
strcat(z,"uvw");
\end{verbatim}

Which one is correct?

\begin{itemize}

\item [(i)] If the machine is little-endian, then the 'u' will have a smaller
address than the 'w'.

\item [(ii)] If the machine is little-endian, then the 'u' will have a greater
address than the 'w'.

\item [(iii)] Regardless of endian-ness of the machine, the 'u' will have a
smaller address than the 'w'.

\item [(iv)] Regardless of endian-ness of the machine, the 'u' will have a
greater address than the 'w'.

\end{itemize}

{\bf 6.} (10) Next to each of the following instructions, write ``Yes'' if
that instruction would cause the assembler to temporarily substitute an
offset for an address, and thus need to ``leave a note'' in the {\bf .o}
file.  Otherwise write ``No.''  Here, {\bf z}, {\bf y} and {\bf w} are
labels.

\begin{verbatim}
jmp z
call y
movl $w, %eax
addl $8, %eax
\end{verbatim}

{\bf 7.}  Consider the program in Section 8 of the PLN unit on Linux
Intel assembly language.  

\begin{itemize}

\item [(a)] (15) Look at the three instructions ({\bf decl}, {\bf jz}
and {\bf addl}) which start at {\bf nextj}.  Show the output of {\bf as
-a} for those three instructions (minus line numbers and offsets).
You'll need to know that the register code for EBP is 101.  Also, assume
for the sake of this problem that the format for the {\bf addl}
instruction is 11111DDDIMM4.

\item [(b)] (10) Suppose the {\bf .data} segment starts at 0x1248.  As we
enter {\bf swap()} for the first time, what values will be in EAX and
ESI?

\end{itemize}

{\bf 8.} (30) A program contains the code excerpt shown below, which finds
the first instance of the bit substring 111 in ECX.  (That substring is
guaranteed to be in ECX somewhere.)  The answer, reported in EDX, will
be the position of the first 1 in 111, and will range from 31 (meaning
at the most significant bit position) to 0 (meaning the least
significant bit position).  For example, if c(ECX) = 0x70f02222 (that 7
is the most significant hex digit), then the final value in EDX will be
30.  If c(ECX) = 0x11f02222, then EDX will turn out to be 24.  Fill in
the blanks.

\begin{verbatim}
    movl $31, %edx
top:
    movl %ecx, %ebx
    andl ___________________, %ebx
    cmpl $0xe0000000, %ebx
    _________________ found111
    decl _____________________
    shll _______________, %ecx
    jmp top
found111: # (no blank here)
\end{verbatim}

{\bf Solutions:}

{\bf 1.}  0xbd6

{\bf 2.}  0x34

{\bf 3.a.}  EFLAGS (or Sign Flag)

{\bf 3.b.}  Each CPU has a different instruction set.  JS is an Intel
instruction, so we don't have to specify Intel.

{\bf 4.} compiler, CPU

{\bf 5.} (iii)

{\bf 6.} No; Yes (No acceptable though actually wrong); Yes; No

{\bf 7.a.}

\begin{verbatim}
4D
7407
FA04000000
\end{verbatim}

{\bf 7.b.}  The first swap occurs when ``x[i]'' is the 5, and the
``x[j]'' is the 2, at 0x124c and 0x1250, respectively.  So, EAX and ESI
contain those values.

{\bf 8.}

\begin{verbatim}
    # overall plan:  
    #    while (1) 
    #       check for 111 on left end
    #       if found break
    #       shift left 1 bit}
    # EDX will be the original bit position of the 3-bit set we
    #    are currently checking
    movl $31, %edx
top:
    movl %ecx, %ebx
    # check for 111 on left end
    andl $0xe0000000, %ebx
    cmpl $0xe0000000, %ebx
    # if found break
    jz found111
    decl %edx
    shll $1, %ecx
    jmp top
found111: # (no blank here)
\end{verbatim}

\end{document}


