-
Notifications
You must be signed in to change notification settings - Fork 0
Control flow
The ifeq instruction is used when comparing states (boolean).
It check if the top of the stack (last element) is 0, if it is then it jumps to the specified label, if not flow continues on.
Let's say you found this block of bytecode:
ldc 1
ifeq BThis would not jump due to the fact that the last stack element (1) is not 0 so the condition isn't satisfied.
The ifne instruction is simply the opposite meaning it would jump if it saw that the last stack element wasn't 0.
The if_icmpeq simply checks two integer values for equality using of course the stack.
Here's an example:
ldc 5
ldc 6
if_icmpeq BIn this case it wouldn't jump as 5 != 6.
The if_icmpne instruction is again simply the opposite meaning with the same bytecode it would jump this time because it checks for inequality opposed to the if_icmpeq instruction.
The lookupswitch instruction can be confusing so here's an explanation on how it works.
Let's take this lookupswitch using the JASM syntax:
lookupswitch
.case 1 A
.case 2 B
.case 3 C
.default DNow a lookupswitch is very easy to understand if we imagine what a switch in Java would look like:
switch (expression) {
case 1:
// do something in case expression is equal to be 1
break;
case 2:
// do something in case expression is equal to be 2
break;
default:
break;
}Now as we can see the switch statement depends on what the expression equals to, for example if the expression is 1 + 1 it would obviously equal to 2 meaning our case 2: code will be hit.
This works about the same in bytecode.
The lookupswitch looks up the last stack element and depending on what it is, it jumps to a specific label, in our previous code we can see that if the last stack element happens to be 1 then we jump to the A label if it's 2 then we jump to the B label and if it's none then our default is triggered which jumps to D an example where default would be triggered is this:
bipush 2
lookupswitch
.case 1 A
.default BAnd that's it now you know how different control flow instructions work in the JVM.