101 \noindent |
101 \noindent |
102 If possible use Jasmin for the coursework. The Krakatau assembler |
102 If possible use Jasmin for the coursework. The Krakatau assembler |
103 below as a slightly different syntax. |
103 below as a slightly different syntax. |
104 |
104 |
105 |
105 |
106 \subsection*{Krakatau Assembler} |
106 \subsection*{Krakatau Assembler (Version 1 \& 2)} |
107 |
107 |
108 The Krakatau assembler is available from |
108 The Krakatau assembler is available from |
109 |
109 |
110 \begin{center} |
110 \begin{center} |
111 \url{https://github.com/Storyyeller/Krakatau/tree/master} |
111 \url{https://github.com/Storyyeller/Krakatau/tree/master} |
117 \begin{center} |
117 \begin{center} |
118 \url{https://pypi.python.org/pypi/ply} |
118 \url{https://pypi.python.org/pypi/ply} |
119 \end{center} |
119 \end{center} |
120 |
120 |
121 \noindent This assembler is largely compatible with the Jasmin |
121 \noindent This assembler is largely compatible with the Jasmin |
122 syntax---that means for the files we are concerned with here, |
122 syntax---that means for the files we are concerned with here, it |
123 it understands the same input syntax (no changes to your |
123 understands the same input syntax (no changes to your compiler need to |
124 compiler need to be made; ok maybe some small syntactic |
124 be made; ok maybe some small syntactic adjustments are needed, for |
125 adjustments are needed). You can generate Java Byte Code by |
125 example labels need to start with a capital '\texttt{L}'). You can generate Java |
126 using |
126 Byte Code by using |
127 |
127 |
128 \begin{center} |
128 \begin{center} |
129 \texttt{python Krakatau-master/assemble.py loops.j} |
129 \texttt{python Krakatau-master/assemble.py loops.j} |
130 \end{center} |
130 \end{center} |
131 |
131 |
159 %number and whether you are BSc / MSci student and for the latter in which |
159 %number and whether you are BSc / MSci student and for the latter in which |
160 %year your are in. Thanks! |
160 %year your are in. Thanks! |
161 |
161 |
162 \subsection*{Question 1} |
162 \subsection*{Question 1} |
163 |
163 |
164 You need to lex and parse WHILE programs, and then generate |
164 You need to lex and parse WHILE programs, and then generate Java Byte |
165 Java Byte Code instructions for the Jasmin assembler (or |
165 Code instructions for the Jasmin assembler (or Krakatau |
166 Krakatau assembler). As solution you need to submit the |
166 assembler). For this you should use the ASTs defined in CW3 (including |
167 assembler instructions for the Fibonacci and Factorial |
167 logical operators). As part of the solution you need to submit the assembler |
168 programs. Both should be so modified that a user can input on |
168 instructions for the Fibonacci and Factorial programs. Both should be |
169 the console which Fibonacci number and which Factorial should |
169 so modified that a user can input on the console which Fibonacci |
170 be calculated. The Fibonacci program is given in |
170 number and which Factorial should be calculated. The Fibonacci program |
171 Figure~\ref{fibs}. You can write your own program for |
171 is given in Figure~\ref{fibs}. You can write your own program for |
172 calculating factorials. Submit your assembler code as |
172 calculating factorials. Submit your assembler code as a file that can |
173 a file that can be run, not as PDF-text. |
173 be run, not as PDF-text. |
174 |
174 |
175 \begin{figure}[t] |
175 \begin{figure}[t] |
176 \lstinputlisting[language=while]{../cwtests/cw04/fib.while} |
176 \lstinputlisting[language=while]{../cwtests/cw04/fib.while} |
177 \caption{The Fibonacci program in the WHILE language.\label{fibs}} |
177 \caption{The Fibonacci program in the WHILE language.\label{fibs}} |
178 \end{figure} |
178 \end{figure} |
253 |
253 |
254 \subsection*{Question 4} |
254 \subsection*{Question 4} |
255 |
255 |
256 Extend the lexer and parser to add a \textcolor{codepurple}{\pcode{break}} keyword. Modify |
256 Extend the lexer and parser to add a \textcolor{codepurple}{\pcode{break}} keyword. Modify |
257 the compiler (including lexer and parser) such that when a \textcolor{codepurple}{\texttt{break}}-statement is encountered |
257 the compiler (including lexer and parser) such that when a \textcolor{codepurple}{\texttt{break}}-statement is encountered |
258 the code should jump out of the ``enclosing'' for-loop, or in case it |
258 the code should jump out of the ``enclosing'' for/while-loop, or in case it |
259 is not inside a for-loop to the end of the program. For example the |
259 is not inside such a loop to the end of the program. For example the |
260 program |
260 program |
261 |
261 |
262 \begin{center} |
262 \begin{center} |
263 \begin{minipage}{12cm} |
263 \begin{minipage}{12cm} |
264 \begin{lstlisting}[language=While, numbers=none] |
264 \begin{lstlisting}[language=While, numbers=none] |
284 |
284 |
285 \noindent |
285 \noindent |
286 should print out 0 to 10 with the first for-loop, but only 0 |
286 should print out 0 to 10 with the first for-loop, but only 0 |
287 to 4 in the second. Similarly it should print out \code{"Should print"}, |
287 to 4 in the second. Similarly it should print out \code{"Should print"}, |
288 but not \code{"Should not print"}. For this you need to add |
288 but not \code{"Should not print"}. For this you need to add |
289 a label to the end of every for-loop and also to the end of the |
289 a label to the end of every for- and while-loop and also to the end of the |
290 whole program just in case you need to jump to that label via a |
290 whole program just in case you need to jump to that label via a |
291 \code{break}. The file you need to be able to process for this question |
291 \code{break}. The file you need to be able to process for this question |
292 is called \texttt{break.while}. |
292 is called \texttt{break.while}. |
293 |
293 |
294 |
294 |