The subroutine might use the stack for evaluating arithmetic expressions (as in chapter 23) or other algorithms.
Those rules are complicated. In broad outline it works the same way as the previous chapter's stack-based linkage convention. But now, the subroutine prolog pushes room on the stack for local variables, and the epilog pops that room.
The picture shows the sections of subroutine linkage. The basic tasks of each section are:
Prolog: Push$raand the caller's$fp. Push anySregister the subroutine will alter. Make room for variables and initialize the subroutine's$fpand$sp.
Body: Normal code, except it must follow these conventions if it calls another subroutine.TandAregisters can be used freely, as can anySregisters that were saved in the prolog. Usedisp($fp)to access variables on the stack.
Subroutine Call: Push anyTregisters that contain values that are needed. Put arguments inAregisters.jalto the subroutine.
Regaining Control: Pop any previously pushed
T registers.
Epilog: Put return values inVregisters. Reset$sp. Pop anySregisters. Pop the caller's$fpand$ra.jr $raback to the caller.
Is there a limit to how many variables a subroutine may have?