// Basic: c.sam // // int main() { // int x,y; // x := 10; // y := 20; // return add(x,y); // } // int add(int a,int b) { // return a+b; // } PUSHIMM 0 // space for return value of program (result of main()) PUSHFBR // save current FBR (starts at 0) PUSHIMM 1 // need to push 1 for FBR POPFBR // set FBR to 1 JSR main // invoke main and push return address on the stack POPFBR // reinstall old FBR STOP // end program main: // label of first command is function name ADDSP 2 // allocate local variables PUSHIMM 10 // push 10 STOREOFF 2 // store x PUSHIMM 20 // push 20 STOREOFF 3 // store y //-----------------------// // set up frame for add // //-----------------------// // top of old frame: PUSHIMM 0 // space for rv // build new frame: PUSHFBR // store current FBR (saved FBR) PUSHOFF 2 // push parameter x (value of x) PUSHOFF 3 // push parameter y (value of y) PUSHSP // push SP --------- PUSHIMM 3 // # of params+1 \___________ set up FBR SUB // SP - (params+1) / POPFBR // store new FBR --- // execute callee code: JSR add // go to add function // finish execution: ADDSP -2 // pop off params POPFBR // set FBR to previous, or saved, value (meaning: restore FBR) STOREOFF -1 // save returned value ADDSP -2 // pop off local vars JUMPIND // return from main (pops off ra) // add function: add: PUSHOFF 1 // push value of a PUSHOFF 2 // push value of b ADD STOREOFF -1 // store x+y to return JUMPIND // return to caller (pops off ra)