Lecture29 - 0306-250 Assembly Language Programming Lecture...

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: 0306-250 Assembly Language Programming Lecture Twenty-Eight • Stack Frame Instructions • Subroutine Stack Frames Portions following D. Scott Wills, ECE, Georgia Institute of Technology. LEAS S12CPUV2 Reference Manual, Rev. 4.0, p. 220. 2 LEAS Stack Allocation/Deallocation • Allocate stack space for two bytes LEAS -2,SP • Allocate stack space for two words LEAS -4,SP • Deallocate stack space for two bytes LEAS 2,SP • Deallocate stack space for two words LEAS 4,SP 3 Run-Time Memory Typical Organization Addresses low Code Static data (global variables) Stack (memory for subroutines) high Heap (memory for dynamically allocated data) 4 Storage Allocation Strategies • Static allocation – All storage allocated by compiler – No recursion or dynamic memory allocation • Stack allocation – Run time storage managed as a stack – Values of locals—not retained when an activation ends – Called activation—can not outlive caller • Heap allocation – Activation records allocated/deallocated in any order – Some form of garbage collection or compaction needed to reclaim space 5 Example: Storage Allocation Running Example: int P; (Global or Static) int foo (int F) { (Local or Automatic) int G, H = 5; return (G); } movw tfr pulx rts H G x SP Ret. Addr. Ret. Value Foo: pshx tfr leas movw FP sp,x -4,x #5,-4,x ;Save old FP ;New FP ;Alloc. 2 W ;Z ß 5 -2,x,4,x ;R.V. x,sp ;Dealloc. ;Restore FP Activation Frame • Dynamically allocated storage associated with a subroutine call • Frame Pointer (X) ® Activation Frame ToS = Top of stack 6 Organization of Stack Frames Foo Example: int Foo (int F) { int G, H = 5; return (G); } Bar Example: int Bar () { int M; Active Stack Frames Foo’’ Foo’ Bar Foo Main Stack H Callee Allocated HCS12 Allocated Caller Allocated G Old FP Return addr. Active Frame For Foo Return value F M = foo (M); } 7 Function Calls and Returns • Call sequence of caller – Allocate stack space for actual parameters and return value – Put actual parameters onto stack where callee can find them – Branch to callee • Call sequence of callee – Allocate stack space for frame pointer and local variables – Execute function code • Return sequence of callee – Copy return value to caller-allocated stack space for it – Deallocate stack space for local variables and frame pointer – Return to caller • Return sequence of caller – Copy return value, if necessary – Deallocate stack space for actual parameters and return value – Continue execution 8 Example: Procedure Call and Return X: Frame Pointer—Points to subroutine’s frame on stack Running Example: int foo (int F, *G) { int A, B = 7; int C[4]; return (A); } C[3] C[2] -10 C[1] -8 C[0] foo: pshx tfr sp,x leas -12,sp movw #7,-4,x -12 -6 B 7 A movw -2,x,4,x tfr x,sp pulx rts X -4 -2 FP (ToS) R.A. R.V. A G SP F 9 Example: Multiple Procedure Calls and Returns Running Example: int foo (int F, *G) { return (A); } int bar () { int M, N; M = foo (M, &N); } bar: pshx tfr leas Bar FP sp,x -4,sp movw leax pshx leax leas foo 2,sp+,-2,x 4,sp tfr pulx 2 -2,x,2,-sp -4,x foo’s R.A. 4 8,sp -2,sp jsr movw leas 0 R.V. x,sp 6 8 G &N F M -10 -8 N M -2 old F.P. SP -6 -4 FP bar’s R.A. 10 ...
View Full Document

This note was uploaded on 05/06/2010 for the course EECC 0306-250 taught by Professor Roymelton during the Fall '10 term at RIT.

Ask a homework question - tutors are online