📄 chap14.html
字号:
<TR><TD VALIGN="TOP"><STRONG>Opcode</STRONG></TD><TD VALIGN="TOP"><STRONG>Operand(s)</STRONG></TD><TD VALIGN="TOP"><STRONG>Description</STRONG></TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">fneg</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pops a <FONT FACE="Courier New">float</FONT>, negates it, and pushes the result</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">dneg</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pops a <FONT FACE="Courier New">double</FONT>, negates it, and pushes the result</TD></TR>
</TABLE>
<I><STRONG><P>Circle of Squares: A Simulation</P>
</I></STRONG><H3><P></H3>The <STRONG><I>Circle of Squares</I></STRONG> applet, shown in Figure 14-2, demonstrates a Java Virtual Machine executing a sequence of bytecodes that perform floating-point arithmetic. This applet is part of a web page on the CD-ROM in file <FONT FACE="Courier New">applets/CircleOfSquares.html</FONT>. The bytecode sequence in the simulation was generated by <TT>javac</TT> for the <FONT FACE="Courier New">squareItForever()</FONT> method of the class shown below: </P>
<PRE><P><FONT FACE="Courier New">begin</FONT></P>
<FONT SIZE="2"><P></FONT><FONT FACE="Courier New">// On CD-ROM in file opcodes/ex1/SquareCircle.java
<P>class SquareCircle {</P>
<P> </P>
<P> static void squareItForever() {</P>
<P> float f = 2;</P>
<P> for (;;) {</P>
<P> f *= f;</P>
<P> f = 0 - f;</P>
<P> }</P>
<P> }</P>
<P>}</P>
</FONT><FONT SIZE="2"><P> </P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>The bytecodes generated by <TT>javac</TT> for <FONT FACE="Courier New">squareItForever()</FONT> are shown below: </P>
<PRE><P><FONT FACE="Courier New">begin</FONT></P>
<FONT SIZE="2"><P></FONT><FONT FACE="Courier New"> 0 fconst_2 // Push float constant 2.
<P> 1 fstore_0 // Pop to local variable 0 (float f): float f = 2;</P>
<P> 2 fload_0 // Push local variable 0 (float f).</P>
<P> 3 fload_0 // Push local variable 0 (float f).</P>
<P> 4 fmul // Pop top two floats, multiply, push float result.</P>
<P> 5 fstore_0 // Pop to local variable 0 (float f): f *= f;</P>
<P> 6 fconst_0 // Push float constant 0.</P>
<P> 7 fload_0 // Push local variable 0 (float f).</P>
<P> 8 fsub // Subtract top float from next to top float:</P>
<P> // imByte = (byte) imInt;</P>
<P> 9 fstore_0 // Pop result to local variable 0 (float f): f = 0 - f;</P>
<P>10 goto 2 // Jump back to the first fload_0 instruction:</P>
<P> // for (;;) {}</P>
</FONT><FONT SIZE="2"><P> </P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>The <FONT FACE="Courier New">squareItForever()</FONT> method repeatedly squares a <FONT FACE="Courier New">float</FONT> value until it hits infinity. Each time the <FONT FACE="Courier New">float</FONT> is squared it is also negated. The <FONT FACE="Courier New">float</FONT> starts out as 2. It only takes seven iterations before infinity is reached, which isn't nearly as long as it takes in real life. The hex representation of the bits that make up the <FONT FACE="Courier New">float</FONT> are shown in the "hex value" column in the applet. The "value" column shows the number as humans are used to seeing it. This human-friendly value is generated by the <FONT FACE="Courier New">Float.toString()</FONT> method.</P>
<P>To drive the <I>Circle of Squares</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="fig14-2.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/images/fig14-2.gif" ALT="Figure 14-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">opcodes</FONT> directory.</P>
<P>The <I>Inner Float</I> applet is embedded in a web page on the CD-ROM in file <FONT FACE="Courier New">applets/InnerFloat.html</FONT>. The text of the web page includes several button-press sequences that demonstrate various properties of floating point numbers. The source code for this applet is found alongside its class files in the <FONT FACE="Courier New">applets/InnerFloat</FONT> directory.</P>
<P>The <I>Circle of Squares</I> applet is in a web page on the CD-ROM in file <FONT FACE="Courier New">applets/CircleOfSquares.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/COM/artima/jvmsim</FONT> directories.</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/float.html" tppabs="http://www.artima.com/insidejvm/float.html">http://www.artima.com/insidejvm/float.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="chap13.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap13.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="chap15.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap15.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 + -