EEE 4775 Chapter Notes - Chapter 5: Subroutine, Increment And Decrement Operators, Lookup Table

48 views6 pages
Experiment 5
Subroutines in Assembly Language
A program is made up of instructions which implement the solution to a problem. In a given
program, it is often needed to perform a particular sub-task many times on different data
values. So,we split the program into smaller units which solve a particular part of the problem.
These task specific sub-units are termed as functions, procedures or subroutines. In assembly
language, we use the word subroutine for all subprograms to distinguish between functions used
in other programming languages and those used in assembly languages.
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 can be accessed repeatedly.
Components of Subroutines
An assembly language routine has:
1. An entry point
The location of the first instruction in the routine.
2. Parameters
The list of registers or memory locations that contain the parameters for the routine.
3. Return values
The list of registers or memory locations to save the results.
4. Working storage
The registers or memory location required by the routine to perform its task.
Calling Subroutines
There are two ideas behind a subroutine:
1. You should be able to call the subroutine from anywhere.
2. Once the subroutine is complete, it should return back to the place that called the sub-
routine.
There are special instructions for transferring control to subroutines and restoring control to
the main program. The instruction that transfers the control is usually termed as call, jump or
43
Unlock document

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

Already have an account? Log in
44 CHAPTER 5. SUBROUTINES IN ASSEMBLY LANGUAGE
branch to subroutine. The calling program is called Caller and the subroutine called is known
as Callee. The instruction that transfer control back to the caller is known as Return.
Calling a subroutine requires a deviation from the default sequential execution of instructions.
When a program branches to a subroutine, the processor begins execution of the instructions
that make up the subroutine and branch to the subroutine by modifying the Program Counter
(PC). In ARM, the Branch and Link instruction (BL) is used to Branch to subroutine.
BL m y su br o ut i ne ; m y su br ou ti ne p o i n t s t o t he f i r s t l i n e o f a s u b r o u t i n e
This instruction saves the current address of program counter (PC) in the link register (LR)
before placing the starting address of the subroutine in the program counter.
When the subroutine has completed its task, the processor must be able to branch back (return)
to the instruction immediately following the branch instruction that invoked the subroutine. To
return from the subroutine we move the value stored in the link register to program counter
which returns the control to the next instruction from which subroutine was called. Thus, to
return from subroutine we should use the following instruction:
BX LR ; return back t o t he i n s t r u c t i o n a f t e r t he s u b r o u t i n e c a l l
MOV PC, LR ; s e r v e s the same purpos e
ARM Application Procedure Call Standard
The ARM Application Procedure Call Standard (AAPCS) for the ARM architecture defines
how subroutines can be independently written, separately complied, and assembled to work
together. Some parts of the specifications are listed below:
The first four registers R0 R3 are used to pass argument values into a subroutine and to
return a result value from a function.
The registers R4 R8, R10, and R11 are normally used to hold the values of a routines
local variables.
ARM and THUMB C and C++ compilers always use a full descending stack and it must
be eight-byte aligned.
Although your program may work without conforming to the above specification, it is a good
practice to have these specifications in mind. In particularly, when calling between C, C++,
and assembly language is needed, assembly language modules must conform to the appropriate
AAPCS standard.
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
Unlock document

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

Already have an account? Log in

Document Summary

A program is made up of instructions which implement the solution to a problem. In a given program, it is often needed to perform a particular sub-task many times on di erent data values. So,we split the program into smaller units which solve a particular part of the problem. These task speci c sub-units are termed as functions, procedures or subroutines. In assembly language, we use the word subroutine for all subprograms to distinguish between functions used in other programming languages and those used in assembly languages. 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 can be accessed repeatedly. An assembly language routine has: an entry point. The location of the rst instruction in the routine: parameters.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents