Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 00:46 13 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Weird program crash

Author Message
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 08:04pm 16 Jun 2019
Copy link to clipboard 
Print this post

I have a program that is part of a game and it runs quite well, however, it seems to crash every once in a while. So I started to strip away as much as possible while still holding on to the crash. The code below is the least amount that will still run but will crash with an error of...

Error: Too many SUBs for interrupt - This happens after the 49th loop.

"Number of tries" will go from 1 to 49 and then it crashes.

Any ideas why? I got around it in the main program by doing a cpu restart after every 20 "plays", and with that it will run hundreds of times without a crash so I'm assuming something is compounding or accumulating and causing the crash after the 49th loop.

option autorun on
Option Base 1

playtime = 4
Tries = 0

main:

Do
pause 100
Tries = Tries + 1
Print "Number of tries = "; Tries
GameStart
Loop

'******************** SUB - GameStart **********************
Sub GameStart
ShowTime = PlayTime * 10
SetTick 25, UpdateTim, 1
Do while ShowTime > 0
print "showtime = ";ShowTime
CheckScore
Loop
goto main
end sub

'******************** SUB - UpdateTimer **********************
Sub UpdateTim
ShowTime = ShowTime - 1
If ShowTime = 0 then
SetTick 0, 0, 1
ShowTime = 0
end if
End Sub

'******************** SUB - Check Score **********************
Sub CheckScore
Print ""
End Sub
 
Bizzie
Senior Member

Joined: 06/07/2014
Location: Australia
Posts: 192
Posted: 08:47pm 16 Jun 2019
Copy link to clipboard 
Print this post

Take the GOTO main out of the sub GameStart. The sub never returns.
Rob White
 
cosmic frog
Guru

Joined: 09/02/2012
Location: United Kingdom
Posts: 302
Posted: 08:48pm 16 Jun 2019
Copy link to clipboard 
Print this post

Is it because the sub GAMESTART never returns?
Try removing the GOTO MAIN and just go with the END SUB.

Dave.
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 06:29am 17 Jun 2019
Copy link to clipboard 
Print this post

Classic example of why BASIC gets a bad rap for its GOTO.

However, assembly has jmp, but that's OK because you're totally respected if you program in asm.

 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2171
Posted: 09:11am 17 Jun 2019
Copy link to clipboard 
Print this post

LOL - so true.

Personally I don't give a stuff for language/structure snobbery. Do your best to stick to the "rules", keep it tidy and neat but if the "only" way out is a GOTO then do it... just try not to jump right out of a structure
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1106
Posted: 10:33am 17 Jun 2019
Copy link to clipboard 
Print this post

I had one GOTO in my rather long CMM program that I couldn't work out how to avoid.

Decided to print out a hard copy the other week for the first time in ages to check things out.
When I reviewed the section with the GOTO statement in it, I immediately saw how to dispense with it.
A simple IF & ELSE did the trick.
(Of course, I also found other things that needed improving & changing which was the point of the exercise).

I've often wondered about jumps & goto's in Machine code & Assm & not BASIC.
ChopperP
 
cosmic frog
Guru

Joined: 09/02/2012
Location: United Kingdom
Posts: 302
Posted: 11:11am 17 Jun 2019
Copy link to clipboard 
Print this post

When I first heard of not using GOTOs in a BASIC program I wondered if it was even possible. That was when I had a ZX81. But now Basic has come a long way and I find it easy and better not to use them.

Dave.
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2171
Posted: 11:12am 17 Jun 2019
Copy link to clipboard 
Print this post

  Chopperp said  
Decided to print out a hard copy the other week


you've done it now!... you'll be forever gazing at it and tweaking stuff.

I managed to procure a dot matrix printer with fan-fold paper, just for listings. I am such a sad case but somehow looking over a program on separate pristine A4 laser prints just doesn't cut it.Edited by CaptainBoing 2019-06-18
 
goc30

Guru

Joined: 12/04/2017
Location: France
Posts: 435
Posted: 11:43am 17 Jun 2019
Copy link to clipboard 
Print this post

  cosmic frog said   When I first heard of not using GOTOs in a BASIC program I wondered if it was even possible. That was when I had a ZX81. But now Basic has come a long way and I find it easy and better not to use them.

Dave.


