|
|
View previous topic :: View next topic |
Author |
Message |
Michaelc
Joined: 18 Nov 2003 Posts: 14 Location: Ireland
|
Stack overflow |
Posted: Wed Mar 24, 2004 5:58 am |
|
|
Hey
I am havin a problem with stack overflow in MPLAB, i keep getting a stack overflow after 7 or 8 runs of the program. I know i can disable Stack overflow warning for simulation but i need to know what is causing the problem because when i look at the call tree for the program it should'nt be using the full 8 level deep stack for pic16f877 and also My code is crashing out when i program the pic with it, but not after it has at least run once! While simulating i opened the stack window and noticed that calls were getting pushed onto the stack but didn't seem to be getting popped of after returns. Can anyone advise me on why this is so. The function that appears to causes the problem never actually returns, what i do in the function is load the program counter therefore it never returns, is this the problem?? What should i do??
Thanks |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Mar 24, 2004 6:16 am |
|
|
Pls post a piece of code that cause the problem and the compiler version.
Humberto |
|
|
Michaelc
Joined: 18 Nov 2003 Posts: 14 Location: Ireland
|
Stack overflow |
Posted: Wed Mar 24, 2004 6:51 am |
|
|
Its actually an operating system iam working on so posting all the relevant code would be impossible, but i have the the code below for a typical task, as you can see its in an infinite loop, below that is the function i think maybe causing the problem again as you can see it never really returns but instead changes the pc.
void outputs()
{
for(;;)
{//printf("out");
OSM_WAIT_BIN(SEM_INPUT, OUT1, OUTPUTS_id);
write_expanded_outputs(current_status);
OSM_YIELD(OUT2 ,OUTPUTS_id);
//The function executes
//from here when it is elegible,
#asm
nop
#endasm
}
}
void OSJump()
{//printf("%x", pRunningTask->return_add_high);
//printf("%x", pRunningTask->return_add_low);
jump_label = pRunningTask->return_add_high ;
#asm
movf jump_label,w
movwf PCLATH
#endasm
jump_label = pRunningTask->return_add_low ;
#asm
movf jump_label,w
movwf PCL
#endasm
return ; //won't actually return because it changes the program counter
}
There is about 3 functions which do similar operations as shown above.
Compiler version is
IDE 2.32
PCM 2.734
Not sure if this will help!
Thanks for the reply! |
|
|
Michaelc
Joined: 18 Nov 2003 Posts: 14 Location: Ireland
|
|
Posted: Wed Mar 24, 2004 6:59 am |
|
|
How is the stack popped on the PIC and does loading the pc in a function and not actually returning from the function in the natural way cause the stack not to be popped? |
|
|
Ttelmah Guest
|
Re: Stack overflow |
Posted: Wed Mar 24, 2004 7:04 am |
|
|
Michaelc wrote: | Its actually an operating system iam working on so posting all the relevant code would be impossible, but i have the the code below for a typical task, as you can see its in an infinite loop, below that is the function i think maybe causing the problem again as you can see it never really returns but instead changes the pc.
void outputs()
{
for(;;)
{//printf("out");
OSM_WAIT_BIN(SEM_INPUT, OUT1, OUTPUTS_id);
write_expanded_outputs(current_status);
OSM_YIELD(OUT2 ,OUTPUTS_id);
//The function executes
//from here when it is elegible,
#asm
nop
#endasm
}
}
void OSJump()
{//printf("%x", pRunningTask->return_add_high);
//printf("%x", pRunningTask->return_add_low);
jump_label = pRunningTask->return_add_high ;
#asm
movf jump_label,w
movwf PCLATH
#endasm
jump_label = pRunningTask->return_add_low ;
#asm
movf jump_label,w
movwf PCL
#endasm
return ; //won't actually return because it changes the program counter
}
There is about 3 functions which do similar operations as shown above.
Compiler version is
IDE 2.32
PCM 2.734
Not sure if this will help!
Thanks for the reply! |
If you are jumping out of a called routine, and never returning, then of course the stack will overflow. You have the following choices that I can think of:
1) 'goto' the routine, rather than calling it.
2) Switch to using one of the 18 family chips, which allow you to 'pop' the top entry on the stack.
Best Wishes |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Fri Mar 26, 2004 9:24 am |
|
|
Quote: |
void OSJump()
{//printf("%x", pRunningTask->return_add_high);
//printf("%x", pRunningTask->return_add_low);
jump_label = pRunningTask->return_add_high ;
#asm
movf jump_label,w
movwf PCLATH
#endasm
jump_label = pRunningTask->return_add_low ;
#asm
movf jump_label,w
movwf PCL
#endasm
return ; //won't actually return because it changes the program counter
}
|
1) Unless a time sensitive function - may be an interrupt handler- I try to
ovoid the use of #asm mixed with C.
2) I prefer not to use an #asm call inside a function, just do it with C.
3) With C you almost always will find a way to replace an #asm routine.
Humberto |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|