I have a strange question concerning subroutines: As I'm creating a minimal language and I don't want to add high-level loops like while
or for
I was planning on just adding gotos to keep it Turing-Complete.
Now I thought, eww - gotos - I wouldn't want to program in that language if I had to use gotos so often. So I thought about adding subroutines instead.
I see the difference as the following:
gotos
subroutines
Okay. Why didn't I just add the more function-like, nice looking subroutines? Because:
In order to make return work if I call subroutines from within subroutines from within other subroutines, I'd have to use a stack containing the point where the currently running subroutine came from at top.
That would then mean that I would, if I create loops using the subroutines, end up with an extremely memory-eating, overflowing stack with return locations. Not good.
Don't think of my subroutines as functions. They are just gotos that return to the point they were called from, they don't actually give back values like the return x;
statement in nearly all today's languages.
Now to my actual questions:
How can I solve the above problem with the stack overflow on loops with subroutines? Do I have to add a separate goto
language construct without the return
option?
Assembler doesn't have loops but as I have seen myJumpPoint:, jnz
, jz
, retn
. That means to me that there must also be a stack containing all the return locations.
retn
symbol in assembler totally wrong? If yes, please explain it to me.Well, couldn't you solve that problem with a conditional jump? A loop checks some condition to see if it has to run again. Go with your subroutines, but add a conditional return, that either returns to the point of call, or returns to the beginning of the routine depending on the evaluation of the condition.