5. Method cmp() in class homework_1 is used to compare two integer number. It is
ID: 670524 • Letter: 5
Question
5. Method cmp() in class homework_1 is used to compare two integer number. It is written in Java as bellow:
public class homework_1{
public int cmp(int a, int b){
if(a>b) return 1;
else if(a<b) return -1;
else return 0;
}
}
After using javap to dissemble the generated .class file, you can find an equivalent assembly program for method cmp() as bellow:
0: iload_1
1: iload_2
2: if_icmple 7
5: iconst_1
6: ireturn
7: iload_1
8: iload_2
9: if_icmpge 14
12: iconst_m1
13: ireturn
14: iconst_0
15: ireturn
Also, you can use xxd command to check a hex representation of .class file. Part of it is shown here:
00000c0: 052a b700 01b1 0000 0001 0007 0000 0006 .*..............
00000e0: 0000 003a 0002 0003 0000 0010 1b1c a400 ...:............
00000f0: 0504 ac1b 1ca2 0005 02ac 03ac 0000 0002 ................
The corresponding bytecode has been highlighted for that piece of assembly code. Filling the hex representation of bytecode for corresponding instructions in the following table. For example, for iload_1 in line 0, the hex representation of bytecode is 1b. And for ireturn in line 15, the hex representation of bytecode is ac. For if_icmple 7 in line 2, the hex representation of bytecode is a4 00 05.
1b
0: iload_1
1: iload_2
2: if_icmple 7
5: iconst_1
6: ireturn
7: iload_1
8: iload_2
9: if_icmpge 14
12: iconst_m1
13: ireturn
14: iconst_0
15: ireturn
a4 00 05
ac
Hint: Opcodes for operators in each instruction can be found in https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-7.html
Then answering the following questions.
What does iconst_1 mean? Can we use bipush to replace it? And how to replace it?
What does iload_1 mean? And which variable it relates to compared with java code?(Hint: variable a or b) Can we use iload_0 instead? And why if we cannot?
Why for if_icmple 7 in line 2, the hex representation of bytecode is a4 00 05, but not a4 00 07?
0: iload_1
1: iload_2
2: if_icmple 7
5: iconst_1
6: ireturn
7: iload_1
8: iload_2
9: if_icmpge 14
12: iconst_m1
13: ireturn
14: iconst_0
15: ireturn
Explanation / Answer
What does iconst_1 mean? Can we use bipush to replace it? And how to replace it?
iconst_1 means loading integer value 1..since we have condition (a>b) return 1...
yes we can use bipush..it will store into top of stack...and then we can pop out
usage: BIPUSH 1
OUT
---------------------------------------------------------------------------
What does iload_1 mean? And which variable it relates to compared with java code?(Hint: variable a or b) Can we use iload_0 instead? And why if we cannot?
iload_1 means loading first parameter of function....as we sending two paremetres
to functin (int a, int b).. So, iload_1 means variable a. It takes first parameter
from iload_0..So it accepts iload_0.
----------------------------------------------------------------------------------
Why for if_icmple 7 in line 2, the hex representation of bytecode is a4 00 05, but not a4 00 07?
Since it was executed as fifth statement... this statement has bytecode a4 00 05
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.