\documentclass[noanswers,addpoints,10pt]{exam}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[most]{tcolorbox}
\usepackage{datetime}
\usepackage{enumerate}
\usepackage{enumitem}
\usepackage{xltxtra}
\usepackage{listings}
\usepackage{listings-mips}

\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{makecell}
\usepackage{multicol}
\usepackage{multirow}

\usepackage{tabularx}
\usepackage{textcomp}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,decorations.pathmorphing,backgrounds,positioning,fit}
\usepackage{pgf-umlcd}

% compact itemized and then enumerated
\setlist{leftmargin=*,
  noitemsep,
  topsep=5pt,
  parsep=0pt,
  partopsep=0pt,
}

\setlist[enumerate]{leftmargin=*,
  itemsep=2pt,
  topsep=0pt,
  parsep=0pt,
  partopsep=0pt,
}


\usepackage{fontspec}
\setmainfont{Gentium Basic}
\setmonofont{Comic Code}[
  Scale          = 0.80,
  UprightFont    = *-Regular,
  ItalicFont     = *-Italic,
  BoldFont       = *-Bold,
  BoldItalicFont = *-Bold Italic
]

\lstset{
  language=[mips]assembler,
  basicstyle=\ttfamily\small,
}

\DeclareMathOperator{\limplies}{\Rightarrow}
\DeclareMathOperator{\liff}{\iff}
\DeclareMathOperator{\lxor}{\oplus}
\DeclareMathOperator{\qeq}{\overset{?}{=}}
\DeclareMathOperator{\modulus}{\mathbf{mod}}
\DeclareMathOperator{\even}{even}
\DeclareMathOperator{\odd}{odd}
\DeclareMathOperator{\ddiv}{|}
\DeclareMathOperator{\ndiv}{\not|}
\DeclareMathOperator{\contra}{\Rightarrow\mskip-\thinmuskip\Leftarrow}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\Z}{\mathbb{Z}}
\renewcommand{\P}[1]{\mathbb{P}(#1)}

\DeclareMathOperator{\vowel}{vowel}
\DeclareMathOperator{\consonant}{consonant}
\DeclareMathOperator{\isprime}{prime}
\DeclareMathOperator{\equals}{equals}
\DeclareMathOperator{\divides}{divides}

% classname
\newcommand\examNumber{gFunctions - MIPS Calling Conventions}
\newcommand\semester{Spring 2024}
\newcommand\className{Computer Organization}
\newcommand\classNumber{CIS~356}
\newcommand\classFullName{\classNumber~\className}
\renewcommand{\dateseparator}{-}
\yyyymmdddate

% /classname

\pagestyle{headandfoot}
\firstpageheader{\classFullName\\\semester}{\examNumber\\~}{\today\\~}
\runningheader{\examNumber}{}{\today}


\title{\examNumber}


\newenvironment{showExample}[1]{
  \begingroup
  \renewcommand{\solutiontitle}{\noindent\textbf{Example:}\enspace}
  \printanswers
  \begin{solution}[#1]
  }{
  \end{solution}
  \endgroup
}

\begin{document}

\subsection*{Introduction}
This is a group assignment on \emph{calling} and \emph{defining}
simple functions in MIPS assembly. Parameter passing, stack frames,
and return values are all explored.

\section*{Assignment Goals}
\paragraph{Learning Outcomes} After completing this group assignment,
each student is expected to be able to
  \begin{itemize}
  \item \textbf{Trace} MIPS assembly code across function call and return.
  \item Use \lstinline{$a*} and \lstinline{$v*} registers according to
    MIPS calling conventions.
  \item \textbf{Push}, \textbf{pop}, and access values in the
    \emph{activation record} of a function.
\end{itemize}

\section*{Procedure}

Get out paper for a \emph{single} turn-in at the end of class. Copy
enough of each question so that the paper could stand alone as a study
guide.

\noindent
\textbf{Assign (Least-recently Held) Roles:} \emph{Manager}, \emph{Recorder},
\emph{Reflector}, \emph{Speaker}. Everyone should help the whole team
contribute and manage time.

\noindent
\textbf{Answer these questions:}

\begin{questions}
\question{}
  \begin{parts}
  \part{} Assume \lstinline{int sum(int a, int b, int c, int d)} is
    translated to MIPS assembly. The following block of code, in
    \lstinline{main}, calls \lstinline{sum}.

    What are the (decimal) values of the four (4) parameters when control is
    transfered to \lstinline{sum}?

\begin{lstlisting}[frame=single,]
  li   $a0, 101
  li   $a1, 0x19
  sub  $a2, $a0, $a1
  li   $a3, 0x111
  jal  sum

  mov  $a0, $v0
  li   $v0, 1
  syscall
\end{lstlisting}

  \part{} Assuming \lstinline{sum} returns the sum of its parameters,
    what is the output of the above snippet? Be \emph{exact} with the
    formatting.

  \part{} Implement \lstinline{sum} in Java as simply as you can. What
    other functions does your Java function \emph{call}?

  \part{} Given what you know about MIPS calling conventions,
    implement \lstinline{sum} in MIPS, again \emph{as simply as you
      can}.
  \end{parts}

\question{} Playing with the stack:
  \begin{parts}
  \part{} Write the two lines we have used to push \lstinline{$ra}
    on the stack.
  \part{} Compare what happens in memory if the two lines in the push
    above were \emph{reversed}.
  \part{} What does that tell you about where the \lstinline{$sp}
    points (relative to the topmost item on the calling stack) in the
    way we are using it?
  \part{} Write the two lines we use to pop the top of the calling
    stack into \lstinline{$ra}.
  \end{parts}

\newpage
\question{} Consider the following code
  \setlength{\columnseprule}{1pt}
\def\columnseprulecolor{\color{blue}}

  \begin{multicols}{2}
\begin{lstlisting}[numbers=left]
  .data
prefix:
  .asciiz "["
suffix:
  .asciiz "]"
eoln:
  .asciiz "\n"

  .text
  .globl main
main:
  li    $t0, 0x10

  # while $t0 >= 0
test:
  bltz  $t0, afterWhile

  # println [ i ]
  li    $v0, 4
  la    $a0, prefix
  syscall

  li    $v0, 1
  move  $a0, $t0
  syscall

  li    $v0, 4
  la    $a0, suffix
  syscall

  li    $v0, 4
  la    $a0, eoln
  syscall

decrement:
  subi  $t0, $t0, 1
  j     test

afterWhile:
  li    $v0, 10
  syscall
\end{lstlisting}
  \end{multicols}

  \begin{parts}
  \part{} What is the meaning of the \lstinline{j} instruction in line
    37? How is it different from the \lstinline{bltz} in line 16?
  \part{} What is the \emph{output} of the code?
  \part{} What is \lstinline{$t0} being used for here? What are the
    largest and smallest values it will hold while this is running?
  \part{} Dr. Ladd split \lstinline{suffix} and \lstinline{eoln} into
    two strings. Give pros and cons to this decision.
  \end{parts}

\question{} Consider the operation in the previous question. Imagine
  rewriting it as a \emph{recursive} function,
  \lstinline{printEm}. With the same \lstinline{.data} segment,
  \lstinline{main} is rewritten to the following:

\begin{lstlisting}[firstnumber=9,numbers=left]
  .text
  .globl main
main:
  li    $a0, 0x10
  jal   printEm

  li    $v0, 10
  syscall
\end{lstlisting}

  \begin{parts}
  \part{} Write the Java function signature of the \lstinline{printEm}
    function. How do you know the number/order of parameters.
  \part{} Implement \lstinline{printEm}, in Java, so the rewritten
    \lstinline{main} produces the same output as question 2.
  \part{} Translate the recursive \lstinline{printEm} into MIPS. You
    probably want to start with the prologue/epilogue, then the base
    case, then setting up the recursive call. Feel free to keep the
    code as simple as possible (but no simpler).
  \end{parts}

\end{questions}
\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
