Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 04:55 28 Nov 2024 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 : Popping the Gosub stack...

     Page 1 of 3    
Author Message
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 06:17pm 08 Jul 2013
Copy link to clipboard 
Print this post

Hi everyone.


Is there any easy way to pop the gosub stack?

Sometimes, I need to exit from a gosub WITHOUT a return, and go somewhere else in the code.

You can do this(goto some other routine) without explicitly issuing a return for the gosub you are leaving, but it is bad practice, as you end up with the situation where returns from gosubs are dropping off the stack(depending on how many levels of gosub you can have in MMBASIC).

Back in the days of the Atari, they had a POP command, which was specifically used to pop the gosub stack, and bring you "Up one level" in the gosub stack, so that you did not end up with stack overflow errors. You would use that POP command just before you left the gosub routine, if you were not leaving the gosub by the normal RETURN route.

I don't suppose there is any way to do this in MMBASIC is there?
Smoke makes things work. When the smoke gets out, it stops!
 
kiiid

Guru

Joined: 11/05/2013
Location: United Kingdom
Posts: 671
Posted: 07:05pm 08 Jul 2013
Copy link to clipboard 
Print this post

This is something that you shouldn't be needing in a well written program. Even using the GOTO command is commonly considered as a bad programming style.
The best way to deal with this is just to redo this part of the code. Even if there was such thing as popping a RETURN from the stack, it will be a really bad way to go and a potential source of many other issues in the program.
http://rittle.org

--------------
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 07:26pm 08 Jul 2013
Copy link to clipboard 
Print this post

REALLY?

I used it with great success in the Atari years ago......

It's not that easy, really, to exit from a Gosub with a Return, if you need to be somewhere else, not back at the point where the Gosub was called from.
Smoke makes things work. When the smoke gets out, it stops!
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 07:28pm 08 Jul 2013
Copy link to clipboard 
Print this post

  kiiid said  Even using the GOTO command is commonly considered as a bad programming style.


Now you're taking the piss.


Goto another line number or label is a native command in just about ANY language you care to nominate.

How do you justify that statement?

Smoke makes things work. When the smoke gets out, it stops!
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 07:44pm 08 Jul 2013
Copy link to clipboard 
Print this post

The answer to the exit from a sub routine is see the EXIT command.

Kon is not taking the piss. I am sure he is deadly serious about GOTOs and exiting sub routines early. Use DO LOOPs etc. rather than GOTOs. The idea is called structured programming and you have a main sequence which calls sub routines and functions. Other languages call them procedures and functions. All the same thing. Try Googling structured programming.Edited by BobD 2013-07-10
 
kiiid

Guru

Joined: 11/05/2013
Location: United Kingdom
Posts: 671
Posted: 08:08pm 08 Jul 2013
Copy link to clipboard 
Print this post

  Grogster said  
  kiiid said  Even using the GOTO command is commonly considered as a bad programming style.


Now you're taking the piss.


Goto another line number or label is a native command in just about ANY language you care to nominate.

How do you justify that statement?


Hey, this is not to drag you into some pointless disputes
Just stating what is a common knowledge. GOTO is considered as a "bad" command. This is the truth. I know it is native to BASIC, but that does not change the fact. Also, you can probably exit from a subroutine the hard way, but again, doing it does not mean it is good. Better post the piece of code which needs that and I am sure there will be another way.
Just trying to help, that's all...


http://rittle.org

--------------
 
djuqa

Guru

Joined: 23/11/2011
Location: Australia
Posts: 447
Posted: 08:13pm 08 Jul 2013
Copy link to clipboard 
Print this post

Should N ever,ever or need to jump out of a Nested gosub/goto location.
Goto is BAD programming practice.
Gosub's ok but used correctly.

Jumping directly out of nested gosubs would get any programming assignment a "F"ail mark .

Yes several crap versions of BASIC had ability to POP the stack, but still is BAD practice.

Edited by djuqa 2013-07-10
VK4MU MicroController Units

 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3194
Posted: 08:17pm 08 Jul 2013
Copy link to clipboard 
Print this post

