Java Reference
In-Depth Information
iconst_0 ; Push 0
istore_1 ; Store into variable #1 (sum)
iconst_1 ; Push 1
istore_2 ; Store into variable #2 (i)
goto L2 ; Go to end of loop test
L1: iload_1 ; Push var #1 (sum) onto stack
iload_2 ; Push var #2 (i) onto stack
iadd ; Add sum + i
istore_1 ; Store sum + i into var #1 (sum)
iinc 2 1 ; Increment var #2 (i) by 1
L2: iload_2 ; Push var #2 (i)
iload_0 ; Push var #0 (limit)
if_icmple L1 ; Goto L1 if i <= limit
iload_1
; Push var #1 (sum) onto stack
; Call toString:
invokestatic
java/lang/Integer/toString(I)Ljava/lang/String;
areturn
; Return String reference to caller
Figure 13.7: Bytecodes for method stringSum
public static String stringSum(int limit){
int sum = 0;
for (int i = 1; i <= limit; i++)
sum += i;
return Integer.toString(sum);
}
The bytecodes listed in Figure 13.7 implement stringSum.
In analyzing stringSum, we see references to three local variables (includ-
ing the limit parameter). Adding in two words of control information, we
conclude that a frame size of 5 words (20 bytes) is required: limit will be
placed at o
ff
set 8, sum at o
ff
set 12, and i at o
ff
set 16.
In the code shown in Figure 13.8, we will follow the MIPS convention
that one word function values, including object references, will be returned in
register $v0. We will also exploit the fact that register $0 always contains a
zero value. The code we generate will begin with a method prologue (to push
stringSum's frame) then a line-by-line translation of its bytecodes, followed
by an epilogue to pop stringSum's frame and return to its caller.
Search WWH ::




Custom Search