Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Function stack, interrupt stack

Status
Not open for further replies.

cyberthor

Newbie level 3
Newbie level 3
Joined
Oct 26, 2018
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
42
Hi,

I am confused about the stack.

I understand that in embedded C programming for MCU, when a function is called, a stack frame is created so that the following can be stored temporarily on the stack: return address, function parameters, return value, local variables, etc

How about when an interrupt happens, and the CPU registers need to be temporarily stored? Are they stored in the same stack memory? Or we are talking about two different stack at all?

Thank you
 

Depends on the processor architecture but on most there is only one stack and it is up to the software to ensure the orderly placement and retrieval of values on it. For example, during an interrupt the hardware will normally push the return address to the stack and the ISR may push additional data as well. Before leaving, the software must ensure the data has been removed so the stack pointer is back as it was when the ISR started before returning. Some processors allow direct manipulation of the stack pointer, some don't. Similarly, some establish a stack in accessible memory and some use a dedicated block of memory for it.

Brian.
 

In C programming, you don't care about microcontroller's registers contents, there is an abstraction on this sense done by the compiler memory managemet made transparently, so that you just take care about the variables you have declared in order to not handle them (more exactly, a writing operation) in both the reset and interrupt vectors.
 

Depends on the processor architecture but on most there is only one stack and it is up to the software to ensure the orderly placement and retrieval of values on it. For example, during an interrupt the hardware will normally push the return address to the stack and the ISR may push additional data as well. Before leaving, the software must ensure the data has been removed so the stack pointer is back as it was when the ISR started before returning. Some processors allow direct manipulation of the stack pointer, some don't. Similarly, some establish a stack in accessible memory and some use a dedicated block of memory for it.

Brian.

Ok I understand what you said. But what about function called? Would it push data onto the same stack?
For example, in the following sequence:
1. main program runs. Stack is empty
2. A function is called, therefore some data (parameters, local variables, etc) are pushed on stack
3. Before function exits, an interrupt happens
4. Return address pushed onto stack <- is this pushed location directly after the location in 3?
5. ISR push additional data

I guess what I mean is, do function call, and interrupt, "share " the stack?
 

Yes, that's exactly what happens. As Andre points out, most of it is done by the compiler routines so unless you either overflow the stack or can change the stack pointer in your code (unwise unless you understand the consequences), it shouldn't ever be an issue. Remember that as you descend deeper into nested subroutines or ISR you normally 'reverse out', restoring the stack pointer to it's original value as you do so.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top