I agree with Konstantin, excessive use of GOTOs can lead to unreadable (and therefore buggy) code. Popping the stack would only make things worse.
Geoff Graham - http://geoffg.net
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 09:18pm 08 Jul 2013
Copy link to clipboard 
Print this post

Well bugger me.


I have been using Gosubs and Goto's for years - ever since I first learned programming.
I was totally unaware that Goto is considered bad programming - that really has come as something of a genuine shock.

So, is it fair to say, that most code written these days tries to avoid Goto's completely?

I will re-evaluate the routine that needed the POP, and see if I can teach myself some new tricks.


Oh, and BTW kiiid - I was not trying to be nasty(hence the smilies) in my post about you taking the piss. I truly thought you were taking the piss, as Goto is one of the first commands you learn in BASIC. Well, in the BASIC that I learnt years ago.

Having said that, it is pretty well acknowledged, that Atari BASIC was not the best BASIC even then, but habits can be hard to break.

EDIT: @ BobD - thanks for the link - forgot to mention that - will read all about it.

ANOTHER EDIT: If Goto is such a bad command, and it would seem, is actively discouraged, why is it included in MMBASIC at all? I mean, if there are other alternatives to using Goto, it confuses me why it was allowed into the language at all when Geoff was developing it. Force the use of the more standard methods, rather then even providing a Goto command in the first place, if you see what I am getting at.Edited by Grogster 2013-07-10
Smoke makes things work. When the smoke gets out, it stops!
 
kiiid

Guru

Joined: 11/05/2013
Location: United Kingdom
Posts: 671
Posted: 09:31pm 08 Jul 2013
Copy link to clipboard 
Print this post

Nothing nasty. We are all in the same team here
I remember back in my early days GOTO was one of the first things to learn indeed. Then Pascal got more popular and I had to switch not by own choice. Only then I learned the structural way BobD is talking about. I remember it was written in some fundamental book (can't recall its name), that a well written program should not include even a single GOTO. Couldn't believe back then...
Now thinking in retrospect... I haven't used GOTO (even in BASIC) for many years.

http://rittle.org

--------------
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 09:43pm 08 Jul 2013
Copy link to clipboard 
Print this post

Wow.

This is INTENSLY interesting.


Smoke makes things work. When the smoke gets out, it stops!
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 10:10pm 08 Jul 2013
Copy link to clipboard 
Print this post

  Grogster said  
So, is it fair to say, that most code written these days tries to avoid Goto's completely?

ANOTHER EDIT: If Goto is such a bad command, and it would seem, is actively discouraged, why is it included in MMBASIC at all? I mean, if there are other alternatives to using Goto, it confuses me why it was allowed into the language at all when Geoff was developing it. Force the use of the more standard methods, rather then even providing a Goto command in the first place, if you see what I am getting at.


It is a good move to minimise the number of GOTOs in a program. A level somewhere very close to or below zero is very good.

MMBasic when it started out at V2.1 was nowhere near the same beast it is now. You could only use line numbers and many other commands and functions that we have now were not there. There was also the goal of compatibility with one of the other BASICs. While structured is good there is no need to force people to do one or the other. The gates for the discussion were opened and you can see the flood of well meaning advice that was unleashed.

I would put a good smiley in here but I was too old when they came out and I never got the hang of them. I don't want to put the wrong one.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 10:24pm 08 Jul 2013
Copy link to clipboard 
Print this post

  BobD said  MMBasic when it started out at V2.1 was nowhere near the same beast it is now. You could only use line numbers and many other commands and functions that we have now were not there. There was also the goal of compatibility with one of the other BASICs.


Ahhhh - I see. Good point.


  BobD said  While structured is good there is no need to force people to do one or the other. The gates for the discussion were opened and you can see the flood of well meaning advice that was unleashed.


HELL yes.
Very surprising indeed - I feel I have opened a can of worms.
GOTO seeking worms

  BobD said  I would put a good smiley in here but I was too old when they came out and I never got the hang of them. I don't want to put the wrong one.


LOL! - Laugh Out Loud - Edited by Grogster 2013-07-10
Smoke makes things work. When the smoke gets out, it stops!
 
Juri74

Senior Member

Joined: 06/02/2012
Location: Italy
Posts: 162
Posted: 11:33pm 08 Jul 2013
Copy link to clipboard 
Print this post

I agree that using gosub & goto commands it's very easy to insert bugs, after Geoff added the Function/Sub commands to MMbasic i stopped definitely to use gosub command and i mantain as low as possible the numbers of goto commands, but actually i can't reach 0 :D

Grogster however if you use a Sub you can exit from it with a Goto command, but i will never do such thing, the problem is of course that the code tends to become unreadable. and if a bug come out start to be very difficult to fix it...

i don't know how long is your program.. but think at a 1000 lines long prog...

 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 11:50pm 08 Jul 2013
Copy link to clipboard 
Print this post

Thanks, Juri74.

Currently, my code length is 776 lines.
Smoke makes things work. When the smoke gets out, it stops!
 
Juri74

Senior Member

Joined: 06/02/2012
Location: Italy
Posts: 162
Posted: 12:07am 09 Jul 2013
Copy link to clipboard 
Print this post

ok i'm working at a game, my code length is a bit smaller than yours, 767 lines. have you noticed the general slowdown of the full screen editor after the 450th-500th line? especially when scrolling the program using keyup and keydown keys...
 
Dylan
Regular Member

Joined: 17/06/2013
Location: Netherlands
Posts: 81
Posted: 12:26am 09 Jul 2013
Copy link to clipboard 
Print this post

http://en.wikipedia.org/wiki/Considered_Harmful in particular footnote 3 (pick a link).
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 01:45am 09 Jul 2013
Copy link to clipboard 
Print this post

I use(d) pop and gotos (jumps) all the time.
But that is in assembly. :)

