CSC362 2011F, Class 39: The Run-Time Stack Overview: * Format of stack frames. * Tracing the stack. * Function/procedure initialization. * Function/procedure cleanup. Admin: * Phase 6 of the project is due next Wednesday. * Don't need to do arrays. * My primary plans for this coming weekend are to celebrate my wife's birthday and grade stuff from your class. I hope to be caught up by Monday. * Thoughts on Steganography CS extra? * EC for TK recital Saturday at 2pm. * EC for Erin Nichols events next Tuesday: Lunchtime talk and Tuesday extra. * EC for swim meet (Friday 6-9 pm, Saturday all day; two hours suffice). * Do you still want to ask questions on Phase 5? * Sam has a general observation. * Don't do everything at once within a phase. Eventually, you need to turn offsets within the stack frame into things that can be parameters to STAC instructions. We'll want to write new_relative (SP->info.reg, offset) ---- Last thing: Function calls! Let's review the layout of the stack and what happens to the stack while the program is running. * What goes on the stack for a function all? * Local variables * Parameters * Return address Original layout | | 1028 +--------+----------------------<- frame pointer |PREVFP | CURRENT FRAME 1024 +--------+ |RETADDR | 1020 +--------+ |PARAM0 | 1016 +--------+ |PARAM1 | STACK GROWS DOWN 1012 +--------+ | |LOCAL0 | v 1008 +--------+ |LOCAL1 | 1008 +--------+ <- stack pointer | | Call a procedure; What happens to the stack? | | 1028 +--------+--------------------------------- |PREVFP | PARENT FRAME 1024 +--------+ |RETADDR | 1020 +--------+ |PARAM0 | 1016 +--------+ |PARAM1 | STACK GROWS DOWN 1012 +--------+ | |LOCAL0 | v 1008 +--------+ |LOCAL1 | 1004 +--------+--------------------------------- <--- FRAME POINTER |1028 | CURRENT FRAME 1000 +--------+ |RETADDR | 996 +--------+ | PARAM0 | +--------+ | LOCAL0 | +--------+<- stack pointer | | When we're done with the procedure, we want to pop the frame. What are the STAC instructions to build up the stack for a procedure call? * Push frame poiner * Push return address * Push parameters * Decrement the stack pointer by the size of the activation record What are the STAC instructions to restore after we're done with the call? * Restore the stack pointer MOV %sp %fp (aka %sp = %fp) 1004 +--------+--------------------------------- <--- FRAME POINTER stack pointer |1028 | CURRENT FRAME 1000 +--------+ |RETADDR | 996 +--------+ | PARAM0 | +--------+ | LOCAL0 | +--------+ | | * "Pop" into the current frame pointer MOV %fp %sp(-4) OR MOV %fp %fp(-4) * JUMP %sp(-8) Note: poier == pointer == pointner == poitner We have left out a few things that go the stack ... * Return value! * Temporaries!