A calculation like “the factorial of a number” may be used several times in a large program. Subroutines allow this kind of functionality to be abstracted into a unit. It’s a benefit for code reuse and generally makes it easier to work with the code too. Even though PASM is just an assembly language running on a virtual processor, it has a number of features to support high-level subroutine calls. IMCC offers a smoother interface to those features.
Unlike most high-level languages, PIR and PASM don’t provide a single statement to call a subroutine, pass in the arguments, and return the result. This is a language designed to implement other languages, and every language does subroutine calls a little differently. What’s needed is a set of building blocks and tools, not a prepackaged solution.
PIR has several directives and
instructions relevant to subroutine calls. The most important is
, which simply branches to a subroutine
label. On the side of the caller,
passes an argument to a subroutine,
retrieves a result. Within the
retrieves an argument passed to the
returns a value to the caller:
.sub _main .local int counter counter = 5 .arg counter # pass an argument call _fact # call the subroutine .local int product .result product # retrieve the result print product print "\n" end .end .sub _fact saveall # save caller's registers .param int N # retrieve the parameter .local ...