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

\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.}  Consider the cache example on pp.19-20 of the notes on
``computer engines.''

\begin{itemize}

\item [(a)] (10) When a cache \_\_\_\_\_\_\_\_ occurs, the CPU will need
to read a \_\_\_\_\_\_\_\_\_ from memory, which consists of \_\_\_\_\_\_
(fill in a number) bytes.

\item [(b)] (10) Consider the scenario described in footnote 22, for
Version I (not II) of the code, at the time immediately after we have
added {\bf X[0][31]} to {\bf Sum}.  (Suppose the latter is stored in a
register, thus not affecting the cache.)  How many misses---if
any---have occurred up to this point?  {\bf Explain \underline{fully} in
order to receive full credit.}

\end{itemize}

{\bf 2.} (25) Suppose in Homework IV we also have a function {\bf
totfr()}.  It has no arguments, but it returns an {\bf int} value which
is the total number of bytes currently free in all of {\bf heap}.  Fill
in the blanks in the following code for this function:

\begin{verbatim}
.____________
totfr:
   _________________ # fill in one instruction
   movl $0, %eax
   movl _______, %ebx
top:
   cmpl _____, (%ebx)
   jz done
   addl _____, %ebx
   addl _______, __________
   addl $4, %ebx
   jmp top
done:
   _________________ # fill in one instruction
   ret
\end{verbatim}

{\bf 3.}  This problem concerns the example in Sec. 3 of the unit on
machine language.

\begin{itemize}

\item [(a)] (5) Suppose on line 26 the instruction were to use EDX
instead of EBX.  What would the machine code 0xbb00000000 change to
in the output of {\bf as -a}? 

\item [(b)] (10) Suppose line 34 were to have a label {\bf verydone}, and
the instruction in line 32 were {\bf jnz verydone}.  What would the
machine code 0x75f8 for that instruction change to in the output of {\bf
as -a}? 

\end{itemize}

In the remaining parts, assume that {\bf ld} arranges for the {\bf
.data} and {\bf .text} segments to be loaded beginning at 0x200 and
0x8000, respectively.  Do not assume any changes mentioned in parts
(a) and (b) above.

\begin{itemize}

\item [(c)] (10) Consider the DEC instruction in line 31.  List (in hex)
all values which will pass through the MAR during this instruction
(including all of Steps A, B and C, though do not break down your answer
by step), and then do the same for the MDR.  Ignore effects of
prefetching and caches.

\item [(d)] (5) Would the machine language for any of the instructions
change from what is listed in the output of {\bf as -a}?  For example,
in line 30, would the 0x83c104 change to, say 0x83c304?  Either give an
example of an instruction in this program whose machine code would
change, and state what the new machine code would be, or explain
\underline{clearly} why none of the machine language in the output of
{\bf as -a} would change.

\end{itemize}

{\bf 4.} This problem concerns the function {\bf sum()} on p.10 of the
notes unit on subroutines.

\begin{itemize}

\item [(a)] (5) The compiler wasn't very efficient here.  Give two
instructions it could have used in place of 

\begin{verbatim}
subl $8, %esp
movl $0, -8(%ebp) 
movl $0, -4(%ebp)
\end{verbatim}

\item [(b)] (10) Give specific assembly code, consisting of just one
instruction, that the compiler will likely produce from the line

\begin{verbatim}
return s
\end{verbatim}

\end{itemize}

{\bf 5.} (10) Suppose in a C program {\bf main()} calls {\bf f()}, which
in turn calls {\bf g()}.  Suppose also that the declaration of
{\bf f()} begins with

\begin{verbatim}
int f(int x, int y)

{  int u,v,w;
...
\end{verbatim}

Also, {\bf g()} has no arguments and has only one local variable, {\bf
z}, of type {\bf int}.

Write a single C statement, which would be in {\bf g()}, which would set
{\bf f()}'s local variable w to 12.  Your statement should involve {\bf
\&z}

{\bf Solutions:}

{\bf 1.a.}  miss; block; 64

{\bf 1.b.}  The key point is that when the read of {\bf X[0][0]} causes
the first cache miss, only the array through {\bf X[0][30]} will be
brought in.  The read of {\bf X[0][31]} will then cause a second miss.
The answer is 2.

{\bf 2.}

\begin{verbatim}
# make totfr accessible from the C code
.globl totfr
totfr:
   # save old EBX, since we will write to it
   pushl %ebx
   movl $0, %eax
   # map contains the locations and sizes 
   # of the free chunks, so point
   # EBX to it
   movl $map, %ebx
top:
   # map uses -1 as a termination signal
   cmpl $-1, (%ebx)
   jz done
   # no termination, so move to the next 
   # element of map, which gives the
   # size of this free chunk
   addl $4, %ebx
   # read that size
   addl (%ebx), %eax
   # go to the next element of map, which 
   # we will check for termination
   addl $4, %ebx
   jmp top
done:
   # restore old EBX
   popl %ebx
   ret
\end{verbatim}

{\bf 3.a.}  0xba00000000

{\bf 3.b.}  0x7506

{\bf 3.c.}  The instruction is fetched.  Since it is in offset 0x14 of
the {\bf .text} segment, which itself begins at 0x8014, the CPU will put
0x8014 in the MAR.  The memory will then send back the contents of that
location, i.e. the instruction, 0x48, which the CPU will receive in the
MAR.  The instruction itself doesn't access memory, so this is the end
of activity in MAR/MDR for this instruction.

{\bf 3.d.}  As pointed out in the notes, any machine code which the
assembler had tentatively filled with an offset must be changed by the
linker to the actual address of the item in question.  The instruction
at offset 0x27 the {\bf .text} segment will change to 0xb900200000,
because {\bf x}, which had been at offset 0 of the {\bf .data} segment,
will be at absolute location 0x200 + 0 = 0x200.  The instruction at
offset 0x33 will change to 0x891d10200000.  (The linker will place these
two instructions at 0x8027 and 0x8033, but this is irrelevant to the
question.)

{\bf 4.a.}

\begin{verbatim}
pushl $0
pushl $0
\end{verbatim}

{\bf 4.b.}

\begin{verbatim}
movl -8(%ebp), %eax
ret
\end{verbatim}

(The question was misstated.  It should have asked for two instructions,
the second of which was {\bf ret}.  So, the grading here was liberal.)

Note that

\begin{verbatim}
movl s, %eax
\end{verbatim}

will NOT work.  Local variables are stored on the stack, not in the {\bf
.data} segment.

{\bf 5.}

\begin{verbatim}
*(&z+3) = 12;
\end{verbatim}

Here is a picture of the stack at the time we are exectuing {\bf g()}:

\begin{verbatim}
z
saved EBP
return address
w
v 
u
saved EBP
return address
x
y
\end{verbatim}

As you can see, the address of {\bf w} is 12 bytes more than that of
{\bf z}.  However, {\bf \&z} is considered a pointer, so pointer
arithmetic is used, hence the expression {\bf \&z+3}.


\end{document}

