EE357Lecture10-4

EE357Lecture10-4 - Frame Pointer and Subroutines Problem is...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
EE 357 Lecture 10 Exceptions TRAPs Frame Pointer and Subroutines • Problem is that each executing subroutine needs its own value of FP • The called subroutine must save the caller’s FP and setup its own FP as the first thing it does when it is called. • The called subroutine must restore the caller’s FP before it returns • LINK and UNLK instructions are used to save/restore old FP and allocate space for locals FP SP FP + disp. -d i sp . caller’s stack area (its locals, saved regs.) params. RA local vars. Saved FP caller’s FP LINK Instruction • LINK should be first instruction of a subroutine – Pushes FP on top of stack and can also allocate local variable space • LINK An, #imm – An should be the FP – Saves current An (FP) onto the stack • (SP) – 4 ĺ SP; (An) ĺ (SP) – Sets FP to current SP (top of stack) • (SP) ĺ An – Adds imm. to SP to allocate space on the stack for local variables • (SP) + imm ĺ SP • imm. should be negative to allocate space caller’s stack area params. RA old FP caller’s stack frame params. RA local vars. SP new FP old FP SP old FP Before LINK After LINK UNLK Instruction • UNLK should be used just before RTS – Restores old FP by popping it off the stack as well as deallocating local variable space on the stack. • UNLK An – Copies An (i.e. FP) to SP so that SP now points at old FP. •(An ) ĺ SP – Pops old FP into An • ((SP)) ĺ An • (SP) + 4 ĺ SP caller’s stack area params. RA FP caller’s stack area params. RA local vars. SP new FP old FP SP old FP Before UNLK After UNLK
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Stack Frame Summary • A6 is used as FP • FP always points at old FP – Local variables are at lower addresses (above) where FP points [i.e. -4(FP), -8(FP), …] – RA is directly below old FP [i.e. 4(FP)] – Parameters are below RA [i.e. 8(FP), 12(FP), 16(FP)…] • SP is always pointing at current top of stack (above FP) params. RA local vars. SP FP old FP Return Values • Subroutines often need to return a value • HLL’s like C only allow 1 return value – Usually returned in a specific register – For M68000, let us assume D0 is the return value Return Value D0 temp ans short ans; void main() { ans = avg(1,5); } short avg(short a, short b) { short temp = 1; return a + b >> temp; } Stack Frame Example ANS DS.W 1 MAIN ... MOVE.W #5,-(SP) MOVE.W #1,-(SP) ($101A) BSR.L AVG ($101E) ADDA.L #4,SP MOVE.W D0,ANS ORG $1200 AVG LINK A6,#-2 MOVE.W #1,-2(A6) MOVE.W 8(A6),D0 ADD.W 10(A6),D0 MOVE.W -2(A6),D1 LSR.W D1,D0 UNLK A6 RTS ... ... 3FF0 3FEE 3FEC 3FEA 3FE8 3FE6 3FE4 3FF6 3FF2 3FF2 SP FP 3FF8 3FE2 Originally for MAIN, FP points to $3FF6 and SP points to $3FF2 Originally for MAIN, FP points to $3FF6 and SP points to $3FF2 Stack Frame Example ... ... 3FF0 0005 3FEE 0001 3FEC 3FEA 3FE8 3FE6 3FE4 3FF6 3FEE 3FF2 SP FP 3FF8 3FE2 We must push the parameters on in reverse order (5 then 1) before we can call AVG This causes the SP to now point to $3FEE We must push the parameters on in reverse order (5 then 1) before we can call AVG This causes the SP to now point to $3FEE ANS DS.W 1 MAIN ...
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

EE357Lecture10-4 - Frame Pointer and Subroutines Problem is...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online