coursework/cw03.tex
changeset 214 5be68de225e9
parent 213 c41f193ffaf6
child 216 f5ec7c597c5b
equal deleted inserted replaced
213:c41f193ffaf6 214:5be68de225e9
   101 \begin{center}
   101 \begin{center}
   102 \texttt{java loops}
   102 \texttt{java loops}
   103 \end{center}
   103 \end{center}
   104 
   104 
   105 \noindent
   105 \noindent
   106 where you potentially need to give the path to the class file.\bigskip
   106 where you might need to give the correct path to the class file.\bigskip
   107 
   107 
   108 \noindent
   108 \noindent
   109 You need to submit a document containing the answers for the two questions 
   109 You need to submit a document containing the answers for the two questions 
   110 below. You can do the implementation in any programming language you like, but you need 
   110 below. You can do the implementation in any programming language you like, but you need 
   111 to submit the source code with which you answered the questions. Otherwise
   111 to submit the source code with which you answered the questions. Otherwise
   167 \end{minipage}
   167 \end{minipage}
   168 \end{center}
   168 \end{center}
   169 
   169 
   170 \noindent
   170 \noindent
   171 In this question you are supposed to give the assembler instructions for the
   171 In this question you are supposed to give the assembler instructions for the
   172 for the program
   172 program
   173 
   173 
   174 \begin{center}
   174 \begin{center}
   175 \begin{minipage}{6cm}
   175 \begin{minipage}{6cm}
   176 \begin{lstlisting}[language=While,basicstyle=\ttfamily, numbers=none]
   176 \begin{lstlisting}[language=While,basicstyle=\ttfamily, numbers=none]
   177 for i := 1 upto 10000 do {
   177 for i := 1 upto 10000 do {
   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, the simple Hello World 
   195 following example: Compile using the usual Java compiler the simple Hello World 
   196 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]
   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 the assembler instructions of the Java byte code that has been generated for this
   218 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 World.
   220 version of Hello World.
   221 
   221 
   222 \begin{center}
   222 \begin{center}
   223 \begin{minipage}{10cm}
   223 \begin{minipage}{10cm}
   231 \end{minipage}
   231 \end{minipage}
   232 \end{center}
   232 \end{center}
   233 
   233 
   234 \subsection*{Library Functions}
   234 \subsection*{Library Functions}
   235 
   235 
   236 You need to generate code for the instruction \texttt{write} and \texttt{read}. This
   236 You need to generate code for the commands \texttt{write} and \texttt{read}. This
   237 will require to add some ``library'' functions to your generated code. The first
   237 will require the addition of some ``library'' functions to your generated code. The first
   238 command even needs two versions, because you might want to write out an
   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.
   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
   240 For writing out an integer, you can use the assembler code
   241 
   241 
   242 \begin{center}
   242 \begin{center}
   243 \begin{minipage}{12cm}
   243 \begin{minipage}{12cm}
   244 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
   244 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
   245 .method public static write(I)V 
   245 .method public static write(I)V 
   272 where \texttt{n} is the index where the value of the variable \texttt{x} is
   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 
   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
   274 which you use to generate the code (for example \texttt{fib/fib} in case
   275 of the Fibonacci numbers).
   275 of the Fibonacci numbers).
   276 
   276 
   277 Writing out a string is similar. The corresponding library function is
   277 Writing out a string is similar. The corresponding library function uses strings 
       
   278 instead of integers:
   278 
   279 
   279 \begin{center}
   280 \begin{center}
   280 \begin{minipage}{12cm}
   281 \begin{minipage}{12cm}
   281 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
   282 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
   282 .method public static writes(Ljava/lang/String;)V
   283 .method public static writes(Ljava/lang/String;)V
   290 \end{lstlisting}
   291 \end{lstlisting}
   291 \end{minipage}
   292 \end{minipage}
   292 \end{center}
   293 \end{center}
   293 
   294 
   294 \noindent
   295 \noindent
   295 and the code that needs to be generated for \texttt{write "some\_string"} commands 
   296 The code that needs to be generated for \texttt{write "some\_string"} commands 
   296 is
   297 is
   297 
   298 
   298 \begin{center}
   299 \begin{center}
   299 \begin{minipage}{8cm}
   300 \begin{minipage}{8cm}
   300 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
   301 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
   304 \end{minipage}
   305 \end{minipage}
   305 \end{center}
   306 \end{center}
   306 
   307 
   307 \noindent
   308 \noindent
   308 Again you need to adjust the \texttt{XXX/XXX} part in each call.
   309 Again you need to adjust the \texttt{XXX/XXX} part in each call.
       
   310 
       
   311 The code for \texttt{read} is more complicated. The reason is that inputting a string
       
   312 will need to be transformed into an integer. The code in Figure~\ref{read} does this.
       
   313 It can be called with
       
   314 
       
   315 \begin{center}
       
   316 \begin{minipage}{8cm}
       
   317 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
       
   318 invokestatic XXX/XXX/read()I 
       
   319 istore n
       
   320 \end{lstlisting}
       
   321 \end{minipage}
       
   322 \end{center}
       
   323 
       
   324 \noindent 
       
   325 where \texttt{n} is the index of the variable that requires an input.
       
   326 
       
   327 
       
   328 \begin{figure}[p]\small
       
   329 \begin{lstlisting}[basicstyle=\ttfamily, numbers=none]
       
   330 .method public static read()I 
       
   331       .limit locals 10 
       
   332       .limit stack 10
       
   333 
       
   334       ldc 0 
       
   335       istore 1  ; this will hold our final integer 
       
   336 Label1: 
       
   337       getstatic java/lang/System/in Ljava/io/InputStream; 
       
   338       invokevirtual java/io/InputStream/read()I 
       
   339       istore 2 
       
   340       iload 2 
       
   341       ldc 10   ; the newline delimiter 
       
   342       isub 
       
   343       ifeq Label2 
       
   344       iload 2 
       
   345       ldc 32   ; the space delimiter 
       
   346       isub 
       
   347       ifeq Label2
       
   348 
       
   349       iload 2 
       
   350       ldc 48   ; we have our digit in ASCII, have to subtract it from 48 
       
   351       isub 
       
   352       ldc 10 
       
   353       iload 1 
       
   354       imul 
       
   355       iadd 
       
   356       istore 1 
       
   357       goto Label1 
       
   358 Label2: 
       
   359       ;when we come here we have our integer computed in Local Variable 1 
       
   360       iload 1 
       
   361       ireturn 
       
   362 .end method
       
   363 \end{lstlisting}\normalsize
       
   364 \caption{Assembler code for reading an integer from the console.\label{read}}
       
   365 \end{figure}
   309 
   366 
   310 \end{document}
   367 \end{document}
   311 
   368 
   312 %%% Local Variables: 
   369 %%% Local Variables: 
   313 %%% mode: latex
   370 %%% mode: latex