Higher level languages, as basic is, try to avoid that to minimize programming errors.
Counting how many return addresses are on a stack is such a tricky business it should be left to assembly language. In assembly it is great to sometimes just pop 2 from the stack and then return if it saves codespace and processing time.

A Goto statement is almost never used anymore.
It is good that it is still there because sometimes there is no substitute and it can actually make a program more readable.
Even in new languages like C# there still is a goto statement.
And goto is still everywhere but under disguise using names like 'continue' and 'break' which can also cause spaghetti code. Goto at least has the decency to tell you right away where you are going. Break and continue send you on a search for the right opening or closing bracket.





Microblocks. Build with logic.
 
Dylan
Regular Member

Joined: 17/06/2013
Location: Netherlands
Posts: 81
Posted: 05:31am 09 Jul 2013
Copy link to clipboard 
Print this post

The real danger of GOTO once you have structured constructs is that you should almost never want to jump into the middle of a subroutine, even in assembly.

I believe I last used on in anger in Turbo Pascal back in the early 90's for the "standard" application, namely to get out from an inner loop to outside the outer.

Nowadays the "break" version of that is to raise an exception, and of course if the innermost loop has found the answer to make it into a function (and "continue" at the end of the outermost loop).

As a Pascal programmer used to case...of, it always seemed odd that the C family had a break inside switch'es, but once I learned assembler it made sense: after all, falling through saves a JMP.

break and continue should, generally speaking, be very local. I've never had to search for brackets in my own code (however little I can initially understand of the functioning on revisiting a year later, it is at least readable.)

It is nice to see that MMBasic has an ON...GOTO/GOSUB, which allows for menus very easily (ON ASC(INKEY$) GOTO 100, 100, 100 ...) but I imagine I would choose for the GOSUB over GOTO every time.
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3802
Posted: 08:39am 09 Jul 2013
Copy link to clipboard 
Print this post

To come back to what it was wanted for, maybe you could change the SUB into a function and use its returned value to signal that you want to do something other than the current inline next statement.

Some of the problems with GOTO:
makes it hard for other people reading your code
makes it hard to prove the code is correct - this is one of the really big issues
makes it hard to maintain

There are still, arguably, a few cases where GOTO makes sense and some of them are in the Linux kernel code. Basically, local jumps in smallish files where you can clearly see what's going on.

But it's your code so do as you like, of course.

John
 
     Page 1 of 3    
Print this page
© JAQ Software 2024