Stack Overflow or Not?

Status
Not open for further replies.

Suraj143

Active Member
This update routine I'm calling at first.Inside it detects a pulse (PORTA,0) & if pulse present it is looping maximum 15 times between Getpulse & Process routines.When pulse stops it will expire the delay inside Getpulse routine & exit the whole routine.

I want to know after this routine calls will it return to the place where it has called?

Code:
Update		---			;a small delay
		---

Getpulse	btfsc	PORTA,0		;check the button
		goto	Process		;yes,it is pressed
		---			;a small delay
		---			;no,then call a delay
		goto	Getpulse	;
		return

Process		[COLOR="Red"]call	Che_Flag	[/COLOR];this is done 15 times
		---
		---
		movwf	Data		;final data
		goto	Getpulse
 
Because I have heard somewhere that you can call only 7 call routines inside a single call routine.

But in the above example it will do 15 calls (Che_Flag).
 
Each time you call a subroutine the processor places a return address on the stack. Whenever a return intruction (or retlw) is encountered an address is removed from the stack. So, in the above example the same stack location will get used 15 times. If your Che_Flag routine does a call to another subroutine then that will use a second stack location. The pic has a stack that can hold 8 address' and is a lifo buffer (last in first out). Keep in mind that an interrupt can come along at any time and use a stack location.

Mike.
 
Pommie said:
Each time you call a subroutine the processor places a return address on the stack.
Totally understood.

Whenever a return intruction (or retlw) is encountered an address is removed from the stack.
Thats the point.This is what I want to know.

So, in the above example the same stack location will get used 15 times.
Wow thats nice.

If your Che_Flag routine does a call to another subroutine then that will use a second stack location.
Now I got the point.Luckily it doesn't another subroutine

The pic has a stack that can hold 8 address' and is a lifo buffer (last in first out). Keep in mind that an interrupt can come along at any time and use a stack location.

This is very useful until now I didn't no about this.

Thanks mike for the wonderful answers regarding stack.I won't get details like this anywhere in the world.

I have a small question I'll post that in few minutes.
 
Hi Mike Here it is.

When start it will call the Update routine & waiting for a pulse & also waiting another button press.If the button pressed it will goto edit routine.But in edit routine & it will again call the whole Update routine.Because it needs to save the Data register.

I think this also won't cause problem with stack.

Thanks

Code:
Start		call	Update
		---
		---

Update		btfsc	PORTA,0		;check the Pulse
		goto	Getpulse	;yes, pulse coming
		[COLOR="Red"]btfss	PORTA,1		;check the edit button
		goto	Edit		;yes, it is pressed[/COLOR]
		goto	Update		;no,

Getpulse	btfss	PORTA,0		;check the Pulse
		goto	Process		;yes,it is pressed
		---			;a small delay
		---			;no,then call a delay
		goto	Getpulse	;
		return

Process		call	Che_Flag	;this is done 15 times
		---
		---
		movwf	Data		;final data
		goto	Getpulse


[COLOR="Red"]Edit		call	Update
		movf	Data,W
		call	EEwrite
		goto	Start		;goto begin of the program
[/COLOR]
 
Last edited:
That code will definitely cause a stack problem. You can't (normally) have a subroutine calling itself without it filling the stack very quickly. BTW, that code will work as it doesn't ever do a return and so the stack just keeps filling up.

Mike.
 
The above code will cause the stack to overflow while the edit button is pressed. It's OK to write code that is recursive like that but you must have a mechanism to prevent an endless calling loop like that.
 
This a place where you need the same main routine in the edit routine as well.

So what do I have to do? Shall I replace the last line (goto Start) with a return instruction.
 
Here I have shifted the edit routine I think this will be ok now.

Code:
Start		call	Update
		[COLOR="Red"]btfss	PORTA,1		
		goto	Edit		;yes, button has pressed[/COLOR]
		---
		---

[COLOR="Red"]Edit		btfss	PORTA,1		;wait until button relesed
		goto	$-1
		call	Update
		movf	Data,W
		call	EEwrite
		goto	Start		;goto begin of the program[/COLOR]
		
Update		btfsc	PORTA,0		;check the Pulse
		goto	Getpulse	;yes, pulse coming
		btfss	PORTA,1		;check the edit button
		[COLOR="Red"]return	[/COLOR]		;yes, it is pressed
		goto	Update		;no,

Getpulse	btfss	PORTA,0		;check the Pulse
		goto	Process		;yes,it is pressed
		---			;a small delay
		---			;no,then call a delay
		goto	Getpulse	;
		return

Process		call	Che_Flag	;this is done 15 times
		---
		---
		movwf	Data		;final data
		goto	Getpulse
 
blueroomelectronics said:
Can you change the I/O such as PORTB instead of PORTA?

I can but no need isn't it?Because my ISR routine is doing a serious coding.So I cannot disturb it.
 
Hola Suraj,

Have you checked all this in the simulator? There, you have the possibility to see the stack positions and when it goes into overflow.

Have you read the datasheet on this regard? It's clear enough.
 
Pommie said:
Did I miss something? ISR = Interrupt Service Routine.

Mike.

Many posters are often secretive or vague about their projects for whatever reason. Reading some of Suraj143 previous posts showed an interest in IR at one point.
Folks new to microcontrollers often ignore the alternate functions of a devices I/O pins and just attach devices to any handy pin.
For example if you're using IR decoding a pin with interrupts available might make the project easier to work on. INT0 comes to mind, or even a CCP input if one's available. He's polling I/O and that's fine but more often than not posting your code & schematic will allow us members to see a better picture of what the OP has in mind, and can better suggest solutions.
How many posts go on for ages till the OP finally explains what they were trying to do and then the common response is "why didn't you show us that in the first place?"
 
blueroomelectronics said:
How many posts go on for ages till the OP finally explains what they were trying to do and then the common response is "why didn't you show us that in the first place?"

Far, far, too many!
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…