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

📄 chap10.html

📁 Inside the java virtualMachine,深入研究java虚拟机
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<!-- All material contained herein is copyright (c) McGraw-Hill Professional Books
All Rights Reserved. No use of this material may be made without express written
permission of the copyright holder. HTML conversions by Mega Space [barry@megaspace.com] -->

<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<TITLE>Understanding Digital Signatures: Inside the Java Virtual Machine
 by Bill Venners - Beta Version</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<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>&nbsp;<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>

<TR><TD COLSPAN="2"><FONT FACE="ARIEL,HELVETICA" SIZE="-1"><I>&copy; 1997 The McGraw-Hill Companies, Inc.  All rights reserved.  <BR>Any use of this Beta Book is subject to the rules stated in 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>.</I></FONT><br>
<script language="javascript">
    document.write("<a href='http://banners.linkbuddies.com/click.php?id=237296'><img src='http://banners.linkbuddies.com/image.php?id=237296&ref=" + document.referrer + "' width=468 height=60 alt='Click Here' border=0></a>");
</script></TD></TR>

</TABLE>
<HR>
<P><H1>Chapter Ten</H1></P>
<P><H2>Stack and Local Variable Operations</H2></P>
<P>As mentioned in Chapter 5, &quot;The Java Virtual Machine,&quot; the abstract specification of the execution engine is defined in terms of an instruction set. The remaining chapters of this book (Chapters 10 through 20) are a tutorial of that instruction set. They describe the instructions in functional groups and give relevant background information for each group.</P>
<P>This chapter covers instructions that deal with the operand stack and local variables. As the Java Virtual Machine is a stack-based machine, almost all of its instructions involve the operand stack in some way. Most instructions push values, pop values, or both as they perform their functions. This chapter describes the instructions that focus most exclusively on the operand stack--those that push constants onto the operand stack, perform generic stack operations, and transfer values back and forth between the operand stack and local variables.</P>
<P>Accompanying this chapter on the CD-ROM is an applet that interactively illustrates the material presented in the chapter. The applet, named <I>Fibonacci Forever</I>, simulates the Java Virtual Machine executing a method that generates the Fibonacci sequence. In the process, it demonstrates how the virtual machine pushes constants, pops values to local variables, and pushes values from local variables. At the end of this chapter, you will find a description of this applet and the bytecodes it executes.</P>
<I><P>Pushing Constants Onto the Stack</P>
</I><P>Many opcodes push constants onto the stack. Opcodes indicate the constant value to push in three different ways. The constant value is either implicit in the opcode itself, follows the opcode in the bytecode stream as an operand, or is taken from the constant pool.</P>
<P>Some opcodes by themselves indicate a type and constant value to push. For example, the <CODE>iconst_1</CODE> opcode tells the Java Virtual Machine to push integer value one. Such opcodes are defined for some commonly pushed numbers of various types. All of these instructions are redundant to the instructions that take operands from the bytecode stream or refer to the constant pool, but these are more efficient. Because these instructions occupy only one byte in the bytecode stream, they increase the efficiency of bytecode execution and reduce the size of bytecode streams. The opcodes that push <FONT FACE="Courier New">int</FONT>s and <FONT FACE="Courier New">float</FONT>s are shown in Table 10-1.</P>
<P>Table 10-1. Pushing single-word constants onto the stack</P>
<TABLE WIDTH="500">
<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">iconst_m1</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">int</FONT> -1 onto the stack</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">iconst_0</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">int</FONT> 0 onto the stack</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">iconst_1</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">int</FONT> 1 onto the stack</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">iconst_2</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">int</FONT> 2 onto the stack</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">iconst_3</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">int</FONT> 3 onto the stack</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">iconst_4</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">int</FONT> 4 onto the stack</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">iconst_5</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">int</FONT> 5 onto the stack</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">fconst_0</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">float</FONT> 0 onto the stack</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">fconst_1</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">float</FONT> 1 onto the stack</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">fconst_2</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">float</FONT> 2 onto the stack</TD></TR>
</TABLE>
<P>The opcodes shown in the previous table push <FONT FACE="Courier New">int</FONT>s and <FONT FACE="Courier New">float</FONT>s, which are single-word values. Each slot on the Java stack is one word in size (at least 32 bits wide). Therefore each time an <FONT FACE="Courier New">int</FONT> or <FONT FACE="Courier New">float</FONT> is pushed onto the stack, it occupies one slot. </P>
<P>The opcodes shown in Table 10-2 push <FONT FACE="Courier New">long</FONT>s and <FONT FACE="Courier New">double</FONT>s. <FONT FACE="Courier New">long</FONT> and <FONT FACE="Courier New">double</FONT> values occupy 64 bits. Each time a <FONT FACE="Courier New">long</FONT> or <FONT FACE="Courier New">double</FONT> is pushed onto the stack, its value occupies two slots on the stack. Opcodes that indicate a specific <FONT FACE="Courier New">long</FONT> or <FONT FACE="Courier New">double</FONT> value to push are shown in Table 10-2. </P>
<P>Table 10-2. Pushing dual-word constants onto the stack</P>
<TABLE WIDTH="500">
<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">lconst_0</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">long</FONT> 0 onto the stack</P>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">lconst_1</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">long</FONT> 1 onto the stack</P>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">dconst_0</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">double</FONT> 0 onto the stack</P>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">dconst_1</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes <FONT FACE="Courier New">double</FONT> 1 onto the stack</P>
</TABLE>
<P>One other opcode pushes an implicit constant value onto the stack. The <CODE>aconst_null</CODE> opcode, shown in Table 10-3, pushes a <FONT FACE="Courier New">null</FONT> object reference onto the stack.</P>
<P>As mentioned in earlier chapters, the format of an object reference depends upon the Java Virtual Machine implementation. An object reference will somehow refer to a Java object on the garbage-collected heap. A <FONT FACE="Courier New">null</FONT> object reference indicates an object reference variable does not currently refer to any valid object. The <CODE>aconst_null</CODE> opcode is used in the process of assigning <FONT FACE="Courier New">null</FONT> to an object reference variable. </P>
<P>Table 10-3. Pushing a <FONT FACE="Courier New">null</FONT> reference onto the stack</P>
<TABLE WIDTH="500">
<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">aconst_null</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pushes a null object reference onto the stack</TD></TR>
</TABLE>
<P>Two opcodes indicate the constant to push with an operand that immediately follows the opcode. These opcodes, shown in Table 10-4, are used to push integer constants that are within the valid range for <FONT FACE="Courier New">byte</FONT> or <FONT FACE="Courier New">short</FONT> types. The <FONT FACE="Courier New">byte</FONT> or <FONT FACE="Courier New">short</FONT> that follows the opcode is expanded to an <FONT FACE="Courier New">int</FONT> before it is pushed onto the stack. Operations on <FONT FACE="Courier New">bytes</FONT> and <FONT FACE="Courier New">short</FONT>s that have been pushed onto the stack are actually done on their <FONT FACE="Courier New">int</FONT> equivalents.</STRONG></TD></TR>
<P>Table 10-4. Pushing <FONT FACE="Courier New">byte</FONT> and <FONT FACE="Courier New">short</FONT> constants onto the stack</STRONG></TD></TR>
<TABLE WIDTH="500">
<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">bipush</FONT></TD><TD VALIGN="TOP">byte1</TD><TD VALIGN="TOP">expands byte1 (a <FONT FACE="Courier New">byte</FONT> type) to an <FONT FACE="Courier New">int</FONT> and pushes it onto the stack</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">sipush</FONT></TD><TD VALIGN="TOP">byte1, byte2</TD><TD VALIGN="TOP">expands byte1, byte2 (a <FONT FACE="Courier New">short</FONT> type) to an <FONT FACE="Courier New">int</FONT> and pushes it onto the stack</TD></TR>
</TABLE>

⌨️ 快捷键说明

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