
diff -r 47acfd7f9096 -r ae9782e62bdd hws/hw08.tex
--- a/hws/hw08.tex	Fri Nov 17 20:06:43 2023 +0000
+++ b/hws/hw08.tex	Tue Nov 28 11:42:31 2023 +0000
@@ -11,20 +11,63 @@
 
 \begin{enumerate}
 \item Write a program in the WHILE-language that calculates
-      the factorial function.
+  the factorial function.
+
+\begin{lstlisting}
+write "factorial: ";
+read n;
+minus1 := 1;
+while n > 0 do {
+minus1 := minus1 * n;
+n := n - 1
+};
+write "Result: ";
+write minus1 ;
+write "\n"
+\end{lstlisting}
 
 \item What optimisations could a compiler perform when
-      compiling a WHILE-program?
+  compiling a WHILE-program?
+
+  \solution{
+    \begin{itemize}
+    \item peephole optimisations (more specific instructions)
+    \item common sub-expression elimination %, for example
+
+%\begin{lstlisting}
+%x := 3 + a
+%y := 3 + a
+%\end{lstlisting}
+
+    %can be optimised to
+
+    %\begin{lstlisting}[numbers=none,language={}]
+    %  z := 3 + a
+    %  x := z
+    %  y := z
+    %\end{lstlisting}
+
+    \item constant folding / constant propagation (that is calculate the result of 3 + 4 already during
+      compilation)
+    \item tail-recursion optimisation cannot be applied to the WHILE language
+      because there are no recursive functions
+    \end{itemize}
+  }
 
 \item What is the main difference between the Java assembler
       (as processed by Jasmin) and Java Byte Code?
 
+      \solution{ The main difference is that the j-files have symbols
+        for places where to jump, while class files have this resolved
+        to concrete addresses (or relative jumps). That is what the
+        assembler has to generate.  }
+      
 \item Remember symbolic labels in the Jasmin-assembler are meant to
       be used for jumps (like in loops or if-conditions). Assume
       you generated a Jasmin-file with some redundant
       labels, that is some labels are not used in your code for any jumps. For
       example \texttt{L\_begin} and \texttt{L\_end} are not used
-      in the fol,lowing code-snippet:
+      in the following code-snippet:
 
 \begin{lstlisting}[language=JVMIS,numbers=none]
 L_begin:        
@@ -42,7 +85,7 @@
       
       Do these redundant labels affect the size of the generated
       JVM-code? (Hint: What are the labels translated to by
-      the Jasmin-assembler?).
+      the Jasmin-assembler?).      
 
       \solution{The answer is no. The reason is that assemblers
         calculate for labels either relative or explicit adresses,
@@ -67,10 +110,21 @@
       
 Do they cause stack-overflows when compiled to the JVM (for example by Scala)?
 
+\solution{
+Scala cannot generate jumps in between different methods (to which functions are compiled to). So cannot eliminate the tail-calls. Haskell for example can do this because it compiles the code in a big ``blob'' inside a main-method (similar to the WHILE language).
+}  
+
 
 \item Explain what is meant by the terms lazy evaluation and eager
   evaluation.
 
+  \solution{ Lazy evaluation only evaluates expressions when they are
+    needed and if they are needed twice, the results will be
+    re-used. Eager evaluation immediately evaluates expressions, for
+    example if they are arguments to function calls or allocated to
+    variables.}
+    
+
 \item \POSTSCRIPT    
 \end{enumerate}
 




