cws/cw04.tex
changeset 859 f766d3486c9d
parent 852 8706b846a3e0
child 877 43460c7b2010
equal deleted inserted replaced
858:13a6eb21706b 859:f766d3486c9d
   290 
   290 
   291 \begin{center}
   291 \begin{center}
   292 \begin{minipage}{12cm}
   292 \begin{minipage}{12cm}
   293 \begin{lstlisting}[language=JVMIS, numbers=none]
   293 \begin{lstlisting}[language=JVMIS, numbers=none]
   294 .method public static write(I)V 
   294 .method public static write(I)V 
   295     .limit locals 1 
   295   .limit locals 1 
   296     .limit stack 2  
   296   .limit stack 2  
   297     getstatic java/lang/System/out Ljava/io/PrintStream; 
   297   getstatic java/lang/System/out Ljava/io/PrintStream; 
   298     iload 0
   298   iload 0
   299     invokevirtual java/io/PrintStream/println(I)V 
   299   invokevirtual java/io/PrintStream/println(I)V 
   300     return 
   300   return 
   301 .end method
   301 .end method
   302 \end{lstlisting}
   302 \end{lstlisting}
   303 \end{minipage}
   303 \end{minipage}
   304 \end{center}
   304 \end{center}
   305 
   305 
   378 
   378 
   379 
   379 
   380 \begin{figure}[t]\small
   380 \begin{figure}[t]\small
   381 \begin{lstlisting}[language=JVMIS,numbers=left]
   381 \begin{lstlisting}[language=JVMIS,numbers=left]
   382 .method public static read()I 
   382 .method public static read()I 
   383       .limit locals 10 
   383     .limit locals 10 
   384       .limit stack 10
   384     .limit stack 10
   385 
   385 
   386       ldc 0 
   386     ldc 0 
   387       istore 1  ; this will hold our final integer 
   387     istore 1  ; this will hold our final integer 
   388 Label1: 
   388 Label1: 
   389       getstatic java/lang/System/in Ljava/io/InputStream; 
   389     getstatic java/lang/System/in Ljava/io/InputStream; 
   390       invokevirtual java/io/InputStream/read()I 
   390     invokevirtual java/io/InputStream/read()I 
   391       istore 2 
   391     istore 2 
   392       iload 2 
   392     iload 2 
   393       ldc 10  ; the newline delimiter for Unix (Windows 13)
   393     ldc 10  ; the newline delimiter for Unix (Windows 13)
   394       isub 
   394     isub 
   395       ifeq Label2 
   395     ifeq Label2 
   396       iload 2 
   396     iload 2 
   397       ldc 32   ; the space delimiter 
   397     ldc 32   ; the space delimiter 
   398       isub 
   398     isub 
   399       ifeq Label2
   399     ifeq Label2
   400       iload 2 
   400     iload 2 
   401       ldc 48   ; we have our digit in ASCII, have to subtract it from 48 
   401     ldc 48   ; we have our digit in ASCII, have to subtract it from 48 
   402       isub 
   402     isub 
   403       ldc 10 
   403     ldc 10 
   404       iload 1 
   404     iload 1 
   405       imul 
   405     imul 
   406       iadd 
   406     iadd 
   407       istore 1 
   407     istore 1 
   408       goto Label1 
   408     goto Label1 
   409 Label2: 
   409 Label2: 
   410       ;when we come here we have our integer computed in Local Variable 1 
   410     ; when we come here we have our integer computed
   411       iload 1 
   411     ; in local variable 1 
   412       ireturn 
   412     iload 1 
       
   413     ireturn 
   413 .end method
   414 .end method
   414 \end{lstlisting}\normalsize
   415 \end{lstlisting}\normalsize
   415 \caption{Assembler code for reading an integer from the console.\label{read}}
   416 \caption{Assembler code for reading an integer from the console.\label{read}}
   416 \end{figure}
   417 \end{figure}
   417 
   418