Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

GoTo command question

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
techknight

Bascom Member



Joined: 21 Apr 2008
Posts: 230

usa.gif
PostPosted: Sun May 25, 2014 5:18 pm    Post subject: GoTo command question Reply with quote

I know its bad practice to use GoTo, (never understood why, but anyway).

Is it ok to "GoTo" out of an If/select case statement without hurting anything, such as the stack? just curious.

There are several points in my program where I goto out of an If statement to prevent code execution further down the line when something doesn check out upstream.

Of course there are better ways to do this, but using GoTo isnt hurting anything as I can tell.

But I do get a random crash/reboot every GREAT once in awhile so I figured i would ask.

(BASCOM-AVR version : 2.0.7.7 )
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5069
Location: Holland

blank.gif
PostPosted: Sun May 25, 2014 10:50 pm    Post subject: Reply with quote

well the micro could not work without its own goto (jmp). the reason why it is considered bad practice is that you loose overview of your code very quick. so i would use it only when there is no other good way.

you can safely goto anywhere in your code with some exceptions:
- do NOT goto into a function/sub that passes parameters.
- do not goto out of a functions/sub that passes parameters.
- do not goto out of an ISR since otherwise no new interrupt will occur

_________________
Mark
Back to top
View user's profile Visit poster's website
techknight

Bascom Member



Joined: 21 Apr 2008
Posts: 230

usa.gif
PostPosted: Sun May 25, 2014 11:06 pm    Post subject: Reply with quote

Yea, I dont do that. But it is safe to jump out of IF statements then.
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5069
Location: Holland

blank.gif
PostPosted: Sun May 25, 2014 11:22 pm    Post subject: Reply with quote

sure when you take into account the previous points.
i have seen code with lot of gosub and goto and some conditions use GOTO while at that point in the code a GOSUB was used so a RETURN would have been right. goto will work but at some stage your simply crash.

_________________
Mark
Back to top
View user's profile Visit poster's website
techknight

Bascom Member



Joined: 21 Apr 2008
Posts: 230

usa.gif
PostPosted: Sun May 25, 2014 11:29 pm    Post subject: Reply with quote

Well in pseudocode my mainloop is structured like this:
Code:

Do
<Check the uarts code here.>

If A = this then
    do some stuff
    if B <> that then
         Reset some flags
         goto endcheck
    end if
   if blah blah blah
       blah blah blah
   else
        blahblah blah
   end if

else if A = that
   blah blah blah

end if

<code that runs after the If block>


Endcheck:
reset flags
Loop
 


This is rough, and far what it actually is, but its showing how I am using the GoTos. I guess I could probably stick the mainloop inside another nested do loop. Then Exit Do instead of Goto.
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5069
Location: Holland

blank.gif
PostPosted: Sun May 25, 2014 11:37 pm    Post subject: Reply with quote

no problem if you use it like that.
_________________
Mark
Back to top
View user's profile Visit poster's website
techknight

Bascom Member



Joined: 21 Apr 2008
Posts: 230

usa.gif
PostPosted: Sun May 25, 2014 11:39 pm    Post subject: Reply with quote

Yea, I didnt think I was having an issue with the GoTo statements. I figured I would ask because I get a random crash every now and then. So the issue might be elsewhere. And When I say random, its totally random.

It could go for hours, or even days with no issues. but then it takes just one time the CPU will either randomly reset, or itll bounce into bootloader by accident and corrupt the first block of flash memory. Ive had 1 instance of this.
Back to top
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 938
Location: France

france.gif
PostPosted: Sat May 31, 2014 11:01 am    Post subject: Reply with quote

Hi,
Are you sure it is a pb of code ?
it could be a pb of alim. noises, bad filtration...
1000f plus some 100nf (2 or 3) in // are a good protection.

JP
Wink
Back to top
View user's profile Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum