Search code examples
javabytecode

java bytecode .. replacing icosnt_ with bipush<>


I am trying to understand why when I replace the iconst_5 (08) with bipush 20 (1014) it won't work. Would it be possible to set an integer other than -1 to 5 in this case?

A7 00 07     goto                pos.00000170
15 0E        iload               local.0E
08           iconst_5
6C           idiv
36 16        istore              local.16
2A           aload_0
2B           iload_1
15 16        iload               local.16
B7 00 E9     invokespecial       void hui.surf.I.V.A(java.io.PrintWriter, int)
84 0F 01     iinc                local.0F, 1

here's the decompiled snippet:

enter code here
{       
 i6 = paramBoolean1 ? i4 / 2 : i4 / 5;
    A(paramPrintWriter, i6);
  }

I need to replace the integer number 5 with 20.

If I replace 08 with 10 14 this is what the editor does:

A7 00 07    goto                pos.00000170
15 0E   iload               local.0E
10 14   bipush              20
36 16   istore              local.16
2A  aload_0
2B  aload_1
15 16   iload               local.16
B7 00 E9    invokespecial       void hui.surf.I.V.A(java.io.PrintWriter, int)
84 0F 01    iinc                local.0F, 1
A7 FE F3    goto                pos.0000006F

this is what the decompiled code looks like: (JDgui decompiler)

{
  i6 = paramBoolean1 ? i4 / 2 : 20;
  A(paramPrintWriter, i6);
 }

Solution

  • The problem here is that the instructions are different sizes, so changing it will upset all the branch offsets.

    If you use Krakatau, it will replace all the numerical offsets with labels, allowing for easier editing. (Disclosure: I wrote it). Based on the comments, it looks like ReJ works too. I immagine most tools will, since it's common behavior for assemblers.