\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: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

\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 Remember:}  $2^{10} = 1024$ and $2^{16} = 65536$.  This may help
your calculations.

{\bf 1.}  For each of the following operations, give a single assembly
language instruction which will accomplish the task:

\begin{itemize}

\item [(a)] (5) Copy the number 12 to EDX.

\item [(b)] (5) Copy the number 12 to the word in memory pointed to by EDX.

\item [(c)] (5) Copy the number 12 to the byte in memory pointed to by EDX.

\item [(d)] (5) Copy the number 12 to the word in memory EDX bytes past location
200.

\end{itemize}

{\bf 2.} (10) There are two systems described in Sec. 3.2 of our notes on
Data Storage and Representation for storing floating-point numbers, one
a simple system and the other being the IEEE standard.  For each of the
following, state whether storage in the given system is unique or allows
multiple representations of a number:

\begin{itemize} 
\item [(a)] The simple system.  
\item [(b)] The IEEE system.  
\end{itemize}

{\bf 3.} (10) Suppose the source code for a program contains the
instruction {\bf mov 4(\%esp), \%ecx}.  When we run {\bf as -a}, we find
that the corresponding machine code is 8B4C2404.  Suppose we
accidentally run {\bf cat} on the executable program file.  Give a
complete list, if any, of the printable characters we can be sure to see
on the screen.

{\bf 4.} (10) Consider the instruction {\bf shll \$23, \%eax}, with
c(EAX) = x being considered a signed number.  Fill in the blank:  If x
happens to be nnonegative, then the use of this instruction will be safe
as long as x $\leq$ \_\_\_\_\_\_\_\_\_.

{\bf 5.} (25) The code below copies a string which starts at {\bf x} to
{\bf y}, in reverse order.  Fill in the blanks.

\begin{Verbatim}[fontsize=\relsize{-2}]
    movl n, ___________
    movl $x, %eax
    movl $y, %ebx
    addl n, %ebx
    decl %ebx
top:
    movb (%eax), %dl
    movb %dl, __________
    incl %eax
    ___________________
    decl %ecx
    jns top

\end{Verbatim}

{\bf 6.} (15) Consider the code 

\begin{verbatim}
x: .long 65574
y: .string "cumulus"
\end{verbatim}

Find base-10 values m and n such that

\begin{verbatim}
x: .byte u
   .byte v
   .word n
y: .string "cumulus"
\end{verbatim}

produces the same effect, i.e. the same set of bytes when the program is
loaded into memory to be run, as the original code. 

{\bf 7.} (10) A program includes the line

\begin{verbatim}
s: .string "\0+\0\0rst"
\end{verbatim}

where the first, third and fourth characters are null.  During
debugging, if we type

\begin{verbatim}
(gdb) p s
\end{verbatim}

in GDB, what will be printed out?

{\bf 1.}  

\begin{verbatim}
movl $12, %edx
movl $12, (%edx)
movb $12, (%edx)
movl $12, 200(%edx)
\end{verbatim}

{\bf 2.}  (a) multiple; (b) unique

{\bf 3.}  L,\$

{\bf 4.}  We need to avoid changing a positive to a negative, i.e.
letting a 1 bit get to the MSB position.  Since we are shifting left 23
bit positions, this implies that our number must be confined to Bits 7-0
(where Bit 0 is the LSB), which in turn means a number at most
$2^8-1=511$.

{\bf 5.}  

\begin{verbatim}
%ecx
(%ebx)
decl %ebx
\end{verbatim}

{\bf 6.}  The number 65574 has the hex form 0x00010026.  Now, to put
this in byte/byte/word form, note that that first byte will have the
lowest address, and thus due to little-endianness is the least
significant byte of the number, i.e. 0x26, i.e. 38.  Continuing this
reasoning, we get u = 38, v = 0 and w = 1.

{\bf 7.}  The print ({\bf p}) command prints out one 32-bit quantity,
i.e. one word.  Thus it will print out the first 4 bytes starting at
{\bf s}.  The fact that we originally filled those bytes using the {\bf
.string} directive is irrelevant; remember, there are no data types at
the machine level.  So, GDB treats the first 4 bytes as an integer,
printing them out in base-10 (not hex, since there was no {\tt /x} in
the command).  That number is $43 \times 256 = 11008$.

\end{document}

