retagged by
894 views
1 votes
1 votes

Consider the following sequence of instructions intended for execution on a stack machine. Each arithmetic operation pops the second operand, then pops the first operand, operates on them, and then pushes the result back onto the stack.

Push b
Push x
Add
Pop c
Push c
Push y
Add
Push c
Sub
Pop z

Which of the following statements is/are correct?

  1. If push and pop instructions each require $5$ bytes of storage, and arithmetic operations each require $1 byte$ of storage, then the instruction sequence as a whole requires a total of $40 bytes$ of storage.
  2. At the end of execution, $z$ contains the same value as y.
  3. At the end of execution, the stack is empty.
    1. I and III only
    2. II and III only
    3. II only
    4. I, II, and III
retagged by

1 Answer

Best answer
2 votes
2 votes

If push and pop instructions each require 5 bytes of storage , is given in the question, there are in total 7 push and pops operations, so total cost 7*5= 35 Bytes

There are 7 pushes and pops for a cost of 35 bytes, plus 3 arithmetic instructions, for a total of 38 bytes. which contradict option I .

After Instruction Stack Contains New Variables
push  b b  
push x b, x   
add b +  x   
pop  c    c = b + x;
push  c  b + x   c = b + x;
push y b +x , y   c = b + x;
add  b + x + y  c = b + x;
push c 

b + x + y ; b + x 

 c = b + x;
sub  c = b + x;
pop z    c = b + x; z= y

 

 $$\begin{array}{|c|c|c|} \hline   \textbf{After Instruction} & \textbf{Stack Contains} & \textbf{New Variables}  \\ \hline \text{push b} & b & \\ \hline \text{push x} & b,x &  \\ \hline \text{add} & b+x & \\ \hline \text{pop c} & & c = b +x; \\ \hline \text{push c} & b+x & c = b+x; \\ \hline \text{push y} & b+x,y & c = b+x; \\ \hline \text{add} & b+x+y & c = b+x; \\ \hline \text{push c} & b+x+y\:;b+x & c = b+x; \\ \hline \text{sub} & y & c=b+x; \\ \hline \text{pop z} & & c=b+x\:;z=y \\ \hline \end{array}$$ 

so you can see it clearly from above table that  At the end of execution, z contains the same value as y. which supports option II .

At the end of execution, the stack is empty. it is also true which supports option III

so option II and III is true.

edited by
Answer:

Related questions

3 votes
3 votes
2 answers
1