\documentclass[11pt]{article}
\usepackage{geometry} % See geometry.pdf to learn the layout options. There are lots.
\geometry{letterpaper}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{epstopdf}
\DeclareGraphicsRule{.tif}{png}{.png}{`convert #1 `dirname #1`/`basename #1 .tif`.png}

\usepackage{url}

\title{CMSI 284 Simple Machine Exercise\\
or, the Da Vinci Opcode}
\author{Submitted by: \underline{\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad}}
\date{}

\begin{document}
\maketitle


\section*{Instructions}

Work out and answer the following questions based on the simple machine described in
\url{http://cs.lmu.edu/~ray/notes/simplecomputer/}. You may submit this assignment in one of
these ways:

\begin{itemize}
\item If you know \LaTeX\ sufficiently, copy the \emph{source file} of this exercise and add your
solutions to this copy. Commit and push the file to your GitHub repository. \emph{Advantage:}
Drop-dead clear, sharp, unambiguous presentation. \emph{Disadvantage:} Intermediate work such as
the state of the machine at a given moment may be harder to write down.

\item Alternatively, you may \emph{print} the PDF version of this exercise and do your work on
paper. Submit this printout with your \emph{name} in the designated blank up top. \emph{Advantage:}
More convenient for showing your work. \emph{Disadvantage:} Handwritten answers may be harder to
read.

If answering on paper, use separate blank sheets for your answers/code if necessary. Don't try to
cram everything solely within the available space.
\end{itemize}


\section*{Mapping to Outcomes and Proficiencies}

The overall assignment covers outcomes \emph{1c}, \emph{4d}, and \emph{4f}. Each question specifically
pertains to \emph{1c} and will be given a score ranging from 0 to 4 based on the correctness of the
answer. The average score for a given outcome, rounded, determines the final proficiency for the
assignment.

Outcome \emph{4d} will be determined by how well you use the information given in class to compute
the requested answers, and how accurately you follow the instructions in this assignment.

Outcome \emph{4f} will be determined by whether you submit the assignment on time.

\pagebreak

\section{Disassembly}

Choose two (2) out of the following five machine language programs then: (a) disassemble them
into an equivalent assembly language program and (b) state the computation that they perform.
Some ground rules and tips:
\begin{itemize}
\item There might be ``garbage'' (i.e., unused) words in the memory dump, so just follow the
instructions and trust in what they make the machine do.

\item When disassembling the programs, choose meaningful labels, just as you would when writing
programs in higher-level languages.

\item If you recognize that the operations being performed result in something that is overall more
meaningful (e.g., ``the program squares the given number then multiplies it by \(\pi\)'') then describe
the program in those more meaningful terms rather than just spelling out the operations (e.g., ``the
program computes the area of a circle with the given radius'').

\item To help remember how you are interpreting the code, you may annotate your code with
a comment (e.g., ``\texttt{; current value of counter}'').

\item Many of these programs read or write values to and from ports. Make sure to recognize which
ports are being used for what.
\end{itemize}

\begin{enumerate}
\item \begin{verbatim}
0000000: C0000004
0000001: 0000000A
0000002: 00000001
0000003: 00000001
0000004: 00000002
0000005: 60000001
0000006: 10000002
0000007: 00000001
0000008: 50000003
0000009: D000000C
000000A: 10000001
000000B: C0000004
000000C: 00000002
000000D: 30000FAC
000000E: C000000E
000000F: 000000F1
0000010: 10000002
0000011: 30000004
0000012: 8000009A
0000013: 10000000
0000014: 20000AEF
0000015: FFFFFFFF
0000016: 00000000
\end{verbatim}

\item (\emph{Hint:} Keep those bitwise operations in mind.)

\begin{verbatim}
0000000: 20000016
0000001: 10000008
0000002: 50000009
0000003: D0000003
0000004: 00000008
0000005: B0000009
0000006: 30000050
0000007: C0000000
0000008: 00000000
0000009: FFFFFFFF
000000A: 20000111
000000B: 00000003
000000C: 10000022
000000D: C0000005
000000E: 40000001
000000F: 30000001
0000010: 40000004
0000011: A000001D
0000012: 90000002
0000013: B000001E
0000014: 00000001
0000015: F0000002
0000016: 40000EBD
\end{verbatim}

\item (\emph{Hint:} You have probably written this program before.)

\begin{verbatim}
0000000: 200EA000
0000001: 10000014
0000002: 80000011
0000003: D0000005
0000004: C000000C
0000005: 00000014
0000006: 80000012
0000007: D0000009
0000008: C000000E
0000009: 00000014
000000A: 80000013
000000B: D000000E
000000C: 00000015
000000D: C000000F
000000E: 00000016
000000F: 300FF000
0000010: C0000010
0000011: 00000004
0000012: 00000064
0000013: 00000190
0000014: 00000000
0000015: 00000000
0000016: FFFFFFFF
\end{verbatim}

\item (\emph{Hint:} What is happening here relates to a well-known theorem.)

\begin{verbatim}
0000000: C0000003
0000001: 00FEA007
0000002: 9001A2B3
0000003: 20F80000
0000004: 10000001
0000005: 60000001
0000006: 10000001
0000007: 20F80000
0000008: 10000002
0000009: 60000002
000000A: 40000001
000000B: 30F90000
000000C: C000000C
000000D: 10000014
000000E: 80000011
000000F: D0000005
0000010: C000000C
0000011: 00000014
0000012: 80000012
0000013: D0000009
0000014: C000000E
0000015: 00000014
0000016: 80000013
\end{verbatim}

\item (\emph{Hint:} Remember the first 128 Unicode codepoints when reading through this one.)

\begin{verbatim}
0000000: C0000005
0000001: 00000061
0000002: 0000007A
0000003: 00000020
0000004: FA001F1F
0000005: 20000032
0000006: D0000006
0000007: 10000004
0000008: 50000001
0000009: E0000010
000000A: 00000004
000000B: 50000002
000000C: F0000010
000000D: 00000004
000000E: 50000003
000000F: 10000004
0000010: 00000004
0000011: 30000064
0000012: C0000005
0000013: 10000008
0000014: 50000009
0000015: D0000003
0000016: 00000008
\end{verbatim}
\end{enumerate}


\section{Assembly}

Choose three (3) out of the following five program descriptions then: (a) implement them in assembly
language and (b) assemble your code into machine language. Start your assembled programs at memory
location \texttt{0000000}. When reading/writing to/from ports is requested, you may choose the port
numbers to use, but you should explicitly state which port is being used for what.

In all cases, assume that the programs work in infinite loops: after accepting the input and processing
the output, they all start over and accept new input from the designated port.

\begin{enumerate}
\item \emph{Two digit decimal-to-hex converter:}
A program that reads two words from a given port that are meant to represent individual decimal
digits. If the words are not in the range \texttt{0} to \texttt{9}, then the error result \texttt{FFFFFFFF}
is sent to an output port. Otherwise, the integer represented by the two decimal digits is sent.

e.g., If the input port receives \texttt{00000003} then \texttt{00000008}, the output port gets
\texttt{00000026} because 38 decimal is \texttt{26} hex. If the input port receives \texttt{00000001}
then \texttt{0000000E}, the output port gets \texttt{FFFFFFFF} because \texttt{0000000E} is not a
decimal digit.

\item \emph{Overflow possibility checker:}
A program that reads two words from a given port then checks whether adding them together \emph{might}
cause overflow. If overflow is possible, an output port receives \texttt{1}. If overflow cannot happen,
the output port receives \texttt{0}.

e.g., If the input port receives \texttt{FFFFAD05} then \texttt{6A004301}, the output port gets
\texttt{0} because adding these numbers will not result in overflow. If the input port receives
\texttt{0000001B} then \texttt{00000002}, the output port gets \texttt{1} because adding these numbers
\emph{might} result in overflow.

\item \emph{Simulated bank account:}
A program that reads two words then updates an in-memory word representing a current bank account
balance. The first word may either be UTF-32 \texttt{D} or \texttt{W}. The second word is some integer
amount. \texttt{D} stands for ``deposit,'' and adds the given amount to the balance in memory.
\texttt{W} stands for ``withdrawal,'' and subtracts the given amount from the balance in memory.
Any character other than \texttt{D} or \texttt{W} results in no action; both input words are ignored.
You may assume that the amount is always a positive number.

e.g., If the in-memory balance is \$100 decimal and the input port receives \texttt{00000044} then
\texttt{0000000A}, the balance becomes \$110 decimal because the two words requested a deposit of \$10.
If the input port next receives \texttt{00000057} then \texttt{0000003C}, the balance becomes \$50
decimal because the two words requested a withdrawal of \$60. If the two words are \texttt{00000041}
then \texttt{00000100}, nothing happens because \texttt{00000041} is neither Unicode \texttt{D} nor
\texttt{W}.

\item \emph{Two-number sorter:}
A program that reads two words then echoes them back through an output port in ascending order.
e.g., If the input port receives \texttt{00AA0123} then \texttt{01FF9231}, the output port gets
\texttt{00AA0123} then \texttt{01FF9231}. If the input port receives \texttt{008012AB} then
\texttt{000039EF}, the output port gets \texttt{000039EF} then \texttt{008012AB}.

\item \emph{Single-digit expression adder/subtractor:}
A program that reads a single word meant to be a UTF-8 sum or difference of single digits, then
evaluates that expression. The expression may be assumed to be valid.

e.g., If the input port receives \texttt{00342B37}---i.e., \texttt{4+7}---the output port gets
\texttt{0000000B} because that is the sum of those two numbers. If the input port receives
\texttt{00312D39}---i.e., \texttt{1-9}---the output port gets \texttt{FFFFFFF8} because that is
the difference of those two numbers.
\end{enumerate}

\section{Reflection}

Which task do you think is harder---\emph{following} instructions to determine what they compute (the first
section), or \emph{writing} instructions to perform a specified computation (the second section)? Why do
you think so?

\end{document}
