\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}

\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) What is the hex (i.e. base-16) form of the decimal number 52?

{\bf 2.}  (5) Fill in the blank:  If you are using subroutines, you may not
use the \_\_\_\_\_\_\_\_\_\_\_\_\_\_ register for data storage.

{\bf 3.}  The following questions are about the {\it revised} version of
the program in Sec. 6 of our unit on Linux assembly language.  Assume
throughout that {\bf x} begins in location 0x500.

\begin{itemize}

\item [(a)]  (5) What will be the hex address of the word in the {\bf
.data} segment whose initial value is 22?

\item [(b)]  (5) After the instruction labeled {\bf init} is executed,
what value will be in EAX?

\item [(c)]  (5) Which instruction will be executed after the {\bf ret}
in {\bf init()}?  (Write out the exact assembly language instruction here.)

\item [(d)]  (5) What value will be in EBX at the time the first call to
{\bf swap()} is made?

\item [(e)]  (10) With the data we have here, the array is correctly
rearranged from (1, 5, 2, 18, 25, 22, 4) to (1, 2, 4, 5, 18, 22, 25).
But suppose we had accidentally forgotten to put the parentheses in the
instruction labeled {\bf top}.  What would happen?  Would the program
likely generate a seg fault?  If so, explain how.  If not, what would
the final ordering be in the array?

\end{itemize}

{\bf 4.} (5) In the example of the speaker in Sec. 13.4 in our unit on Linux
assembly language, why not use \$0xc instead of \$0xfc?  Explain fully.

{\bf 5.} (10) Consider the C statement

\begin{verbatim}
int g = 1985229328;
\end{verbatim}

Note that $1985229328 = 
7 \times 16^7 + 
6 \times 16^6 +
5 \times 16^5 +
4 \times 16^4 +
3 \times 16^3 +
2 \times 16^2 +
1 \times 16^1 +
0 \times 16^0$.
Suppose \&g = 0x234.  What is the content of byte 0x236, assuming this
is run on an Intel machine?

{\bf 6.}  (20) Suppose we have written an assembly language program {\bf
z.s} and we run {\bf as} on it with the {\bf --gstabs} option, so that
the assembler saves the symbol table.  This produces a file {\bf z.o},
which we link to create an executable file {\bf z}.  For each of the
following, answer either Yes or No, indicating whether the indicated
information is saved in the symbol table or not.

\begin{itemize}

\item [(i)] The labels (i.e. names like {\bf x}, {\bf sum} and {\bf
top}) will be saved. 

\item [(ii)] For labels in the {\bf .data} segment, the types of the
data (integer, character, etc.) will be saved.

\item [(iii)] For labels in the {\bf .data} segment, the sizes in bytes
of the data items will be saved.

\item [(iv)] For labels in the {\bf .data} segment, the addresses (at
least the offsets) of the labels will be saved.

\end{itemize}

{\bf 7.}  (25) The following code might appear in an English/Chinese text
editor:  

\begin{verbatim}
.data
line: .space 12
nb: .long 12
neb:  .long 0

.text
.globl _start
_start:
   movl $0, %ebx
   movl $line, %eax
   movl nb, %ecx
top:
   movb (%eax), %dl
   ______ ______, %dl  # fill in operation and 
                       # source operand
   jz x
   addl ______, %eax
   decl %ecx
   jmp next
x:
   incl %ebx
   ___________  # fill in one instruction here
next:
   decl %ecx
   jnz top
   movl ______, neb
\end{verbatim}

The string of bytes which comprise a line in the file to be edited
starts at {\bf line}.  The label {\bf nb} is the name of a word which
contains the number of bytes in the line.  

The code is supposed to count the number of English characters in the
line, and place that count in {\bf neb}.  Fill in the blanks.

{\bf Solutions:}

{\bf 1.}  0x34

{\bf 2.}  ESP

{\bf 3.a.}  0x514

{\bf 3.b.}  0x500

{\bf 3.c.}

\begin{verbatim}
movl (%eax), %ecx
\end{verbatim}

{\bf 3.d.}  5

{\bf 3.e.}  2, 4, 22, 0x504, 0x508, 0x510, 0x50c

{\bf 4.}  We need to preserve the original values of the other bits.

{\bf 5.}  The number in {\bf g} is 0x76543210.  Since Intel is
little-endian, that implies that 0x10 is in byte 0x234, 0x32 is in byte
0x235, 0x54 is in byte 0x236 and 0x76 is in byte 0x237.

{\bf 6.}  (i) Yes.  (ii) No.  Remember, there \underline{are} no types
at the assembly language level.  (iii)  No.  Similarly, there are no
sizes associated with labels.  A label is only a name for the given byte
(or, equivalently, the given word).  (iv) Yes.  This is how we can do
things like {\tt x/7w \&x} in GDB or DDD.

{\bf 7.}

\begin{verbatim}
.data
line: .space 12  # a line of characters from the file being edited
nb: .long 12  # length of "line" in bytes
neb:  .long 0  # in end, will contain number of English characters

.text
.globl _start
_start:

   # EBX will hold a count of English characters
   # EAX will always point to the current character in "line"
   # ECX will serve as a loop counter
   movl $0, %ebx   
   movl $line, %eax  
   movl nb, %ecx  
top:
   movb (%eax), %dl # get byte from "line"
   # test for high bit being a 1
   andb $0x80, %dl  
   jz x
   # this is the Chinese case
   addl $2, %eax 
   decl %ecx
   jmp next
x:
   # English case
   incl %ebx
   incl %eax  
next:
   decl %ecx
   jnz top
   movl %ebx, neb
\end{verbatim}

\end{document}