this is an "urban legend" :-). "goto" in asm are like" jmp/conditionals jumps" and subroutines calls. After "load/move" they are the most used instruction in machine langage, who is result of compilation
The problem is that "goto" is near of asm instructions and for that it is necessary to undestand ASM (instructions and stacks).
the best use of "goto" is in a block "do/loop" or "sub/end sub" and never to go out of block. And in this case you can win many execution times.

in case of this topic, "goto" instruction is bad in subroutine because it load stack at each subroutine call and because that, interpreter think that it is an call of other subrutine. After that you have this error (too much subroutine)Edited by goc30 2019-06-18
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4135
Posted: 12:38pm 17 Jun 2019
Copy link to clipboard 
Print this post

I'm surprised a GOTO within a SUB/FUNCTION is allowed to go anywhere outside the SUB/FUNCTION.

Perhaps it's time to disallow it.

In case it's currently allowed to GOTO a label within a SUB/FUNC from outside, I'd disallow that as well.

John
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3470
Posted: 01:08pm 17 Jun 2019
Copy link to clipboard 
Print this post

  CaptainBoing said  ... fan-fold paper

Ah, back in the day. I used to have 18" stacks of it on my desk, 17" wide, green and white--"core dumps". Can't say that those were the "good old days". Poor trees.


PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 06:56pm 17 Jun 2019
Copy link to clipboard 
Print this post

Hi Jim, your program got caught when you repeatedly called GameStart from main.

Every time the thread entered GameStart it made a record of the return point. When it encounters the END SUB it jumps to the return address and erases the return address. When you jumped out of the subroutine using GOTO it did not erase the return address and it repeatedly left return address records in place. After a bunch of calls it gave up.

GOTO is a useful command but it has to be used carefully. Some people like to locate the definitions section of a program at the end of the listing for readability. At the end of the definitions you use a second GOTO to jump back to the beginning.

' first you insert OPTION commands before any other commands
goto DAFYNITIONS

CONTINUE:
' here you insert thousands of lines of code which might do something useful
END

DAFYNITIONS:
'here you insert all of your DIM commands
goto CONTINUE

Paul in NYEdited by Paul_L 2019-06-19
 
PeterB
Guru

Joined: 05/02/2015
Location: Australia
Posts: 655
Posted: 12:02am 18 Jun 2019
Copy link to clipboard 
Print this post

Good morning All.

I think I am getting the hang of this.
Things like MULTI LINE IF, SUB, et al can only exit via the final ENDIF,ENDSUB,whatever. I think you can leap around inside but never leap out
Except there is EXIT SUB just to complicate things.

JohnS.

I think GOTOing to inside one of these things would lead to madness.

Peter
 
goc30

Guru

Joined: 12/04/2017
Location: France
Posts: 435
Posted: 01:03am 18 Jun 2019
Copy link to clipboard 
Print this post

for the fun, here an exemple of use multi "goto"
it is a part of my "graphicals tests" programme


etq31:
if nbinfo>0 then 'if function MM.info exist
txdrvlcd=tminfo(3,2) 'lcdinfo
flgtouch=tmvinfo(5) 'flag touch installed
txt=tminfo(2,2) 'speed
goto etq2 'go to next step - this goto is not necessary, just for futur
else
txt="0" 'speed unkown

txdrvlcd="No driver" 'name of driver lcd'
flgtouch =0 'flag if touch active'
vnbus=0 'num of lcd bus'
if flgsavdb=0 then goto etq2 'if no saving datas function
input "What is your LCD driver (ex: ILI9341)",txdrvlcd
if txdrvlcd="" then txdrvlcd="No driver":goto etq2 'go to next step
etq1:
input "have-you Touch function active (Y or N)", rep$
rep$=Ucase$(rep$)
if (rep$<>"Y" and rep$<>"N") then
print "You must answer Y or N"
goto etq1
end if
if rep$="Y" then flgtouch=1
etq1a:
input "what type of bus (1=SPI,2=I2c,3=16b,4=8b,5=other)", rep$
vnbus=val(rep$)
if (vnbus<1 or vnbus>5) then
print "you have to answer number from 1 to 5"
goto etq1a
end if
end if
....

etq2:



you can try to do the same without the "goto" and with less number of linesEdited by goc30 2019-06-19
 
isochronic
Guru

Joined: 21/01/2012
Location: Australia
Posts: 689
Posted: 05:28am 18 Jun 2019
Copy link to clipboard 
Print this post

  Quote  I think GOTOing to inside one of these things would lead to madness.



