Hardware Reference
In-Depth Information
line numbers from 1 to 15 at the left of the assembly language program are not part
of the compiler output. Nor are the comments (starting with //). They are there to
help explain a subsequent figure. The Java assembler would then translate the as-
sembly program into the binary program shown in Fig. 4-14(c). (Actually, the Java
compiler does its own assembly and produces the binary program directly.) For
this example, we have assumed that i is local variable 1, j is local variable 2, and k
is local variable 3.
i=j+k;
1
ILOADj
//i=j+k
0x15 0x02
if (i == 3)
2
ILOAD k
0x15 0x03
k = 0;
3
IADD
0x60
else
4
ISTORE i
0x36 0x01
j=j 1;
5
ILOAD i
// if (i == 3)
0x15 0x01
6
BIPUSH 3
0x10 0x03
7
IF ICMPEQ L1
0x9F 0x00 0x0D
8
ILOADj
//j=j 1
0x15 0x02
9
BIPUSH 1
0x10 0x01
10
ISUB
0x64
11
ISTORE j
0x36 0x02
12
GOTO L2
0xA7 0x00 0x07
13 L1: BIPUSH 0
//k=0
0x10 0x00
14
ISTORE k
0x36 0x03
15 L2:
(a)
(b)
(c)
Figure 4-14. (a) A Java fragment. (b) The corresponding Java assembly lan-
guage. (c) The IJVM program in hexadecimal.
The compiled code is straightforward. First j and k are pushed onto the stack,
added, and the result stored in i . Then i and the constant 3 are pushed onto the
stack and compared. If they are equal, a branch is taken to L1 , where k is set to 0.
If they are unequal, the compare fails and code following IF ICMPEQ is executed.
When it is done, it branches to L2 , where the then and else parts merge.
The operand stack for the IJVM program of Fig. 4-14(b) is shown in Fig. 4-15.
Before the code starts executing, the stack is empty, indicated by the horizontal line
above the 0. After the first ILOAD , j is on the stack, as indicated by the boxed j
above the 1 (meaning instruction 1 has executed). After the second ILOAD ,two
words are on the stack, as shown above the 2. After the IADD , only one word is on
the stack, and it contains the sum j
k . When the top word is popped from the
stack and stored in i , the stack is empty, as shown above the 4.
Instruction 5 ( ILOAD ) starts the if statement by pushing i onto the stack (in 5)
Next comes the constant 3 (in 6). After the comparison, the stack is empty again
(7). Instruction 8 is the start of the else part of the Java program fragment. The
else part continues until instruction 12, at which time it branches over the then part
and goes to label L2 .
+
 
Search WWH ::




Custom Search