Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Gosub without return questions.

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
CBailey

Bascom Member



Joined: 07 May 2015
Posts: 108

blank.gif
PostPosted: Tue Mar 12, 2024 6:09 am    Post subject: Gosub without return questions. Reply with quote

Looking over my old code, I saw I did this. Not sure if it was on purpose or not. I just wanted to make sure this wouldn't cause any weird problems.

Code:

Gosub 123


123:
' We do something here
' we don't return

456:
' Since we didn't return, we should be here
If a = b then goto 456
' more stuff
return
 


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

Administrator



Joined: 09 Apr 2004
Posts: 5922
Location: Holland

blank.gif
PostPosted: Tue Mar 12, 2024 12:38 pm    Post subject: Reply with quote

simple rule : every gosub must have a RETURN at some point. Then it continues on the next line of code.
The processor hardware pushes the address of the next code on the hardware stack before it jumps to the label.
When a RETURN is found, the address is popped back from the stack so the code can continue.

When you only have gosub and never a RETURN it will give problems.

your example is not real good since it is not clear to me what happens.
But when you say we do not return in : never return from nowhere, there is no problem.

gosub test
print "high"

test:
goto test
return

that will not be a problem when it is intended that way.
So hard to give an answer for the example.
But simple advise : try to use goto little as possible.

this code will be bad :

main:
gosub test
print "abc"
end

test:
nop
goto main
return 'never executed so this will trash the stack

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

Bascom Member



Joined: 07 May 2015
Posts: 108

blank.gif
PostPosted: Tue Mar 12, 2024 3:27 pm    Post subject: Reply with quote

Great, thanks for the advice!
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Tue Mar 12, 2024 6:00 pm    Post subject: Reply with quote

Stricte about "no return"
I saw this many times because it can be a developer mistake and you dont get warning from the compiler because it can also be intended.

Next line will be executed.
This can easly be tested in the simulator.
First Gosub 123 will increase Test variable twice and then print it because no return.
Second Gosub 456 will increase Test only once and then print it.



GoTo in the sub is an another story.

_________________
Check B-Flash -my MCS bootloader app for Android
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR 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