
\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.} (10) Give a single assembly language instruction equivalent to

\begin{Verbatim}[fontsize=\relsize{-2}]
popl %ecx
popl %ecx
popl %ecx
\end{Verbatim}

assuming that we do not care what the popped values actually are.

{\bf 2.} (10) When the call {\bf scanf("\%d\%d\%d",\&u,\&v,\&w)} is
compiled, how many push operations will appear before the CALL
instruction?

{\bf 3.} (10) Say you are running some program on CSIF that makes use of
a special library in your own home directory, say {\bf /home/thisisme/}.
What command should you run to enable the OS to find that library when
you execute the program?

{\bf 4.} (10) List the Intel-specific registers (using their official
Intel names) whose values are affected when a RET instruction executes.

{\bf 5.} This problem concerns the code in pp.137-139.  Suppose we were
to change things so that {\bf addone()} would have (as viewed as a
function callable from C) the signature

\begin{Verbatim}[fontsize=\relsize{-2}]
int addone(int x)
\end{Verbatim}

as opposed to what was in the version in the book,

\begin{Verbatim}[fontsize=\relsize{-2}]
void addone(int *x)
\end{Verbatim}

The function will now return the value of its argument plus 1.

\begin{itemize}

\item [(a)] (30) Fill in the gap in the revised version of {\bf
addone()}:

\begin{Verbatim}[fontsize=\relsize{-2}]
.text
.globl addone
addone:
                            # insert at most 4 instructions here
   ret
\end{Verbatim}

\item [(b)] (30) Suppose the call in {\bf TryAddOne.c} is now wrapped
inside a print call:

\begin{Verbatim}[fontsize=\relsize{-2}]
printf("%d\n",addone(x));
\end{Verbatim}

I ran the new code through {\bf gcc -S}, an excerpt of which appears
below.  Fill in the gaps.

\begin{Verbatim}[fontsize=\relsize{-2}]
movl	$7, x
movl	x, %eax
movl	              (%esp)    # gap 1
call	addone
movl	                        # gap 2
movl	$.LC0, (%esp)
call	printf
\end{Verbatim}

\end{itemize}

{\bf Solutions:}

{\bf 1.} {\bf addl \$12, \%esp}

{\bf 2.} 4

{\bf 3} 

\begin{Verbatim}[fontsize=\relsize{-2}]
setenv LD_LIBRARY_PATH /home/thisisme
\end{Verbatim}

{\bf 4.} ESP, EIP

{\bf 5.a}

\begin{Verbatim}[fontsize=\relsize{-2}]
.text
.globl addone
addone:
   movl 4(%esp), %eax  
   incl %eax 
   ret
\end{Verbatim}

{\bf 5.b}

\begin{Verbatim}[fontsize=\relsize{-2}]
movl    $7, x
movl    x, %eax
movl    %eax, (%esp)
call    addone
movl    %eax, 4(%esp)
movl    $.LC0, (%esp)
call    printf
\end{Verbatim}

\end{document}

