Author Topic: Subroutine Argument overwriting  (Read 2896 times)

0 Members and 1 Guest are viewing this topic.

Offline leafy

  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1554
  • Rating: +475/-97
  • Seizon senryakuuuu!
    • View Profile
    • keff.me
Subroutine Argument overwriting
« on: February 03, 2011, 01:21:43 am »
If you call a subroutine inside a subroutine with arguments of its own, will it overwrite the arguments of the original subroutine? For example,

sub(GRV,A,B,C)

Lbl GRV
sub(SE,D,E,F)
Return

Lbl SE
some other code

Since they both depend on r1 to r6, will the variables get overwritten?
Also, if you have something like sub(GRV,X,Y) and you want to save the value created in the subroutine back into Y, does the subroutine do that automatically? Like

Lbl GRV
r2+1 -> r2
Return

Will Y get added an increment of 1?
In-progress: Graviter (...)

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Subroutine Argument overwriting
« Reply #1 on: February 03, 2011, 01:31:50 am »
So the only thing that happens when you call a routine with arguments, is that the numbers you put into the sub(LBL,#,#,#) get *copied* into r1-r6.  When the numbers are copied into r1-r6, anything inside of the r1-r6 is overwritten.
 r1-r6 are completely independent variables from A-Z, and can be used the same way.

Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Re: Subroutine Argument overwriting
« Reply #2 on: February 03, 2011, 02:16:37 am »
Normally, becaus r1 through r6 are global vars, their value gets overridden every time a subroutine using those are called.

Axe provides a way around this -- tack a radian sign after the name of the subroutine, and the values of r1 to r6 will be restored after that subroutine is finished.  Personally, I'm a little leery of using that (even if it does allow for more recursiveness, always a plus) because the values of r1 to r6 are saved to a stack, and I don't know how much memory that stack can hold before something bad happens.

To actually change the value of the variable you loaded, pass a pointer.
Code: [Select]
.TEST
5->Y
sub(ABC,<degree sign here>Y)
ClrHome
Disp Y>Dec
Pause 2000
Return

Lbl ABC
{r1}r+1->{r1}r
Return
The value of Y should be changed to 6.
Note: not tested, but should work.  Prefix a variable with the circular degrees symbol to return a pointer to it.
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: Subroutine Argument overwriting
« Reply #3 on: February 03, 2011, 03:47:29 am »
Using recursive subroutines with the r modifier only backs up the variables it uses so if you mess with the other r1-r6 that are not passed as arguments, there values will be changed as well.  So calling:

:sub(A,50)
:
:Lbl A
:Disp r1>Dec
:sub(B,100)
:Disp r1>Dec
:Return
:
:Lbl B
:Disp r1>Dec
:Return


Prints: 50,100,100

:sub(A,50)
:
:Lbl A
:Disp r1>Dec
:sub(Br,100)
:Disp r1>Dec
:Return
:
:Lbl B
:Disp r1>Dec
:Return


Prints: 50,100,50 since r1, and only r1, is restored after the call.
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Re: Subroutine Argument overwriting
« Reply #4 on: February 03, 2011, 02:52:44 pm »
Out of curiosity, Quigbo, how deep can I nest recursive subroutines before it starts flipping out?
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.