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.

HOW TO RECTIFY THE ERROR OF ''Stack under flow error occurred from''

Status
Not open for further replies.
and how did you get "return" if no "call" in the first place? even it exists, can you even simulate it in debug mode? i havent heard this "stack underflow" my entire life.
 
and how did you get "return" if no "call" in the first place? even it exists, can you even simulate it in debug mode? i havent heard this "stack underflow" my entire life.

Try this,

Code:
start    return

Mike.
 
and how did you get "return" if no "call" in the first place? even it exists, can you even simulate it in debug mode? i havent heard this "stack underflow" my entire life.

Its very common on small pics.... They only have a small stack to begin with..

srikanthind!! Post your code......
 
Its very common on small pics.... They only have a small stack to begin with..

srikanthind!! Post your code......

It's very common on ALL systems - size of stack doesn't matter - it's essential to keep all calls and returns balanced.

It's simply a programming error that needs correcting.

As you said Ian, post the code.
 
from my quick googling, its something about calling from the stack itself bypassing the default construct. hence by the time the last call is returned (by the default de-construct) the stack is already depleted (stack push < stack pop). i suspect the IDE or the hardware is not stack-safe or stack-protected, which i never worked with, so thats why this thread caught my eye.
 
Last edited:
from my quick googling, its something about calling from the stack itself bypassing the default de-constructor. hence by the time the last call is returned (by the default de-construct) the stack is already depleted. i suspect the IDE is not stack-safe or stack-protected, which i never worked with, so thats why this thread caught my eye.

Woah!!! I doubt if the OP needs to know all that.... He's obviously using assembler....Low level programming pitfalls, that's all...
 
i edited my post, but not much difference in the meaning. as others said, for asm you just need to watch out your push-pop pair, for every push, there must be a pop (you cannot pop if you havent push). higher level language such as C should be bulletproof at this error?, if you are not intending to hack into the stack (stack should be of not our concern, there is no point hacking it :p) or make direct asm jump into a miscellaneous function (with return statement) FWIW and sorry my english.
 
Thank you Ian

the code is like below. there are about 3-4 delay loops. I tried with even 1 delay loop, but still the program is running properly. It was not showing errors but, just after the returns the call going to start of the program code, instead of going to called function (called c).


Code:
DELAY5 	movlw	0x2D
	movwf	d1
	movlw	0xE7
	movwf	d2
	movlw	0x0B
	movwf	d3
DELAY_LOOP1
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	DELAY_LOOP1
Return


DELAY2 movlw	0x11
	movwf	d1
	movlw	0x5D
	movwf	d2
	movlw	0x05
	movwf	d3
DELAY_LOOP2
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	DELAY_LOOP2
			;4 cycles
	goto	$+1
	goto	$+1

Return



DELAY3 movlw	0x1A
	movwf	d1
	movlw	0x8B
	movwf	d2
	movlw	0x07
	movwf	d3
DELAY_LOOP3
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	DELAY_LOOP3

			;5 cycles
	goto	$+1
	goto	$+1
	nop
RETURN

VEHICLES 			BSF GPIO,4    ; LED ON
					GOTO DELAY5   ; CALLED FOR 5 SECONDS DELAY

					BSF GPIO,1
					BCF GPIO,2
					
					GOTO DELAY2    ; CALLED FOR 2 SECONDS DELAY

					BSF GPIO,0
					BCF GPIO,1
					BCF GPIO,4
					BSF GPIO,5


					GOTO DELAY5

					MOVLW B'000011'
					MOVWF GPIO      ; PART C IN ASSIGNMENT

					
					GOTO DELAY3

					

					;BCF GPIO,3					
					
					RETURN
START               
					BSF STATUS, 5 ; Select Bank 1
					MOVLW B'111111'
					MOVWF OSCCAL
					

					CLRF ANSEL
					MOVLW B'00001000'
					MOVWF   TRISIO
          			BCF STATUS,5
					;MOVLW B'00010100'
					;MOVWF GPIO
	                CLRF GPIO
					MOVLW 07H
					MOVWF CMCON
	DEFAULT
					CLRF GPIO
					BSF GPIO,2
					;BSF GPIO,4
					BTFSC GPIO,3
					GOTO VEHICLES
					GOTO START
                    END
 
Last edited by a moderator:
You are using a "Return" from a "Goto" instruction for each delay routine. You are effectively attempting to "Pop" the return address from the stack, without having pushed it on to the stack to begin with :)

The Goto instruction does not "Push" the return address onto the stack. Use a"Call" instruction instead to "Push" the return address onto the stack, then your "Return" from each delay routine should work as you expect.
 
You are using a "Return" from a "Goto" instruction for each delay routine. You are effectively attempting to "Pop" the return address from the stack, without having pushed it on to the stack to begin with :)

The Goto instruction does not "Push" the return address onto the stack. Use a"Call" instruction instead to "Push" the return address onto the stack, then your "Return" from each delay routine should work as you expect.

HI tunedwolf,

thanks for your quick response. Realised silly mistake , it's working now. thank you very much indeed.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top