# HG changeset patch # User Christian Urban # Date 1385643061 0 # Node ID 33175abd54742635e4f069eee4049c4a37880d3b # Parent ad9b08267fa4cd20307e41a270fe9ad785faadb2 added diff -r ad9b08267fa4 -r 33175abd5474 coursework/cw03.pdf Binary file coursework/cw03.pdf has changed diff -r ad9b08267fa4 -r 33175abd5474 coursework/cw03.tex --- a/coursework/cw03.tex Wed Nov 27 21:36:52 2013 +0000 +++ b/coursework/cw03.tex Thu Nov 28 12:51:01 2013 +0000 @@ -95,7 +95,7 @@ \end{center} \noindent -in order to translate it to Java Byte Code. The resulting class file can be +in order to translate it to Java byte code. The resulting class file can be run with \begin{center} @@ -133,7 +133,7 @@ \noindent The intended meaning is to first assign the variable \textit{Id} the value of the first arithmetic expression, then go through the loop, at the end increase the value of the variable by 1, -and finally test wether the value is not less or equal anymore to the value of the second +and finally test wether the value is not less or equal to the value of the second arithmetic expression. For example the following instance of a \texttt{for}-loop is supposed to print out the numbers \texttt{2}, \texttt{3}, \texttt{4}. @@ -191,9 +191,9 @@ (therefore you need to download the additional package Jasmin---see above). But it does contain a disassembler, called \texttt{javap}. A dissembler does the ``opposite'' of an assembler: it -generates readable assembler code from Java Byte Code. Have a look at the -following example. Compile using the usual Java compiler, -\texttt{java}, the simple Hello World program below: +generates readable assembler code from Java byte code. Have a look at the +following example. Compile using the usual Java compiler, the simple Hello World +program below: \begin{center} \begin{minipage}{10cm} @@ -215,9 +215,9 @@ \end{center} \noindent -in order to see which Java Byte Code has been generated for this +in order to see the assembler instructions of the Java byte code that has been generated for this program. You can compare this with the code generated for the Scala -version of Hello Worlds. +version of Hello World. \begin{center} \begin{minipage}{10cm} @@ -231,6 +231,81 @@ \end{minipage} \end{center} +\subsection*{Library Functions} + +You need to generate code for the instruction \texttt{write} and \texttt{read}. This +will require to add some ``library'' functions to your generated code. The first +command even needs two versions, because you might want to write out an +integer or a string. The Java byte code will need two separate functions for this. +For writing out an integer, you can use the code + +\begin{center} +\begin{minipage}{12cm} +\begin{lstlisting}[basicstyle=\ttfamily, numbers=none] +.method public static write(I)V + .limit locals 5 + .limit stack 5 + iload 0 + getstatic java/lang/System/out Ljava/io/PrintStream; + swap + invokevirtual java/io/PrintStream/println(I)V + return +.end method +\end{lstlisting} +\end{minipage} +\end{center} + +\noindent +This function will invoke Java's \texttt{println} function for integers. Then if you need +to generate code for \texttt{write x} where \texttt{x} is an integer variable, you can generate + +\begin{center} +\begin{minipage}{8cm} +\begin{lstlisting}[basicstyle=\ttfamily, numbers=none] +iload n +invokestatic XXX/XXX/write(I)V +\end{lstlisting} +\end{minipage} +\end{center} + +\noindent +where \texttt{n} is the index where the value of the variable \texttt{x} is +stored. The \texttt{XXX/XXX} needs to be replaced with the class name +which you use to generate the code (for example \texttt{fib/fib} in case +of the Fibonacci numbers). + +Writing out a string is similar. The corresponding library function is + +\begin{center} +\begin{minipage}{12cm} +\begin{lstlisting}[basicstyle=\ttfamily, numbers=none] +.method public static writes(Ljava/lang/String;)V + .limit stack 2 + .limit locals 2 + getstatic java/lang/System/out Ljava/io/PrintStream; + aload 0 + invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V + return +.end method +\end{lstlisting} +\end{minipage} +\end{center} + +\noindent +and the code that needs to be generated for \texttt{write "some\_string"} commands +is + +\begin{center} +\begin{minipage}{8cm} +\begin{lstlisting}[basicstyle=\ttfamily, numbers=none] +ldc "some_string" +invokestatic XXX/XXX/writes(Ljava/lang/String;)V +\end{lstlisting} +\end{minipage} +\end{center} + +\noindent +Again you need to adjust the \texttt{XXX/XXX} part in each call. \end{document}