2. Call Stack
A stack data structure storing
information of a computer program's
active subroutines.
Whilst important for the software's
proper functioning, its details are
usually hidden and usage is automated
in high-level programming languages.
3. Call Stack Purpose
Keeping record of the point to which
each active subroutine should return
control when finished.
4. Active Subroutine
A routine that has been called but
has not finished execution yet.
Afterwords, control should be
returned to the point where the call
has been made.
Routines may be nested to any level
and recursion is possibe – hence the
stack structure.
5. For example, the DrawSquare
subroutine calls the DrawLine
subroutine from four different
places. DrawLine must know where
to return once completed.
This is accomplished by pushing the
address following the call
instruction – the return address –
onto the stack with each call.
6. Call Stack Inner Workings
The caller pushes the return address onto
the stack (winding).
The called subroutine, when it finishes,
pops the return address off the stack and
transfers control to it (unwinding).
If a called subroutine calls on yet another
subroutine, it will push another address
onto the stack, and so on, with the
information stacking up and unstacking as
the program dictates.
7. Should pushing consume all the
space allocated for the call stack, an
error called stack overflow will
occur.
There is usually a single call stack
associated with each process thread.
However, the program may create
additional call stacks for tasks such as
signal-hadling or cooperative
multitasking.
8. Additional Call Stack Functions
Local data storage – keeping local-scope
variable values.
Parameter passing – storage for values
passed by calling code.
Evalution stack – in some cases
operands for logical and
arithmetic operations may be
stored in the call stack.
Current instance pointer –
for this pointer in
object-oriented languages.
9. Structure
A call stack is
composed of
stack frames,
machine and
application banary
interface-
dependant data
structures
containing
subroutine state
information.
10. The Stack Frame
Generally speaking, a
procedure's stack frame contains
all the information necessary to
save and restore the state of the
procedure.
11. Strictly speaking, it is only necessary
for the calling program and the
called procedure to agree on the
structure of the stack frame for each
procedure call.
However, the specification of a
calling convention facilitates the use
of procedure libraries by defining
the structure of the stack frame
uniformly for all procedure calls.
12. The frame pointer is
stored in register $30,
also known as $fp. A
stack frame consists of
the memory on the
stack between the
frame pointer and the
stack pointer.
Three steps are
necessary to call a
Calling convention used in the
MIPS architecture stack frame procedure.
13. 1. Pass the arguments. The first
four arguments are passed in
registers $a0-$a3. The remaining
arguments are pushed onto the
stack.
2. Save the caller-saved registers.
This includes registers $t0-$t9, if
they contain live values at the call
site.
The endless MIPS
3. Execute a jal instruction. cycle
14. 1. Pass the arguments. The first
four arguments are passed in
registers $a0-$a3. The remaining
arguments are pushed onto the
stack.
2. Save the caller-saved registers.
This includes registers $t0-$t9, if
they contain live values at the call
site.
The endless MIPS
3. Execute a jal instruction. cycle
15. Within the called routine, the following steps
are necessary:
1. Establish the stack frame by subtracting
the frame size from the stack pointer.
2. Save the callee-saved registers in the
frame. Register $fp is always saved. Register
$ra and registers $a0-$a3 need to be saved if
they are in use and the routine itself makes
calls. Any of the registers $s0- $s7 that are
used by the callee need to be saved.
3. Establish the frame pointer by adding the
stack frame size to the address in $sp.
16. To return from a call, a function places the
returned value into $v0 and executes the
following steps:
1. Restore any callee-saved registers that
were saved upon entry.
2. Pop the stack frame by subtracting the
frame size from $sp.
3. Return by jumping to the address in
register $ra.
17. Debugging
The purpose of a debugger such
as GDB (gnu.org/software/gdb) is
to allow you to see what is going
on “inside” another program
while it executes--or what
another program was doing at
the moment it crashed.
18. In Practice
An illustration to
viewing the call
stack using GDB.
1. Compile your
program with the
-g option, like
cc -g -o p1 p1.c
19. 2. Navigate to your program's directory
and run GDB: gdb your_program
If all went fine, you will land on a
command prompt.
20. 3. Then install some breakpoints using the
break command:
break function_name
4. Now run. The program will proceed
until the first breakpoint.
21. You can select a frame using the frame n
command and view frame information
using the info frame n command.
Some of the details this command
displays are the addresses of the frame,
the next frame down (called by this
frame) and the next frame up (caller of
this frame).
22. View the call stack using the backtrace
command. A backtrace is a summary of
how your program got where it is. It
shows one line per frame, for many
frames, starting with the one currently
in execution (frame zero), followed by its
caller (frame one), and on up the stack.