📄 chap10.html
字号:
<P> long a = 1;</P>
<P> long b = 1;</P>
<P> </P>
<P> for (;;) {</P>
<P> fiboNum = a + b;</P>
<P> a = b;</P>
<P> b = fiboNum;</P>
<P> }</P>
<P> }</P>
<P>}</P>
</FONT><FONT SIZE="2"><P> </P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>The <FONT FACE="Courier New">calcSequence()</FONT> method produces the Fibonacci series and places each Fibonacci number successively in the <FONT FACE="Courier New">fiboNum</FONT> variable. The first two numbers of the Fibonacci series are both ones. Each subsequent number is calculated by summing the previous two numbers, as in: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, and so on.</P>
<P>The bytecodes generated by <CODE>javac</CODE> for the <FONT FACE="Courier New">calcSequence</FONT><CODE>()</CODE> method are shown below:</P>
<PRE><P><FONT FACE="Courier New">begin</FONT></P>
<FONT SIZE="2"><P></FONT><FONT FACE="Courier New"> 0 lconst_1 // Push long constant 1
<P> 1 lstore_0 // Pop long into local vars 0 & 1: long a = 1;</P>
<P> 2 lconst_1 // Push long constant 1</P>
<P> 3 lstore_2 // Pop long into local vars 2 & 3: long b = 1;</P>
<P> 4 lconst_1 // Push long constant 1</P>
<P> 5 lstore 4 // Pop long into local vars 4 & 5: long fiboNum = 1;</P>
<P> 7 lload_0 // Push long from local vars 0 & 1</P>
<P> 8 lload_2 // Push long from local vars 2 & 3</P>
<P> 9 ladd // Pop two longs, add them, push result</P>
<P>10 lstore 4 // Pop long into local vars 4 & 5: fiboNum = a + b;</P>
<P>12 lload_2 // Push long from local vars 2 & 3</P>
<P>13 lstore_0 // Pop long into local vars 0 & 1: a = b;</P>
<P>14 lload 4 // Push long from local vars 4 & 5</P>
<P>16 lstore_2 // Pop long into local vars 2 & 3: b = fiboNum;</P>
<P>17 goto 7 // Jump back to offset 7: for (;;) {}</P>
</FONT><FONT SIZE="2"><P> </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">a</FONT> from the source into local variable slots 0 and 1 on the stack frame. It put <FONT FACE="Courier New">b</FONT> into slots 2 and 3 and <FONT FACE="Courier New">fiboNum</FONT> into slots 4 and 5. As this method calculates each successive Fibonacci number, it places the number into the <FONT FACE="Courier New">fiboNum</FONT> variable. As you run the simulation, therefore, you will see the Fibonacci series appear in the <FONT FACE="Courier New">long</FONT> value stored in local variable slots 4 and 5.</P>
<P>You may notice that <FONT FACE="Courier New">long</FONT> values are split across the two words they occupy in the local variables by placing the lower half (bits 0 through 31) in the first slot and the upper half (bits 32 through 63) in the second slot. For example, the lower half of the <FONT FACE="Courier New">fiboNum</FONT> variable is stored in local variable slot 4. The upper half of <FONT FACE="Courier New">fiboNum</FONT> is stored in local variable slot 5. On the operand stack, a similar representation is used. When a <FONT FACE="Courier New">long</FONT> value is pushed onto the operand stack, the lower half of the word is pushed, then the upper half.</P>
<P>Keep in mind that this manner of representing <FONT FACE="Courier New">long</FONT> values in the local variables and on the operand stack is an artifact of this particular (simulated) implementation of the Java Virtual Machine. As mentioned in Chapter 5, "The Java Virtual Machine," the specification does not dictate any particular way to layout <FONT FACE="Courier New">long</FONT>s and <FONT FACE="Courier New">double</FONT>s across the two words they occupy on the stack frame.</P>
<P>Although according to the best mathematical minds, the Fibonacci series does indeed go on forever, the <FONT FACE="Courier New">calcSequence()</FONT> method is able to generate Fibonacci numbers only for a while. Unfortunately for <FONT FACE="Courier New">calcSequence()</FONT>, the <FONT FACE="Courier New">long</FONT> type has a finite range. The highest Fibonacci number this simulation can calculate, therefore, is the highest Fibonacci number that can be represented in a long: 7540113804746346429L. After the simulation arrives at this point in the Fibonacci series, the next addition will overflow.</P>
<P>To drive the <I>Fibonacci Forever</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="fig10-1.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/images/fig10-1.gif" ALT="Figure 10-1"></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">stackops</FONT> directory. The <I>Fibonacci Forever</I> applet is contained in a web page on the CD-ROM in file <FONT FACE="Courier New">applets/FibonacciForever.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/stackops.html" tppabs="http://www.artima.com/insidejvm/stackops.html">http://www.artima.com/insidejvm/stackops.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="chap09.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap09.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> <A HREF="chap11.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap11.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> <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 © 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 + -