Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Can someone please explain stack depth?

Status
Not open for further replies.
I've been messing with PICs for a while, but I can't say that I ever really gained a grasp of what stack depth meant.

Say I have a PIC with a 16 deep call stack. What does this mean in practice?

It seems like I have made a call that in turn makes more that 16 calls without a problem. Can I make a call from my main code, and then make more than 16 other calls, as long as I don't branch more than 16 times from the ORIGINALLY called routine?

Thanks!
 
You can only have a maximum of 16 entries in total on the stack, each call uses one entry - you need to consider interrupts as well (as these use the stack), which is why it's bad practice to use calls from within interrupts.

If your first call makes 16 further calls, then that's exceeded the stack, and it WILL crash, probably messily.

Bear in mind the 16 calls must be nested to cause a problem, any call which has returned clears it's space on the stack.
 
Okay, so let's consider this example:
I know it is silly and by no means practical, but it's just for example.


Code:
;SUBROUTINE SECTION:

LED0ON
BSF  PORTB,0
RETLW   0

LED1ON
BSF  PORTB,1
RETLW   0

LED2ON
BSF  PORTB,2
RETLW   0

LED3ON
BSF  PORTB,3
RETLW   0

LED4ON
BSF    PORTB,4
RETLW     0

ODDLEDON
CALL    LED1ON
CALL    LED3ON
RETLW   0

EVENLEDON
CALL    LED0ON
CALL    LED2ON
CALL    LED4ON
RETLW      0

ALLLEDON
CALL    EVENLEDON
CALL    ODDLEDON
RETLW      0

;program section

START

CLRF    PORTB
CALL    ODDLEDON

CLRF    PORTB

CALL    ALLLEDON

GOTO   START

Alirght, so with this..... how many levels are used on is the "Call ALLLEDON" command?

I'm thinking 1 for the call itself, and then even though the "CALL EVENLEDON" command makes three more calls, I've just used a total of two levels for the "CALL ALLLEDON" command.

How many more calls from the "EVENLEDON" call would I have had to of made to have caused the stack to overflow?
 
I'm no expert as I'm learning PICs myself, but as I understand it the stack is a circular buffer used to store the program counter contents during the calling of a subroutine as well as interrupts. It is LIFO...meaning Last In First Out...meaning that the last program counter that was PUSHED onto the stack upon a "CALL" or an interrupt is the first one that gets "POPPED" off of the stack when a return instruction is executed.

The main thing is that when dealing with nested subroutines (i.e. calling subroutines within a subroutine) you need to consider stack space to prevent stack overflow. When you have say an 8 level stack...you can PUSH the stack 8 times without a call to POP the stack (i.e. the execution of a return instruction) before the stack if full. If a 9th PUSH occurs prior to the 8th PUSH being POPPED off the stack (i.e. either from an interrupt or a 9th call isntruction), stack overflow occurs and the first Program Counter data that PUSHED the stack gets pushed out. Not sure about the other PICs but I know with the mid range PICs there is no "stack overflow error" bit that gets set when this happens...it just simply pushes the first push out from under the stack.

When this happens, once you execute all of your return instructions to pop the stack, it won't know where to go when the return from the first push is called and essentially you end up with a rogue program.
 
Last edited:
Alirght, so with this..... how many levels are used on is the "Call ALLLEDON" command?

I'm thinking 1 for the call itself, and then even though the "CALL EVENLEDON" command makes three more calls, I've just used a total of two levels for the "CALL ALLLEDON" command.

How many more calls from the "EVENLEDON" call would I have had to of made to have caused the stack to overflow?

If called in that way you can call as many as you like, as each one is completed before another one is called.

Try it with a stack of 8 coins - each time you 'call' take a coin from the stack, and each time you 'retlw' put the coin back on the stack.

That's how calls and the stack work.
 
Isn't that a fuse option on 18F' devices, where you can either reset the device after a stack overflow or just rollover the stack pointer after a stack overflow?
 
Last edited:
Isn't that a fuse option on 18F' devices, where you can either reset the device after a stack overflow or just rollover the stack pointer after a stack overflow?

hi Mike,
The STACK OVL can be tested or directed on the 18F2550
 

Attachments

  • AAesp03.gif
    AAesp03.gif
    21.4 KB · Views: 168
  • AAesp05.gif
    AAesp05.gif
    38.6 KB · Views: 173
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top