⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap12.html

📁 Inside the java virtualMachine,深入研究java虚拟机
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<P>&nbsp;</P>
<P>// Next, test the inner for loop's termination condition</P>
<P>10 goto 27       // Jump to for loop condition check</P>
<P>&nbsp;</P>
<P>// The body of the inner for loop begins here.</P>
<P>13 iload_1       // Push the int in local var 1 (numToCheck)</P>
<P>14 iload_3       // Push the int in local var 3 (divisor)</P>
<P>15 irem          // Pop two ints, remainder them, push result</P>
<P>                 // Pop int, jump if equal to zero:</P>
<P>16 ifne 24       // if (numToCheck % divisor == 0)</P>
<P>19 iconst_0      // Push int constant 0</P>
<P>20 istore_2      // Pop into local var 2: foundPrime = false;</P>
<P>21 goto 32       // Jump out of inner for loop</P>
<P>&nbsp;</P>
<P>// At this point, the body of the inner for loop is done. Now just</P>
<P>// perform the third statement of the for expression: decrement</P>
<P>// divisor.</P>
<P>24 iinc 3 -1     // Increment local var 3 by -1: --divisor</P>
<P>&nbsp;</P>
<P>// The test for the inner for loop's termination condition</P>
<P>// begins here. This loop will keep on looping while (divisor </FONT> 1).</P>
<P>27 iload_3       // Push int from local var 3 (divisor)</P>
<P>28 iconst_1      // Push int constant 1</P>
<P>29 if_icmpgt 13  // Pop top two ints, jump if greater than</P>
<P>&nbsp;</P>
<P>// At this point, the inner for loop has completed. Next check</P>
<P>// to see if a prime number was found.</P>
<P>32 iload_2       // Push int from local var 2 (foundPrime)</P>
<P>33 ifeq 38       // Pop top int, jump if zero: if (foundPrime) {</P>
<P>36 iload_1       // Push int from local var 1 (numToCheck)</P>
<P>37 istore_0      // Pop into local var 0: primeNum = numToCheck;</P>
<P>38 iinc 1 1      // Increment local var 1 by 1: ++numToCheck;</P>
<P>41 goto 4        // Jump back to top of outer for loop.</P>
</FONT><FONT SIZE="2"><P>&nbsp;</P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>The <FONT FACE="Courier New">javac</FONT> compiler placed local variable <FONT FACE="Courier New">primeNum</FONT> from the source into local variable slots 0 on the stack frame. It put <FONT FACE="Courier New">numToCheck</FONT> into slot 1, <FONT FACE="Courier New">foundPrime</FONT> into slot 2,  and <FONT FACE="Courier New">divisor</FONT> into slot 3. As mentioned above, as this method finds each successive prime number, it places the number into the <FONT FACE="Courier New">primeNum</FONT> variable. As you run the simulation, therefore, you will see the prime numbers appear sequentially in the <FONT FACE="Courier New">int</FONT> value stored in local variable slot 0.</P>
<P>One thing to note about this bytecode sequence is that it demonstrates the way in which <FONT FACE="Courier New">boolean</FONT>s from Java source code are treated on the stack frame by Java bytecodes. The value stored in local variable slot 2, which represents the <FONT FACE="Courier New">boolean</FONT> <FONT FACE="Courier New">foundPrime</FONT> variable from the source, is an <FONT FACE="Courier New">int</FONT>. It is set to <FONT FACE="Courier New">true</FONT> or <FONT FACE="Courier New">false</FONT> by instructions that push a constant <FONT FACE="Courier New">int</FONT> zero or one. Its boolean value is checked by instructions that compare an <FONT FACE="Courier New">int</FONT> against zero.</P>
<P>Another thing to note about this simulation is that eventually the <FONT FACE="Courier New">numToCheck</FONT> value will overflow. When it does, the virtual machine will throw no exceptions. It will just continue executing the <FONT FACE="Courier New">findPrimes()</FONT> method with <FONT FACE="Courier New">int</FONT> values that no longer hold any relationship to the prime numbers.</P>
<P>To drive the <I>Prime Time</I> simulation, use the Step, Reset, Run, and Stop buttons. Each time you press the Step button, the simulator will execute the instruction pointed to by the pc register. If you press the Run button, the simulation will continue with no further coaxing on your part until you press the Stop button. To start the simulation over, press the Reset button. For each step of the simulation, a panel at the bottom of the applet contains an explanation of what the next instruction will do. Happy clicking.</P>
<P><IMG SRC="fig12-2.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/images/fig12-2.gif" ALT="Figure 12-2"></P>

