author | Christian Urban <christian.urban@kcl.ac.uk> |
Fri, 22 Nov 2024 12:42:07 +0000 | |
changeset 974 | 0cb4bf2469d1 |
parent 959 | 64ec1884d860 |
permissions | -rw-r--r-- |
630 | 1 |
% !TEX program = xelatex |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
\documentclass{article} |
309
640e4a05cd9b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
298
diff
changeset
|
3 |
\usepackage{../style} |
216
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
214
diff
changeset
|
4 |
\usepackage{../langs} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
\begin{document} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
|
354
86b2aeae3e98
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
8 |
%https://github.com/Storyyeller/Krakatau |
86b2aeae3e98
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
9 |
%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
|
10 |
|
706 | 11 |
% Jasmin Tutorial |
12 |
%http://saksagan.ceng.metu.edu.tr/courses/ceng444/link/jvm-cpm.html |
|
13 |
||
751 | 14 |
\section*{Coursework 4} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
|
836 | 16 |
\noindent This coursework is worth 15\% and is due on \cwFOUR{} |
877 | 17 |
at 16:00. You are asked to implement a compiler for |
309
640e4a05cd9b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
298
diff
changeset
|
18 |
the WHILE language that targets the assembler language |
823 | 19 |
provided by Jasmin or Krakatau (both have a very similar |
974 | 20 |
syntax). You can do the implementation in any programming |
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
21 |
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
|
22 |
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
|
23 |
be awarded. You should use the lexer and parser from the |
952 | 24 |
previous courseworks. Implement your compiler in the file \texttt{cw04.sc}. |
920 | 25 |
|
26 |
%Please package \emph{everything}(!) in |
|
27 |
%a zip-file that creates a directory with the name |
|
28 |
%\texttt{YournameYourFamilyname} on my end. |
|
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
29 |
|
751 | 30 |
\subsection*{Disclaimer\alert} |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
31 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
32 |
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
|
33 |
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
|
34 |
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
|
35 |
which you can use. You can also use your own code from the |
886 | 36 |
CW~1, CW~2 and CW~3. But do not |
37 |
be tempted to ask Github Copilot for help or do any other |
|
38 |
shenanigans like this! |
|
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
39 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
40 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
41 |
\subsection*{Jasmin Assembler} |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
42 |
|
951 | 43 |
For this coursework you will need an assembler. |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
354
diff
changeset
|
44 |
The Jasmin assembler is available from |
201
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
45 |
|
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
46 |
\begin{center} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
47 |
\url{http://jasmin.sourceforge.net} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
48 |
\end{center} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
49 |
|
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
50 |
\noindent |
951 | 51 |
This is a jar-file you can run on the commandline. |
201
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
52 |
There is a user guide for Jasmin |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
53 |
|
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
54 |
\begin{center} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
55 |
\url{http://jasmin.sourceforge.net/guide.html} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
56 |
\end{center} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
57 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
58 |
\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
|
59 |
that the JVM understands |
201
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
60 |
|
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
61 |
\begin{center} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
62 |
\url{http://jasmin.sourceforge.net/instructions.html} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
63 |
\end{center} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
64 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
65 |
\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
|
66 |
Jasmin, for example \texttt{loops.j}, you can use |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
|
205
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
68 |
\begin{center} |
951 | 69 |
\texttt{java -jar jasmin.jar loops.j} |
205
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
70 |
\end{center} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
71 |
|
852 | 72 |
\noindent in order to translate it into Java Byte Code. If needed, you |
73 |
need to give the path to the Jasmin jar-file. The resulting class |
|
74 |
file can be run with |
|
205
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
75 |
|
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
76 |
\begin{center} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
77 |
\texttt{java loops} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
78 |
\end{center} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
79 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
80 |
\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
|
81 |
class file. For example: |
322
698ed1c96cd0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
321
diff
changeset
|
82 |
|
698ed1c96cd0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
321
diff
changeset
|
83 |
\begin{center} |
698ed1c96cd0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
321
diff
changeset
|
84 |
\texttt{java -cp . loops/loops} |
698ed1c96cd0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
321
diff
changeset
|
85 |
\end{center} |
698ed1c96cd0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
321
diff
changeset
|
86 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
87 |
\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
|
88 |
Internet, for example |
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
89 |
|
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
90 |
\begin{center} |
836 | 91 |
\small\url{https://saksagan.ceng.metu.edu.tr/courses/ceng444/link/f3jasmintutorial.html} |
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
92 |
\end{center} |
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
93 |
|
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
94 |
\noindent and |
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
95 |
|
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
96 |
\begin{center} |
905 | 97 |
\small\url{http://www.csc.villanova.edu/~tway/courses/csc4181/s2022/labs/finalproject/JVM.pdf} |
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
98 |
\end{center} |
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
99 |
|
951 | 100 |
\noindent |
101 |
If possible use Jasmin for the coursework. The Krakatau assembler |
|
974 | 102 |
below has a slightly different syntax. |
951 | 103 |
|
104 |
||
959
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
952
diff
changeset
|
105 |
\subsection*{Krakatau Assembler (Version 1 \& 2)} |
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
106 |
|
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
107 |
The Krakatau assembler is available from |
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
108 |
|
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
109 |
\begin{center} |
951 | 110 |
\url{https://github.com/Storyyeller/Krakatau/tree/master} |
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
111 |
\end{center} |
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
112 |
|
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
113 |
\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
|
114 |
\pcode{ply} available from |
313
90ccc385c547
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
309
diff
changeset
|
115 |
|
90ccc385c547
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
309
diff
changeset
|
116 |
\begin{center} |
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
117 |
\url{https://pypi.python.org/pypi/ply} |
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
118 |
\end{center} |
313
90ccc385c547
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
309
diff
changeset
|
119 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
120 |
\noindent This assembler is largely compatible with the Jasmin |
959
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
952
diff
changeset
|
121 |
syntax---that means for the files we are concerned with here, it |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
952
diff
changeset
|
122 |
understands the same input syntax (no changes to your compiler need to |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
952
diff
changeset
|
123 |
be made; ok maybe some small syntactic adjustments are needed, for |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
952
diff
changeset
|
124 |
example labels need to start with a capital '\texttt{L}'). You can generate Java |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
952
diff
changeset
|
125 |
Byte Code by using |
313
90ccc385c547
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
309
diff
changeset
|
126 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
127 |
\begin{center} |
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
128 |
\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
|
129 |
\end{center} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
130 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
131 |
\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
|
132 |
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
|
133 |
Github and \pcode{Krakatau-master} was the directory where it |
473 | 134 |
was installed). Again the resulting class-file you can run with |
951 | 135 |
\texttt{java}. There is also a newer version of Krakatau available at |
136 |
||
137 |
\begin{center} |
|
138 |
\url{https://github.com/Storyyeller/Krakatau/tree/v2} |
|
139 |
\end{center} |
|
140 |
||
141 |
\noindent |
|
142 |
This is now a Rust program using Cargo as package manager (I have not tried this |
|
143 |
version---I assume it should produce the same output, but might be |
|
144 |
easier to install because it avoids Python's \emph{dependency hell}). |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
145 |
|
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
146 |
|
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
147 |
%\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
|
148 |
%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
|
149 |
%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
|
150 |
%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
|
151 |
%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
|
152 |
%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
|
153 |
%submit your answers in a txt-file or as pdf.\bigskip |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
154 |
|
920 | 155 |
%\subsection*{Question 0 (Unmarked)} |
156 |
% |
|
157 |
%Please include in the PDF at the beginning your email address, your student |
|
158 |
%number and whether you are BSc / MSci student and for the latter in which |
|
159 |
%year your are in. Thanks! |
|
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
160 |
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
391
diff
changeset
|
161 |
\subsection*{Question 1} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
162 |
|
959
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
952
diff
changeset
|
163 |
You need to lex and parse WHILE programs, and then generate Java Byte |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
952
diff
changeset
|
164 |
Code instructions for the Jasmin assembler (or Krakatau |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
952
diff
changeset
|
165 |
assembler). For this you should use the ASTs defined in CW3 (including |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
952
diff
changeset
|
166 |
logical operators). As part of the solution you need to submit the assembler |
974 | 167 |
instructions for the Fibonacci and Factorial programs. |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
168 |
|
313
90ccc385c547
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
309
diff
changeset
|
169 |
\begin{figure}[t] |
852 | 170 |
\lstinputlisting[language=while]{../cwtests/cw04/fib.while} |
320
0c43ef11e086
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
313
diff
changeset
|
171 |
\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
|
172 |
\end{figure} |
90ccc385c547
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
309
diff
changeset
|
173 |
|
419
4110ab35e5d8
updated courseworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
391
diff
changeset
|
174 |
\subsection*{Question 2} |
201
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
175 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
176 |
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
|
177 |
\texttt{for}-loops, like |
201
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
178 |
|
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
179 |
\begin{center} |
391
f352cb238c32
updated meaning of for-loop
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
390
diff
changeset
|
180 |
\lstset{language=While} |
f352cb238c32
updated meaning of for-loop
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
390
diff
changeset
|
181 |
\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
|
182 |
\;\textit{AExp}\; \code{do} \textit{Block} |
201
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
183 |
\end{center} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
184 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
185 |
\noindent The intended meaning is to first assign the variable |
951 | 186 |
\textit{Id} the value of the first arithmetic expression, then test |
598 | 187 |
whether this value is less or equal than the value of the |
391
f352cb238c32
updated meaning of for-loop
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
390
diff
changeset
|
188 |
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
|
189 |
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
|
190 |
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
|
191 |
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
|
192 |
print out the numbers \pcode{2}, \pcode{3}, \pcode{4}. |
202
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
193 |
|
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
194 |
|
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
195 |
\begin{center} |
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
196 |
\begin{minipage}{12cm} |
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
197 |
\begin{lstlisting}[language=While, numbers=none] |
202
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
198 |
for i := 2 upto 4 do { |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
199 |
write i |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
200 |
} |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
201 |
\end{lstlisting} |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
202 |
\end{minipage} |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
203 |
\end{center} |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
204 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
205 |
\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
|
206 |
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
|
207 |
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
|
208 |
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
|
209 |
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
|
210 |
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
|
211 |
following \code{while}-loop: |
203
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
212 |
|
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
213 |
\begin{center} |
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
214 |
\begin{minipage}{12cm} |
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
215 |
\begin{lstlisting}[language=While, numbers=none] |
203
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
216 |
i := 2; |
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
217 |
while (i <= 4) do { |
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
218 |
write i; |
905 | 219 |
i := i + 1 |
203
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
220 |
} |
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
221 |
\end{lstlisting} |
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
222 |
\end{minipage} |
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
223 |
\end{center} |
202
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
224 |
|
974 | 225 |
\subsection*{Question 3 (OPTIONAL)} |
333
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
226 |
|
974 | 227 |
\noindent In this question you are asked to think about the following |
228 |
program: |
|
205
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
229 |
|
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
230 |
\begin{center} |
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
231 |
\begin{minipage}{12cm} |
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
232 |
\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
|
233 |
for i := 1 upto 10 do { |
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
234 |
for i := 1 upto 10 do { |
8890852e18b7
updated coursework
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
328
diff
changeset
|
235 |
write i |
205
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
236 |
} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
237 |
} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
238 |
\end{lstlisting} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
239 |
\end{minipage} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
240 |
\end{center} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
241 |
|
313
90ccc385c547
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
309
diff
changeset
|
242 |
\noindent |
90ccc385c547
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
309
diff
changeset
|
243 |
Note that in this program the variable \pcode{i} is used |
974 | 244 |
twice. Therefore you need to make a decision about how it should be compiled? |
245 |
What should the program print? |
|
202
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
246 |
|
920 | 247 |
\subsection*{Question 4} |
248 |
||
943 | 249 |
Extend the lexer and parser to add a \textcolor{codepurple}{\pcode{break}} keyword. Modify |
250 |
the compiler (including lexer and parser) such that when a \textcolor{codepurple}{\texttt{break}}-statement is encountered |
|
959
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
952
diff
changeset
|
251 |
the code should jump out of the ``enclosing'' for/while-loop, or in case it |
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
952
diff
changeset
|
252 |
is not inside such a loop to the end of the program. For example the |
920 | 253 |
program |
254 |
||
255 |
\begin{center} |
|
256 |
\begin{minipage}{12cm} |
|
257 |
\begin{lstlisting}[language=While, numbers=none] |
|
943 | 258 |
// should print 0 .. 10 |
920 | 259 |
for i := 0 upto 10 do { |
260 |
write i; |
|
261 |
write "\n" |
|
262 |
}; |
|
910 | 263 |
|
943 | 264 |
// should print 0 .. 4 |
920 | 265 |
for i := 0 upto 10 do { |
266 |
if i > 4 then break else skip; |
|
267 |
write i; |
|
268 |
write "\n" |
|
269 |
}; |
|
910 | 270 |
|
920 | 271 |
write "Should print\n"; |
272 |
break; |
|
273 |
write "Should not print\n" |
|
274 |
\end{lstlisting} |
|
275 |
\end{minipage} |
|
276 |
\end{center} |
|
277 |
||
278 |
\noindent |
|
951 | 279 |
should print out 0 to 10 with the first for-loop, but only 0 |
920 | 280 |
to 4 in the second. Similarly it should print out \code{"Should print"}, |
281 |
but not \code{"Should not print"}. For this you need to add |
|
959
64ec1884d860
updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
952
diff
changeset
|
282 |
a label to the end of every for- and while-loop and also to the end of the |
920 | 283 |
whole program just in case you need to jump to that label via a |
952 | 284 |
\code{break}. The file you need to be able to process for this question |
285 |
is called \texttt{break.while}. |
|
920 | 286 |
|
910 | 287 |
|
204
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
288 |
\subsection*{Further Information} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
289 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
290 |
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
|
291 |
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
|
292 |
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
|
293 |
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
|
294 |
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
|
295 |
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
|
296 |
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
|
297 |
the simple Hello World program below: |
204
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
298 |
|
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
299 |
\begin{center} |
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
300 |
\begin{minipage}{12cm} |
313
90ccc385c547
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
309
diff
changeset
|
301 |
\begin{lstlisting}[language=Java,numbers=none] |
204
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
302 |
class HelloWorld { |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
303 |
public static void main(String[] args) { |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
304 |
System.out.println("Hello World!"); |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
305 |
} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
306 |
} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
307 |
\end{lstlisting} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
308 |
\end{minipage} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
309 |
\end{center} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
310 |
|
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
311 |
\noindent |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
312 |
You can use the command |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
313 |
|
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
314 |
\begin{center} |
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
315 |
\begin{minipage}{12cm} |
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
316 |
\begin{lstlisting}[language={},numbers=none] |
951 | 317 |
javap -c HelloWorld |
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
318 |
\end{lstlisting} |
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
319 |
\end{minipage} |
204
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
320 |
\end{center} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
321 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
322 |
\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
|
323 |
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
|
324 |
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
|
325 |
World. |
204
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
326 |
|
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
327 |
\begin{center} |
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
328 |
\begin{minipage}{12cm} |
313
90ccc385c547
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
309
diff
changeset
|
329 |
\begin{lstlisting}[language=Scala,numbers=none] |
204
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
330 |
object HelloWorld { |
951 | 331 |
def main(args: Array[String]) = { |
204
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
332 |
println("Hello World!") |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
333 |
} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
334 |
} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
335 |
\end{lstlisting} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
336 |
\end{minipage} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
337 |
\end{center} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
338 |
|
222
b712519b41d3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
216
diff
changeset
|
339 |
|
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
340 |
\subsection*{Library Functions} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
341 |
|
313
90ccc385c547
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
309
diff
changeset
|
342 |
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
|
343 |
\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
|
344 |
``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
|
345 |
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
|
346 |
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
|
347 |
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
|
348 |
can use the assembler code |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
349 |
|
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
350 |
\begin{center} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
351 |
\begin{minipage}{12cm} |
313
90ccc385c547
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
309
diff
changeset
|
352 |
\begin{lstlisting}[language=JVMIS, numbers=none] |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
353 |
.method public static write(I)V |
859 | 354 |
.limit locals 1 |
355 |
.limit stack 2 |
|
356 |
getstatic java/lang/System/out Ljava/io/PrintStream; |
|
357 |
iload 0 |
|
358 |
invokevirtual java/io/PrintStream/println(I)V |
|
359 |
return |
|
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
360 |
.end method |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
361 |
\end{lstlisting} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
362 |
\end{minipage} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
363 |
\end{center} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
364 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
365 |
\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
|
366 |
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
|
367 |
\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
|
368 |
can generate |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
369 |
|
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
370 |
\begin{center} |
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
371 |
\begin{minipage}{12cm} |
313
90ccc385c547
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
309
diff
changeset
|
372 |
\begin{lstlisting}[language=JVMIS, numbers=none] |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
373 |
iload n |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
374 |
invokestatic XXX/XXX/write(I)V |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
375 |
\end{lstlisting} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
376 |
\end{minipage} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
377 |
\end{center} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
378 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
379 |
\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
|
380 |
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
|
381 |
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
|
382 |
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
|
383 |
numbers). |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
384 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
385 |
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
|
386 |
function uses strings instead of integers: |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
387 |
|
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
388 |
\begin{center} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
389 |
\begin{minipage}{12cm} |
313
90ccc385c547
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
309
diff
changeset
|
390 |
\begin{lstlisting}[language=JVMIS, numbers=none] |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
391 |
.method public static writes(Ljava/lang/String;)V |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
392 |
.limit stack 2 |
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
393 |
.limit locals 1 |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
394 |
getstatic java/lang/System/out Ljava/io/PrintStream; |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
395 |
aload 0 |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
396 |
invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
397 |
return |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
398 |
.end method |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
399 |
\end{lstlisting} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
400 |
\end{minipage} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
401 |
\end{center} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
402 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
403 |
\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
|
404 |
"some_string"} commands is |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
405 |
|
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
406 |
\begin{center} |
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
407 |
\begin{minipage}{12cm} |
313
90ccc385c547
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
309
diff
changeset
|
408 |
\begin{lstlisting}[language=JVMIS,numbers=none] |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
409 |
ldc "some_string" |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
410 |
invokestatic XXX/XXX/writes(Ljava/lang/String;)V |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
411 |
\end{lstlisting} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
412 |
\end{minipage} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
413 |
\end{center} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
414 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
415 |
\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
|
416 |
in each call. |
202
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
417 |
|
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
418 |
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
|
419 |
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
|
420 |
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
|
421 |
called with |
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
422 |
|
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
423 |
\begin{center} |
390
a1fe591a3df3
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
373
diff
changeset
|
424 |
\begin{minipage}{12cm} |
313
90ccc385c547
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
309
diff
changeset
|
425 |
\begin{lstlisting}[language=JVMIS,numbers=none] |
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
426 |
invokestatic XXX/XXX/read()I |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
427 |
istore n |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
428 |
\end{lstlisting} |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
429 |
\end{minipage} |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
430 |
\end{center} |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
431 |
|
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
432 |
\noindent |
951 | 433 |
where \texttt{n} is the index of the variable that requires an input. |
434 |
%If you |
|
435 |
%use Windows you need to take into account that a ``return'' is not just a newline, |
|
436 |
%\code{'\\10'}, but \code{'\\13\\10'}. This means you need to change line~12 in |
|
437 |
%Figure~\ref{read} to \pcode{ldc 13}. |
|
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
438 |
|
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
439 |
|
321
c5850f8f3f5e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
320
diff
changeset
|
440 |
\begin{figure}[t]\small |
540 | 441 |
\begin{lstlisting}[language=JVMIS,numbers=left] |
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
442 |
.method public static read()I |
859 | 443 |
.limit locals 10 |
444 |
.limit stack 10 |
|
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
445 |
|
859 | 446 |
ldc 0 |
447 |
istore 1 ; this will hold our final integer |
|
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
448 |
Label1: |
859 | 449 |
getstatic java/lang/System/in Ljava/io/InputStream; |
450 |
invokevirtual java/io/InputStream/read()I |
|
451 |
istore 2 |
|
452 |
iload 2 |
|
951 | 453 |
ldc 10 ; test for the newline delimiter for Unix |
859 | 454 |
isub |
951 | 455 |
ifeq Label2 |
456 |
iload 2 |
|
457 |
ldc 13 ; test for the carriage-return in Windows |
|
458 |
isub |
|
459 |
ifeq Label2 |
|
859 | 460 |
iload 2 |
461 |
ldc 32 ; the space delimiter |
|
462 |
isub |
|
463 |
ifeq Label2 |
|
464 |
iload 2 |
|
465 |
ldc 48 ; we have our digit in ASCII, have to subtract it from 48 |
|
466 |
isub |
|
467 |
ldc 10 |
|
468 |
iload 1 |
|
469 |
imul |
|
470 |
iadd |
|
471 |
istore 1 |
|
472 |
goto Label1 |
|
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
473 |
Label2: |
859 | 474 |
; when we come here we have our integer computed |
475 |
; in local variable 1 |
|
476 |
iload 1 |
|
477 |
ireturn |
|
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
478 |
.end method |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
479 |
\end{lstlisting}\normalsize |
951 | 480 |
\caption{Assembler code for reading an integer from the console. |
481 |
This code is portable for Unix and Windows (see Lines 11--18 for 2 separate |
|
482 |
tests for the various end-of-line markers). Thanks to Harry |
|
483 |
Dilnot to make it portable.\label{read}} |
|
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
484 |
\end{figure} |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
485 |
|
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
486 |
\end{document} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
487 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
488 |
%%% Local Variables: |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
489 |
%%% mode: latex |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
490 |
%%% TeX-master: t |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
491 |
%%% End: |