Question: We assumed in Example 15.4 and Figure 15.4 that a, b, c, and s were all among the first few local variables of the current
We assumed in Example 15.4 and Figure 15.4 that a, b, c, and s were all among the first few local variables of the current method, and could be pushed onto or popped from the operand stack with a single one byte instruction. Suppose that this is not the case: that is, that the push and pop instructions require three bytes each. How many bytes will now be required for the code on the left side of Figure 15.4? Most stack-based languages, Java bytecode and CIL among them, provide a swap instruction that reverses the order of the top two values on the stack, and a duplicate instruction that pushes a second copy of the value currently at top of stack. Show how to use swap and duplicate to eliminate the pop and the pushes of s in the left side of Figure 15.4. Feel free to exploit the associativity of multiplication. How many instructions is your new sequence? How many bytes?
Figure 15.4:

push a push b push c r2 := a r3:= b r4 := c add r1 := r2 + r3 add r1 := r1 + r4 push 2 r1 := r1/2 --S divide pop s push s push s r2 := r1 r2 --S - a push a subtract push s push b r3:= r1 r3 --s-b subtract push s push c r4:= r1 - r4 --S - C subtract r3:= r3 x r4 multiply multiply multiply push sqrt r2:= r2 x r3 r1:= r1 x r2 call sqrt call
Step by Step Solution
3.43 Rating (159 Votes )
There are 3 Steps involved in it
If the push and pop instructions require three bytes each instead of one byte then the code on the l... View full answer
Get step-by-step solutions from verified subject matter experts
