what is the difference between sub-Endsub and Gosub-Return

Started by Yves, Dec 15, 2021, 07:37 AM

Previous topic - Next topic

Yves

Hello All,

I was wondering if there are any advantages  between sub-Endsub and Gosub-Return?

Regards

Yves
Yves

trastikata

No real difference, both will produce Return call and the same hex code. Using it is matter of habit.

top204

I added the Sub-EndSub before I worked out a way to add true procedures to the, once, flat language. It helps to structure a program, so you can see where a subroutines starts and ends, and also resets any RAM banks more efficiently, if required, when a subroutine starts and ends.

A Sub-EndSub works exactly the same as a Gosub-Return, so the code is always placed, regardless whether it is called or not, unlike procedures.

Yves

Many thanks for your replies,
I like Sub-Endsub format.
regards,

Yves
Yves

John Lawton

There is a difference, the calling Gosub command seems to be optional when the subs are constructed with the alternative Sub EndSub

John

Stephen Moss

Quote from: John Lawton on Dec 15, 2021, 02:29 PMThere is a difference, the calling Gosub command seems to be optional when the subs are constructed with the alternative Sub EndSub

John
Are you are referring to being able to use both the
GoSub My_Subroutine() and
My_Subroutine()
syntax to call a Sub-EndSub defined subroutine?

If so then V4.0.1.0 of the compiler does not allow that, using GoSub My_Subtoutine() to call a Sub-EndSub subroutine fails with an error whereas as calling it with My_Subroutine() compiler without error I would expect.

joesaliba

What I really like about Gosub is the ability to use the "On Gosub:.

Not sure if I can make use of it using Sub-Endsub.

Joe

top204

You can call a Sub-EndSub with a Gosub command, or by itself, because it is, essentially, a subroutine's label name and a Return command. And the texts stand for: (Subroutine... End Subroutine)

    GoSub MySub
   
    MySub()

Sub MySub()
EndSub

However, the syntax of the compiler requires a label name alone after the Gosub command, so the open/close parenthasis after the label name are not allowed. i.e. Gosub MySub() That is why it gives the error that there are unrecognised characters found on the line. i.e. The parenthasis after the subroutine's name.

This is something I will see if I can change in the compiler's syntax, just to make it a bit more fluid for the user.

Because Sub-EndSub creates standard subroutine labels, the On Gosub directive can be used with them, but not with the open/close parenthasis after the name.

The On Gosub directive was tricky to create on the devices, because it has to alter the device's hardware call stack and move the return address, but luckily, the 18F devices allow this to happen because they expose the stack SFRs. However, the 14-bit core devices do not have SFRs associated with the call stack, so it could not be implemented on them, and they do not have the Pop or Push mnemonics.

John Drew

Hmm, I'm getting confused. I think I'll stick with "Gosub test' and a label with a return, or preferably Procedures.
It's a problem with maintaining backward compatibility. Too many alternatives for my 81 year old grey matter :)
John