\documentclass[answers,addpoints,10pt,x11names]{exam}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[most]{tcolorbox}
\usepackage{colortbl}
\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}
\usepackage{xcolor}

% 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,
}

\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{gMultipy: Slow and Fast}
\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}

\section*{Introduction}
MIPS has a \lstinline{mul} instruction. What if you are not permitted
to use it? Writing some multiplication using addition and
bit-twiddling MIPS code.

\section*{Assignment Goals}
\textbf{Learning Outcomes} After completing this group assignment,
each student is expected to be able to
\begin{itemize}
\item Multiply 2's-compliment integers with a naive count-controlled
  loop and three (3) registers.
\item Count the number of bits in a word that are set.
\item Understand doubling and adding can be used to multiply
  \emph{faster}, using the same three (3) registers.
\end{itemize}


\section*{Procedure}

\textbf{Get out paper.} The group will turn in \emph{one}
document. Make sure all participating members' names are on the
page.

\noindent
\textbf{Copy each question before the answer.} Since the point is to
make your answer sheet a stand-alone study guide, the copy need not be
verbatim but must give your answer enough context to evaluate.

\noindent
\textbf{Assign Roles.} Students should take roles they
have not held recently (or, perhaps, ever):

\begin{description}
\item[Manager] Move discussion forward.
\item[Recorder] Writes the report that will be turned in.
\item[Reflector] Monitor that everyone gets heard and is caught
  up. (This is a \textbf{group} obligation, really.)
\item[Speaker] (Combine w/ \textbf{Reflector} if necessary.) Asks
  facilitator questions; communicates what the team has done.
\end{description}

\noindent
\textbf{Answer these questions.}

\begin{questions}
\question{} Write (in MIPS assembly) the \lstinline{multiply}
  function. The function takes two parameters, \lstinline{x} and
  \lstinline{y}, and returns their product in \lstinline{$v0}. You may
  safely assume the product fits in one word.

\question{} Write a snippet of MIPS code printing ``even'' if the
  value in \lstinline{$t7} is even and ``odd'' if the value in the
  register is odd. You should use \lstinline{bnez} (or
  \lstinline{beqz}) for the if statement.

\question{} Write a stippet of MIPS code that does not use
  \lstinline{div} or \lstinline{mod} that puts half the value in
  \lstinline{$t0} in register \lstinline{$t1}. Assume the value is
  non-negative and round \textbf{down}.

\question{} When the value in \lstinline{$t1} in the previous question
  is \emph{odd}, what do you know about the bit pattern in
  \lstinline{$t0}?

\question{} Can you \textbf{double} the value in register
  \lstinline{$t5} \emph{without} adding or subtracting? (Or multiplying.)

\question{} Write a loop in MIPS code that counts the number of 1 bits
  in the value in register \lstinline{$t0}.

\question{} Can you use your loop from above to rewrite the body of
  \lstinline{multiply}, using doubling, halving, and adding if
  necessary to get the answer in no more than 32 times through the loop?

\end{questions}
\end{document}

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