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

1. Given that the stack is initially empty. What will it contain after these ins

ID: 3686200 • Letter: 1

Question

1.       Given that the stack is initially empty.
What will it contain after these instructions? (starting with the top)
       BiPush 1
       BiPush 2
       DUP
       BiPush 4
       BiPush 2
       BiPush 3
       IADD
       IF _ICMPEQ L1
       ISTORE x
L1 SWAP

2.       Given that the stack initially contains [0, 300] (starting with the top)
What will it contain after these instructions? (starting with the top)
L1   SWAP
       DUP      
       IFEQ L2
       BiPush -1
       IADD
       SWAP
       BiPush 2
       IADD
       GOTO L1
L2   POP

Write the Java code for this IJVM code:
2 points for the least number of correct statements, 1 point for all other correct answers.
                ILOAD x
                ILOAD y
                ISUB
                IFLT L1
                BiPUSH 2
                GOTO L2
L1                 ILOAD z
                BiPush 1
                IADD
L2           ISTORE z         

Explanation / Answer

this question has multiple subparts. I am answering the first 2.

1)

bipush 1 will push 1 in the stack. bipush 2 will push 2 in the stack.
stack will have 1 2

Dup will increase the size of the stack

and with three new bipush we will have 3 2 4 1 2 in stack

there will be add operation on the stack and a store to follow

2)

value in stack will be -1,0,300

after iadd it will add it and 2 will be pushed to stack

so stack will have 2,-1,0,300

with pop instruction 2 will be popped out of the stack. So finally stack will contain -1,0,300

2)

The code is to multiply x and y and storing the result in z