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