Section 3 with Remik (rz33@cornell.edu) Meeting #3 Notes ******************************************************** Administrivia: Include headers for each file submitted, otherwise 1 point deduction per file Grading scale will be 100 point based (not 7 as I alluded to last week) Get the grades from the CSUGLAB grades link (find on course website) Review: High level language compiled to Sam - The JUMP(C) instruction - If, else statements - do, while loops Bring together with syntactic sugar example: for loop - explain equivalency - Sam code example JUMPC and the 'if' statement **************************** Bali program: main() int x; x := 98; if ( (x < 99) ) x := (x + 1); else x := 100; return x; SAM equivalent: ADDSP 2 // rv & x variables PUSHIMM 98 STOREOFF 1 // initialize x PUSHOFF 1 PUSHIMM 99 LESS // precondition JUMPC true false: // test failed; label used for illustrative purposes PUSHIMM 100 STOREOFF 1 // x := 100 true: // test passed PUSHIMM 1 PUSHOFF 1 ADD // x := x + 1 STOREOFF 0 ADDSP -1 STOP Syntactic Sugar Employing JUMPC: the 'for' loop statement ********************************************************* The 'for' loop is just a 'while' loop rewritten in a more concise, precondition/postcondition form. For example, we might have a loop with a general structure as follows: int counter, googol; counter := 0; googol := 10; while (counter < 100) { googol := (googol * 10); counter := (counter + 1); } which is equivalent to int counter, googol; for( counter := 0, googol := 10; // initialize (counter < 100); // test counter := (counter + 1) // increment ) { googol := (googol * 10); } In SAM we would have: ADDSP 3 // rv, counter & googol PUSHIMM 0 STOREOFF 1 // counter PUSHIMM 10 STOREOFF 2 // googol looptest: PUSHOFF 1 PUSHIMM 100 LESS JUMPC true JUMP exitloop // we've calculated googol true: PUSHOFF 2 PUSHIMM 10 TIMES STOREOFF 2 // googol = googol * 10 PUSHOFF 1 PUSHIMM 1 ADD STOREOFF 1 // counter = counter + 1 JUMP looptest // try to loop again exitloop: // code following loop PUSHOFF 2 STOREOFF 0 ADDSP -2 STOP // what's the final answer? should be non-sense :)