
diff -r c41f193ffaf6 -r 5be68de225e9 coursework/cw03.tex
--- a/coursework/cw03.tex	Fri Nov 29 23:16:18 2013 +0000
+++ b/coursework/cw03.tex	Sat Nov 30 00:06:02 2013 +0000
@@ -103,7 +103,7 @@
 \end{center}
 
 \noindent
-where you potentially need to give the path to the class file.\bigskip
+where you might need to give the correct path to the class file.\bigskip
 
 \noindent
 You need to submit a document containing the answers for the two questions 
@@ -169,7 +169,7 @@
 
 \noindent
 In this question you are supposed to give the assembler instructions for the
-for the program
+program
 
 \begin{center}
 \begin{minipage}{6cm}
@@ -192,7 +192,7 @@
 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, the simple Hello World 
+following example: Compile using the usual Java compiler the simple Hello World 
 program below:
 
 \begin{center}
@@ -215,7 +215,7 @@
 \end{center}
 
 \noindent
-in order to see the assembler instructions of the Java byte code that has been generated for this
+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 World.
 
@@ -233,11 +233,11 @@
 
 \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
+You need to generate code for the commands \texttt{write} and \texttt{read}. This
+will require the addition of 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
+For writing out an integer, you can use the assembler code
 
 \begin{center}
 \begin{minipage}{12cm}
@@ -274,7 +274,8 @@
 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
+Writing out a string is similar. The corresponding library function uses strings 
+instead of integers:
 
 \begin{center}
 \begin{minipage}{12cm}
@@ -292,7 +293,7 @@
 \end{center}
 
 \noindent
-and the code that needs to be generated for \texttt{write "some\_string"} commands 
+The code that needs to be generated for \texttt{write "some\_string"} commands 
 is
 
 \begin{center}
@@ -307,6 +308,62 @@
 \noindent
 Again you need to adjust the \texttt{XXX/XXX} part in each call.
 
+The code for \texttt{read} is more complicated. The reason is that inputting a string
+will need to be transformed into an integer. The code in Figure~\ref{read} does this.
+It can be called with
+
+\begin{center}
+\begin{minipage}{8cm}
+\begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
+invokestatic XXX/XXX/read()I 
+istore n
+\end{lstlisting}
+\end{minipage}
+\end{center}
+
+\noindent 
+where \texttt{n} is the index of the variable that requires an input.
+
+
+\begin{figure}[p]\small
+\begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
+.method public static read()I 
+      .limit locals 10 
+      .limit stack 10
+
+      ldc 0 
+      istore 1  ; this will hold our final integer 
+Label1: 
+      getstatic java/lang/System/in Ljava/io/InputStream; 
+      invokevirtual java/io/InputStream/read()I 
+      istore 2 
+      iload 2 
+      ldc 10   ; the newline delimiter 
+      isub 
+      ifeq Label2 
+      iload 2 
+      ldc 32   ; the space delimiter 
+      isub 
+      ifeq Label2
+
+      iload 2 
+      ldc 48   ; we have our digit in ASCII, have to subtract it from 48 
+      isub 
+      ldc 10 
+      iload 1 
+      imul 
+      iadd 
+      istore 1 
+      goto Label1 
+Label2: 
+      ;when we come here we have our integer computed in Local Variable 1 
+      iload 1 
+      ireturn 
+.end method
+\end{lstlisting}\normalsize
+\caption{Assembler code for reading an integer from the console.\label{read}}
+\end{figure}
+
 \end{document}
 
 %%% Local Variables: 




