+ Post New Thread
Results 1 to 5 of 5
  1. #1
    Newbie level 3
    Points: 66, Level: 1

    Join Date
    Oct 2018
    Posts
    4
    Helped
    0 / 0
    Points
    66
    Level
    1

    Function stack, interrupt stack

    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

    •   AltAdvertisment

        
       

  2. #2
    Super Moderator
    Points: 76,228, Level: 67
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    12,463
    Helped
    4152 / 4152
    Points
    76,228
    Level
    67

    Re: Function stack, interrupt stack

    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.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



    •   AltAdvertisment

        
       

  3. #3
    Super Moderator
    Points: 28,249, Level: 40
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,418
    Helped
    1066 / 1066
    Points
    28,249
    Level
    40
    Blog Entries
    6

    Re: Function stack, interrupt stack

    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.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



    •   AltAdvertisment

        
       

  4. #4
    Newbie level 3
    Points: 66, Level: 1

    Join Date
    Oct 2018
    Posts
    4
    Helped
    0 / 0
    Points
    66
    Level
    1

    Re: Function stack, interrupt stack

    Quote Originally Posted by betwixt View Post
    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?



  5. #5
    Super Moderator
    Points: 76,228, Level: 67
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    12,463
    Helped
    4152 / 4152
    Points
    76,228
    Level
    67

    Re: Function stack, interrupt 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.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



--[[ ]]--