I was surprised but... It was originally a deliberate item in the first computer language spec (guess which language). IE program flow could jump "outside" a loop, then go back into the loop, and the loop would continue as before ie the loop operating state was not lost. Kind of like a spaghetti factory, with a few optional external pub tours after lunch .

Pity really, the spec itself is a beautiful example of concise technical writing.
 
PeterB
Guru

Joined: 05/02/2015
Location: Australia
Posts: 655
Posted: 06:01am 18 Jun 2019
Copy link to clipboard 
Print this post

It is OK to GOSUB - RETURN but that is not quite the same as leaping out to Lord knows where or leaping in from Lord knows where.
I have been looking at goc30's code. I think he hates me.
It's that final end if that has me worried.
PLEASE HELP!!!!!!!!!!!!!

Peter
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4135
Posted: 06:16am 18 Jun 2019
Copy link to clipboard 
Print this post

  PeterB said   I have been looking at goc30's code. I think he hates me.
It's that final end if that has me worried.
PLEASE HELP!!!!!!!!!!!!!

Peter


Stop looking at it. It's the kind of code that I am fairly sure experts would advise never to write.

E.g. the etq1a part should just be a loop until or the like.

If go30's happy with it, no problem as it's his code, but I see it more as an example of obfuscated badly-structured code which is likely a nightmare to debug / maintain.

John
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9755
Posted: 07:20am 18 Jun 2019
Copy link to clipboard 
Print this post

Just my 2c here. I used to GOTO all over the place back in the day. But that was with Atari BASIC, and it was line-numbered code, and single-line at that, so you could get away with kinda jumping around wherever you wanted.

These days, I have ALL(and I mean ALL) of the main program functionality inside a master DO/LOOP, and you just go around and around and around.

Any interrupts can change variables on the fly, and your master loop can check these each time through the loop and act if it needs to.

There are still times when I find it IS required to be able to GOTO, but again, that is within sub-do's of the main loop, and NEVER outside of the current loop or sub. If I need to get out of a loop when a condition is met, I set a flag and EXIT DO, which will drop you one step back in the DO/LOOP stack so everything adds up at the end of the master-loop. The first line AFTER the LOOP, is an IF/THEN to test what has happened: IF FLAG THEN....
When the loop exits normally, this flag will NOT be set, so that line is simply ignored.

As I say, sometimes you do need to jump around inside a loop, but that is safe enough so long as you DON'T jump to a label outside of that loop, as then eventually, you will run into stack overflows, which is what the OP was having issues with. (GOTO-ing out of a sub without exiting it - the stack handle for that sub is kept open, and is never closed, and eventually you run out of stack depth!)
Smoke makes things work. When the smoke gets out, it stops!
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1106
Posted: 07:22am 18 Jun 2019
Copy link to clipboard 
Print this post

  CaptainBoing said  
  Chopperp said  
Decided to print out a hard copy the other week


you've done it now!... you'll be forever gazing at it and tweaking stuff.

I managed to procure a dot matrix printer with fan-fold paper, just for listings. I am such a sad case but somehow looking over a program on separate pristine A4 laser prints just doesn't cut it.


You could be right.

My print out was with a colour inkjet on A3 so I could use a larger font & still minimise word wrap. Still 14 pages with about 75% of that code.

Ah, fan fold paper... The stuff one needlessly used to print out.
ChopperP
 
goc30

Guru

Joined: 12/04/2017
Location: France
Posts: 435
Posted: 12:08pm 18 Jun 2019
Copy link to clipboard 
Print this post

to johneS

decidably you can not stop yourself from being aggrieved and insulting with me
f you had read the beginning of my post you would have seen "for the fun"

  JohnS said  
Stop looking at it. It's the kind of code that I am fairly sure experts would advise never to write.

E.g. the etq1a part should just be a loop until or the like.

You probably did not know many computer experts or they would have explained to you that a do..until loop generates at the compilation, the same code structure that I used, so with lots of "goto "

  JohnS said  
If go30's happy with it, no problem as it's his code, but I see it more as an example of obfuscated badly-structured code which is likely a nightmare to debug / maintain.

John

As for your skills, I have some doubt that has the idea that a dozen lines of code in basic, may cause you problems maintenance and debugging. Unless your preconceived ideas, to see your sectarianism, pushes you to emit banalities heard here and there, without even having understood their meaning.
Edited by goc30 2019-06-19
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025