1 / 20

Procedures and Stacks

Learn about procedures, stacks, and static allocation in computer programming, including passing arguments, local storage, and returning results. Includes examples of recursive factorial.

charlottej
Download Presentation

Procedures and Stacks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Procedures and Stacks • Static Allocation • Stacks • Recursion As you push, so shall you pop! Handouts: Lecture Slides

  2. Need mechanisms for: Passing arguments Local storage Returning result Linking control Procedures int Fact( N ) int N ; { int F ; F = 1 ; while ( N > 0) { F = F * N ; N = N - 1 ; } return ( F ) ; }

  3. Static Allocation Given int x, y, z ; … z = x + y ; Translate as follows: • Assign fixed (constant) addresses to variables x, y and z • Translate each variable reference to a direct read/write of the assigned memory location Storage allocated at compile time!

  4. Move argument value K to local storageN Call toFact, save caller’s PC in r28 Fact will return result inr0by convention Store result in Z Static Allocation for Procedures How do we code a call Z = Fact( K ) ? LD(r31, K, r2) ST(r2, N, r31) BEQ(r31, Fact-label, r28) ST(r0, Z, r31)

  5. Unnecessary if r28 not modified in body of Fact Static Allocation for Procedures - II How do we code the procedure Fact( ) ? Fact-label: ST(r28, caller-PC, r31) code for body of Fact LD(r31, F, r0) LD(r31, caller-PC, r28) JMP(r28, r31) PC <PC> + 4 rc  <PC> JMP(ra, rc) PC  <ra>

  6. Problems with Static Allocation int Fact( N ) int N ; { if ( N > 0) return Fact( N-1 ) * N ; else return 1 ; } Factassumes Nis in0x1024 Overwrite value ofNwithN-1 when we call Fact( N-1)from Fact (N) ! Problems: 1. Nested procedure calls 2. Recursion We need a STACK !

  7. Stacks • 1960’s revolution in storage management • Easy to implement using contiguous memory and a “stack pointer” register • Used for: • Procedure calls and returns. Stack pointer points to storage locations of active procedure • Automatic allocation of local variables N argument Caller-PC F <SP> local storage

  8. Stack Implementation Conventions: Builds UP on push Stack pointer points to first unused location N Caller-PC F increasing addresses N-1 Fact-PC To push <x> SP <SP> + 4 MEM[ <SP> - 4 ] <x> To pop( ) a value into x x MEM[ <SP> - 4 > ] SP <SP> - 4 F <SP> unused unused

  9. Activation Record a.k.a. Stack Frame args • Return address of routine that called f (caller routine) old <LP> old <BP> increasing addresses <BP> locals ... temps <SP> unused reserved registers r27 = BP Base pointer: Points to 1st local. r28 = LP Linkage pointer: Saved <PC>. r29 = SP Stack pointer: Points to 1st unused location. r30 = XP Exception pointer: Saved <PC>.

  10. Stack Frame Details old old <LP> CALLER’s FRAME old old <BP> caller’s local 1 ... Caller’s return PC caller’s local p where does this point to? arg n increasing addresses ... arg 1 CALLEE’s FRAME old <LP> old <BP> <BP> local 1 ... local k <SP> unused

  11. Access to Local Frame To access jth local variable: LD(BP, 4(j-1), rx) ST(rx, 4(j-1), BP) To access kth argument: LD(BP, - 4(k+2), rx) ST(rx, - 4(k+2), BP) arg n ... arg 1 old <LP> old <BP> <BP> local 1 ... local k <SP> unused Why push arguments in reverse order ?

  12. Stack Macros PUSH(rx) push 4-byte value onto stack ADDC(SP, 4, SP) ST(rx, -4, SP) POP(rx) pop 4-byte value intorx LD(SP, -4, rx) SUBC(SP, 4, SP) ALLOCATE(k) reservekwords of stack ADDC(SP, 4*k, SP) DEALLOCATE(k) give backkwords SUBC(SP, 4*k, SP)

  13. Procedure Linkage Contract Calling Sequence: PUSH(arg n) Push arguments in right to … left (reverse) order to acco- PUSH(arg 1) mmodate variable #args. BEQ(r31, f, LP) Callf. DEALLOCATE(n) Clean up. Entry Sequence: PUSH(LP) Save <LP> <BP> PUSH(BP) for new calls. ADDC(SP, 0, BP) SetBPto frame base. ALLOCATE(locals) Allocate locals. (PUSH other registers) Preserve registers used.

  14. Procedure Linkage Contract - II Exit and Return Sequence: (POP other registers) Restore registers. ADDC(rval, 0, r0) Result in r0. ADDC(BP, 0, SP) Strip locals. POP(BP) Back to caller frame. POP(LP) Obtain return address. JMP(LP, r31) Return to caller. Stack pointer before and after Calling sequence should be the same!

  15. Recursive Factorial Stack Frames … z = Fact(3) ; … int Fact( N ) int N ; { if ( N > 0) return Fact( N-1 ) * N ; else return 1 ; } Caller • N = 3 Fact(3) • N = 2 Fact(2) • N = 1 Fact(1) • N = 0 <BP> Fact(0) <SP> unused

  16. Recursive Factorial Assembly - I z = Fact( N ) ; 0 PUSH(arg 1) Push argument BEQ(r31, Fact, LP) CallFact. DEALLOCATE( 1 ) Clean up arg upon return Calling sequence Fact: PUSH(LP) PUSH(BP) ADDC(SP, 0, BP) PUSH(r1) LD(BP, -12, r1) r1 = N BNE(r1, recur, r31) if (N > 0) ADDC(r31, 1, r0) BEQ(r31, rtn, r31) else return 1 ; Assume N is non-negative! 1 Entry sequence 2 Body of Fact

  17. Recursive Factorial Assembly - II … LD(BP, -12, r1) r1 = N BNE(r1, recur, r31) if (N > 0) ADDC(r31, 1, r0) BEQ(r31, rtn, r31) else return 1 ; recur: SUBC(r1, 1, r1) r1 = N - 1 PUSH(r1) push arg 1 BEQ(r31, Fact, LP) call Fact(N - 1) DEALLOCATE( 1 ) LD(BP, -12, r1) r1 = N MUL(r0, r1, r0) r0 = Fact(N - 1) * N rtn: POP(r1) restore register ADDC(BP, 0, SP) strip locals POP(BP) POP(LP) Restore link JMP(LP, r31) return Body of Fact (contd.) 3 4 Exit/Return sequence 5

  18. For Your Viewing PleasureSnapshots of Stack Frames - I 1 <BP> 0 Caller <BP> Caller ... ... N = 1 <SP> Fact(1) Caller <LP> Caller <BP> <SP> 3 2 Caller Caller ... ... N = 1 N = 1 Fact(1) Fact(1) Caller <LP> Caller <LP> Caller <BP> Caller <BP> <BP> <BP> Caller <r1> Caller <r1> <SP> N = 0 / <r1> Fact(0) <SP>

  19. For Your Viewing PleasureSnapshots of Stack Frames - II Caller ... 5 Caller ... N = 1 Fact(1) Caller <LP> 1 2 N = 1 Fact(1) Caller <BP> Caller <LP> Caller <r1> Caller <BP> <BP> N = 0 / <r1> Caller <r1> Fact(0) Fact <LP> N = 0 / <r1> Fact(0) <SP> Fact <BP> <BP> Fact <r1> <SP> 5 Caller ... <BP> 4 N = 1 Caller ... Fact(1) Caller <LP> N = 1 Caller <BP> <SP> Fact(1) <BP> Caller <r1> <SP>

  20. Next Time: b Processor Dilbert : S. Adams

More Related