In: Computer Science
Give the JAVA code for the following IJVM:
ILOAD j
ILOAD k
IADD
BIPUSH 3
I CMPEQ L1
ILOAD j
BIPUSH 1
ISUB
ISTORE j
GOTO L2
L1: BIPUSH 0
ISTORE k
L2:
Given code:
ILOAD j
ILOAD k
IADD
BIPUSH 3
I CMPEQ L1
ILOAD j
BIPUSH 1
ISUB
ISTORE j
GOTO L2
L1: BIPUSH 0
ISTORE k
L2:
Note: The given code has some corrections to make it work properly, so here is the updated code (Result is same but verify the code once for any correction. It is for student best understanding)
Updated Code:
ILOAD j //push j onto the stack
ILOAD k //push k onto the stack
IADD //pop j and k then add both
ISTORE i //i=j+k, store in i
ILOAD i //push i onto the stack
BIPUSH 3 //push 3 onto the stack
I CMPEQ L1 //compare 3 with i, if equal goto L1
ILOAD j //if i not equal with 3, push j onto the stack
BIPUSH 1 //push 1 onto the stack
ISUB //subtract 1 from j
ISTORE j //store j=j-1 in j, then push j onto the stack
GOTO L2 //go to L2
L1: BIPUSH 0 //if -==3, then push 0 onto the stack
ISTORE k //store 0 in k, then push onto the stack
L2: //end of the else loop
==================
Java code:
i = j + k;
if( i == 3)
k = 0;
else
j = j-1;
==================
Code cheats:
ILOAD variable: push local variable onto the stack
IADD: pop top two elements from stack then add, push the sum onto the stack
ISTORE variable: pop top element from stack and store it in variable
BIPUSH num: push a byte number onto the stack
I CMPEQ: compares top two elements from the stack
I CMPEQ L1: compares top two elements from the stack, if equal goto L1 else proceed with next step
ISUB: pop top two elements from stack then subtract them, push the subtract onto the stack
GOTO L2: tells to go to particular step, here it is L2
============================================