coursework/cw04.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Mon, 07 Dec 2015 17:50:05 +0000
changeset 391 f352cb238c32
parent 390 a1fe591a3df3
child 419 4110ab35e5d8
permissions -rw-r--r--
updated meaning of for-loop
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
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
     7
%https://github.com/Storyyeller/Krakatau
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
     8
%https://docs.oracle.com/javase/specs/jvms/se7/html/
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
     9
309
640e4a05cd9b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 298
diff changeset
    10
\section*{Coursework 4 (Strand 1)}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
328
bc03ff3d347c updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 322
diff changeset
    12
\noindent This coursework is worth 10\% and is due on 11
309
640e4a05cd9b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 298
diff changeset
    13
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
    14
the WHILE language that targets the assembler language
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    15
provided by Jasmin or Krakatau (both have very similar
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    16
syntax). You can do the implementation in any programming
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    17
language you like, but you need to submit the source code with
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    18
which you answered the questions, otherwise a mark of 0\% will
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    19
be awarded. You should use the lexer and parser from the
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    20
previous courseworks. 
358
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    21
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    22
\subsection*{Disclaimer}
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    23
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    24
It should be understood that the work you submit represents
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    25
your own effort. You have not copied from anyone else. An
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    26
exception is the Scala code I showed during the lectures,
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    27
which you can use. You can also use your own code from the
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    28
CW~1, CW~2 and CW~3.
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    29
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    30
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    31
\subsection*{Jasmin Assembler}
358
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    32
b3129cff41e9 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    33
The Jasmin assembler is available from
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    34
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    35
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    36
\url{http://jasmin.sourceforge.net}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    37
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    38
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    39
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    40
There is a user guide for Jasmin
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    41
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    42
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    43
\url{http://jasmin.sourceforge.net/guide.html}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    44
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    45
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    46
\noindent and also a description of some of the instructions
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    47
that the JVM understands
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    48
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    49
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    50
\url{http://jasmin.sourceforge.net/instructions.html}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    51
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
    52
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    53
\noindent If you generated a correct assembler file for
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    54
Jasmin, for example \texttt{loops.j}, you can use
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
205
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    56
\begin{center}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    57
\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
    58
\end{center}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    59
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    60
\noindent in order to translate it into Java Byte Code. The
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    61
resulting class file can be run with
205
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    62
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    63
\begin{center}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    64
\texttt{java loops}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    65
\end{center}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
    66
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    67
\noindent where you might need to give the correct path to the
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    68
class file. For example:
322
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 321
diff changeset
    69
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 321
diff changeset
    70
\begin{center}
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 321
diff changeset
    71
\texttt{java -cp . loops/loops}
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 321
diff changeset
    72
\end{center}
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 321
diff changeset
    73
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    74
\noindent There are also other resources about Jasmin on the
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    75
Internet, for example
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    76
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    77
\begin{center}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    78
\small\url{http://www.ceng.metu.edu.tr/courses/ceng444/link/f3jasmintutorial.html}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    79
\end{center}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    80
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    81
\noindent and
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    82
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    83
\begin{center}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    84
\small\url{http://www.csc.villanova.edu/~tway/courses/csc8505/s2011/handouts/JVM%20and%20Jasmin.pdf}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    85
\end{center}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    86
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    87
\subsection*{Krakatau Assembler}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    88
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    89
The Krakatau assembler is available from
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    90
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    91
\begin{center}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    92
\url{https://github.com/Storyyeller/Krakatau}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    93
\end{center}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    94
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    95
\noindent This assembler requires Python and a package called
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    96
\pcode{ply} available from
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
    97
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
    98
\begin{center}
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
    99
\url{https://pypi.python.org/pypi/ply}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   100
\end{center}
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   101
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   102
\noindent This assembler is largely compatible with the Jasmin
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   103
syntax---that means for the files we look are concerned with,
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   104
it understands the same input syntax (no changes to your
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   105
compiler need to be made). You can generate Java Byte Code by
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   106
using 
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   107
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   108
\begin{center}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   109
\texttt{python Krakatau-master/assemble.py loops.j}
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   110
\end{center}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   112
\noindent where you may have to adapt the directory where
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   113
Krakatau is installed (I just downloaded the zip file from
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   114
Github and \pcode{Krakatau-master} was the directory where it
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   115
was installed).
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   116
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   117
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   118
%\noindent You need to submit a document containing the answers
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   119
%for the two questions below. You can do the implementation in
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   120
%any programming language you like, but you need to submit the
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   121
%source code with which you answered the questions. Otherwise
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   122
%the submission will not be counted. However, the coursework
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   123
%will \emph{only} be judged according to the answers. You can
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   124
%submit your answers in a txt-file or as pdf.\bigskip
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   127
\subsection*{Question 1 (marked with 5\%)}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   129
You need to lex and parse WHILE programs, and then generate
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   130
Java Byte Code instructions for the Jasmin assembler (or
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   131
Krakatau assembler). As solution you need to submit the
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   132
assembler instructions for the Fibonacci and Factorial
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   133
programs. Both should be so modified that a user can input on
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   134
the console which Fibonacci number and which Factorial should
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   135
be calculated. The Fibonacci program is given in
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   136
Figure~\ref{fibs}. You can write your own program for
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   137
calculating factorials.
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   139
\begin{figure}[t]
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   140
\lstinputlisting[language=while]{../progs/fib.while}
320
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 313
diff changeset
   141
\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
   142
\end{figure}
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   143
333
8890852e18b7 updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 328
diff changeset
   144
\subsection*{Question 2 (marked with 4\%)}
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
   145
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   146
Extend the syntax of your language so that it contains also
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   147
\texttt{for}-loops, like
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
   148
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
   149
\begin{center}
391
f352cb238c32 updated meaning of for-loop
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 390
diff changeset
   150
\lstset{language=While}
f352cb238c32 updated meaning of for-loop
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 390
diff changeset
   151
\code{for} \;\textit{Id} \texttt{:=} \textit{AExp}\; \code{upto} 
f352cb238c32 updated meaning of for-loop
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 390
diff changeset
   152
\;\textit{AExp}\; \code{do} \textit{Block} 
201
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 200
diff changeset
   153
\end{center}
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   155
\noindent The intended meaning is to first assign the variable
391
f352cb238c32 updated meaning of for-loop
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 390
diff changeset
   156
\textit{Id} the value of the first arithmetic expression, test
f352cb238c32 updated meaning of for-loop
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 390
diff changeset
   157
wether this value is less or equal than the value of the
f352cb238c32 updated meaning of for-loop
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 390
diff changeset
   158
second arithmetic expression. If yes, go through the loop, and
f352cb238c32 updated meaning of for-loop
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 390
diff changeset
   159
at the end increase the value of the loop variable by 1 and
f352cb238c32 updated meaning of for-loop
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 390
diff changeset
   160
start again with the test. If no, leave the loop. For example
f352cb238c32 updated meaning of for-loop
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 390
diff changeset
   161
the following instance of a \code{for}-loop is supposed to
f352cb238c32 updated meaning of for-loop
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 390
diff changeset
   162
print out the numbers \pcode{2}, \pcode{3}, \pcode{4}.
202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   163
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   164
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   165
\begin{center}
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   166
\begin{minipage}{12cm}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   167
\begin{lstlisting}[language=While, numbers=none]
202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   168
for i := 2 upto 4 do {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   169
    write i	
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   170
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   171
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   172
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   173
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   174
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   175
\noindent There are two ways how this can be implemented: one
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   176
is to adapt the code generation part of the compiler and
391
f352cb238c32 updated meaning of for-loop
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 390
diff changeset
   177
generate specific code for \code{for}-loops; the other is to
f352cb238c32 updated meaning of for-loop
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 390
diff changeset
   178
translate the abstract syntax tree of \code{for}-loops into
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   179
an abstract syntax tree using existing language constructs.
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   180
For example the loop above could be translated to the
391
f352cb238c32 updated meaning of for-loop
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 390
diff changeset
   181
following \code{while}-loop:
203
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   182
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   183
\begin{center}
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   184
\begin{minipage}{12cm}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   185
\begin{lstlisting}[language=While, numbers=none]
203
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   186
i := 2;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   187
while (i <= 4) do {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   188
    write i;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   189
    i := i + 1;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   190
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   191
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   192
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 202
diff changeset
   193
\end{center}
202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   194
333
8890852e18b7 updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 328
diff changeset
   195
\subsection*{Question 3 (marked with 1\%)}
8890852e18b7 updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 328
diff changeset
   196
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   197
\noindent In this question you are supposed to give the
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   198
assembler instructions for the program
205
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   199
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   200
\begin{center}
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   201
\begin{minipage}{12cm}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   202
\begin{lstlisting}[language=While, numbers=none]
333
8890852e18b7 updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 328
diff changeset
   203
for i := 1 upto 10 do {
8890852e18b7 updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 328
diff changeset
   204
  for i := 1 upto 10 do {
8890852e18b7 updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 328
diff changeset
   205
    write i
205
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   206
  }
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   207
} 
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   208
\end{lstlisting}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   209
\end{minipage}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   210
\end{center}
0b59588d28d2 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 204
diff changeset
   211
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   212
\noindent 
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   213
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
   214
twice. You need to make a decision how it should be compiled?
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   215
Explain your decision and indicate what this program would
333
8890852e18b7 updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 328
diff changeset
   216
print out.
202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   217
204
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   218
\subsection*{Further Information}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   219
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   220
The Java infrastructure unfortunately does not contain an
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   221
assembler out-of-the-box (therefore you need to download the
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   222
additional package Jasmin or Krakatau---see above). But it
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   223
does contain a disassembler, called \texttt{javap}. A
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   224
dissembler does the ``opposite'' of an assembler: it generates
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   225
readable assembler code from Java Byte Code. Have a look at
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   226
the following example: Compile using the usual Java compiler
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   227
the simple Hello World program below:
204
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
\begin{center}
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   230
\begin{minipage}{12cm}
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   231
\begin{lstlisting}[language=Java,numbers=none]
204
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   232
class HelloWorld {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   233
    public static void main(String[] args) {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   234
        System.out.println("Hello World!");
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   235
    }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   236
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   237
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   238
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   239
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   240
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   241
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   242
You can use the command
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   243
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   244
\begin{center}
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   245
\begin{minipage}{12cm}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   246
\begin{lstlisting}[language={},numbers=none]
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   247
javap -v HelloWorld
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   248
\end{lstlisting}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   249
\end{minipage}
204
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   250
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   251
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   252
\noindent to see the assembler instructions of the Java Byte
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   253
Code that has been generated for this program. You can compare
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   254
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
   255
World.
204
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   256
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   257
\begin{center}
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   258
\begin{minipage}{12cm}
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   259
\begin{lstlisting}[language=Scala,numbers=none]
204
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   260
object HelloWorld {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   261
   def main(args: Array[String]) {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   262
      println("Hello World!")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   263
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   264
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   265
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   266
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   267
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 203
diff changeset
   268
222
b712519b41d3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 216
diff changeset
   269
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   270
\subsection*{Library Functions}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   271
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   272
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
   273
\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
   274
``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
   275
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
   276
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
   277
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
   278
can use the assembler code
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   279
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   280
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   281
\begin{minipage}{12cm}
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   282
\begin{lstlisting}[language=JVMIS, numbers=none]
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   283
.method public static write(I)V 
373
b018234c9126 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 358
diff changeset
   284
    .limit locals 1 
b018234c9126 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 358
diff changeset
   285
    .limit stack 2  
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   286
    getstatic java/lang/System/out Ljava/io/PrintStream; 
373
b018234c9126 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 358
diff changeset
   287
    iload 0
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   288
    invokevirtual java/io/PrintStream/println(I)V 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   289
    return 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   290
.end method
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   291
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   292
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   293
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   294
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   295
\noindent This function will invoke Java's \texttt{println}
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   296
function for integers. Then if you need to generate code for
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   297
\texttt{write x} where \texttt{x} is an integer variable, you
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   298
can generate
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   299
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   300
\begin{center}
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   301
\begin{minipage}{12cm}
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   302
\begin{lstlisting}[language=JVMIS, numbers=none]
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   303
iload n 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   304
invokestatic XXX/XXX/write(I)V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   305
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   306
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   307
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   308
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   309
\noindent where \texttt{n} is the index where the value of the
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   310
variable \texttt{x} is stored. The \texttt{XXX/XXX} needs to
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   311
be replaced with the class name which you use to generate the
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   312
code (for example \texttt{fib/fib} in case of the Fibonacci
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   313
numbers).
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   314
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   315
Writing out a string is similar. The corresponding library
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   316
function uses strings instead of integers:
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   317
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   318
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   319
\begin{minipage}{12cm}
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   320
\begin{lstlisting}[language=JVMIS, numbers=none]
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   321
.method public static writes(Ljava/lang/String;)V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   322
   .limit stack 2
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   323
   .limit locals 1
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   324
   getstatic java/lang/System/out Ljava/io/PrintStream;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   325
   aload 0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   326
   invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   327
   return
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   328
.end method
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   329
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   330
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   331
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   332
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   333
\noindent The code that needs to be generated for \code{write
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   334
"some_string"} commands is
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   335
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   336
\begin{center}
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   337
\begin{minipage}{12cm}
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   338
\begin{lstlisting}[language=JVMIS,numbers=none]
210
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   339
ldc "some_string"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   340
invokestatic XXX/XXX/writes(Ljava/lang/String;)V
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   341
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   342
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   343
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 205
diff changeset
   344
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   345
\noindent Again you need to adjust the \texttt{XXX/XXX} part
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   346
in each call.
202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 201
diff changeset
   347
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   348
The code for \texttt{read} is more complicated. The reason is
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   349
that inputting a string will need to be transformed into an
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   350
integer. The code in Figure~\ref{read} does this. It can be
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   351
called with
214
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   352
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   353
\begin{center}
390
a1fe591a3df3 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 373
diff changeset
   354
\begin{minipage}{12cm}
313
90ccc385c547 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 309
diff changeset
   355
\begin{lstlisting}[language=JVMIS,numbers=none]
214
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   356
invokestatic XXX/XXX/read()I 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   357
istore n
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   358
\end{lstlisting}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   359
\end{minipage}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   360
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   361
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   362
\noindent 
321
c5850f8f3f5e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 320
diff changeset
   363
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
   364
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
   365
\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
   366
Figure~\ref{read} to \pcode{ldc 13}.  
214
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   367
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   368
321
c5850f8f3f5e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 320
diff changeset
   369
\begin{figure}[t]\small
c5850f8f3f5e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 320
diff changeset
   370
\begin{lstlisting}[language=JVMIS]
214
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   371
.method public static read()I 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   372
      .limit locals 10 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   373
      .limit stack 10
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   374
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   375
      ldc 0 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   376
      istore 1  ; this will hold our final integer 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   377
Label1: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   378
      getstatic java/lang/System/in Ljava/io/InputStream; 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   379
      invokevirtual java/io/InputStream/read()I 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   380
      istore 2 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   381
      iload 2 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   382
      ldc 10   ; the newline delimiter 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   383
      isub 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   384
      ifeq Label2 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   385
      iload 2 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   386
      ldc 32   ; the space delimiter 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   387
      isub 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   388
      ifeq Label2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   389
      iload 2 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   390
      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
   391
      isub 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   392
      ldc 10 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   393
      iload 1 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   394
      imul 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   395
      iadd 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   396
      istore 1 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   397
      goto Label1 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   398
Label2: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   399
      ;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
   400
      iload 1 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   401
      ireturn 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   402
.end method
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   403
\end{lstlisting}\normalsize
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   404
\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
   405
\end{figure}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 213
diff changeset
   406
200
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   407
\end{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   408
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   409
%%% Local Variables: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   410
%%% mode: latex
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   411
%%% TeX-master: t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   412
%%% End: