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

📄 chap13.html

📁 Inside the java virtualMachine,深入研究java虚拟机
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!-- 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="chap12.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap12.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="chap14.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap14.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 Thirteen</H1></P>
<P><H2>Logic</H2></P>
<P>This chapter describes the instructions that perform bitwise logical operations inside the Java Virtual Machine. These instructions include opcodes to perform shifting and boolean operations on integers. The boolean operations are performed on individual bits of integer values. </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>Logical Results</I>, simulates the Java Virtual Machine executing a method that includes uses several of the logic opcodes. At the end of this chapter, you will find a description of this applet and the bytecodes it executes.</P>
<H3><EM><P>The Logic Opcodes</P>
</EM></H3><P>The Java Virtual Machine's logic capabilities operate on <FONT FACE="Courier New">int</FONT>s and <FONT FACE="Courier New">long</FONT>s. These operations treat <FONT FACE="Courier New">int</FONT>s and <FONT FACE="Courier New">long</FONT>s not so much as signed two's-complement numbers, necessarily, but as generic bit patterns. Integer shifting is accomplished via the <FONT FACE="Courier New">ishl</FONT>, <FONT FACE="Courier New">ishr</FONT>, and <FONT FACE="Courier New">iushr</FONT> opcodes. Java's <FONT FACE="Courier New"><FONT FACE="Courier New"></FONT> operator is implemented by <FONT FACE="Courier New">ishl</FONT>. The <FONT FACE="Courier New"></FONT></FONT> operator is implemented by <FONT FACE="Courier New">ishr</FONT>, and the <FONT FACE="Courier New"></FONT></FONT> operator is implemented by <FONT FACE="Courier New">iushl</FONT>. The difference between <FONT FACE="Courier New">ishr</FONT> and <FONT FACE="Courier New">iushr</FONT> is that only <FONT FACE="Courier New">ishr</FONT> does sign extension. Table 13-1 shows the instructions that shift <FONT FACE="Courier New">int</FONT>s left and right.</P>
<P>Table 13-1. Shifting <FONT FACE="Courier New">int</FONT>s</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">ishl</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">shifts <FONT FACE="Courier New">int</FONT> left</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">ishr</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">arithmetic shifts <FONT FACE="Courier New">int</FONT> right</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">iushr</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">logical shifts <FONT FACE="Courier New">int</FONT> right</TD></TR>
</TABLE>
<P>Table 13-2 shows the instructions that shift <FONT FACE="Courier New">long</FONT>s left and right.</P>
<P>Table 13-2. Shifting <FONT FACE="Courier New">long</FONT>s</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">lshl</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">shifts <FONT FACE="Courier New">long</FONT> left</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">lshr</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">arithmetic shifts <FONT FACE="Courier New">long</FONT> right</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">lushr</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">logical shifts <FONT FACE="Courier New">long</FONT> right</TD></TR>
</TABLE>
<P>The opcodes shown in Table 13-3 perform bitwise logical operations on <FONT FACE="Courier New">int</FONT>s. The opcodes implement Java's <FONT FACE="Courier New">&amp;</FONT>, <FONT FACE="Courier New">|</FONT>, and <FONT FACE="Courier New">^</FONT> operators.</P>
<P>Table 13-3. Bitwise logical operations on <FONT FACE="Courier New">int</FONT>s</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">iand</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">boolean ands two <FONT FACE="Courier New">int</FONT>s</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">ior</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">boolean ors two <FONT FACE="Courier New">int</FONT>s</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">ixor</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">boolean xors two <FONT FACE="Courier New">int</FONT>s</TD></TR>
</TABLE>
<P>Table 13-4 shows the opcodes that perform bitwise logical operations on <FONT FACE="Courier New">long</FONT>s.</P>
<P>Table 13-4. Bitwise logical operations on <FONT FACE="Courier New">long</FONT>s</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">land</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">boolean ands two <FONT FACE="Courier New">long</FONT>s</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">lor</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">boolean ors two <FONT FACE="Courier New">long</FONT>s</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">lxor</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">boolean xors two <FONT FACE="Courier New">long</FONT>s</TD></TR>
</TABLE>
<P>As mentioned in previous chapters, there is no native <FONT FACE="Courier New">boolean</FONT> type in the Java Virtual Machine. The Java Virtual Machine uses <FONT FACE="Courier New">int</FONT>s to represent <FONT FACE="Courier New">boolean</FONT>s. The instruction set includes many instructions that decide whether or not to jump based on an <FONT FACE="Courier New">int</FONT> value interpreted as a <FONT FACE="Courier New">boolean</FONT>. This instructions are covered in Chapter 16, &quot;Control Flow.&quot;</P>
<I><STRONG><P>Logical Results: A Simulation</P>
</I><P></STRONG>The <I>Logical Results</I> applet, shown in Figure 13-1, demonstrates a Java Virtual Machine executing a sequence of bytecodes. This applet is in a web page on the CD-ROM in file <FONT FACE="Courier New">applets/LogicalResults.html</FONT>. The bytecode sequence in the simulation was generated by <TT>javac</TT> for the <FONT FACE="Courier New">incrementLogically()</FONT> method of the <FONT FACE="Courier New">VulcanCounter</FONT> class:</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/VulcanCounter.java
<P>class VulcanCounter {</P>
<P>&nbsp;</P>
<P>    static void incrementLogically() {</P>
<P>        int spock = 0;</P>
<P>        for (;;) {</P>
<P>            int tempSpock = spock;</P>
<P>            for (int i = 0; i &lt; 32; ++i) {</P>
<P>                int mask = 0x1 <FONT FACE="Courier New"> i;</P>
<P>                if ((tempSpock &amp; mask) == 0) {</P>
<P>                    tempSpock |= mask;  // Change 0 to 1</P>
<P>                    break;</P>
<P>                }</P>
<P>                else {</P>
<P>                    tempSpock &amp;= ~mask; // Change 1 to 0</P>
<P>                }</P>
<P>            }</P>

⌨️ 快捷键说明

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