author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Sat, 30 Nov 2013 00:06:02 +0000 | |
changeset 214 | 5be68de225e9 |
parent 213 | c41f193ffaf6 |
child 216 | f5ec7c597c5b |
permissions | -rw-r--r-- |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
\documentclass{article} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
\usepackage{charter} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
\usepackage{hyperref} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
\usepackage{amssymb} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
\usepackage{amsmath} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
\usepackage{listings} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
\usepackage{xcolor} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
\begin{document} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
\definecolor{javared}{rgb}{0.6,0,0} % for strings |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
\definecolor{javagreen}{rgb}{0.25,0.5,0.35} % comments |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
\definecolor{javapurple}{rgb}{0.5,0,0.35} % keywords |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
\definecolor{javadocblue}{rgb}{0.25,0.35,0.75} % javadoc |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
\lstdefinelanguage{scala}{ |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
morekeywords={abstract,case,catch,class,def,% |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
do,else,extends,false,final,finally,% |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
for,if,implicit,import,match,mixin,% |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
new,null,object,override,package,% |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
private,protected,requires,return,sealed,% |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
super,this,throw,trait,true,try, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
type,val,var,while,with,yield}, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
otherkeywords={=>,<-,<\%,<:,>:,\#,@}, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
sensitive=true, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
morecomment=[l]{//}, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
morecomment=[n]{/*}{*/}, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
morestring=[b]", |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
morestring=[b]', |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
morestring=[b]""" |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
\lstdefinelanguage{while}{ |
203
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
36 |
morekeywords={while, if, then. else, read, write, for, upto, do}, |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
otherkeywords={=>,<-,<\%,<:,>:,\#,@}, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
sensitive=true, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
morecomment=[l]{//}, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
morecomment=[n]{/*}{*/}, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
morestring=[b]", |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
morestring=[b]', |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
morestring=[b]""" |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
\lstset{language=Scala, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
basicstyle=\ttfamily, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
keywordstyle=\color{javapurple}\bfseries, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
stringstyle=\color{javagreen}, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
commentstyle=\color{javagreen}, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
morecomment=[s][\color{javadocblue}]{/**}{*/}, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
numbers=left, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
numberstyle=\tiny\color{black}, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
stepnumber=1, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
numbersep=10pt, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
tabsize=2, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
showspaces=false, |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
showstringspaces=false} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
\section*{Coursework 3} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
\noindent |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
This coursework is worth 4\% and is due on 13 December at 16:00. You are asked to |
204
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
67 |
implement a compiler for the WHILE language that targets the |
213
c41f193ffaf6
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
210
diff
changeset
|
68 |
assembler language provided by Jasmin. This assembler |
201
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
69 |
is available from |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
70 |
|
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
71 |
\begin{center} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
72 |
\url{http://jasmin.sourceforge.net} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
73 |
\end{center} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
74 |
|
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
75 |
\noindent |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
76 |
There is a user guide for Jasmin |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
77 |
|
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
78 |
\begin{center} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
79 |
\url{http://jasmin.sourceforge.net/guide.html} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
80 |
\end{center} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
81 |
|
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
82 |
\noindent |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
83 |
and also a description of some of the instructions that the JVM understands |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
84 |
|
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
85 |
\begin{center} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
86 |
\url{http://jasmin.sourceforge.net/instructions.html} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
87 |
\end{center} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
88 |
|
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
89 |
\noindent |
205
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
90 |
If you generated a correct assembler file for Jasmin, for example |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
91 |
\texttt{loops.j}, you can use |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
|
205
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
93 |
\begin{center} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
94 |
\texttt{java -jar jasmin-2.4/jasmin.jar loops.j} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
95 |
\end{center} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
96 |
|
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
97 |
\noindent |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
98 |
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
|
99 |
run with |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
100 |
|
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
101 |
\begin{center} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
102 |
\texttt{java loops} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
103 |
\end{center} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
104 |
|
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
105 |
\noindent |
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
106 |
where you might need to give the correct path to the class file.\bigskip |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
107 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
108 |
\noindent |
204
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
109 |
You need to submit a document containing the answers for the two questions |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
110 |
below. You can do the implementation in any programming language you like, but you need |
205
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
111 |
to submit the source code with which you answered the questions. Otherwise |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
112 |
the submission will not be counted. However, the coursework |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
113 |
will \emph{only} be judged according to the answers. You can submit your answers |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
114 |
in a txt-file or as pdf.\bigskip |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
115 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
116 |
|
202
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
117 |
\subsection*{Question 1 (marked with 2\%)} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
118 |
|
201
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
119 |
You need to lex and parse WHILE programs and submit the assembler |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
120 |
instructions for the Fibonacci program and for the program you submitted |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
121 |
in Coursework 2 in Question 3. The latter should be so modified that |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
122 |
a user can input the upper bound on the console (in the original question |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
123 |
it was fixed to 100). |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
124 |
|
201
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
125 |
\subsection*{Question 2 (marked with 2\%)} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
126 |
|
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
127 |
Extend the syntax of you language so that it contains also \texttt{for}-loops, like |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
128 |
|
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
129 |
\begin{center} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
130 |
\texttt{for} \;\textit{Id} \texttt{:=} \textit{AExp}\; \texttt{upto} \;\textit{AExp}\; \texttt{do} \textit{Block} |
c813506e0ee8
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
131 |
\end{center} |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
132 |
|
202
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
133 |
\noindent |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
134 |
The intended meaning is to first assign the variable \textit{Id} the value of the first arithmetic |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
135 |
expression, then go through the loop, at the end increase the value of the variable by 1, |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
136 |
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
|
137 |
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
|
138 |
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
|
139 |
|
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
140 |
|
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
141 |
\begin{center} |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
142 |
\begin{minipage}{6cm} |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
143 |
\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
|
144 |
for i := 2 upto 4 do { |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
145 |
write i |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
146 |
} |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
147 |
\end{lstlisting} |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
148 |
\end{minipage} |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
149 |
\end{center} |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
150 |
|
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
151 |
\noindent |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
152 |
There are two ways how this can be implemented: one is to adapt the code generation |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
153 |
part of the compiler and generate specific code for \texttt{for}-loops; the other is to |
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
154 |
translate the abstract syntax tree of \texttt{for}-loops into an abstract syntax tree using |
203
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
155 |
existing language constructs. For example the loop above could be translated |
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
156 |
to the following \texttt{while}-loop: |
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
157 |
|
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
158 |
\begin{center} |
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
159 |
\begin{minipage}{6cm} |
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
160 |
\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
|
161 |
i := 2; |
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
162 |
while (i <= 4) do { |
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
163 |
write i; |
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
164 |
i := i + 1; |
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
165 |
} |
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
166 |
\end{lstlisting} |
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
167 |
\end{minipage} |
f1335c171d50
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
202
diff
changeset
|
168 |
\end{center} |
202
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
169 |
|
205
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
170 |
\noindent |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
171 |
In this question you are supposed to give the assembler instructions for the |
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
172 |
program |
205
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
173 |
|
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
174 |
\begin{center} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
175 |
\begin{minipage}{6cm} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
176 |
\begin{lstlisting}[language=While,basicstyle=\ttfamily, numbers=none] |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
177 |
for i := 1 upto 10000 do { |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
178 |
for i := 1 upto 10000 do { |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
179 |
skip |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
180 |
} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
181 |
} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
182 |
\end{lstlisting} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
183 |
\end{minipage} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
184 |
\end{center} |
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
185 |
|
0b59588d28d2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
204
diff
changeset
|
186 |
|
202
180cbfc1520a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
201
diff
changeset
|
187 |
|
204
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
188 |
\subsection*{Further Information} |
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 |
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
|
191 |
(therefore |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
192 |
you need to download the additional package Jasmin---see above). But it does contain a |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
193 |
disassembler, called \texttt{javap}. A dissembler does the ``opposite'' of an assembler: it |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
194 |
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
|
195 |
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
|
196 |
program below: |
204
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 |
\begin{center} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
199 |
\begin{minipage}{10cm} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
200 |
\begin{lstlisting}[language=Java,basicstyle=\ttfamily] |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
201 |
class HelloWorld { |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
202 |
public static void main(String[] args) { |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
203 |
System.out.println("Hello World!"); |
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 |
} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
206 |
\end{lstlisting} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
207 |
\end{minipage} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
208 |
\end{center} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
209 |
|
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
210 |
\noindent |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
211 |
You can use the command |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
212 |
|
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
213 |
\begin{center} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
214 |
\texttt{javap -v HelloWorld} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
215 |
\end{center} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
216 |
|
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
217 |
\noindent |
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
218 |
to see the assembler instructions of the Java byte code that has been generated for this |
204
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
219 |
program. You can compare this with the code generated for the Scala |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
220 |
version of Hello World. |
204
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 |
\begin{center} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
223 |
\begin{minipage}{10cm} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
224 |
\begin{lstlisting}[language=Scala,basicstyle=\ttfamily] |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
225 |
object HelloWorld { |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
226 |
def main(args: Array[String]) { |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
227 |
println("Hello World!") |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
228 |
} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
229 |
} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
230 |
\end{lstlisting} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
231 |
\end{minipage} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
232 |
\end{center} |
fec99c437965
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
203
diff
changeset
|
233 |
|
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
234 |
\subsection*{Library Functions} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
235 |
|
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
236 |
You need to generate code for the commands \texttt{write} and \texttt{read}. This |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
237 |
will require the addition of some ``library'' functions to your generated code. The first |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
238 |
command even needs two versions, because you might want to write out an |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
239 |
integer or a string. The Java byte code will need two separate functions for this. |
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
240 |
For writing out an integer, you can use the assembler code |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
241 |
|
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
242 |
\begin{center} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
243 |
\begin{minipage}{12cm} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
244 |
\begin{lstlisting}[basicstyle=\ttfamily, numbers=none] |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
245 |
.method public static write(I)V |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
246 |
.limit locals 5 |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
247 |
.limit stack 5 |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
248 |
iload 0 |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
249 |
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
|
250 |
swap |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
251 |
invokevirtual java/io/PrintStream/println(I)V |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
252 |
return |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
253 |
.end method |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
254 |
\end{lstlisting} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
255 |
\end{minipage} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
256 |
\end{center} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
257 |
|
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
258 |
\noindent |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
259 |
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
|
260 |
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
|
261 |
|
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
262 |
\begin{center} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
263 |
\begin{minipage}{8cm} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
264 |
\begin{lstlisting}[basicstyle=\ttfamily, numbers=none] |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
265 |
iload n |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
266 |
invokestatic XXX/XXX/write(I)V |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
267 |
\end{lstlisting} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
268 |
\end{minipage} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
269 |
\end{center} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
270 |
|
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
271 |
\noindent |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
272 |
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
|
273 |
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
|
274 |
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
|
275 |
of the Fibonacci numbers). |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
276 |
|
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
277 |
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
|
278 |
instead of integers: |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
279 |
|
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
280 |
\begin{center} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
281 |
\begin{minipage}{12cm} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
282 |
\begin{lstlisting}[basicstyle=\ttfamily, numbers=none] |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
283 |
.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
|
284 |
.limit stack 2 |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
285 |
.limit locals 2 |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
286 |
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
|
287 |
aload 0 |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
288 |
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
|
289 |
return |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
290 |
.end method |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
291 |
\end{lstlisting} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
292 |
\end{minipage} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
293 |
\end{center} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
294 |
|
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
295 |
\noindent |
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
296 |
The code that needs to be generated for \texttt{write "some\_string"} commands |
210
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
297 |
is |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
298 |
|
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
299 |
\begin{center} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
300 |
\begin{minipage}{8cm} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
301 |
\begin{lstlisting}[basicstyle=\ttfamily, numbers=none] |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
302 |
ldc "some_string" |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
303 |
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
|
304 |
\end{lstlisting} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
305 |
\end{minipage} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
306 |
\end{center} |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
307 |
|
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
308 |
\noindent |
33175abd5474
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
205
diff
changeset
|
309 |
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
|
310 |
|
214
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
311 |
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
|
312 |
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
|
313 |
It can be called with |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
314 |
|
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
315 |
\begin{center} |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
316 |
\begin{minipage}{8cm} |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
317 |
\begin{lstlisting}[basicstyle=\ttfamily, numbers=none] |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
318 |
invokestatic XXX/XXX/read()I |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
319 |
istore n |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
320 |
\end{lstlisting} |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
321 |
\end{minipage} |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
322 |
\end{center} |
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 |
\noindent |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
325 |
where \texttt{n} is the index of the variable that requires an input. |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
326 |
|
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
327 |
|
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
328 |
\begin{figure}[p]\small |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
329 |
\begin{lstlisting}[basicstyle=\ttfamily, numbers=none] |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
330 |
.method public static read()I |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
331 |
.limit locals 10 |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
332 |
.limit stack 10 |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
333 |
|
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
334 |
ldc 0 |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
335 |
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
|
336 |
Label1: |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
337 |
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
|
338 |
invokevirtual java/io/InputStream/read()I |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
339 |
istore 2 |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
340 |
iload 2 |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
341 |
ldc 10 ; the newline delimiter |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
342 |
isub |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
343 |
ifeq Label2 |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
344 |
iload 2 |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
345 |
ldc 32 ; the space delimiter |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
346 |
isub |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
347 |
ifeq Label2 |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
348 |
|
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
349 |
iload 2 |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
350 |
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
|
351 |
isub |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
352 |
ldc 10 |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
353 |
iload 1 |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
354 |
imul |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
355 |
iadd |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
356 |
istore 1 |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
357 |
goto Label1 |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
358 |
Label2: |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
359 |
;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
|
360 |
iload 1 |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
361 |
ireturn |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
362 |
.end method |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
363 |
\end{lstlisting}\normalsize |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
364 |
\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
|
365 |
\end{figure} |
5be68de225e9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
213
diff
changeset
|
366 |
|
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
367 |
\end{document} |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
368 |
|
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
369 |
%%% Local Variables: |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
370 |
%%% mode: latex |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
371 |
%%% TeX-master: t |
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
372 |
%%% End: |