I'm curious about how PowerBasic does gosubs when you gosub from a subroutine or function to a line label that exists in the main program, or even in a different subroutine. This compiles okay, but is it kosher to do? Coming at this from a C point of view, this is the equivalent of using setjmp to mark a spot, jump to the line label, and then return using a longjmp. In C this works in the same stack contact, and can even be used to return to an older stack frame, but not in reverse, as the stack contexts would be invalidated (or corrupted). Here's a PB program to illustrate what I'm asking:
This code just seems like it ought to be illegal because it's calling down the stack and then returning! What if the code at mainline: made other subroutine calls, using the stack (which ought to really corrupt it when the gosub returns)? Does PB to some special work to turn mainline into a real subroutine with a special link to the main program's variable area?
This behavior is odd. I've also notices pb3.2 lets me gosub between subroutines! This leads me to believe PB somehow makes gosubs be normal procedure calls.
Can anyone shed some light on this behavior? I'll check my pb programmer's guide in the meantime...
Michael
------------------
Code:
sub test_sub print "gosubing to part of the main program" gosub mainline print "I'm back." end sub a=5 print "This is the main program" print "a =";a test_sub end mainline: print "This is the main program being called by the subroutine" print "This is a:";a return
This behavior is odd. I've also notices pb3.2 lets me gosub between subroutines! This leads me to believe PB somehow makes gosubs be normal procedure calls.
Can anyone shed some light on this behavior? I'll check my pb programmer's guide in the meantime...
Michael
------------------
Comment