Section 3 with Remik (rz33@cornell.edu) Meeting #8 Notes ----------------------------------------------------------- Administrivia: Assignment #3 is out Review: Frame Base Registers by Example (Bali->SAM) Bali program: ----------------------------------------------------------- main() int x, y; x := 3; y := 4; return euclideanDistanceSquared(x, y); euclideanDistanceSquared(x, y) x := sqr(x); y := sqr(y); return (x + y); sqr(x) return (x * x); SAM equivalent: ----------------------------------------------------------- virtual_machine: // reserve return variable PUSHIMM 0 // save where main entry was PUSHFBR // load the new offset for the next frame PUSHIMM 1 POPFBR // load the program JSR main POPFBR STOP =========================================================== main: // we have 2 local variables, the return will use the frame ADDSP 2 PUSHIMM 3 STOREOFF 1 PUSHIMM 4 STOREOFF 2 // prepare for function call // first allocate the return variable placeholder PUSHIMM 0 // remember the state for 'main' PUSHFBR // set up the parameters to be passed PUSHOFF 1 PUSHOFF 2 PUSHSP // # of arguments plus 1 PUSHIMM 3 SUB POPFBR // now call function to get Euclidean Distance Squared JSR euclideanDistanceSquared // clean up by removing passed arguments ADDSP -2 POPFBR // now return // first copy the value returned from function call to virtual_machine frame STOREOFF -1 // remove local variables - x & y ADDSP -2 // go to caller - virtual_machine JUMPIND =========================================================== euclideanDistanceSquared: // no local variables, just function calls // prepare for function call on variable x PUSHIMM 0 PUSHFBR // supply x as function argument PUSHOFF 1 PUSHSP PUSHIMM 2 SUB POPFBR JSR sqr ADDSP -1 POPFBR // assign result to x, which is only a temporary cell STOREOFF 1 // prepare to call function with y PUSHIMM PUSHFBR // supply y as argument PUSHOFF 1 PUSHSP PUSHIMM 2 SUB POPFBR JSR sqr ADDSP -1 POPFBR // assign result to y STOREOFF // add x & y and return PUSHOFF 1 PUSHOFF 2 ADD // make the result the return variable STOREOFF -1 JUMPIND =========================================================== sqr: // no local variables, just 1 expression to be returned // x * x PUSHOFF 1 PUSHOFF 1 TIMES // return (x*x) STOREOFF -1 JUMPIND