EEE 4775 Lecture Notes - Return Statement, Subroutine, Program Counter

24 views7 pages
Assembly Language Programming: Subroutines
by Alex Milenkovich, [email protected]
Objectives: Introduce subroutines, subroutine nesting, processor stack, and passing the
parameters to subroutines.
1. Subroutines
In a given program, it is often needed to perform a particular sub-task many times on different
data values. Such a subtask is usually called a subroutine. For example, a subroutine may sort
numbers in an integer array or perform a complex mathematical operation on an input variable
(e.g., calculate sin(x)). It should be noted, that the block of instructions that constitute a
subroutine can be included at every point in the main program when that task is needed.
However, this would result in unnecessary waste of memory space. Rather, only one copy of the
instructions that constitute the subroutine is placed in memory and any program that requires the
use of the subroutine simply branches to its starting location in memory. The instruction that
performs this branch is named a CALL instruction. The calling program is called CALLER and
the subroutine called is called CALLEE.
The instruction that is executed right after the CALL instruction is the first instruction of the
subroutine. The last instruction in the subroutine is a RETURN instruction, and we say that the
subroutine returns to the program that called it. Since a subroutine can be called from different
places in a calling program, we must have a mechanism to return to the appropriate location (the
first instruction that follows the CALL instruction in the calling program). At the time of
executing the CALL instruction we know the program location of the instruction that follows the
CALL (the next program counter or PC). Hence, we should save the return address at the time
the CALL instruction is executed. The way in which a machine makes it possible to call and
return from subroutines is referred to as its subroutine linkage method.
The simplest subroutine linkage method is to save the return address in a specific location. This
location may be a register dedicated to this function, often referred to as the link register. When
the subroutine completes its task, the return instruction returns to the calling program by
branching indirectly through the link register.
The CALL instruction is a special branch instruction and performs the following operations:
Store the contents of the PC in the link register
Branch to the target address specified by the instruction.
The RETURN instruction is a special branch instruction that performs the following operations:
Branch to the address contained in the link register.
1.1. Subroutine Nesting
A common programming practice, called subroutine nesting, is to have one subroutine call
another. In this case, the return address of the second call is also stored in the link register
destroying the previous contents. Hence, it is essential to save the contents of the link register in
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 7 pages and 3 million more documents.

Already have an account? Log in
some other location before calling another subroutine. Subroutine nesting can be carried out to
any depth. Imagine the following sequence: subroutine A calls subroutine B, subroutine B calls
subroutine C, and finally subroutine C calls subroutine D. In this case, the last subroutine D
completes its computations and returns to the subroutine C that called it; next, C completes its
execution and returns to the subroutine B that called it and so on. The sequence of returns is D
returns to C, C returns to B, and B returns to A. That is, the return addresses are generated and
used in the last-in-first-out order. This suggests that the return addresses associated with
subroutine calls should be pushed onto a stack. Many processors do this automatically. A
particular register is designated as the stack pointer, SP, that is implicitly used in this operation.
The stack pointer point to a stack called the processor stack.
The CALL instruction is a special branch instruction and performs the following operations:
Push the contents of the PC on the top of the stack
Update the stack pointer
Branch to the target address specified by the instruction
The RETURN instruction is a special branch instruction that performs the following operations:
Pop the return address from the top of the stack into the PC
Update the stack pointer.
1.2. Parameter Passing
When calling a subroutine, a calling program needs a mechanism to provide to the subroutine the
input parameters, the operands that will be used in computation in the subroutine or their
addresses. Later, the subroutine needs a mechanism to return output parameters, the results of
the subroutine computation. This exchange of information between a calling program and a
subroutine is referred to as parameter passing. Parameter passing may be accomplished in
several ways. The parameters can be placed in registers or in memory locations, where they can
be accessed by subroutine. Alternatively, the parameters may be placed on a processor stack.
Let us consider the following program Figure 1. We have two integer arrays arr1 and arr2. The
program finds the sum of the first one and displays the result on the ports P1 and P2, and then
finds the sum of the second array and displays the result on the ports P3 and P4. It is obvious
that we can have a single subroutine that will perform this operation and thus make our code
more readable and reusable. The subroutine needs to get three input parameters: what is the
starting address of the input array, how many parameters the array has, and where to display the
result. In this example the subroutine does not return any output parameter to the calling
program.
Let us first consider the main program (Figure 2) and corresponding subroutine (suma_rp.s43,
Figure 3) if we pass parameters through registers. Passing parameters through registers is
straightforward and efficient. Three input parameters are placed in registers R12 (starting
address), R13 (array length), and R14 (display id, #0 for P1&P2 and #1 for P3&P4). The calling
program places the parameters in these registers, and then calls the subroutine using CALL
#suma_rp instruction. The subroutine uses register R7 as an accumulator. The register R7 may
contain valid data that belong to the calling program, so our first step should be to push the
content of the register R7 on the stack. The last instruction before the return from the subroutine
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 7 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Assembly language programming: subroutines by alex milenkovich, milenkovic@computer. org. Objectives: introduce subroutines, subroutine nesting, processor stack, and passing the parameters to subroutines. In a given program, it is often needed to perform a particular sub-task many times on different data values. Such a subtask is usually called a subroutine. For example, a subroutine may sort numbers in an integer array or perform a complex mathematical operation on an input variable (e. g. , calculate sin(x)). It should be noted, that the block of instructions that constitute a subroutine can be included at every point in the main program when that task is needed. However, this would result in unnecessary waste of memory space. Rather, only one copy of the instructions that constitute the subroutine is placed in memory and any program that requires the use of the subroutine simply branches to its starting location in memory. The instruction that performs this branch is named a call instruction.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents