"Stack underflow, more returns than gosubs!" error message

mrburnette

Senior Member
I only have 4 of each and yet I get the above error message.
Can anyone assist with this please?
Many thanks.View attachment 11403View attachment 11404
I restructured the code a weebit to look like below. If I comment the MAIN: section (using #rem / #remend) then the code runs in the PE simulator... as an 18M2 chip.

I did not dig into the code to see what you were intending on doing nor did I load into ISIS. So, I realize that running in PE is not exactly what you were asking, but maybe the indented code will help you track down your issue.

Code:
#picaxe 18M2

symbol random_number = b0
symbol base_address = b1
symbol counter = b2
symbol memory_address = b3
symbol button_pressed = b4
symbol loop_index = b5
symbol current_number = b6

;#rem
main:
	let base_address = 79
	let counter = 1
	let memory_address = base_address + counter
	let button_pressed = 0

	random w5 'generate first random number

	if pin0 =1 then 'loop until start button pressed
		let random_number = b10 'get random byte
		pause 100 'wait 0.1s
		goto main_loop
	end if

	goto main
;#endrem

main_loop:

	let random_number = random_number/52 'turn random number into 1,2,3 or 4

	let memory_address = base_address + counter ' set memory pointer

	poke memory_address, random_number ' put random number into memory

	for loop_index = 1 to counter ' start display loop sequence

		peek memory_address, current_number

		if current_number = 1 then gosub display_1
		if current_number = 2 then gosub display_2
		if current_number = 3 then gosub display_3
		if current_number = 4 then gosub display_4

		let memory_address = base_address + loop_index

	next loop_index ' end of display loop sequence


	for loop_index = 1 to counter ' start input loop sequence

		let memory_address = base_address + counter

		next_number:

			random w5 'generate next random number

			if pins = 0 then goto next_number 'loop until button pressed

			let random_number = b10 'get random byte

			if pin1 = 1 then let button_pressed = 1 endif
			if pin2 = 1 then let button_pressed = 2 endif
			if pin3 = 1 then let button_pressed = 3 endif
			if pin4 = 1 then let button_pressed = 4 endif

			peek memory_address, current_number

			; if current_number <> button_pressed then goto fail

			pause 500

	next loop_index ' end of input loop sequence

	let counter = counter + 1

	goto main_loop


fail:
	stop
	

display_1:
	high 1
	sound 5, (10,50)
	pause 500
	low 1
	return


display_2:
	high 2
	sound 5, (40,50)
	pause 500
	low 2
	return


display_3:
	high 3
	sound 5, (70,50)
	pause 500
	low 3
	return

display_4:
	high 4
	sound 5, (100,50)
	pause 500
	low 4
	return

- Ray
 

Chipchat

New Member
Thanks for your suggestion Ray,
The listing I attached runs fine for me in the program editor as it is in fact, but won't in the VSM.
That is the cause of my confusion.
My version of VSM doesn't support M2, but the 14M should work OK I believe.
 

mrburnette

Senior Member
@Chipchat:

I am hopeful that one of our other members will reply soon with some VSM knowledge. I do not use the product myself.

- Ray
 

hippy

Technical Support
Staff member
My version of VSM doesn't support M2, but the 14M should work OK I believe.
The code looks okay, it did not crash in my version of VSM ( but didn't seem to do much of anything either ). Your screenshot shows it's a 2007 version, so it would be worth upgrading to the latest version which you are licensed for in case it is a known bug that has been fixed ...

www.picaxe.com/vsm
 

Chipchat

New Member
Thanks Hippy,
I have today ordered the latest version, because my upgrade subscription is past its use-by date.
I'll get that on wednesday with luck, so will try it then and report back how it goes.
Encouraging to hear that it works OK on yours.
 
Top