\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.} (10) Almost any kind of CPU has a register which states where the
current (or next, depending on the time) instruction is in memory.  On
most machines, this is called the
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_, and on Intel CPUs it is called
the \_\_\_\_\_\_\_\_\_\_\_\_\_\_.

{\bf 2.} (10) Suppose our word size is 8 bits.  Then the number -4 is
represented as \_\_\_\_\_\_\_ in the 2s complement system, and as
\_\_\_\_\_\_\_ in the signed-magnitude system.  

{\bf 3.} (10) Suppose the beginning of the {\bf .data} segment in
an assembly language source file is

\begin{verbatim}
.data
a: .long 2
b: .long 30
...
\end{verbatim}

Show the exact line in the output of {\bf as -a} for the line
corresponding to the source line labeled {\bf b}.  (Assume the {\bf
.data} line is line 1 of the source file.)

{\bf 4.} (10) Look at page 3 of the PLN unit on machine language.  Suppose
on line 30, the instruction had been a jump to {\bf done}.  Show the
machine language if the jump type is JNZ.  Then show the machine
language if the jump type is JMP.

{\bf 5.}  (10) The C library function {\bf bcopy()} would be better
written in assembly language, as a big advantage could come from using
instructions like \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_.
(The function {\bf bcopy()} copies a sequence of bytes from one place in
memory to another.)

{\bf 6.}  (10) For each of the following instructions, state
how many reads and writes of memory will occur.  The period under
consideration is step C.  Your answers will consist of two numbers for
each instruction; write your answer right next to the instruction.

\begin{itemize} 

\item [(a)] {\bf movl \%eax, \%ebx}

\item [(b)] {\bf addl \%eax, \%ebx}

\item [(c)] {\bf addl (\%eax), \%ebx}

\item [(d)] {\bf cmpl \%eax, (\%ebx)}

\item [(e)] {\bf subl \$8, (\%ebx)}

\item [(f)] {\bf subl \%eax, (\%ebx)}

\item [(g)] {\bf cmpl \$8, (\%ebx)}  

\item [(h)] {\bf jnz y}

\end{itemize}

{\bf 7.} (10) Consider the instruction

\begin{verbatim}
movl $0xfff0eee0, %eax
\end{verbatim}

Suppose we forget the {\bf \$}.  Which one is most likely?

\begin{itemize}

\item [(i)] The assembler will complain.

\item [(ii)] A seg fault will occur.

\item [(iii)] A different kind of execution error than seg fault will occur.

\item [(iv)] No execution errors will be reported, but the program's
results will be incorrect.

\item [(v)] No errors will be reported of any kind, and the program's
results will be correct.

\end{itemize}

{\bf 8.} The following code counts lower-case letters in the array {\bf
x}, placing the results in the \underline{byte} array {\bf counts}.
E.g. the count of the number of occurrences of 'a' and 'e' will be at
{\bf counts} and {\bf counts+4}.

\begin{verbatim} 
.data 
x:  .string "c92jemc82ne<824j8vcm92jq3.,.u"
counts:  
    .rept 26 
    .byte 0 
    .endr
.text  
.globl _start
_start:
    movl $x, %eax
top:  
   movl $0, %ebx  
   _________________________
   cmpb $0, %bl
   jz done
   cmpb $'a', ______________
   js nextchar
   cmpb $'z'+1, ____________
   jge nextchar
   subl $'a',%ebx
   addl $____________, %ebx
   incb _________________
nextchar:
   addl $_________, %eax
   jmp top
done: movl %edx, %edx
\end{verbatim}

\begin{itemize}

\item [(a)] (10) Fill in the blanks.

\item [(b)] (5) State the full GDB command we would use to check whether
the program executed correctly when we get to {\bf done}.

\item [(c)] (5) This program will work correctly as long as no letter has
a count of more than \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_.

\item [(d)] (10) Show how we could replace the lines with {\bf addl}
and {\bf incb} just before {\bf nextchar} by a single instruction.

\end{itemize}

{\bf Solutions:}

{\bf 1.}  PC, EIP

{\bf 2.}  11111100, 10000100

{\bf 3.}  

\begin{verbatim}
2  004  1e000000
\end{verbatim}

{\bf 4.}  7503, EB03

{\bf 5.}  MOVS

{\bf 6.}  0 0; 0 0; 1 0; 1 0; 1 1; 1 1; 1 0; 0 0

{\bf 7.}  (ii)

{\bf 8.a.}

\begin{verbatim}
   # EAX will always point to the current character to be tallied
   movl $x, %eax
top:  
   # need to zero out all of EBX for later use (see subl)
   movl $0, %ebx  
   # get the character to be tallied
   movb (%eax), %bl
   # check for end of string
   cmpb $0, %bl
   jz done
   # check to see if in range 'a'-'z'
   cmpb $'a', %bl
   js nextchar
   cmpb $'z'+1, %bl
   jge nextchar
   # find distance past counts where we will increment
   subl $'a',%ebx
   # add that distance to counts to get address of place to increment
   addl $counts, %ebx
   # now increment
   incb (%ebx)
   # OK, ready to go to the next character in the string
nextchar:
   addl $1, %eax
   jmp top
done: movl %edx, %edx
\end{verbatim}

{\bf 8.b.}

\begin{verbatim}
x/26b &counts
\end{verbatim}

{\bf 8.c.} $2^8-1 = 255$

{\bf 8.d.}

\begin{verbatim}
incb counts(%ebx)
\end{verbatim}

\end{document}

