MAIN - using your main program as a co-routine.

A regular NSC program is actually an implicitly created instance of a co-routine, complete with its own stack segment and initialized header. This means that you may "_ccall" or "_resume" your main program as if it were a co-routine you had created, by using the "_coptr" which describes your main program. There is, however, a catch.

The source of the difficulty lies in the answer to the question "Who is the caller of my main program?" Philosophically, the answer to this might be "the operating system", and you would expect that a "_cret" from your main program would return you to whatever invoked your program as a whole. It may be philosophically disappointing that this isn't the case; empirically though, what actually happens is likely more useful in the long run.

In fact, the caller field in the header of your main NSC program is explicitly initialized to zero. Any attempt to use "_cret" from your main program will result in an attempt to address control words below memory location zero, resulting in an immediate memory fault and the ungraceful end of your program. Although inconsistent with the logic of co-routines as a whole, this situation is justified on the grounds that any "_cret" done from your main program (or from any co-routine simply resumed by your main program) is likely an error which should be caught. If you really want it, you can terminate your program anywhere by calling "exit"; inside "main", you can also terminate your program with a "return" statement.

A second inconsistency is evident when you "fall off the end" of the co-routine which is your main program. In all co-routines which you explicitly create, falling off the end is equivalent to doing a "_cret". From your main program, falling off the end (doing a return from "main") terminates your program. This happens regardless of the validity of the caller field in your main program's co-routine header, which perhaps is just as well considering the disaster of doing a "_cret" with a zero caller field.

The above difficulties lie only with the implicit co-routine which is your main NSC program, i.e. the co-routine which uses the main NSC stack. It is perfectly permissible to pass any of the functions of your main NSC program (including "main") in a call to "_coro", in which case the resulting co-routine is explicit, does not use the main stack, and does not suffer from any of the above limitations/features.

Copyright © 1996, Bull HN and Thinkage Ltd.