coursework/cw04.tex
changeset 321 c5850f8f3f5e
parent 320 0c43ef11e086
child 322 698ed1c96cd0
equal deleted inserted replaced
320:0c43ef11e086 321:c5850f8f3f5e
    66 in a txt-file or as pdf.\bigskip
    66 in a txt-file or as pdf.\bigskip
    67 
    67 
    68 
    68 
    69 \subsection*{Question 1 (marked with 5\%)}
    69 \subsection*{Question 1 (marked with 5\%)}
    70 
    70 
    71 You need to lex and parse WHILE programs, and then generate
    71 You need to lex and parse WHILE programs, and then generate Java Byte
    72 Java Byte Code instructions for the Jasmin assmebler. As
    72 Code instructions for the Jasmin assembler. As solution you need to
    73 solution you need to submit the assembler instructions for the
    73 submit the assembler instructions for the Fibonacci and Factorial
    74 Fibonacci and Factorial programs. Both should be so modified
    74 programs. Both should be so modified that a user can input on the
    75 that a user can input on the console which Fibonacci number 
    75 console which Fibonacci number and which Factorial should
    76 and which Factorial should calculated. The Fibonacci program
    76 calculated. The Fibonacci program is given in Figure~\ref{fibs}. You
    77 is  given in Figure~\ref{fibs}. You can write your own program
    77 can write your own program for calculating factorials.
    78 for calculating factorials.
       
    79 
    78 
    80 \begin{figure}[t]
    79 \begin{figure}[t]
    81 \lstinputlisting[language=while]{../progs/fib.while}
    80 \lstinputlisting[language=while]{../progs/fib.while}
    82 \caption{The Fibonacci program in the WHILE language.\label{fibs}}
    81 \caption{The Fibonacci program in the WHILE language.\label{fibs}}
    83 \end{figure}
    82 \end{figure}
   285 \end{lstlisting}
   284 \end{lstlisting}
   286 \end{minipage}
   285 \end{minipage}
   287 \end{center}
   286 \end{center}
   288 
   287 
   289 \noindent 
   288 \noindent 
   290 where \texttt{n} is the index of the variable that requires an input.
   289 where \texttt{n} is the index of the variable that requires an input. If you
   291 
   290 use Windows you need to take into account that a ``return'' is not just a newline,
   292 
   291 \code{'\\10'}, but \code{'\\13\\10'}. This means you need to change line~12 in 
   293 \begin{figure}[p]\small
   292 Figure~\ref{read} to \pcode{ldc 13}.  
   294 \begin{lstlisting}[language=JVMIS,numbers=none]
   293 
       
   294 
       
   295 \begin{figure}[t]\small
       
   296 \begin{lstlisting}[language=JVMIS]
   295 .method public static read()I 
   297 .method public static read()I 
   296       .limit locals 10 
   298       .limit locals 10 
   297       .limit stack 10
   299       .limit stack 10
   298 
   300 
   299       ldc 0 
   301       ldc 0 
   308       ifeq Label2 
   310       ifeq Label2 
   309       iload 2 
   311       iload 2 
   310       ldc 32   ; the space delimiter 
   312       ldc 32   ; the space delimiter 
   311       isub 
   313       isub 
   312       ifeq Label2
   314       ifeq Label2
   313 
       
   314       iload 2 
   315       iload 2 
   315       ldc 48   ; we have our digit in ASCII, have to subtract it from 48 
   316       ldc 48   ; we have our digit in ASCII, have to subtract it from 48 
   316       isub 
   317       isub 
   317       ldc 10 
   318       ldc 10 
   318       iload 1 
   319       iload 1