<H3><EM><P>On the CD-ROM</P>
</EM></H3><P>The CD-ROM contains the source code examples from this chapter in the <FONT FACE="Courier New">integer</FONT> directory. The <I>Prime Time</I> applet is contained in a web page on the CD-ROM in file <FONT FACE="Courier New">applets/PrimeTime.html</FONT>. The source code for this applet is found alongside its class files, in the <FONT FACE="Courier New">applets/JVMSimulators</FONT> and <FONT FACE="Courier New">applets/JVMSimulators</FONT>.</P>
<H3><EM><P>The Resources Page</P>
</EM></H3><P>For more information about the material presented in this chapter, visit the resources page: <FONT FACE="Courier New"><A HREF="http://www.artima.com/insidejvm/integer.html" tppabs="http://www.artima.com/insidejvm/integer.html">http://www.artima.com/insidejvm/integer.html</A></FONT>.</P>
<TABLE BORDER="0" WIDTH="100%">
<TR><TD><A HREF="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" target="bottom"><IMG SRC="hotkey.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/hotkey.gif" ALIGN="LEFT" BORDER="0" WIDTH="40" HEIGHT="40" ALT="Orders"></A>
<IMG SRC="order_text.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/order_text.gif" WIDTH="103" HEIGHT="41" ALT="Orders"></TD>
<TD ALIGN="RIGHT"><A HREF="chap11.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap11.html"><IMG SRC="backward.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/backward.gif" BORDER="0" ALT="Backward" WIDTH="32" HEIGHT="32"></A>&nbsp;<A HREF="chap13.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap13.html"><IMG SRC="forward.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/forward.gif" BORDER="0" ALT="Forward" WIDTH="32" HEIGHT="32"></A></TD></TR>
<TR><TD COLSPAN="2"><A HREF="mailto:computing@mcgraw-hill.com"><IMG SRC="hotkey.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/hotkey.gif" ALIGN="LEFT" BORDER="0" WIDTH="40" HEIGHT="40" ALT="Comments"></A>
<IMG SRC="comment_text.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/comment_text.gif" WIDTH="73" HEIGHT="39" ALT="Comments"></TD></TR>

</TABLE>
<HR>
<P ALIGN=CENTER>&nbsp;<A HREF="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" TARGET="_top">COMPUTING
MCGRAW-HILL</A> | <A HREF="http://www.pbg.mcgraw-hill.com/betabooks/betabooks-home.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/betabooks-home.html" TARGET="_top">Beta Books</A>
| <A HREF="http://www.pbg.mcgraw-hill.com/computing/contact.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/contact.html" TARGET="_top">Contact Us</A>
| <A HREF="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" TARGET="_top">Order Information</A>
| <A HREF="http://mcgraw-hill.inforonics.com/compsearch.shtml" tppabs="http://mcgraw-hill.inforonics.com/compsearch.shtml" TARGET="_top">Online Catalog</A></P>

<P ALIGN=CENTER><FONT SIZE="-1"><A HREF="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" TARGET="_top">Computing McGraw-Hill</A> is an imprint of the <A HREF="http://www.pbg.mcgraw-hill.com/pbg-home.html" tppabs="http://www.pbg.mcgraw-hill.com/pbg-home.html" TARGET="_top">McGraw-Hill Professional Book Group</A>.</FONT></P>

<!-- begin footer -->
<HR>
<A HREF="http://www.mcgraw-hill.com/" tppabs="http://www.mcgraw-hill.com/" TARGET="_top"><IMG SRC="division-white.gif" tppabs="http://www.pbg.mcgraw-hill.com/images/division-white.gif" WIDTH="350" HEIGHT="44" ALT="A Division of the McGraw-Hill Companies" BORDER="0"></A><BR>
<FONT SIZE="-2">Copyright &copy; 1997 <A HREF="http://www.mcgraw-hill.com/" tppabs="http://www.mcgraw-hill.com/" TARGET="_top">
The McGraw-Hill Companies</A>. All rights reserved. Any use is subject to the 
<A HREF="http://www.mcgraw-hill.com/corporate/news_info/copyrttm.htm" tppabs="http://www.mcgraw-hill.com/corporate/news_info/copyrttm.htm" TARGET="_top">
Terms of Use</A>; the corporation also has a comprehensive <A HREF="http://www.mcgraw-hill.com/corporate/news_info/privacy.html" tppabs="http://www.mcgraw-hill.com/corporate/news_info/privacy.html" TARGET="_top">
Privacy Policy</A> governing information we may collect from our customers.</FONT>
<!-- end footer -->
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -