coursework/cw04.tex
changeset 313 90ccc385c547
parent 309 640e4a05cd9b
child 320 0c43ef11e086
equal deleted inserted replaced
312:5cdb4d40eb80 313:90ccc385c547
    46 \end{center}
    46 \end{center}
    47 
    47 
    48 \noindent
    48 \noindent
    49 where you might need to give the correct path to the class file. There
    49 where you might need to give the correct path to the class file. There
    50 are also other resources about Jasmin on the Internet, for example
    50 are also other resources about Jasmin on the Internet, for example
    51 \mbox{\url{http://goo.gl/Qj8TeK}} and \mbox{\url{http://goo.gl/fpVNyT}}\;.\bigskip
    51 
       
    52 \begin{center}
       
    53 \url{http://www.ceng.metu.edu.tr/courses/ceng444/link/f3jasmintutorial.html}
       
    54 
       
    55 and
       
    56 
       
    57 \url{http://www.csc.villanova.edu/~tway/courses/csc8505/s2011/handouts/JVM%20and%20Jasmin.pdf}
       
    58 \end{center}
    52 
    59 
    53 \noindent
    60 \noindent
    54 You need to submit a document containing the answers for the two questions 
    61 You need to submit a document containing the answers for the two questions 
    55 below. You can do the implementation in any programming language you like, but you need 
    62 below. You can do the implementation in any programming language you like, but you need 
    56 to submit the source code with which you answered the questions. Otherwise
    63 to submit the source code with which you answered the questions. Otherwise
    57 the submission will not be counted.  However, the coursework 
    64 the submission will not be counted.  However, the coursework 
    58 will \emph{only} be judged according to the answers. You can submit your answers
    65 will \emph{only} be judged according to the answers. You can submit your answers
    59 in a txt-file or as pdf.\bigskip
    66 in a txt-file or as pdf.\bigskip
    60 
    67 
    61 
    68 
    62 \subsection*{Question 1 (marked with 2\%)}
    69 \subsection*{Question 1 (marked with 5\%)}
    63 
    70 
    64 You need to lex and parse WHILE programs and submit the assembler 
    71 You need to lex and parse WHILE programs, and then generate
    65 instructions for the Fibonacci program and for the program you submitted
    72 Java Byte Code instructions for the Jasmin assmebler. As
    66 in Coursework 2 in Question 3. The latter should be so modified that 
    73 solution you need to submit the assembler instructions for the
    67 a user can input the upper bound on the console (in the original question
    74 Fibonacci and Factorial programs. Both should be so modified
    68 it was fixed to 100).
    75 that a user can input on the console which Fibonacci number 
    69 
    76 and which Factorial should calculated. The Fibonacci program
    70 \subsection*{Question 2 (marked with 2\%)}
    77 is  given in Figure~\ref{fibs}.
       
    78 
       
    79 \begin{figure}[t]
       
    80 \lstinputlisting[language=while]{../progs/fib.while}
       
    81 \caption{The Fibonacci program in the WHILE language.}
       
    82 \end{figure}
       
    83 
       
    84 \subsection*{Question 2 (marked with 5\%)}
    71 
    85 
    72 Extend the syntax of you language so that it contains also \texttt{for}-loops, like
    86 Extend the syntax of you language so that it contains also \texttt{for}-loops, like
    73 
    87 
    74 \begin{center}
    88 \begin{center}
    75 \texttt{for} \;\textit{Id} \texttt{:=} \textit{AExp}\; \texttt{upto} \;\textit{AExp}\; \texttt{do} \textit{Block} 
    89 \texttt{for} \;\textit{Id} \texttt{:=} \textit{AExp}\; \texttt{upto} \;\textit{AExp}\; \texttt{do} \textit{Block} 
   126 } 
   140 } 
   127 \end{lstlisting}
   141 \end{lstlisting}
   128 \end{minipage}
   142 \end{minipage}
   129 \end{center}
   143 \end{center}
   130 
   144 
   131 
   145 \noindent 
       
   146 Note that in this program the variable \pcode{i} is used
       
   147 twice. You need to make a decision how it should be compiled?
   132 
   148 
   133 \subsection*{Further Information}
   149 \subsection*{Further Information}
   134 
   150 
   135 The Java infrastructure unfortunately does not contain an assembler out-of-the-box
   151 The Java infrastructure unfortunately does not contain an assembler out-of-the-box
   136 (therefore
   152 (therefore
   140 following example: Compile using the usual Java compiler the simple Hello World 
   156 following example: Compile using the usual Java compiler the simple Hello World 
   141 program below:
   157 program below:
   142 
   158 
   143 \begin{center}
   159 \begin{center}
   144 \begin{minipage}{10cm}
   160 \begin{minipage}{10cm}
   145 \begin{lstlisting}[language=Java,basicstyle=\ttfamily]
   161 \begin{lstlisting}[language=Java,numbers=none]
   146 class HelloWorld {
   162 class HelloWorld {
   147     public static void main(String[] args) {
   163     public static void main(String[] args) {
   148         System.out.println("Hello World!");
   164         System.out.println("Hello World!");
   149     }
   165     }
   150 }
   166 }
   157 
   173 
   158 \begin{center}
   174 \begin{center}
   159 \texttt{javap -v HelloWorld}
   175 \texttt{javap -v HelloWorld}
   160 \end{center}
   176 \end{center}
   161 
   177 
   162 \noindent
   178 \noindent to see the assembler instructions of the Java byte
   163 to see the assembler instructions of the Java byte code that has been generated for this
   179 code that has been generated for this program. You can compare
   164 program. You can compare this with the code generated for the Scala
   180 this with the code generated for the Scala version of Hello
   165 version of Hello World.
   181 World.
   166 
   182 
   167 \begin{center}
   183 \begin{center}
   168 \begin{minipage}{10cm}
   184 \begin{minipage}{10cm}
   169 \begin{lstlisting}[language=Scala,basicstyle=\ttfamily]
   185 \begin{lstlisting}[language=Scala,numbers=none]
   170 object HelloWorld {
   186 object HelloWorld {
   171    def main(args: Array[String]) {
   187    def main(args: Array[String]) {
   172       println("Hello World!")
   188       println("Hello World!")
   173   }
   189   }
   174 }
   190 }
   177 \end{center}
   193 \end{center}
   178 
   194 
   179 
   195 
   180 \subsection*{Library Functions}
   196 \subsection*{Library Functions}
   181 
   197 
   182 You need to generate code for the commands \texttt{write} and \texttt{read}. This
   198 You need to generate code for the commands \texttt{write} and
   183 will require the addition of some ``library'' functions to your generated code. The first
   199 \texttt{read}. This will require the addition of some
   184 command even needs two versions, because you might want to write out an
   200 ``library'' functions to your generated code. The first
   185 integer or a string. The Java byte code will need two separate functions for this.
   201 command even needs two versions, because you need to write out
   186 For writing out an integer, you can use the assembler code
   202 an integer and string. The Java byte code will need two
       
   203 separate functions for this. For writing out an integer, you
       
   204 can use the assembler code
   187 
   205 
   188 \begin{center}
   206 \begin{center}
   189 \begin{minipage}{12cm}
   207 \begin{minipage}{12cm}
   190 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
   208 \begin{lstlisting}[language=JVMIS, numbers=none]
   191 .method public static write(I)V 
   209 .method public static write(I)V 
   192     .limit locals 5 
   210     .limit locals 5 
   193     .limit stack 5 
   211     .limit stack 5 
   194     iload 0 
   212     iload 0 
   195     getstatic java/lang/System/out Ljava/io/PrintStream; 
   213     getstatic java/lang/System/out Ljava/io/PrintStream; 
   205 This function will invoke Java's \texttt{println} function for integers. Then if you need
   223 This function will invoke Java's \texttt{println} function for integers. Then if you need
   206 to generate code for \texttt{write x} where \texttt{x} is an integer variable, you can generate
   224 to generate code for \texttt{write x} where \texttt{x} is an integer variable, you can generate
   207 
   225 
   208 \begin{center}
   226 \begin{center}
   209 \begin{minipage}{8cm}
   227 \begin{minipage}{8cm}
   210 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
   228 \begin{lstlisting}[language=JVMIS, numbers=none]
   211 iload n 
   229 iload n 
   212 invokestatic XXX/XXX/write(I)V
   230 invokestatic XXX/XXX/write(I)V
   213 \end{lstlisting}
   231 \end{lstlisting}
   214 \end{minipage}
   232 \end{minipage}
   215 \end{center}
   233 \end{center}
   223 Writing out a string is similar. The corresponding library function uses strings 
   241 Writing out a string is similar. The corresponding library function uses strings 
   224 instead of integers:
   242 instead of integers:
   225 
   243 
   226 \begin{center}
   244 \begin{center}
   227 \begin{minipage}{12cm}
   245 \begin{minipage}{12cm}
   228 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
   246 \begin{lstlisting}[language=JVMIS, numbers=none]
   229 .method public static writes(Ljava/lang/String;)V
   247 .method public static writes(Ljava/lang/String;)V
   230    .limit stack 2
   248    .limit stack 2
   231    .limit locals 2
   249    .limit locals 2
   232    getstatic java/lang/System/out Ljava/io/PrintStream;
   250    getstatic java/lang/System/out Ljava/io/PrintStream;
   233    aload 0
   251    aload 0
   237 \end{lstlisting}
   255 \end{lstlisting}
   238 \end{minipage}
   256 \end{minipage}
   239 \end{center}
   257 \end{center}
   240 
   258 
   241 \noindent
   259 \noindent
   242 The code that needs to be generated for \texttt{write "some\_string"} commands 
   260 The code that needs to be generated for \code{write "some_string"} commands 
   243 is
   261 is
   244 
   262 
   245 \begin{center}
   263 \begin{center}
   246 \begin{minipage}{8cm}
   264 \begin{minipage}{8cm}
   247 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
   265 \begin{lstlisting}[language=JVMIS,numbers=none]
   248 ldc "some_string"
   266 ldc "some_string"
   249 invokestatic XXX/XXX/writes(Ljava/lang/String;)V
   267 invokestatic XXX/XXX/writes(Ljava/lang/String;)V
   250 \end{lstlisting}
   268 \end{lstlisting}
   251 \end{minipage}
   269 \end{minipage}
   252 \end{center}
   270 \end{center}
   258 will need to be transformed into an integer. The code in Figure~\ref{read} does this.
   276 will need to be transformed into an integer. The code in Figure~\ref{read} does this.
   259 It can be called with
   277 It can be called with
   260 
   278 
   261 \begin{center}
   279 \begin{center}
   262 \begin{minipage}{8cm}
   280 \begin{minipage}{8cm}
   263 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
   281 \begin{lstlisting}[language=JVMIS,numbers=none]
   264 invokestatic XXX/XXX/read()I 
   282 invokestatic XXX/XXX/read()I 
   265 istore n
   283 istore n
   266 \end{lstlisting}
   284 \end{lstlisting}
   267 \end{minipage}
   285 \end{minipage}
   268 \end{center}
   286 \end{center}
   270 \noindent 
   288 \noindent 
   271 where \texttt{n} is the index of the variable that requires an input.
   289 where \texttt{n} is the index of the variable that requires an input.
   272 
   290 
   273 
   291 
   274 \begin{figure}[p]\small
   292 \begin{figure}[p]\small
   275 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
   293 \begin{lstlisting}[language=JVMIS,numbers=none]
   276 .method public static read()I 
   294 .method public static read()I 
   277       .limit locals 10 
   295       .limit locals 10 
   278       .limit stack 10
   296       .limit stack 10
   279 
   297 
   280       ldc 0 
   298       ldc 0