EE357 Lecture 9

EE357 Lecture 9 - EE 357 Lecture 9 Use of system stack in recursive calls Stack Frames M68000 System Stack RAs are used in reverse(LIFO order

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: EE 357 Lecture 9 Use of system stack in recursive calls Stack Frames M68000 System Stack RAs are used in reverse (LIFO) order Processor automatically maintains system stack A7 is used as system stack pointer Points to top occupied element of stack Aliased with name SP [i.e. use (SP)+, -(SP), etc.] Easy68K initializes SP/A7 to $01000000 In this class we will start our stack at $3FF8 (just to use a smaller address). BSR will automatically PUSH the RA RTS will automatically POP the RA Subroutines and Stacks BSR instruction pushes the RA RTL: (SP) 4 SP RA (SP) (PC) + disp. PC RTS instruction pops the RA RTL: ((SP)) PC (SP) + 4 SP Recursive Factorial Routine Implementation Detail: Make sure each call of Fact is working with its own value of n Assembly: int ans; void fact(int n) { if(n == 1) ans = 1; else { // calculate (n-1)! fact(n-1); // now ans = (n-1)! // so calculate n! ans = n * ans; } } C Code: ORG $7000 ANS DS.W 1 ORG $8000 START MOVE.W #3,D0 BSR.L FACT MOVE D1,ANS STOP #$2700 ORG $8800 FACT CMPI.W #1,D0 BEQ.S NEQ1 MOVE.W D0,-(SP) SUBI.W #1,D0 BSR.S FACT MOVE.W (SP)+,D0 MULU.W D0,D1 BRA.S DONE NEQ1 MOVE.W #1,D1 DONE RTS Recursive Call Timeline Before calling yourself, you need to save copies of all your locally declared variables/parameters (e.g. n) Fact(3) {Save n=3} Fact(2) {Save n=2} Fact(1) ans=1 Restore n=2 Restore n=3 ans = (3 * ans) = 3 *2 Time ans = (2 * ans) = 2 *1 Class Exercise 2 ORG $7000 ANS DS.W 1 ORG $8000 START MOVE.W #3,D0 BSR.L FACT MOVE D1,ANS STOP #$2700 ORG $8800 FACT CMPI.W #1,D0 BEQ.S NEQ1 MOVE.W D0,-(SP) SUBI.W #1,D0 BSR.S FACT MOVE.W (SP)+,D0 MULU.W D0,D1 BRA.S DONE NEQ1 MOVE.W #1,D1 DONE RTS 0000 0000 00000000 (D0)= 0000 0000 ... 0000 0000 3FF8 (SP)= 0000 0000 3FE2 3FE4 3FE8 3FEA 0000 0000 0000 0000 7000 = ANS ... 3FF2 3FF4 3FF6 3FF8 3FF0 3FEE 3FEC 00000000 (D1)= Show how the contents of Stack modified as we step through the factorial program execution in class Recursive Routine Example ORG $7000 ANS DS.W 1 ORG $8000 START MOVE.W #3,D0 BSR.L FACT MOVE D1,ANS STOP #$2700 ORG $8800 FACT CMPI.W #1,D0 BEQ.S NEQ1 MOVE.W D0,-(SP) SUBI.W #1,D0 BSR.S FACT MOVE.W (SP)+,D0 MULU.W D0,D1 BRA.S DONE NEQ1 MOVE.W #1,D1 DONE RTS 0000 0000 00000000 (D0)= 0000 0000 ... 0000 0000 This is where we will place the final n! answer when finished calculating it. During calculation we will keep it in D1 This is where we will place the final n! answer when finished calculating it. During calculation we will keep it in D1 3FF8 (SP)= 0000 0000 3FE2 3FE4 3FE8 3FEA 0000 0000 0000 0000 7000 = ANS ... 3FF2 3FF4 3FF6 3FF8 3FF0 3FEE 3FEC 00000000 (D1)= Recursive Routine Example ORG $7000 ANS DS.W 1 ORG $8000 START MOVE.W #3,D0 BSR.L FACT MOVE D1,ANS STOP #$2700 ORG $8800 FACT CMPI.W #1,D0 BEQ.S NEQ1 MOVE.W D0,-(SP) SUBI.W #1,D0 BSR.S FACT MOVE.W (SP)+,D0 MULU.W D0,D1 BRA.S DONE NEQ1 MOVE.W #1,D1 DONE RTS 0000 0000 00000003 (D0)= 0000 0000...
View Full Document

This note was uploaded on 04/11/2009 for the course EE 357 taught by Professor Mayeda during the Fall '08 term at USC.

Page1 / 16

EE357 Lecture 9 - EE 357 Lecture 9 Use of system stack in recursive calls Stack Frames M68000 System Stack RAs are used in reverse(LIFO order

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