Module 0276: Subroutines using the Toy processor
Tak Auyeung, Ph.D.
April 26, 2017
1 About this module
- Prerequisites:
- Objectives: This module explains various aspects of subroutines and how to implement C subroutines in assembly
language using the Toy processor.
2 Calling and returning
First, let us example the following C program:
1void f()
2{ 3} intmain()f();
∖label
{line:invoke
} 4 return 0;
5}
This program does not do much, but illustrates a single subroutine call. At this point, we are only concerned about
the control structure. In other words, we only want to know what happens when f is invoked from main, and
what happens at the end of the function f when execution gets back to main to execute the return statement in
main.
The invocation on line ?? needs to do the following:
- save the return address, where to continue execution when the invoked subroutine is finished
- continue execution at the beginning of the invoked subroutine