lab3 - Assembly Language Programming: Subroutines by Alex...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 some other location before calling another subroutine. some other location before calling another subroutine....
View Full Document

This note was uploaded on 12/04/2011 for the course CPE 323 taught by Professor Milenkovic during the Spring '10 term at University of Alabama - Huntsville.

Page1 / 7

lab3 - Assembly Language Programming: Subroutines by Alex...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online