<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">
\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.} (15) Give the value (expressed as a decimal number) that will be
in EAX after executing

\begin{Verbatim}[fontsize=\relsize{-2}]
movl $25, %eax
andl $-3, %eax
\end{Verbatim}

Show your work!

{\bf 2.} (15) Say on a 32-bit machine we have C code that includes an {\bf
int} array {\bf x}, with 8 columns.  Suppose {\bf x[2][3]} has address
0x456.  What will be the hex address of {\bf x[3][2]}? 

{\bf 3.} (15) Which program in our homework had multiple stacks?  {\bf
EXPLAIN CAREFULLY}.

{\bf 4.} (15) Fill in the blank with a term from our course for a certain
kind of function/subroutine:  Using real machines in our course brings
large educational benefits, but one advantage of using a simulator is
that we could have had homework in which students wrote
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_s.

{\bf 5.} (40) Write an assembly language subroutine {\bf countsmall()},
callable from C, with C signature 

\begin{Verbatim}[fontsize=\relsize{-2}]
int sep(int *x,int nx)
\end{Verbatim}

Here {\bf x} is an {\bf int} array of length {\bf nx}, and the function
returns a count of the number of elements smaller than {\bf x[0]}.

For instance, the code

\begin{Verbatim}[fontsize=\relsize{-2}]
int y[6] = {5,12,13,2,8,1},m;
m = countsmall(y,6);
\end{Verbatim}

would result in {\bf m} being 2, while {\bf countsmall(y,5}) would return
1.

Write the full assembly code for {\bf countsmall()}.  Be SURE to have
comments at the beginning, stating what you are using the various
registers for.  Assume that NO registers need be saved for the caller.
{\bf WRITE LEGIBLY}; write on scratch paper first, and then copy to your
exam sheet.  (Remember, supplemental papers to your exam sheet are NOT
allowed.)

{\bf Solutions:}

{\bf 1.} 25 is 0x19, i.e. 00011001, and -3 is (in 2 hex digits) 0xfd,
i.e. 11111101.  And-ing the former with the latter puts a 0 in bit
position 1, and leaves the other bits unchanged.  But there is already a
0 in that position, so the 25 is unchanged in all bits.

{\bf 2.} From {\bf x[2][3]} to the end of row 2 is a distance of 4
words.  Then {\bf x[3][2]} is 3 more words, for a total of 7.  Then
compute $0x456 + 7 \times 4 = 0x472$.

{\bf 3.} This was the threaded program.  Each thread has its own stack.

{\bf 4.} Interrupt service routines.

{\bf 5.}

\begin{Verbatim}[fontsize=\relsize{-2}]
# countsmall() number of elements in an array x that are
# smaller than x[0]

# the C call is
#
#    int countsmall(int *x,int nx)
#
# where nx is the length of x 
#
# for instance, the code
#
#    int y[6] = {5,12,13,2,8,1},m;
#    m = countsmall(y,6);
#
# will result in m = 2

.text
.globl countsmall

   # EBX will point to the current element of x 
   # ECX will hold x[0]
   # EDX will hold the loop counter 
   # EAX will hold the return value

   # assume NO registers need to be saved to protect the caller

countsmall:
   movl 4(%esp), %ebx  # get address of x
   movl 8(%esp), %edx  # get nx
   decl %edx  # skip x[0]
   movl $0, %eax  # count is 0 so far
   movl (%ebx), %ecx  # ECX now holds x[0]
   addl $4, ebx  # skip x[0]
top:
   movl (%ebx), %edi  # EDI now holds x[i]
   cmpl %ecx, %edi
   jge decedx
   incl %eax
decedx:
   addl $4, %ebx
   decl %edx
   jnz top
   ret
\end{Verbatim}

\end{document}

</pre></body></html>