Stack Frame - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Stack Frame

Description:

to setup the stack (known as a frame) for the function at the start. to execute ... It is a convention that the frame pointer are 8 byte aligned so it may be ... – PowerPoint PPT presentation

Number of Views:298
Avg rating:3.0/5.0
Slides: 18
Provided by: MurrayP2
Category:
Tags: frame | stack

less

Transcript and Presenter's Notes

Title: Stack Frame


1
Stack Frame
  • Stack Frame can be used to store local variables

For parmeters that I pass to other functions
Parent stack frame
For parmeters that are passed to me
Parameter area
2
Code generated by GCC
  • Introduction
  • Code generation examples that we have looked at
    so far have been Ideal
  • Going to look at code generated by the GCC - MIPs
    cross compiler mcompile -S test.c
  • mcompile invokes the gcc MIPs compiler
  • -S option says stop at assembly stage
  • No optimisation specified so none performed
  • All C statements considered independently
  • All variables stored in memory
  • May appear really Stupid

3
Code generated by GCC
  • Introduction
  • Code generated adheres to a set of conventions
    set down by the MIPs corporation
  • Code generated a function at time
  • Two different types functions
  • non leaf (function calls another)
  • leaf (function doesnt call another)
  • Main difference is in the way they allocate space
    on the stack

4
Non Leaf functions
  • Functions that call other functions
  • Have to follow caller and callee save conventions
  • code generated for function contains sections
  • to setup the stack (known as a frame) for the
    function at the start
  • to execute the function
  • to reset the stack at the end

5
Non Leaf functions(2)
  • As it is possible in some cases for the stack
    pointer to alter during execution of a function
    another register call a frame pointer (30) is
    used as a common point of reference
  • e.g. lw 2, 12(fp) will always load the same
    word

6
Non Leaf Functions(3)
  • Assume
  • No Function has more than 4 arguments
  • All local variables are integers
  • All local variables are stored in memory
  • No need to worry about caller and callee saved
    regs apart from RA and FP
  • All arguments are passed in registers but will be
    copied to the stack on subroutine entry
  • All results returned in registers

7
Non Leaf functions(4)
  • Frame components
  • arguments (to functions it calls)
  • although parameters (1 -4) passed in registers
    (4 - 7) space is still created on the stack
  • never less than 16 (4 words) bytes allocated
  • Locals
  • space for the for a functions local variables
  • Reg Save
  • space to store caller and callee saved registers
    including the stack and frame pointer
  • It is a convention that the frame pointer are 8
    byte aligned so it may be necessary to add 4
    bytes padding

8
Stack frame for non-leaf functions
SP FP (after entry)
Arguments for functions called (max 4)
size_params 4 no_params 16
All local variables
size_locals no_locals 4
Padding
Padding (if (size_params size_locals)mod 8
then 4
else 0)
Saved RA FP
SP FP (before entry)
size_saved 8
Arguments to called function
9
Stack frame for non-leaf functions(2)
  • The size of the stack frame is calculated as
    follows
  • size_proc_frame size_params size_locals
    padding 8
  • The memory locations of the various components
    calculated as
  • parami FP size_proc_frame (i 4)
  • locali FP size_params (i 4)
  • old FP FP size_params size_locals padding
  • old RA FP size_params size_lacals padding
    4

10
Assembler Directives
  • .text
  • Defines the section where the instructions are to
    be placed
  • .sdata
  • Defines the section where static data is to be
    placed
  • .align
  • specifies an alignment greater than would
    normally be required for the next data directive
    (specified as a power of 2)
  • .ent .end
  • Mark the start and end of a function (often used
    by a debugger)

11
Assembler Directives
  • .frame .mask .fmask
  • Used by a debugger to obtain details about the
    current stack frame

12
Example
int absolute(int c) if (c lt 0)
return(-c) else return(c) int
greater(int a, int b) int c if (a gt b)
c absolute(a) else c absolute(b)
return c
main() int x,y,z z greater(x,y)
writeint(z)
13
main()
L7 move sp,fp lw 31,36(sp) lw fp,32(s
p) addu sp,sp,40 j 31 .end main
.text .align 2 .globl main .ent main main .fr
ame fp,40,31 .mask 0xc0000000,-4 .fmask 0x0000
0000,0 subu sp,sp,40 sw 31,36(sp) sw fp,32
(sp) move fp,sp jal __main lw 4,16(fp) lw
5,20(fp) jal greater sw 2,24(fp) lw 4,24(
fp) jal writeint

14
int greater(int a, int b)
.align 2 .globl greater .ent greater greater
.frame fp,32,31 .mask 0xc0000000,-4 .fmask 0x0
0000000,0 subu sp,sp,32 sw 31,28(sp) sw fp
,24(sp) move fp,sp sw 4,32(fp) sw 5,36(f
p) lw 2,32(fp) lw 3,36(fp) slt 2,3,2 be
q 2,0,L5 lw 4,32(fp) jal absolute sw 2,16
(fp) j L6
L5 lw 4,36(fp) jal absolute sw 2,16(fp)
L6 lw 2,16(fp) j L4 L4 move sp,fp lw
31,28(sp) lw fp,24(sp) addu sp,sp,32 j 31
.end greater
15
Stack frame for leaf functions
SP FP (after entry)
All local variables
size_locals no_locals 4
Padding
Padding (if (size_locals mod 8)
then 4
else 0)
Saved RA FP
SP FP (before entry)
size_saved 8
Arguments to called function
16
Stack frame for non-leaf functions(2)
  • The size of the stack frame is calculated as
    follows
  • size_proc_frame size_locals padding
    4
  • The memory locations of the various components
    calculated as
  • parami FP size_proc_frame (i 4)
  • locali FP (i 4)

17
int absolute(int c)
.align 2 .globl absolute .ent absolute absolute
.frame fp,8,31 .mask 0x40000000,-8 .fmask 0
x00000000,0 subu sp,sp,8 sw fp,0(sp) move
fp,sp sw 4,8(fp) lw 2,8(fp) bgez 2,L2 l
w 2,8(fp) subu 3,0,2 move 2,3 j L1 j
L3
L2 lw 2,8(fp) j L1 L3 L1 move sp,fp
lw fp,0(sp) addu sp,sp,8 j 31 .end absolu
te
Write a Comment
User Comments (0)
About PowerShow.com