EmbeddedRelated.com
Forums

STACK IMPLEMENTATION

Started by dk July 30, 2007
Hi,

I am trying to understand if i implement something like this in my
micro what would happen. My understanding is that i will lose the the
value of 'A'

(As i dont have any SIMULATOR available now i did not try this)

CALL PUSH_A
.
.
.
CALL POP_A


PUSH_A : PUSH A
                RET
POP_A: POP A
             RET



Thanks,

-Dk

dk wrote:

> I am trying to understand if i implement something like this in my > micro what would happen. My understanding is that i will lose the the > value of 'A' > > > CALL PUSH_A > . > . > . > CALL POP_A > > > PUSH_A : PUSH A > RET > POP_A: POP A > RET
You'll lose more than the value of A. You'll lose your job too probably. Think about what RET does, and from that guess whether you will ever get to call POP_A.
"Paul Burke" <paul@scazon.com> wrote in message 
news:5h62puF2s3seiU1@mid.individual.net...
> dk wrote: > >> I am trying to understand if i implement something like this in my >> micro what would happen. My understanding is that i will lose the the >> value of 'A' >> >> >> CALL PUSH_A >> . >> . >> . >> CALL POP_A >> >> >> PUSH_A : PUSH A >> RET >> POP_A: POP A >> RET > > You'll lose more than the value of A. You'll lose your job too > probably. Think about what RET does, and from that guess whether you > will ever get to call POP_A.
I don't think there will be jobs at stake - this is a homework question I reckon. Good question to ask students though.
Tom Lucas wrote:
> "Paul Burke" <paul@scazon.com> wrote in message > news:5h62puF2s3seiU1@mid.individual.net... >> dk wrote: >> >>> I am trying to understand if i implement something like this in my >>> micro what would happen. My understanding is that i will lose the the >>> value of 'A' >>> >>> >>> CALL PUSH_A >>> . >>> . >>> . >>> CALL POP_A >>> >>> >>> PUSH_A : PUSH A >>> RET >>> POP_A: POP A >>> RET >> You'll lose more than the value of A. You'll lose your job too >> probably. Think about what RET does, and from that guess whether you >> will ever get to call POP_A. > > I don't think there will be jobs at stake - this is a homework question > I reckon. Good question to ask students though. > >
It's a good interview question. For assembly programming it's not out of the realm of possibility to lose track of your stack, particularly if you're pushing and popping data in the middle of things. Knowing what happens would help debugging, particularly if the misalignment only happened every once in a while as a consequence of conditional execution. Note that on some processors the call stack is separate from the data stack (I believe this is the case on the PIC18 and the 6805, but I haven't picked through either of them in detail). For these processors, pushing a register onto the data stack then returning from a subroutine wouldn't lose you your program counter, although it could certainly mess up your data stack if you weren't planning on that behavior. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" gives you just what it says. See details at http://www.wescottdesign.com/actfes/actfes.html
On Jul 30, 9:01 pm, Tim Wescott <t...@seemywebsite.com> wrote:
> Tom Lucas wrote: > > "Paul Burke" <p...@scazon.com> wrote in message > >news:5h62puF2s3seiU1@mid.individual.net... > >> dk wrote: > > >>> I am trying to understand if i implement something like this in my > >>> micro what would happen. My understanding is that i will lose the the > >>> value of 'A' > > >>> CALL PUSH_A > >>> . > >>> . > >>> . > >>> CALL POP_A > > >>> PUSH_A : PUSH A > >>> RET > >>> POP_A: POP A > >>> RET > >> You'll lose more than the value of A. You'll lose your job too > >> probably. Think about what RET does, and from that guess whether you > >> will ever get to call POP_A. > > > I don't think there will be jobs at stake - this is a homework question > > I reckon. Good question to ask students though. > > It's a good interview question. For assembly programming it's not out > of the realm of possibility to lose track of your stack, particularly if > you're pushing and popping data in the middle of things. Knowing what > happens would help debugging, particularly if the misalignment only > happened every once in a while as a consequence of conditional execution. > > Note that on some processors the call stack is separate from the data > stack (I believe this is the case on the PIC18 and the 6805, but I > haven't picked through either of them in detail). For these processors, > pushing a register onto the data stack then returning from a subroutine > wouldn't lose you your program counter, although it could certainly mess > up your data stack if you weren't planning on that behavior. > > -- > > Tim Wescott > Wescott Design Serviceshttp://www.wescottdesign.com > > Do you need to implement control loops in software? > "Applied Control Theory for Embedded Systems" gives you just what it says. > See details athttp://www.wescottdesign.com/actfes/actfes.html- Hide quoted text - > > - Show quoted text -
Hi Tim, You have hit the nail on my head. Last Friday I had this question from my interview panel where in they tried to confuse me. As a result I landed up here to confirm myself what I have told is correct. Thanks TIM and PAUL. for your time. Regards, -Dk
On Mon, 30 Jul 2007 09:01:10 -0700, Tim Wescott <tim@seemywebsite.com>
wrote:

>Tom Lucas wrote: >> "Paul Burke" <paul@scazon.com> wrote in message >> news:5h62puF2s3seiU1@mid.individual.net... >>> dk wrote: >>> >>>> I am trying to understand if i implement something like this in my >>>> micro what would happen. My understanding is that i will lose the the >>>> value of 'A' >>>> >>>> >>>> CALL PUSH_A >>>> . >>>> . >>>> . >>>> CALL POP_A >>>> >>>> >>>> PUSH_A : PUSH A >>>> RET >>>> POP_A: POP A >>>> RET
> >Note that on some processors the call stack is separate from the data >stack (I believe this is the case on the PIC18 and the 6805, but I >haven't picked through either of them in detail). For these processors, >pushing a register onto the data stack then returning from a subroutine >wouldn't lose you your program counter, although it could certainly mess >up your data stack if you weren't planning on that behavior.
Just let me add, that many RISCs (PPC, ARM) do not push the return address on the stack automatically, so calling will not directly interfere with the stack (I do above things in my own code a lot :-) -- 42Bastian Do not email to bastian42@yahoo.com, it's a spam-only account :-) Use <same-name>@monlynx.de instead !
bastian42@yahoo.com (42Bastian Schick) writes:
> Tim Wescott <tim@seemywebsite.com> wrote: > >Tom Lucas wrote: > >> "Paul Burke" <paul@scazon.com> wrote > >>> dk wrote: > >>> > >>>> I am trying to understand if i implement something like this in my > >>>> micro what would happen. My understanding is that i will lose the the > >>>> value of 'A' > >>>> > >>>> > >>>> CALL PUSH_A > >>>> . > >>>> . > >>>> . > >>>> CALL POP_A > >>>> > >>>> > >>>> PUSH_A : PUSH A > >>>> RET > >>>> POP_A: POP A > >>>> RET > > >Note that on some processors the call stack is separate from the data > >stack (I believe this is the case on the PIC18 and the 6805, but I > >haven't picked through either of them in detail). For these processors, > >pushing a register onto the data stack then returning from a subroutine > >wouldn't lose you your program counter, although it could certainly mess > >up your data stack if you weren't planning on that behavior. > > Just let me add, that many RISCs (PPC, ARM) do not push the return > address on the stack automatically, so calling will > not directly interfere with the stack (I do above things in my own > code a lot :-)
And there are some processors, AVR for instance, where all the direct stack operations are to/from the hardware stack but many code implementations use a separate "software" stack.