Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5. Method cmp() in class homework_1 is used to compare two integer number. It is

ID: 670523 • 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.

1. What does iconst_1 mean? Can we use bipush to replace it? And how to replace it?

2. 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?

3. 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

1)
iconst_1:
The op code iconst_1 pushes the integer value
1 into the stack.
Yes . It is possible to bypush the value into the
stack to replace the existing value.

Instruction used to bypush a constant value.
bipush bytevalue
Replace the code iconst_1 by with
bipush value.

-----------------------------------------------------
2)
iload_0 is used for default static main method arguments, args[0].
Next , iload_1 and iload_2 are used for the method
cmp method to store
the value of a and b into the stack.
The iload_1 stores the value a
The iload_2 stores the value b

-----------------------------------------------------
3)

The hexa code of if_icmple 7 is a4.
The instruction of if_icmple is a4 byte_address1 byte_address2
a4 00(byte address of first value) 05(byte address of second value)
The first a4 is mnemonic code for hte if_cmple
and next two hexa values are iload_1 and iload_2.
Since integer values occupies 4 bytes in java language,
iload_1 is starts from 00 to 04 bytes
iload_2 is second value from 05 to next 4 bytes(05+04=09 valid hexa)
So is hexa code of if_cmple 7 for line 2 is a4 00 05.

Hope this helps you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote