coursework/cw03.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Tue, 26 Nov 2013 19:54:16 +0000
changeset 205 0b59588d28d2
parent 204 fec99c437965
child 210 33175abd5474
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}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
\usepackage{charter}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
\usepackage{hyperref}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
\usepackage{amssymb}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
\usepackage{amsmath}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
\usepackage{listings}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
\usepackage{xcolor}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
\begin{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
\definecolor{javared}{rgb}{0.6,0,0} % for strings
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
\definecolor{javagreen}{rgb}{0.25,0.5,0.35} % comments
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
\definecolor{javapurple}{rgb}{0.5,0,0.35} % keywords
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
\definecolor{javadocblue}{rgb}{0.25,0.35,0.75} % javadoc
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
  
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
\lstdefinelanguage{scala}{
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
  morekeywords={abstract,case,catch,class,def,%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
    do,else,extends,false,final,finally,%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
    for,if,implicit,import,match,mixin,%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
    new,null,object,override,package,%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
    private,protected,requires,return,sealed,%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
    super,this,throw,trait,true,try,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
    type,val,var,while,with,yield},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
  otherkeywords={=>,<-,<\%,<:,>:,\#,@},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  sensitive=true,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
  morecomment=[l]{//},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
  morecomment=[n]{/*}{*/},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
  morestring=[b]",
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  morestring=[b]',
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  morestring=[b]"""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
\lstdefinelanguage{while}{
203
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
    36
  morekeywords={while, if, then. else, read, write, for, upto, do},
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  otherkeywords={=>,<-,<\%,<:,>:,\#,@},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  sensitive=true,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  morecomment=[l]{//},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  morecomment=[n]{/*}{*/},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  morestring=[b]",
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  morestring=[b]',
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
  morestring=[b]"""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
\lstset{language=Scala,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
	basicstyle=\ttfamily,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
	keywordstyle=\color{javapurple}\bfseries,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
	stringstyle=\color{javagreen},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
	commentstyle=\color{javagreen},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
	morecomment=[s][\color{javadocblue}]{/**}{*/},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
	numbers=left,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
	numberstyle=\tiny\color{black},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
	stepnumber=1,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
	numbersep=10pt,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
	tabsize=2,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
	showspaces=false,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
	showstringspaces=false}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
\section*{Coursework 3}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
This coursework is worth 4\% and is due on 13 December at 16:00. You are asked to 
204
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
    67
implement a compiler for the WHILE language that targets the
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    68
assembler language provided by the Jasmin. This assembler 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    69
is available from
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    70
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    71
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    72
\url{http://jasmin.sourceforge.net}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    73
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    74
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    75
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    76
There is a user guide for Jasmin
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    77
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    78
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    79
\url{http://jasmin.sourceforge.net/guide.html}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    80
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    81
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    82
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    83
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
    84
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    85
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    86
\url{http://jasmin.sourceforge.net/instructions.html}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    87
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    88
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    89
\noindent
205
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    90
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
    91
\texttt{loops.j}, you can use
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
205
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    93
\begin{center}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    94
\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
    95
\end{center}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    96
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    97
\noindent
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    98
in order to translate it to Java Byte Code. The resulting class file can be
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    99
run with
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   100
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   101
\begin{center}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   102
\texttt{java loops}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   103
\end{center}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   104
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   105
\noindent
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   106
where you potentially need to give the path to the class file.\bigskip
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
\noindent
204
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   109
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
   110
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
   111
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
   112
the submission will not be counted.  However, the coursework 
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
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
   114
in a txt-file or as pdf.\bigskip
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   117
\subsection*{Question 1 (marked with 2\%)}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
   119
You need to lex and parse WHILE programs and submit the assembler 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
   120
instructions for the Fibonacci program and for the program you submitted
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
   121
in Coursework 2 in Question 3. The latter should be so modified that 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
   122
a user can input the upper bound on the console (in the original question
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
   123
it was fixed to 100).
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
   125
\subsection*{Question 2 (marked with 2\%)}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
   126
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
   127
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
   128
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
   129
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
   130
\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
   131
\end{center}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   133
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   134
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
   135
expression, then go through the loop, at the end increase the value of the variable by 1, 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   136
and finally test wether the value is not less or equal anymore to the value of the second
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   137
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
   138
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
   139
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   140
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   141
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   142
\begin{minipage}{6cm}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   143
\begin{lstlisting}[language=While,basicstyle=\ttfamily, numbers=none]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   144
for i := 2 upto 4 do {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   145
    write i	
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   146
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   147
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   148
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   149
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   150
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   151
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   152
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
   153
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
   154
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
   155
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
   156
to the following \texttt{while}-loop:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   157
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   158
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   159
\begin{minipage}{6cm}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   160
\begin{lstlisting}[language=While,basicstyle=\ttfamily, numbers=none]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   161
i := 2;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   162
while (i <= 4) do {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   163
    write i;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   164
    i := i + 1;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   165
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   166
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   167
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   168
\end{center}
202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   169
205
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   170
\noindent
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   171
In this question you are supposed to give the assembler instructions for the
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   172
for the program
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   173
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   174
\begin{center}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   175
\begin{minipage}{6cm}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   176
\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
   177
for i := 1 upto 10000 do {
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   178
  for i := 1 upto 10000 do {
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   179
  skip
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   180
  }
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   181
} 
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   182
\end{lstlisting}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   183
\end{minipage}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   184
\end{center}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   185
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   186
202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   187
204
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   188
\subsection*{Further Information}
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
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
   191
(therefore
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   192
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
   193
disassembler, called \texttt{javap}. A dissembler does the ``opposite'' of an assembler: it
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   194
generates readable assembler code from Java Byte Code. Have a look at the
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   195
following example. Compile using the usual Java compiler, 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   196
\texttt{java}, the simple Hello World program below:
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
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   199
\begin{minipage}{10cm}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   200
\begin{lstlisting}[language=Java,basicstyle=\ttfamily]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   201
class HelloWorld {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   202
    public static void main(String[] args) {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   203
        System.out.println("Hello World!");
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   204
    }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   205
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   206
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   207
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   208
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   209
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   210
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   211
You can use the command
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   212
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   213
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   214
\texttt{javap -v HelloWorld}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   215
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   216
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   217
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   218
in order to see which Java Byte Code has been generated for this
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   219
program. You can compare this with the code generated for the Scala
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   220
version of Hello Worlds.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   221
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   222
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   223
\begin{minipage}{10cm}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   224
\begin{lstlisting}[language=Scala,basicstyle=\ttfamily]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   225
object HelloWorld {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   226
   def main(args: Array[String]) {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   227
      println("Hello World!")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   228
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   229
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   230
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   231
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   232
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   233
202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   234
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   235
\end{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   236
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   237
%%% Local Variables: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   238
%%% mode: latex
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   239
%%% TeX-master: t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   240
%%% End: