\documentclass[a4paper]{article}
\usepackage{listings}
\usepackage{fontspec}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\usepackage{multicol}

\renewcommand{\thesection}{Task \arabic{section}}
\renewcommand{\thesubsection}{\arabic{subsection})}
\renewcommand{\thesubsubsection}{\alph{subsubsection})}

\setmonofont[Scale=0.9]{Antikor Mono Medium}

\input{fasto.sty}

\setlength{\parskip}{5pt}
\setlength{\parindent}{0pt}

\title{W3 - IPS}
\author{Nikolaj Gade (qhp695)}
\date{May 2022}

\newcommand{\partthing}[3]{%
{\center \rule{\textwidth}{2px}\\\vspace{5px}}

{\large \underline{#1}}

\texttt{#2}

\underline{#3}

\vspace{10px}
}

\begin{document}
\maketitle
\section{}
\subsection{Intermediate code}
See appendix A for the full generation.

\begin{lstlisting}
LABEL label1
t4 := v1
t5 := 1
IF t4 == t5 THEN label3 ELSE label4
LABEL label4
t0 := 1
place2 := v0
place3 := v1
t1 := place2 / place3
IF t0 < t1 THEN label2 ELSE label3
LABEL label2
t0 := v1
t3 := v0
IF t2 < t3 THEN label6 ELSE label7
LABEL label5
place4 := v0
place5 := v1
place0 := place4 - place5
v0 := place0
GOTO label7
LABEL label6
place6 := v1
place7 := v0
place1 := place6 - place7
v1 := place1
GOTO label5
LABEL label7
GOTO label1
LABEL label3
\end{lstlisting}

\newpage
\subsection{MIPS code}

\begin{lstlisting}
label1:
  mov t4, v1
  addi t5, R0, 1
  beq t4, t5, label3
label4:
  addi t0, R0, 1
  mov place2, v0
  mov place3, v1
  div place2, place3
  mflo t1
  addi t1, t1, 1
  slt rd, t0, t1
  beq rd, R0, label3
label2:
  mov t0, v1
  mov t3, v0
  slt rd, t2, t3
  bne rd, R0, label6
  j label7
label5:
  mov place4, v0
  mov place5, v1
  sub place0, place4, place5
  mov v0, place0
  j label7
label6:
  mov place6, v1
  mov place7, v0
  sub place1, place6, place7
  mov v1, place1
  j label5
label7:
  j label1
label3:
\end{lstlisting}

\section{}

\subsection{\texttt{z := x >= y}}

\begin{lstlisting}
sub v, y, x
slti z, v, 1
\end{lstlisting}

\subsection{\texttt{w := !z}}

\begin{lstlisting}
slt w, R0, z
\end{lstlisting}

\subsection{\texttt{z := x >= y, w := !z}}

\begin{lstlisting}
slt w, x, y
\end{lstlisting}

\newpage
\section{}
\subsubsection{}
\begin{lstlisting}
int* y = malloc(n);
int   i = 0;
while (i < n) {
  ne = myop(ne, x[i]);
  y[i] = ne;
  i++;
}
\end{lstlisting}

\subsubsection{}

\begin{lstlisting}
lw    len,  0(x)                // Loads the length of x from the first word in the list

// dynalloc from group project
move  regy, HP                  // Sets y to the start of the non-allocated memory
addi  tmp,  len,  3             // Adds 3 and rounds down, to line up with the 4-byte boundary
sra   tmp,  tmp,  2
sll   tmp,  tmp,  2
addi  tmp,  tmp,  4             // Adds space for the length of the array
add   HP,   HP,   tmp           // Moves HP
sw    len,  0(regy)             // Saves the length of the array in the first word

addi  i,    R0,   0             // int i = 0
loop_beg:
  bge   i,    len,  loop_end    // while (i < n) {..
  add   xi,   x,    i           // x[i]
  add   yi,   regy, i           // y[i]
  MIPS.CALL(myop, (ne, xi), ne) // ne = myop(ne, x[i]);
  sw    ne,   0(yi)             // y[i] = ne;
  addi  i,    i,    1           // i++;
  j loop_beg
loop_end:

\end{lstlisting}


\newpage
\appendix
\section{Intermediate code generation}
\input{appendix_a.tex}

\end{document}