hws/hw08.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 13 Oct 2023 15:07:37 +0100
changeset 941 66adcae6c762
parent 916 10f834eb0a9e
child 956 ae9782e62bdd
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
\documentclass{article}
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
     2
\usepackage{../style}
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
     3
\usepackage{../langs}
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
     4
\usepackage{../graphics}
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
\begin{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
75
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 60
diff changeset
     8
\section*{Homework 8}
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
916
10f834eb0a9e texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
    10
%%\HEADER
359
db106e5b7c4d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    11
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
\begin{enumerate}
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
    13
\item Write a program in the WHILE-language that calculates
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
    14
      the factorial function.
77
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
    15
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
    16
\item What optimisations could a compiler perform when
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
    17
      compiling a WHILE-program?
206
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    18
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
    19
\item What is the main difference between the Java assembler
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 206
diff changeset
    20
      (as processed by Jasmin) and Java Byte Code?
206
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    21
941
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    22
\item Remember symbolic labels in the Jasmin-assembler are meant to
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    23
      be used for jumps (like in loops or if-conditions). Assume
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    24
      you generated a Jasmin-file with some redundant
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    25
      labels, that is some labels are not used in your code for any jumps. For
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    26
      example \texttt{L\_begin} and \texttt{L\_end} are not used
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    27
      in the fol,lowing code-snippet:
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    28
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    29
\begin{lstlisting}[language=JVMIS,numbers=none]
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    30
L_begin:        
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    31
ldc 1
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    32
ldc 2
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    33
ldc 3
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    34
imul
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    35
ldc 4
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    36
ldc 3
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    37
isub
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    38
iadd
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    39
iadd
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    40
L_end:
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    41
\end{lstlisting}
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    42
      
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    43
      Do these redundant labels affect the size of the generated
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    44
      JVM-code? (Hint: What are the labels translated to by
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    45
      the Jasmin-assembler?).
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    46
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    47
      \solution{The answer is no. The reason is that assemblers
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    48
        calculate for labels either relative or explicit adresses,
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    49
        which are then used in the JVM-byte-code. Relative addresses
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    50
        are like ``jump 10 bytes forward'' or ``12 bytes backward''. So
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    51
      additional labels do not increase the size of the generated code.}
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    52
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    53
      
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    54
\item Consider the following Scala snippet. Are the two functions
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    55
\texttt{is\_even} and \texttt{is\_odd} tail-recursive?     
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    56
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    57
\begin{lstlisting}[numbers=none]
901
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 876
diff changeset
    58
def is_even(n: Int) : Boolean = {
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 876
diff changeset
    59
    if (n == 0) true else is_odd(n - 1)
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    60
}
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    61
901
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 876
diff changeset
    62
def is_odd(n: Int) : Boolean = {
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    63
    if (n == 0) false 
901
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 876
diff changeset
    64
    else if (n == 1) true else is_even(n - 1)
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    65
}
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    66
\end{lstlisting}
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    67
      
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    68
Do they cause stack-overflows when compiled to the JVM (for example by Scala)?
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    69
530
cec95ad3a837 updated
cu
parents: 359
diff changeset
    70
cec95ad3a837 updated
cu
parents: 359
diff changeset
    71
\item Explain what is meant by the terms lazy evaluation and eager
577
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
    72
  evaluation.
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
    73
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
    74
\item \POSTSCRIPT    
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
\end{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
\end{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
%%% Local Variables: 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
%%% mode: latex
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
%%% TeX-master: t
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
%%% End: