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: GermanyPosts: 831 |
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: 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 KingdomPosts: 3807 |
I think you may have to create a small sample, post it, and ask about it. John |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6101 |
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: GermanyPosts: 831 |
Hi Jim, thanks a lot! That's a good idea! I will try it... Frank |
||||
Warpspeed Guru Joined: 09/08/2007 Location: AustraliaPosts: 4406 |
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. Cheers, Tony. |
||||
Warpspeed Guru Joined: 09/08/2007 Location: AustraliaPosts: 4406 |
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: AustraliaPosts: 3196 |
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: GermanyPosts: 831 |
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: AustraliaPosts: 4406 |
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 |