{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L13-4up

# L13-4up - Stacks and Procedures Lets see before going to...

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

Stacks&Procedures 1 6.004 – Fal 2010 10/21/10 Stacks and Procedures Fritz’s stack is easily overfowed Lets see, before going to class,. I’d be ± er look over my 6.004 notes… but I’ll need to ±nd my backpack ±rst… that means I’ll need to ±nd the car… meaning, I’ll need to remember where I parked it… maybe it would help if I could remember where I was last night… um, I forget, what was I going to do. .. Lab 4 due Today! Stacks&Procedures 2 6.004 – Fal 2010 10/21/10 Where we le±t things last time… int r = 1; while (n>0) { r = r*n; n = n-1; } int fact(int n) { return r; } fact(4); Procedures & Functions - Reusable code ±ragments that are called as needed - Single “named” entry point - Upon completion control is trans±erred back to caller - Parameterizable - Local state (variables) ADDC(R15, 37, R0) 010010 Z Z Z Z ? Stacks&Procedures 3 6.004 – Fal 2010 10/21/10 Procedure Linkage: First Try int fact(int n) { if (n>0) return n*fact(n-1); else return 1; } fact(4); ±act(4) = 4*±act(3) ±act(3) = 3*±act(2) ±act(2) = 2*±act(1) ±act(1) = 1*±act(0) ±act(0) = 1 “Recursion” de²ned: a recursive defnition is simply a recursive de²nition. Proposed convention: pass arg in R1 pass return addr in R28 return result in R0 questions: nargs > 1? preserve regs? Let’s just use some registers. We’ve got plenty… Stacks&Procedures 4 6.004 – Fal 2010 10/21/10 Procedure Linkage: First Try int fact(int n) { if (n>0) return n*fact(n-1); else return 1; } fact(3); fact: CMPLEC(r1,0,r0) BT(r0,else) MOVE(r1,r2) | save n SUBC(r2,1,r1) BR(fact,r28) MUL(r0,r2,r0) BR(rtn) else: CMOVE(1,r0) rtn: JMP(r28,r31) main: CMOVE(3,r1) BR(fact,r28) HALT() Proposed convention: pass arg in R1 pass return addr in R28 return result in R0 questions: nargs > 1? preserve regs? Need: O(n) storage locations!

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

View Full Document
Stacks&Procedures 5 6.004 – Fal 2010 10/21/10 Revisiting Procedure’s Storage Needs Basic Overhead for Procedures/Functions: • Arguments f(x,y,z) or perhaps. .. sin(a+b) • Return Address when returning to caller • Results to be passed back to caller. In C it’s the caller’s job to evaluate its arguments as expressions, and pass their resulting values to the callee… Thus, a variable name is just a simple case of an expression. Temporary Storage: intermediate results during expression evaluation. (a+b)*(c+d) Local variables: { int x, y; ... x . .. y . ..; } Each of these is speci±c to a particular activation of a procedure; collectively, they may be viewed as the procedure’s activation record. Stacks&Procedures 6 6.004 – Fal 2010 10/21/10 Lives of Activation Records fact(3) TIME A procedure call creates a new activation record. Caller’s record is preserved because we’ll need it when call ±nally returns. Return to previous activation record when procedure ±nishes, permanently discarding activation record created by call we are returning from. fact(3) fact(2) int fact(int n) { if (n > 0) return n*fact(n-1); else return 1; } fact(3) fact(2) fact(1) fact(3) fact(2) fact(1) fact(3) fact(2) fact(1) fact(0) fact(3) fact(2) fact(3) Stacks&Procedures 7 6.004 – Fal 2010 10/21/10 Insight (ca. 1960): We need a STACK!
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 6

L13-4up - Stacks and Procedures Lets see before going to...

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

View Full Document
Ask a homework question - tutors are online