coursework/cw03.tex
changeset 210 33175abd5474
parent 205 0b59588d28d2
child 213 c41f193ffaf6
equal deleted inserted replaced
209:ad9b08267fa4 210:33175abd5474
    93 \begin{center}
    93 \begin{center}
    94 \texttt{java -jar jasmin-2.4/jasmin.jar loops.j}
    94 \texttt{java -jar jasmin-2.4/jasmin.jar loops.j}
    95 \end{center}
    95 \end{center}
    96 
    96 
    97 \noindent
    97 \noindent
    98 in order to translate it to Java Byte Code. The resulting class file can be
    98 in order to translate it to Java byte code. The resulting class file can be
    99 run with
    99 run with
   100 
   100 
   101 \begin{center}
   101 \begin{center}
   102 \texttt{java loops}
   102 \texttt{java loops}
   103 \end{center}
   103 \end{center}
   131 \end{center}
   131 \end{center}
   132 
   132 
   133 \noindent
   133 \noindent
   134 The intended meaning is to first assign the variable \textit{Id} the value of the first arithmetic 
   134 The intended meaning is to first assign the variable \textit{Id} the value of the first arithmetic 
   135 expression, then go through the loop, at the end increase the value of the variable by 1, 
   135 expression, then go through the loop, at the end increase the value of the variable by 1, 
   136 and finally test wether the value is not less or equal anymore to the value of the second
   136 and finally test wether the value is not less or equal to the value of the second
   137 arithmetic expression. For example the following instance of a \texttt{for}-loop 
   137 arithmetic expression. For example the following instance of a \texttt{for}-loop 
   138 is supposed to print out the numbers \texttt{2}, \texttt{3}, \texttt{4}.
   138 is supposed to print out the numbers \texttt{2}, \texttt{3}, \texttt{4}.
   139 
   139 
   140 
   140 
   141 \begin{center}
   141 \begin{center}
   189 
   189 
   190 The Java infrastructure unfortunately does not contain an assembler out-of-the-box
   190 The Java infrastructure unfortunately does not contain an assembler out-of-the-box
   191 (therefore
   191 (therefore
   192 you need to download the additional package Jasmin---see above). But it does contain a 
   192 you need to download the additional package Jasmin---see above). But it does contain a 
   193 disassembler, called \texttt{javap}. A dissembler does the ``opposite'' of an assembler: it
   193 disassembler, called \texttt{javap}. A dissembler does the ``opposite'' of an assembler: it
   194 generates readable assembler code from Java Byte Code. Have a look at the
   194 generates readable assembler code from Java byte code. Have a look at the
   195 following example. Compile using the usual Java compiler, 
   195 following example. Compile using the usual Java compiler, the simple Hello World 
   196 \texttt{java}, the simple Hello World program below:
   196 program below:
   197 
   197 
   198 \begin{center}
   198 \begin{center}
   199 \begin{minipage}{10cm}
   199 \begin{minipage}{10cm}
   200 \begin{lstlisting}[language=Java,basicstyle=\ttfamily]
   200 \begin{lstlisting}[language=Java,basicstyle=\ttfamily]
   201 class HelloWorld {
   201 class HelloWorld {
   213 \begin{center}
   213 \begin{center}
   214 \texttt{javap -v HelloWorld}
   214 \texttt{javap -v HelloWorld}
   215 \end{center}
   215 \end{center}
   216 
   216 
   217 \noindent
   217 \noindent
   218 in order to see which Java Byte Code has been generated for this
   218 in order to see the assembler instructions of the Java byte code that has been generated for this
   219 program. You can compare this with the code generated for the Scala
   219 program. You can compare this with the code generated for the Scala
   220 version of Hello Worlds.
   220 version of Hello World.
   221 
   221 
   222 \begin{center}
   222 \begin{center}
   223 \begin{minipage}{10cm}
   223 \begin{minipage}{10cm}
   224 \begin{lstlisting}[language=Scala,basicstyle=\ttfamily]
   224 \begin{lstlisting}[language=Scala,basicstyle=\ttfamily]
   225 object HelloWorld {
   225 object HelloWorld {
   229 }
   229 }
   230 \end{lstlisting}
   230 \end{lstlisting}
   231 \end{minipage}
   231 \end{minipage}
   232 \end{center}
   232 \end{center}
   233 
   233 
       
   234 \subsection*{Library Functions}
       
   235 
       
   236 You need to generate code for the instruction \texttt{write} and \texttt{read}. This
       
   237 will require to add some ``library'' functions to your generated code. The first
       
   238 command even needs two versions, because you might want to write out an
       
   239 integer or a string. The Java byte code will need two separate functions for this.
       
   240 For writing out an integer, you can use the code
       
   241 
       
   242 \begin{center}
       
   243 \begin{minipage}{12cm}
       
   244 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
       
   245 .method public static write(I)V 
       
   246     .limit locals 5 
       
   247     .limit stack 5 
       
   248     iload 0 
       
   249     getstatic java/lang/System/out Ljava/io/PrintStream; 
       
   250     swap 
       
   251     invokevirtual java/io/PrintStream/println(I)V 
       
   252     return 
       
   253 .end method
       
   254 \end{lstlisting}
       
   255 \end{minipage}
       
   256 \end{center}
       
   257 
       
   258 \noindent 
       
   259 This function will invoke Java's \texttt{println} function for integers. Then if you need
       
   260 to generate code for \texttt{write x} where \texttt{x} is an integer variable, you can generate
       
   261 
       
   262 \begin{center}
       
   263 \begin{minipage}{8cm}
       
   264 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
       
   265 iload n 
       
   266 invokestatic XXX/XXX/write(I)V
       
   267 \end{lstlisting}
       
   268 \end{minipage}
       
   269 \end{center}
       
   270 
       
   271 \noindent
       
   272 where \texttt{n} is the index where the value of the variable \texttt{x} is
       
   273 stored. The \texttt{XXX/XXX} needs to be replaced with the class name 
       
   274 which you use to generate the code (for example \texttt{fib/fib} in case
       
   275 of the Fibonacci numbers).
       
   276 
       
   277 Writing out a string is similar. The corresponding library function is
       
   278 
       
   279 \begin{center}
       
   280 \begin{minipage}{12cm}
       
   281 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
       
   282 .method public static writes(Ljava/lang/String;)V
       
   283    .limit stack 2
       
   284    .limit locals 2
       
   285    getstatic java/lang/System/out Ljava/io/PrintStream;
       
   286    aload 0
       
   287    invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
       
   288    return
       
   289 .end method
       
   290 \end{lstlisting}
       
   291 \end{minipage}
       
   292 \end{center}
       
   293 
       
   294 \noindent
       
   295 and the code that needs to be generated for \texttt{write "some\_string"} commands 
       
   296 is
       
   297 
       
   298 \begin{center}
       
   299 \begin{minipage}{8cm}
       
   300 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
       
   301 ldc "some_string"
       
   302 invokestatic XXX/XXX/writes(Ljava/lang/String;)V
       
   303 \end{lstlisting}
       
   304 \end{minipage}
       
   305 \end{center}
       
   306 
       
   307 \noindent
       
   308 Again you need to adjust the \texttt{XXX/XXX} part in each call.
   234 
   309 
   235 \end{document}
   310 \end{document}
   236 
   311 
   237 %%% Local Variables: 
   312 %%% Local Variables: 
   238 %%% mode: latex
   313 %%% mode: latex