Question:
What will happen if I directly push the value into the memory location pointed by the stack pointer?
?
2020-08-18 01:43:43 UTC
I am learning 8085.

I came to a topic called stacks.

For eg-:

LXI SP,2000H.

This will initialize stack with the last memory location 2000H.

Now, many reputed sources say that to push the value, you need to decrease the stack pointer and then only PUSH the value.

i.e if you want to store the value, you need to do that at 1999H in this case. Why are we not storing value starting from 2000H?


Can't we simply store value at 2000H, and then decrement stack pointer? Why is that not done? Am I missing some concepts?
Three answers:
husoski
2020-08-18 06:07:34 UTC
First off, that "LXI SP,2000h" instruction makes the last stack memory location 1FFFh, since a PUSH decrements SP before storing and you (presumably) won't be popping anything off the stack that you haven't previously pushed.  Note that the location just before 2000h is 1FFFh, not 1999h.  The last hex digit is F, not 9.



The 8085 PUSH and POP instructions all act on register pairs; (B,C), (D,E), (H,L) or (A,F) ... where F is the flags register.  Only the first register is mentioned in the instruction, but two registers are always stored (PUSH) or loaded (POP).



In what follows, I'll use [x] to mean the byte at memory address x.



Here's what happens on a PUSH B instruction, assuming SP starts at 2000h:



    1. SP = SP - 1  ... decrement SP (SP is 1FFFh nos)

    2. [SP] = B       ... store B at [1FFFh]

    3. SP = SP - 1  ... decrement SP

    4. [SP] = C       ... store C at [1FFEh]



A "POP B" instruction reverses that process.  Assuming SP is 1FFEh at the start:



    1. C = [SP]        ... load B from [1FFEh]

    2. SP = SP + 1  ... increment SP

    3. B = [SP]        ... load C from [1FFFh]

    4. SP = SP + 1 .... increment SP



If you look through the list of instructions for some way to store the current value of SP into memory or into a register pair, you won't find it.  It's a glaring omission in the 8080/8085 instruction set, imho.  What that means is that you really can't use SP to access memory except through PUSH and POP instructions.



What that also means is that when you write code for the 8085, you load SP with the address of the first location above the stack area and hope it never goes out of bounds.  There's no way in software to detect stack overflow or underflow conditions. (With hardware debuggers like in-circuit emulators or logic analyzers, you could set a break on access of the memory locations just above and below the stack.  Devices like that were expensive and usually not available to hobbyists running CP/M or TRS-DOS.)



So, it's good to ask "what if" questions, but this particular one won't be useful. 
Robert J
2020-08-18 04:43:12 UTC
If you are emulating a stack push or pop, you must use do it the same way as the hardware function in the CPU you are working with.



If you are using PUSH and POP instructions, then you rarely change the value of the stack pointer once it is initialised. 



Those instructions do both the read or write, _and_ the increment or decrement.



The 8085 POP instruction presumably transfers the current addressed value to the register being loaded, then increments the SP.



A PUSH - or a sequence to emulate that - must do the reverse, decrementing the stack pointer then storing.



If you were purely emulating a stack with other instructions (eg using an index register), you could use either order - but to work with hardware, you must match the way that functions.
Mr. Smartypants
2020-08-18 01:49:19 UTC
8085!  Wow!  Hey are you also learning to speak ancient Sumeranian?  8^)



(I should talk.  I learned 8080.  When I learned machine language the 8085 hadn't been invented yet!)



My guess is that the 'end' of the stack contains a sentinel value.  So if you initiate a stack and then push a byte value, now the stack is 2 bytes.  So you don't have to keep track of stack depth, you just POP values until you get a zero.  Just guessing.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...