coursework/cw04.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 09 Apr 2015 07:42:23 +0100
changeset 322 698ed1c96cd0
parent 321 c5850f8f3f5e
child 328 bc03ff3d347c
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
\documentclass{article}
309
640e4a05cd9b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 298
diff changeset
     2
\usepackage{../style}
216
f5ec7c597c5b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 214
diff changeset
     3
\usepackage{../langs}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
\begin{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
309
640e4a05cd9b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 298
diff changeset
     7
\section*{Coursework 4 (Strand 1)}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
309
640e4a05cd9b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 298
diff changeset
     9
\noindent This coursework is worth 10\% and is due on 12th
640e4a05cd9b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 298
diff changeset
    10
December at 16:00. You are asked to implement a compiler for
640e4a05cd9b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 298
diff changeset
    11
the WHILE language that targets the assembler language
640e4a05cd9b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 298
diff changeset
    12
provided by Jasmin. This assembler is available from
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    13
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    14
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    15
\url{http://jasmin.sourceforge.net}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    16
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    17
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    18
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    19
There is a user guide for Jasmin
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    20
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    21
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    22
\url{http://jasmin.sourceforge.net/guide.html}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    23
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    24
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    25
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    26
and also a description of some of the instructions that the JVM understands
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    27
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    28
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    29
\url{http://jasmin.sourceforge.net/instructions.html}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    30
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    31
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    32
\noindent
205
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    33
If you generated a correct assembler file for Jasmin, for example
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    34
\texttt{loops.j}, you can use
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
205
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    36
\begin{center}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    37
\texttt{java -jar jasmin-2.4/jasmin.jar loops.j}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    38
\end{center}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    39
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    40
\noindent
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
    41
in order to translate it to Java byte code. The resulting class file can be
205
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    42
run with
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    43
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    44
\begin{center}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    45
\texttt{java loops}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    46
\end{center}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    47
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    48
\noindent
322
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 321
diff changeset
    49
where you might need to give the correct path to the class file. 
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 321
diff changeset
    50
For example:
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 321
diff changeset
    51
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 321
diff changeset
    52
\begin{center}
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 321
diff changeset
    53
\texttt{java -cp . loops/loops}
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 321
diff changeset
    54
\end{center}
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 321
diff changeset
    55
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 321
diff changeset
    56
\noindent 
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 321
diff changeset
    57
There are also other resources about Jasmin on the Internet, for example
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
    58
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
    59
\begin{center}
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
    60
\url{http://www.ceng.metu.edu.tr/courses/ceng444/link/f3jasmintutorial.html}
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
    61
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
    62
and
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
    63
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
    64
\url{http://www.csc.villanova.edu/~tway/courses/csc8505/s2011/handouts/JVM%20and%20Jasmin.pdf}
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
    65
\end{center}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
\noindent
204
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
    68
You need to submit a document containing the answers for the two questions 
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
below. You can do the implementation in any programming language you like, but you need 
205
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    70
to submit the source code with which you answered the questions. Otherwise
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    71
the submission will not be counted.  However, the coursework 
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
will \emph{only} be judged according to the answers. You can submit your answers
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
in a txt-file or as pdf.\bigskip
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
    76
\subsection*{Question 1 (marked with 5\%)}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
321
c5850f8f3f5e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 320
diff changeset
    78
You need to lex and parse WHILE programs, and then generate Java Byte
c5850f8f3f5e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 320
diff changeset
    79
Code instructions for the Jasmin assembler. As solution you need to
c5850f8f3f5e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 320
diff changeset
    80
submit the assembler instructions for the Fibonacci and Factorial
c5850f8f3f5e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 320
diff changeset
    81
programs. Both should be so modified that a user can input on the
c5850f8f3f5e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 320
diff changeset
    82
console which Fibonacci number and which Factorial should
c5850f8f3f5e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 320
diff changeset
    83
calculated. The Fibonacci program is given in Figure~\ref{fibs}. You
c5850f8f3f5e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 320
diff changeset
    84
can write your own program for calculating factorials.
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
    86
\begin{figure}[t]
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
    87
\lstinputlisting[language=while]{../progs/fib.while}
320
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 313
diff changeset
    88
\caption{The Fibonacci program in the WHILE language.\label{fibs}}
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
    89
\end{figure}
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
    90
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
    91
\subsection*{Question 2 (marked with 5\%)}
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    92
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    93
Extend the syntax of you language so that it contains also \texttt{for}-loops, like
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    94
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    95
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    96
\texttt{for} \;\textit{Id} \texttt{:=} \textit{AExp}\; \texttt{upto} \;\textit{AExp}\; \texttt{do} \textit{Block} 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    97
\end{center}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
    99
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   100
The intended meaning is to first assign the variable \textit{Id} the value of the first arithmetic 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   101
expression, then go through the loop, at the end increase the value of the variable by 1, 
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   102
and finally test wether the value is not less or equal to the value of the second
202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   103
arithmetic expression. For example the following instance of a \texttt{for}-loop 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   104
is supposed to print out the numbers \texttt{2}, \texttt{3}, \texttt{4}.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   105
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   107
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   108
\begin{minipage}{6cm}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   109
\begin{lstlisting}[language=While,basicstyle=\ttfamily, numbers=none]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   110
for i := 2 upto 4 do {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   111
    write i	
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   112
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   113
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   114
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   115
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   116
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   117
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   118
There are two ways how this can be implemented: one is to adapt the code generation 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   119
part of the compiler and generate specific code for \texttt{for}-loops; the other is to
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   120
translate the abstract syntax tree of \texttt{for}-loops into an abstract syntax tree using
203
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   121
existing language constructs. For example the loop above could be translated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   122
to the following \texttt{while}-loop:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   124
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   125
\begin{minipage}{6cm}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   126
\begin{lstlisting}[language=While,basicstyle=\ttfamily, numbers=none]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   127
i := 2;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   128
while (i <= 4) do {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   129
    write i;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   130
    i := i + 1;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   131
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   132
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   133
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   134
\end{center}
202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   135
205
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   136
\noindent
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   137
In this question you are supposed to give the assembler instructions for the
214
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   138
program
205
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   139
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   140
\begin{center}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   141
\begin{minipage}{6cm}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   142
\begin{lstlisting}[language=While,basicstyle=\ttfamily, numbers=none]
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   143
for i := 1 upto 10000 do {
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   144
  for i := 1 upto 10000 do {
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   145
  skip
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   146
  }
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   147
} 
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   148
\end{lstlisting}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   149
\end{minipage}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   150
\end{center}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   151
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   152
\noindent 
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   153
Note that in this program the variable \pcode{i} is used
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   154
twice. You need to make a decision how it should be compiled?
202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   155
204
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   156
\subsection*{Further Information}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   157
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   158
The Java infrastructure unfortunately does not contain an assembler out-of-the-box
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   159
(therefore
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   160
you need to download the additional package Jasmin---see above). But it does contain a 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   161
disassembler, called \texttt{javap}. A dissembler does the ``opposite'' of an assembler: it
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   162
generates readable assembler code from Java byte code. Have a look at the
214
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   163
following example: Compile using the usual Java compiler the simple Hello World 
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   164
program below:
204
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   165
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   166
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   167
\begin{minipage}{10cm}
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   168
\begin{lstlisting}[language=Java,numbers=none]
204
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   169
class HelloWorld {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   170
    public static void main(String[] args) {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   171
        System.out.println("Hello World!");
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   172
    }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   173
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   174
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   175
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   176
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   177
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   178
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   179
You can use the command
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   180
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   181
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   182
\texttt{javap -v HelloWorld}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   183
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   184
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   185
\noindent to see the assembler instructions of the Java byte
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   186
code that has been generated for this program. You can compare
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   187
this with the code generated for the Scala version of Hello
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   188
World.
204
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   189
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   190
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   191
\begin{minipage}{10cm}
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   192
\begin{lstlisting}[language=Scala,numbers=none]
204
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   193
object HelloWorld {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   194
   def main(args: Array[String]) {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   195
      println("Hello World!")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   196
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   197
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   198
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   199
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   200
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   201
222
b712519b41d3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   202
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   203
\subsection*{Library Functions}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   204
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   205
You need to generate code for the commands \texttt{write} and
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   206
\texttt{read}. This will require the addition of some
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   207
``library'' functions to your generated code. The first
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   208
command even needs two versions, because you need to write out
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   209
an integer and string. The Java byte code will need two
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   210
separate functions for this. For writing out an integer, you
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   211
can use the assembler code
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   212
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   213
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   214
\begin{minipage}{12cm}
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   215
\begin{lstlisting}[language=JVMIS, numbers=none]
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   216
.method public static write(I)V 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   217
    .limit locals 5 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   218
    .limit stack 5 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   219
    iload 0 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   220
    getstatic java/lang/System/out Ljava/io/PrintStream; 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   221
    swap 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   222
    invokevirtual java/io/PrintStream/println(I)V 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   223
    return 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   224
.end method
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   225
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   226
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   227
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   228
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   229
\noindent 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   230
This function will invoke Java's \texttt{println} function for integers. Then if you need
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   231
to generate code for \texttt{write x} where \texttt{x} is an integer variable, you can generate
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   232
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   233
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   234
\begin{minipage}{8cm}
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   235
\begin{lstlisting}[language=JVMIS, numbers=none]
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   236
iload n 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   237
invokestatic XXX/XXX/write(I)V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   238
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   239
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   240
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   241
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   242
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   243
where \texttt{n} is the index where the value of the variable \texttt{x} is
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   244
stored. The \texttt{XXX/XXX} needs to be replaced with the class name 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   245
which you use to generate the code (for example \texttt{fib/fib} in case
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   246
of the Fibonacci numbers).
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   247
214
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   248
Writing out a string is similar. The corresponding library function uses strings 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   249
instead of integers:
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   250
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   251
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   252
\begin{minipage}{12cm}
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   253
\begin{lstlisting}[language=JVMIS, numbers=none]
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   254
.method public static writes(Ljava/lang/String;)V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   255
   .limit stack 2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   256
   .limit locals 2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   257
   getstatic java/lang/System/out Ljava/io/PrintStream;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   258
   aload 0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   259
   invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   260
   return
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   261
.end method
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   262
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   263
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   264
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   265
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   266
\noindent
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   267
The code that needs to be generated for \code{write "some_string"} commands 
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   268
is
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   269
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   270
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   271
\begin{minipage}{8cm}
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   272
\begin{lstlisting}[language=JVMIS,numbers=none]
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   273
ldc "some_string"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   274
invokestatic XXX/XXX/writes(Ljava/lang/String;)V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   275
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   276
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   277
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   278
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   279
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   280
Again you need to adjust the \texttt{XXX/XXX} part in each call.
202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   281
214
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   282
The code for \texttt{read} is more complicated. The reason is that inputting a string
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   283
will need to be transformed into an integer. The code in Figure~\ref{read} does this.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   284
It can be called with
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   285
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   286
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   287
\begin{minipage}{8cm}
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   288
\begin{lstlisting}[language=JVMIS,numbers=none]
214
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   289
invokestatic XXX/XXX/read()I 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   290
istore n
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   291
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   292
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   293
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   294
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   295
\noindent 
321
c5850f8f3f5e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 320
diff changeset
   296
where \texttt{n} is the index of the variable that requires an input. If you
c5850f8f3f5e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 320
diff changeset
   297
use Windows you need to take into account that a ``return'' is not just a newline,
c5850f8f3f5e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 320
diff changeset
   298
\code{'\\10'}, but \code{'\\13\\10'}. This means you need to change line~12 in 
c5850f8f3f5e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 320
diff changeset
   299
Figure~\ref{read} to \pcode{ldc 13}.  
214
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   300
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   301
321
c5850f8f3f5e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 320
diff changeset
   302
\begin{figure}[t]\small
c5850f8f3f5e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 320
diff changeset
   303
\begin{lstlisting}[language=JVMIS]
214
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   304
.method public static read()I 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   305
      .limit locals 10 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   306
      .limit stack 10
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   307
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   308
      ldc 0 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   309
      istore 1  ; this will hold our final integer 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   310
Label1: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   311
      getstatic java/lang/System/in Ljava/io/InputStream; 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   312
      invokevirtual java/io/InputStream/read()I 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   313
      istore 2 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   314
      iload 2 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   315
      ldc 10   ; the newline delimiter 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   316
      isub 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   317
      ifeq Label2 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   318
      iload 2 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   319
      ldc 32   ; the space delimiter 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   320
      isub 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   321
      ifeq Label2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   322
      iload 2 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   323
      ldc 48   ; we have our digit in ASCII, have to subtract it from 48 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   324
      isub 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   325
      ldc 10 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   326
      iload 1 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   327
      imul 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   328
      iadd 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   329
      istore 1 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   330
      goto Label1 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   331
Label2: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   332
      ;when we come here we have our integer computed in Local Variable 1 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   333
      iload 1 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   334
      ireturn 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   335
.end method
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   336
\end{lstlisting}\normalsize
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   337
\caption{Assembler code for reading an integer from the console.\label{read}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   338
\end{figure}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   339
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   340
\end{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   341
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   342
%%% Local Variables: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   343
%%% mode: latex
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   344
%%% TeX-master: t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   345
%%% End: