MIT6_004s09_lec13

MIT6_004s09_lec13 - MIT OpenCourseWare http://ocw.mit.edu...

Info iconThis preview shows pages 1–4. 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

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: MIT OpenCourseWare http://ocw.mit.edu For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms . 6.004 Computation Structures Spring 2009 Stacks&Procedures 1 6.004 Spring 2009 3/19/09 Stacks and Procedures Fritzs stack is easily overowed Lets see, before going to class,. Id better look over my 6.004 notes but Ill need to find my backpack first that means Ill need to find the car meaning, Ill 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 tonight! modified 3/19/09 12:08 Stacks&Procedures 2 6.004 Spring 2009 3/19/09 Where we left things last week int n = 20; int r = 1; while (n>0) { r = r*n; n = n-1; } int fact(int n) { return r; } fact(4); Procedures & Functions - Reusable code fragments that are called as needed - Single named entry point - Upon completion control is transferred back to caller - Parameterizable - Local state (variables) Stacks&Procedures 3 6.004 Spring 2009 3/19/09 Procedure Linkage: First Try int fact(int n) { if (n>0) return n*fact(n-1); else return 1; } fact(4); fact(4) = 4*fact(3) fact(3) = 3*fact(2) fact(2) = 2*fact(1) fact(1) = 1*fact(0) fact(0) = 1 Oh no, not recursion! Didnt we get enough of that in 6.001? Proposed convention: pass arg in R1 pass return addr in R28 return result in R0 questions: nargs > 1? preserve regs? Lets just use some registers. Weve got plenty Stacks&Procedures 4 6.004 Spring 2009 3/19/09 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! Stacks&Procedures 5 6.004 Spring 2009 3/19/09 Revisiting Procedures Storage Needs Basic Overhead for Procedures/Functions: Arguments f(x,y,z) or perhaps... sin(a+b) Return Address back to caller Results to be passed back to caller. In C its the callers 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 specific to a particular activation of a procedure; collectively, they may be viewed as the procedures activation record....
View Full Document

Page1 / 7

MIT6_004s09_lec13 - MIT OpenCourseWare http://ocw.mit.edu...

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

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