CS212 Spring 2002 2/6/2002 Lecture 4: Introduction to Part 2 of the Project ------------------------------------------------------------------------------- Announcements: + BOOM: http://www.cs.cornell.edu/boom/default.htm + online submission: having problems -- part 1 due 1pm Friday + book: on reserve and bookstore readings: 4.1-4.2, all of 6, 8.3 (text files!) + partners: will be sending out names ------------------------------------------------------------------------------- Summary: + project management + group dynamics: forming, storming, norming, performing, transforming + models of programming: pair programming, extreme programming (see CS211, Programming Resources) ------------------------------------------------------------------------------- Overview: + new language: Bali - grammar - statements + sam-code for Bali - basics (declarations, arithmetic, logic, assignment, return) - control structures + compiler - overall goal - file I/O - expression/statement parsing - style issues ------------------------------------------------------------------------------- Introduction to Bali: + what is Bali? + Functions + Variables + Arithmetic + Control ------------------------------------------------------------------------------- Syntax Summary: + Character set: ASCII. + Comments: Use % for single-line comments. + White space: Generally, it is ignored. + Data types: - numbers: strictly integers with limits of Java's int type. - logical values: 0 (false), 1 (true). - strings and characters: none. + Identifiers: a-z, A-Z, case sensitive. + Keywords: main, int, bool, return, if, else, do, while. + Operators: - arithmetic: +, -, *, and #. - relations: = (equal), <, >. - logic: & (and; evaluates both arguments), | (inclusive or), ! (not) - assignment: := + Punctuation: - Expressions with operators must use parentheses (()). - Statements terminate with a semicolon (;). - Example statements: expression. Example: (1+1); declaration: int x, y; assignment: x := 3; return: return (1+2); ------------------------------------------------------------------------------- Bali examples: % Example 1: main() int x; x := 1; return x; % Example 2: main() bool x; x := (0 & 1); return x; % Example 3: 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; % Example 4: main() int x; x := 1; while ( (x < 5) ) x := (x + 1); return x; How to do: not equal? <= and >= ? ------------------------------------------------------------------------------- sam-code: variables, arithmetic, logic, assignment (reminders!) + need for writing more sam-code and compiler + assume all sam-code goes into one function: main() (so FBR=0) + rules for relative addressing: - To allocate space for each variable, including a "return variable" (rv), adjust the stack pointer "up" the stack with ADDSP value. - To place a value onto a stack, use `PUSHIMM value'. - To store a variable value, which means assigning a variable, use `STOREOFF address'. - To retrieve a variable value, use `PUSHOFF address'. - To return a variable value, use `ADDSP value' to move the SP "down" to 1. ------------------------------------------------------------------------------- BALI: main() int x; x := 13; return x; SAM: (sam1.txt) ____________ // allocate space for 2 variables (x, rv) ____________ // push value of 13 onto the stack ____________ // store the value of 13 in x's address ____________ // push the value of x from it's address ____________ // store the value of x in rv to return ____________ // adjust the stack pointer to leave rv "alone" ____________ // stop the program ------------------------------------------------------------------------------- sam-code: Labels and Jumps + JUMP: think "goto": move to a different location in the code + LABEL: text for a place you wish sam-code to jump to Syntax: this_is_a_label: samstuff Example: PUSHIMM 2 JUMP blurt blurt: PUSHIMM 3 ADD STOP Program Counter: PC| Program --+------------------------- : PUSHIMM 2 : JUMP blurt // address 2 : PUSHIMM 3 : ADD : STOP ------------------------------------------------------------------------------- sam-code: selection + 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; ------------------------------------------------------------------------------- + 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 + 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. ------------------------------------------------------------------------------- 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 + 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 ------------------------------------------------------------------------------- compiler: cs211in and file streams expression parsing -------------------------------------------------------------------------------