Stacks and Subroutines

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

STACKS AND SUBROUTINES

SUBROUTINES
• Large programs are hard to handle and so
broken into smaller programs called as
subroutines.
• A subroutine is a block of code that is called
from different places from within a main
program or other subroutines.
SUBROUTINES
When using subroutines, it is necessary to know the
following:
• When should we jump? (use CALL) - A
subroutine call can be implemented by pushing
the return address on the stack and then jumping
to the branch target address.
• Where do we return to? (use RETURN) - When
the subroutine is done, remember to pop out the
saved information so that it will be able to return
to the next instruction immediately after the
calling point.
SUBROUTINES(cont..)
• A Branch and Link (BL) instruction is used to call a
subroutine or a procedure in ARM.
For instance,
• BL foo /*BL- Branch and link instruction, foo is a
subroutine/procedure name*/
• The branch and link is much like a branch, except that before
branching it stores the current PC value in r14. Thus, to return
from a procedure, simply move the value of r14 (LR) to r15
(PC)
MOV r15,r14
SUBROUTINES(cont..)
• When subroutines are nested, the contents of the link
register must be saved on a stack by the subroutine.
Register R13, Stack Pointer is normally used as the
pointer for this stack.

• If, for example, we call a C function within another C


function, the second function call will overwriter14,
destroying the return address for the first function call.
The standard procedure for allowing nested procedure
calls (including recursive procedure calls) is to build a
stack,
Figure 2.11. The C code
shows a series of functions
that call other functions:
f1() calls f2(), which in
turn calls f3(). The right
side of the figure shows
the state of the procedure
call stack during the
execution of f3(). The
stack contains one
activation record for each
active procedure. When
f3() finishes, it can pop the
Figure 2.11 Nested function calls and top of the stack to get its
stacks return address, leaving the
return address for
f2()waiting at the top of
the stack for its return.
STACK
Stack is a Temporary memory storage space by
MPU(memory protection unit) during the execution of a
program.
The stack is a data structure, known as last in first out
(LIFO). In a stack, items entered at one end and leave in the
reversed order.
Stacks in microprocessors are implemented by using register
called the Stack Pointer (SP), similar to the program
counter (PC), to keep track of available stack locations.
As items are added to the stack (pushed), the stack pointer is
moving up, and as items are removed from the stack (pulled
or popped), the stack pointer is moved down.
Instructions to Store and Retrieve
Information from the Stack
• PUSH: Increment the memory address in the
stack pointer (by one) and stores the contents
of the Program Counter on the top of the
stack
• POP: Discards the address of the top of the
stack and decrement the Stack Pointer by one
STACK TYPES
Ascending stack
An Ascending stack grows upwards. It starts from a low memory address
and, as items are pushed on to it, progresses to higher memory addresses.
Descending stack
A Descending stack grows downwards. It starts from a high memory
address, and as items are pushed on to it, progresses to lower memory
addresses.
Empty stack
In an Empty stack, the stack pointers points to the next free (empty)
location on the stack, i.e. the place where the next item to be pushed onto
the stack will be stored.
Full stack
In a Full stack, the stack pointer points to the top most item in the stack,
i.e. the location of the last item to be pushed onto the stack.

You might also like