Sun, 8th Nov 2015, 23:27 Re: EE306 Stack Question

If you understand how PUSH and POP work, then feel free to delete this
and move on.

A student writes:

> Hi Dr. Patt,
> We were reviewing the example on page 266 for stacks and got a bit confused.
> In part (d) why does the add instruction store the result in x3FFF,

Before you ADD, you pop 17 and you pop 25, giving you an empty stack.
The Stack Pointer is x4000. To PUSH the result, you decrement SP, and
store 42 into x3FFF.  Note that SP ALWAYS contains the address to the
top element on the stack.

> but when
> you push in part (e), how does it know that x3FFE is now useless so it
> overrides it instead of simply storing at the top of the stack, or x3FFD.

Since SP = x3FFF, there is nothing in x3FFE.  Of course, there really is
still the value 17, BUT that value was popped, meaning for all practical
purposes it is gone.  Again: SP tells you where the top of stack is.  The
nature of memory is that when you POP, you load from a location, you don't
really remove the value from memory.  BUT calling it a stack means you have
agreed to access the block of memory only in the controlled way via the SP.
So, although 17 is still in the memory location, it is not accessible since
it has been popped, and SP is x3FFF.

> In
> the second push in part (c) it stores in x3FFE, the next open location,
> which is also the top.

Sure, but before we pushed, SP was x3FFF.

If this is still not clear, please do the following.

1. What are the two instructions that carry out the push.

2. In c, what is SP prior to the push.  Where is 17 stored?

3. In e, what is SP prior to the push.  Where is 3 stored?

Got it?

> We would really appreciate a clarification.
> Thanks,
> <<name withheld to protect the student who forgot where you push>>

Good luck preparing for the midterm.

Yale Patt