I know push means placing a value on a stack and pop means retrieving the value from the stack (correct me if I am wrong), but what is the purpose of this? When would I need to use Push and Pop and real life? Please give an example so I can understand.
Please give me an example where I need to use Push and Pop for 8085
1.7k views Asked by Layla Haq AtThere are 3 answers
Every known memory location in our data segment can be very easy read and write all the time again and again and free from the close alliance to the stack mechanic and uncommitted from the position of the stack pointer and without to have a mix between return addresses and call depths. So my feeling is that push and pop instructions are replaceable with write and read instructions and so they are not absolutely needed, but sometimes helpful.
In the case of interrupts--asynchronous events that interrupt the processor in whatever it was doing--the interrupt routine has to save the state of the registers. Typically, that's done by pushing them onto the stack. And then they're popped from the stack before returning.
push a ; save registers
push b
push d
push h
;
; now do whatever the interrupt routine does
;
; and then restore the registers
pop h
pop d
pop b
pop a
ei ; re-enable interrupts
ret
If the interrupt handler didn't do that, the registers would be in some unknown state on return. That, as they say, would be bad.
Push and Pop are used to store data on the stack and take data from the stack respectively.
The stack basically works like a stack of cards. You put a card on the top with the Push instruction and you take the card off the top with the Pop instruction.
For example if a function can take 100 arguments ("printf" function sometimes) there aren't enough registers to store each argument, so you can push the information onto the stack, call the function, and pop it off into a register to work with it. You also sometimes need to use the stack to store return addresses for function calls.