CS212 Spring 2002 2/20/2002 Lecture 5: Introduction to Part 2 of the Project (continued) ------------------------------------------------------------------------------- [0] Announcements: + policy reminder: read online announcements every day + online submission: alive and working! + partners: sent out names already -- contact TAs if you need to find one(s) ------------------------------------------------------------------------------- [1] Summary from last time: + new language: Bali + sam-code for Bali - basics (declarations, arithmetic, logic, assignment, return) - control structures (just labels, jumps) ------------------------------------------------------------------------------- [2] Overview: + more about selection and repetition in SaM + compiler - overall goal - file I/O - expression/statement parsing - style issues ------------------------------------------------------------------------------- [3] sam-code: selection + Bali example: main() int x; bool flag; x := 3; flag := 0; % giving flag a dummy value if ( (x > 2) ) % remember that expressions must be surrounded by () flag = true; else flag = false; return flag; + sam-code instructions: labels and jumps: JUMP label label: instruction JUMPC label If the top of the stack is true (1) go to label. Otherwise, go to instruction just below JUMPC label instruction. + Basics in sam-code (sam3.txt): ADDSP ___ // adjust SP to account for rv, x, and flag PUSHIMM __ // push value of 3 STOREOFF __ // store the 3 in for x PUSHIMM __ // push the value of 0 (false) STOREOFF __ // store the 0 for flag PUSHOFF __ // push the value stored in V[1+FBR] (the address of x) PUSHIMM __ // push the value with to compare x GREATER // find Vbelow > Vtop and push the result to the top |=================================| Page 1 |=================================| + Now what?!? - Jump to a block of sam-code that handles the case of the condition being true; or, - Continue executing sam-code because the condition was false. + To test if the last condition was true: JUMPC label - If Vtop is true, PC<-label. So, SaM will jump to the instruction with label. - Otherwise, PC<-PC+1. So, SaM will execute the next instruction underneath the JUMPC instruction. JUMPC correct // check if the result of GREATER is true (1) or false (0) PUSHIMM 1 // false: push the value 0 (false) STOREOFF 2 // store the value false for flag JUMP continue // continue with remaining program correct: // true: PUSHIMM 1 // push the value 1 (true) STOREOFF 2 // store the value true for flag JUMP continue // continue with remaining program continue: // continue with program: PUSHOFF 2 // push the value of flag STOREOFF 0 // store the value of flag in rv ADDSP -2 // reset the SP STOP // done with the program redundant code: JUMP continue // continue with remaining program continue: // continue with program: The correct block will "fall through" to the remaining program. ------------------------------------------------------------------------------- [4] sam-code: do-while + Example: main() int x; x := 1; do x := (x + 1); while ( (x < 5) ); return x; + what's "while"? begin: if c then do stuff goto begin else Note: Bali requires the else-clause, we need a goto here. goto done done: remaining program |=================================| Page 2 |=================================| + sam-code (sam5.txt) for example: ADDSP __ // leave space for x and rv PUSHIMM __ // push 1 on the stack STOREOFF __ // store the value 1 for x looplabel: // label the loop starting at the "do" PUSHOFF __ // retrieve the current value of x PUSHIMM __ // push 1 onto the stack ADD // add 1 to the current value of x STOREOFF __ // store the new value of x PUSHOFF __ // retrieve the new value of x PUSHIMM __ // the value x is compared to, which is 5 LESS // compare x JUMPC looplabel // repeat the loop if the comparison succeeded PUSHOFF __ // push the current value of x STOREOFF __ // store the current value of x to return ADDSP __ // adjust the SP to return x STOP // stop the program ------------------------------------------------------------------------------- [5] Compiler: + overall goal: Compiler converts ______________________ into ______________________ . (for SaM) Ultimately, you will build an EXPRESSION PARSER (convert one expression into another) Diagram: (I->C->O) + File I/O (streams, Java) + Expression parsing (see also CS211 notes; specifics in written assignment) + General advice - software engineering (top-down, bottom-up, stubbing) |=================================| Page 3 |=================================|