Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Y2K crisis team
Y2K crisis team
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


The dup codes

The three dup instructions duplicate a word from the stack and put the copy back in the stack. They differ as to where in the stack they put the copy. The three dup2 instructions duplicate the two words on the top of the stack and put those words back in the stack. They, too, differ as to where exactly they put the words back in the stack. These are the only instructions in the virtual machine that put words somewhere other than on the top of the stack. All of these instructions enforce the integrity of longs and doubles. That is, they do not allow you to move half of a two-word quantity or to move a word between the two words in a long or a double.

The dup instruction copies the top word on the stack and puts the copy on the top of the stack. Figure 5-11 shows one possible stack before and after the dup instruction.


Figure 5-11  The stack, before and after the dup instruction.

The dup_x1 instruction copies the top word on the stack and puts the copy two words down in the stack. This forces the two words that were on the top of the stack to each move up one place. If the stack is as shown on the left side of Figure 5-12, then after the dup_x1 instruction has been executed, the stack will be in the state shown on the right of Figure 5-12.


Figure 5-12  The stack, before and after the dup_x1 instruction.

The dup_x2 instruction copies the top word on the stack and puts the copy three words down in the stack. This forces the three words on the top of the stack to each move up one. If the stack is as shown on the left side of Figure 5-13, then after the dup_x2 instruction has been executed, the stack will be in the state shown on the right of Figure 5-13.


Figure 5-13  The stack, before and after the dup_x2 instruction.

The dup2 instruction copies the top two words on the stack and puts the copies on the top of the stack. Figure 5-14 illustrates this.


Figure 5-14  The stack, before and after the dup2 instruction.

The dup2_x1 instruction copies the top words on the stack and puts the copies three words down in the stack. Figure 5-15 demonstrates.


Figure 5-15  The stack, before and after the dup2_x1 instruction.

Finally, the dup2_x2 instruction copies the top words on the stack and puts the copies four words down in the stack. Figure 5-16 demonstrates.


Figure 5-16  The stack, before and after the dup2_x2 instruction.

swap

The swap instruction swaps the two words on the top of the stack. That is, the word on the top of the stack moves down one and the word immediately below the top of the stack moves up one, to the top. The size of the stack does not change. Figure 5-17 illustrates this. As always, the swap instruction cannot be used to split or reverse the two words in a long or a double.


Figure 5-17  A stack, before and after the swap instruction.

The local variable array

By far, the largest number of byte codes are the load and store instructions. There are more than 60 of these. The load instructions load a variable onto the stack from the local variable array. The store instructions pop a value from the stack and stores it into the local variable array.

Although there are more than 60 of these instructions, they’re quite easy to understand. All of the load instructions act very much alike. They differ primarily in the type of value that each one loads and secondarily in how they determine the local variable to load. Similarly, all the store instructions act the same. They also differ in the type of value that each one stores and in how they determine the local variable into which to store values.

Each of these instructions begins with a letter that indicates the type of value on which it operates. For example, instructions that begin with the letter l operate on longs, and instructions that begin with f operate on floats. Table 5-2 lists these mappings between first letters and types.

Table 5-2 Type abbreviations

Letter Type

a reference
b byte or boolean
c char
d double
f float
i int
l long
s short


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.