Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 02:50 29 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 : Question to Subroutines

Author Message
Frank N. Furter
Guru

Joined: 28/05/2012
Location: Germany
Posts: 831
Posted: 03:20am 06 Mar 2014
Copy link to clipboard 
Print this post

Hi,

is it possible to jump within a subroutine with GOTO or GOSUB or is it possible to call another SUB from it (without loosing the values within my SUB)?

I am trying to develop a software I2C and I must transfer values to iterant structures.

I want to call my SUB with:
I2Cs_Write (I2Cs_SDA, I2Cs_SCL, I2Cs_speed, I2Cs_timeout, I2Cs_adr, option, I2Cs_len, I2Csbuf)
but when I jump from my SUB to another routine which pulls the Bytes out, I loose my other parameters.
Is it possible to define global parameters with MMBasic?

Geoff wrote in his manual:
  Quote  Inside the subroutine arg1will have the value 23, arg2$the value of "Cat", and so on. The arguments act
like ordinary variables but they exist only within the subroutine and will vanish when the subroutine ends. You
can have variables with the same name in the main program and they will be different from arguments defined
for the subroutine (at the risk of making debugging harder).


That means that when I jump from my SUB with Gosub to another routine that I loose my parameters like I2Cs_SDA, I2Cs_SCL, I2Cs_speed etc.

Can anybody help me?

Thanks a lot!

Frank
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3807
Posted: 05:47am 06 Mar 2014
Copy link to clipboard 
Print this post

I think you may have to create a small sample, post it, and ask about it.

John
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6101
Posted: 10:45am 06 Mar 2014
Copy link to clipboard 
Print this post

Variables are Global by default.
If they are a parameter or declared as LOCAL, then they are Local.

If you set the parameters (I2Cs_SDA etc) first and then call the SUB/FUNCTION without any parameters, you will have your global variables.

The other way is to call the inner SUB with a big list of parameters.

Difficult to describe but it can be done...

Jim

VK7JH
MMedit   MMBasic Help
 
Frank N. Furter
Guru

Joined: 28/05/2012
Location: Germany
Posts: 831
Posted: 08:06pm 06 Mar 2014
Copy link to clipboard 
Print this post

Hi Jim,

thanks a lot! That's a good idea! I will try it...

Frank
 
Warpspeed
Guru

Joined: 09/08/2007
Location: Australia
Posts: 4406
Posted: 09:52pm 06 Mar 2014
Copy link to clipboard 
Print this post

  Frank N. Furter said   Hi,

is it possible to jump within a subroutine with GOTO or GOSUB or is it possible to call another SUB from it (without loosing the values within my SUB)?

Yes of course you can.

When you go into a subroutine all the various parameters in the central processor are stored on "the stack" and that includes the address from which you exited the program.

The program then jumps to the subroutine with all the original parameters in the central processor safely stored, and also still in the central processor itself.
Only thing that changes is the program counter which is updated to the location of the new subroutine.

It then starts executing the instructions in the subroutine.
If it has to go to a second subroutine while still processing the first subroutine the same thing happens again.

Everything is saved on the stack, and the stack grows larger with yet another full set of central processor parameters being saved on the stack. (the stack grows in size).

When the second subroutine reaches a "return" instruction, it recovers all the central processor parameters and continues on with the first subroutine.
When that reaches a "return" instruction, the program goes back to the original main program and continues on as if neither subroutine had been there.

Everything is saved on the stack.
All accumulators
All condition code registers and flags
The program counter
The stack pointer
Any index registers

All this is restored after a return instruction to what it was before entering the subroutine.

You can nest subroutines to any depth, the only limitation is that the growing stack gobbles up RAM pretty quickly, and you must reserve enough RAM space to do the job. Edited by Warpspeed 2014-03-08
Cheers,  Tony.
 
Warpspeed
Guru

Joined: 09/08/2007
Location: Australia
Posts: 4406
Posted: 10:03pm 06 Mar 2014
Copy link to clipboard 
Print this post

  Frank N. Furter said  

That means that when I jump from my SUB with Gosub to another routine that I loose my parameters like I2Cs_SDA, I2Cs_SCL, I2Cs_speed etc.

Can anybody help me?

Thanks a lot!

Frank

Those parameters if they have not been deliberately saved in RAM will still exist going into a subroutine, but may be destroyed coming back out of a subroutine.

Subroutines assume that when you reach the return instruction all housekeeping has been done WITHIN the subroutine. And the return instruction says the subroutine is complete within itself and everything that needs to be saved has already been saved.


Cheers,  Tony.
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3196
Posted: 10:22pm 06 Mar 2014
Copy link to clipboard 
Print this post

  Warpspeed said  You can nest subroutines to any depth, the only limitation is that the growing stack gobbles up RAM pretty quickly, and you must reserve enough RAM space to do the job.

No, not quite. This is interpreted BASIC not assembler or C.

There is a limit to the number of nested subroutines that you can call (32 on the Micromite, more on the Maximite). This is not related to the amount of memory or the stack.

Frank, the arguments to a subroutine are local to the subroutine. This means that they cannot be seen outside of the subroutine. So, if you want them in another subroutine you must make them global (set their values in the main program) or pass them as arguments to the nested subroutine.

Geoff
Geoff Graham - http://geoffg.net
 
Frank N. Furter
Guru

Joined: 28/05/2012
Location: Germany
Posts: 831
Posted: 11:30pm 06 Mar 2014
Copy link to clipboard 
Print this post

Thanks Tony and thanks a lot Geoff - I will rearrange my code...
I will post my code here (when it works )!

Frank
 
Warpspeed
Guru

Joined: 09/08/2007
Location: Australia
Posts: 4406
Posted: 10:51am 07 Mar 2014
Copy link to clipboard 
Print this post

Great stuff Frank.

Going into a subroutine is always pretty safe, its coming back out where the hazards lie.

You need to make absolutely sure the subroutine is complete within itself, and anything being acted upon is saved before you can safely execute a return instruction.

A return instruction restores everything to exactly how it was just before it launched into the subroutine.
That restoration process will overwrite anything remaining in the accumulator or any of the registers associated with the central processor.
Cheers,  Tony.
 
Print this page


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

© JAQ Software 2024