cws/cw04.tex
changeset 951 9e7147f33219
parent 943 5365ef60707e
child 952 33b3e790e1d4
equal deleted inserted replaced
950:fa97d2f60f11 951:9e7147f33219
    39 shenanigans like this!
    39 shenanigans like this!
    40 
    40 
    41 
    41 
    42 \subsection*{Jasmin Assembler}
    42 \subsection*{Jasmin Assembler}
    43 
    43 
       
    44 For this coursework you will need an assembler. 
    44 The Jasmin assembler is available from
    45 The Jasmin assembler is available from
    45 
    46 
    46 \begin{center}
    47 \begin{center}
    47 \url{http://jasmin.sourceforge.net}
    48 \url{http://jasmin.sourceforge.net}
    48 \end{center}
    49 \end{center}
    49 
    50 
    50 \noindent
    51 \noindent
       
    52 This is a jar-file you can run on the commandline.
    51 There is a user guide for Jasmin
    53 There is a user guide for Jasmin
    52 
    54 
    53 \begin{center}
    55 \begin{center}
    54 \url{http://jasmin.sourceforge.net/guide.html}
    56 \url{http://jasmin.sourceforge.net/guide.html}
    55 \end{center}
    57 \end{center}
    63 
    65 
    64 \noindent If you generated a correct assembler file for
    66 \noindent If you generated a correct assembler file for
    65 Jasmin, for example \texttt{loops.j}, you can use
    67 Jasmin, for example \texttt{loops.j}, you can use
    66 
    68 
    67 \begin{center}
    69 \begin{center}
    68 \texttt{java -jar jasmin-2.4/jasmin.jar loops.j}
    70 \texttt{java -jar jasmin.jar loops.j}
    69 \end{center}
    71 \end{center}
    70 
    72 
    71 \noindent in order to translate it into Java Byte Code. If needed, you
    73 \noindent in order to translate it into Java Byte Code. If needed, you
    72 need to give the path to the Jasmin jar-file.  The resulting class
    74 need to give the path to the Jasmin jar-file.  The resulting class
    73 file can be run with
    75 file can be run with
    94 
    96 
    95 \begin{center}
    97 \begin{center}
    96   \small\url{http://www.csc.villanova.edu/~tway/courses/csc4181/s2022/labs/finalproject/JVM.pdf}
    98   \small\url{http://www.csc.villanova.edu/~tway/courses/csc4181/s2022/labs/finalproject/JVM.pdf}
    97 \end{center}
    99 \end{center}
    98 
   100 
       
   101 \noindent
       
   102 If possible use Jasmin for the coursework. The Krakatau assembler
       
   103 below as a slightly different syntax.
       
   104 
       
   105 
    99 \subsection*{Krakatau Assembler}
   106 \subsection*{Krakatau Assembler}
   100 
   107 
   101 The Krakatau assembler is available from
   108 The Krakatau assembler is available from
   102 
   109 
   103 \begin{center}
   110 \begin{center}
   104 \url{https://github.com/Storyyeller/Krakatau}
   111   \url{https://github.com/Storyyeller/Krakatau/tree/master}
   105 \end{center}
   112 \end{center}
   106 
   113 
   107 \noindent This assembler requires Python and a package called
   114 \noindent This assembler requires Python and a package called
   108 \pcode{ply} available from
   115 \pcode{ply} available from
   109 
   116 
   124 
   131 
   125 \noindent where you may have to adapt the directory where
   132 \noindent where you may have to adapt the directory where
   126 Krakatau is installed (I just downloaded the zip file from
   133 Krakatau is installed (I just downloaded the zip file from
   127 Github and \pcode{Krakatau-master} was the directory where it
   134 Github and \pcode{Krakatau-master} was the directory where it
   128 was installed). Again the resulting class-file you can run with
   135 was installed). Again the resulting class-file you can run with
   129 \texttt{java}.
   136 \texttt{java}. There is also a newer version of Krakatau available at
       
   137 
       
   138 \begin{center}
       
   139 \url{https://github.com/Storyyeller/Krakatau/tree/v2}
       
   140 \end{center}
       
   141 
       
   142 \noindent
       
   143 This is now a Rust program using Cargo as package manager (I have not tried this
       
   144 version---I assume it should produce the same output, but might be
       
   145 easier to install because it avoids Python's \emph{dependency hell}).
   130 
   146 
   131 
   147 
   132 %\noindent You need to submit a document containing the answers
   148 %\noindent You need to submit a document containing the answers
   133 %for the two questions below. You can do the implementation in
   149 %for the two questions below. You can do the implementation in
   134 %any programming language you like, but you need to submit the
   150 %any programming language you like, but you need to submit the
   171 \code{for} \;\textit{Id} \texttt{:=} \textit{AExp}\; \code{upto} 
   187 \code{for} \;\textit{Id} \texttt{:=} \textit{AExp}\; \code{upto} 
   172 \;\textit{AExp}\; \code{do} \textit{Block} 
   188 \;\textit{AExp}\; \code{do} \textit{Block} 
   173 \end{center}
   189 \end{center}
   174 
   190 
   175 \noindent The intended meaning is to first assign the variable
   191 \noindent The intended meaning is to first assign the variable
   176 \textit{Id} the value of the first arithmetic expression, test
   192 \textit{Id} the value of the first arithmetic expression, then test
   177 whether this value is less or equal than the value of the
   193 whether this value is less or equal than the value of the
   178 second arithmetic expression. If yes, go through the loop, and
   194 second arithmetic expression. If yes, go through the loop, and
   179 at the end increase the value of the loop variable by 1 and
   195 at the end increase the value of the loop variable by 1 and
   180 start again with the test. If no, leave the loop. For example
   196 start again with the test. If no, leave the loop. For example
   181 the following instance of a \code{for}-loop is supposed to
   197 the following instance of a \code{for}-loop is supposed to
   265 \end{lstlisting}
   281 \end{lstlisting}
   266 \end{minipage}
   282 \end{minipage}
   267 \end{center}
   283 \end{center}
   268 
   284 
   269 \noindent
   285 \noindent
   270 should print out 1 to 10 with the first for-loop, but only 1
   286 should print out 0 to 10 with the first for-loop, but only 0
   271 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"},
   272 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
   273 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-loop and also to the end of the
   274 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
   275 \code{break}.
   291 \code{break}.
   302 You can use the command
   318 You can use the command
   303 
   319 
   304 \begin{center}
   320 \begin{center}
   305 \begin{minipage}{12cm}
   321 \begin{minipage}{12cm}
   306 \begin{lstlisting}[language={},numbers=none]
   322 \begin{lstlisting}[language={},numbers=none]
   307 javap -v HelloWorld
   323 javap -c HelloWorld
   308 \end{lstlisting}
   324 \end{lstlisting}
   309 \end{minipage}
   325 \end{minipage}
   310 \end{center}
   326 \end{center}
   311 
   327 
   312 \noindent to see the assembler instructions of the Java Byte
   328 \noindent to see the assembler instructions of the Java Byte
   316 
   332 
   317 \begin{center}
   333 \begin{center}
   318 \begin{minipage}{12cm}
   334 \begin{minipage}{12cm}
   319 \begin{lstlisting}[language=Scala,numbers=none]
   335 \begin{lstlisting}[language=Scala,numbers=none]
   320 object HelloWorld {
   336 object HelloWorld {
   321    def main(args: Array[String]) {
   337    def main(args: Array[String]) = {
   322       println("Hello World!")
   338       println("Hello World!")
   323   }
   339   }
   324 }
   340 }
   325 \end{lstlisting}
   341 \end{lstlisting}
   326 \end{minipage}
   342 \end{minipage}
   418 \end{lstlisting}
   434 \end{lstlisting}
   419 \end{minipage}
   435 \end{minipage}
   420 \end{center}
   436 \end{center}
   421 
   437 
   422 \noindent 
   438 \noindent 
   423 where \texttt{n} is the index of the variable that requires an input. If you
   439 where \texttt{n} is the index of the variable that requires an input.
   424 use Windows you need to take into account that a ``return'' is not just a newline,
   440 %If you
   425 \code{'\\10'}, but \code{'\\13\\10'}. This means you need to change line~12 in 
   441 %use Windows you need to take into account that a ``return'' is not just a newline,
   426 Figure~\ref{read} to \pcode{ldc 13}.  
   442 %\code{'\\10'}, but \code{'\\13\\10'}. This means you need to change line~12 in 
       
   443 %Figure~\ref{read} to \pcode{ldc 13}.  
   427 
   444 
   428 
   445 
   429 \begin{figure}[t]\small
   446 \begin{figure}[t]\small
   430 \begin{lstlisting}[language=JVMIS,numbers=left]
   447 \begin{lstlisting}[language=JVMIS,numbers=left]
   431 .method public static read()I 
   448 .method public static read()I 
   437 Label1: 
   454 Label1: 
   438     getstatic java/lang/System/in Ljava/io/InputStream; 
   455     getstatic java/lang/System/in Ljava/io/InputStream; 
   439     invokevirtual java/io/InputStream/read()I 
   456     invokevirtual java/io/InputStream/read()I 
   440     istore 2 
   457     istore 2 
   441     iload 2 
   458     iload 2 
   442     ldc 10  ; the newline delimiter for Unix (Windows 13)
   459     ldc 10  ; test for the newline delimiter for Unix
   443     isub 
   460     isub 
   444     ifeq Label2 
   461     ifeq Label2
       
   462     iload 2
       
   463     ldc 13  ; test for the carriage-return in Windows 
       
   464     isub
       
   465     ifeq Label2
   445     iload 2 
   466     iload 2 
   446     ldc 32   ; the space delimiter 
   467     ldc 32   ; the space delimiter 
   447     isub 
   468     isub 
   448     ifeq Label2
   469     ifeq Label2
   449     iload 2 
   470     iload 2 
   460     ; in local variable 1 
   481     ; in local variable 1 
   461     iload 1 
   482     iload 1 
   462     ireturn 
   483     ireturn 
   463 .end method
   484 .end method
   464 \end{lstlisting}\normalsize
   485 \end{lstlisting}\normalsize
   465 \caption{Assembler code for reading an integer from the console.\label{read}}
   486 \caption{Assembler code for reading an integer from the console.
       
   487   This code is portable for Unix and Windows (see Lines 11--18 for 2 separate
       
   488   tests for the various end-of-line markers). Thanks to Harry
       
   489   Dilnot to make it portable.\label{read}}
   466 \end{figure}
   490 \end{figure}
   467 
   491 
   468 \end{document}
   492 \end{document}
   469 
   493 
   470 %%% Local Variables: 
   494 %%% Local Variables: