Chapter 12 Variables and Operators Copyright The Mc
Chapter 12 Variables and Operators
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Agenda • Rules and usage of variables and operators in C • How C variables and operations are mapped to LC-3 • Mapping control structures to LC-3 12 -2
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Basic C Elements Variables • named, typed data items Operators • predefined actions performed on data items • combined with variables to form expressions, statements 12 -3
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Data Types C has three basic data types int double char integer (at least 16 bits) floating point (at least 32 bits) character (at least 8 bits) Exact size can vary, depending on processor • int is supposed to be "natural" integer size; for LC-3, that's 16 bits -- 32 bits for most modern processors 12 -4
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Scope: Global and Local Where is the variable accessible? Global: accessed anywhere in program Local: only accessible in a particular region Compiler infers scope from where variable is declared • programmer doesn't have to explicitly state Variable is local to the block in which it is declared • block defined by open and closed braces { } • can access variable declared in any "containing" block Global variable is declared outside all blocks 12 -5
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Example #include <stdio. h> void foo(); int global = 0; int main() { int local = 1; // local to main printf("main: global %d local %dn", global, local); global = 1; foo(); printf("main: global %d local %dn", global, local); } void foo() { int local; // local to foo local = 3; printf("foo: global %dn", global); global = 2; } 12 -6
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Example #include <stdio. h> void foo(); int global = 0; global variable int main() { int local = 1; // local to main printf("main: global %d local %dn", global, local); global = 1; foo(); printf("main: global %d local %dn", global, local); } void foo() { int local; // local to foo local = 3; printf("foo: global %dn", global); global = 2; } 12 -7
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Example #include <stdio. h> void foo(); int global = 0; int main() { int local = 1; // local to main printf("main: global %d local %dn", global, local); global = 1; foo(); printf("main: global %d local %dn", global, local); } void foo() { int local; // local to foo local = 3; printf("foo: global %dn", global); global = 2; } 12 -8
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Example #include <stdio. h> void foo(); int global = 0; main: global 0 local 1 int main() { int local = 1; // local to main printf("main: global %d local %dn", global, local); global = 1; foo(); printf("main: global %d local %dn", global, local); } void foo() { int local; // local to foo local = 3; printf("foo: global %dn", global); global = 2; } 12 -9
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Example #include <stdio. h> void foo(); int global = 0; main: global 0 local 1 foo: global 1 int main() { int local = 1; // local to main printf("main: global %d local %dn", global, local); global = 1; foo(); printf("main: global %d local %dn", global, local); } void foo() { int local; // local to foo local = 3; printf("foo: global %dn", global); global = 2; } 12 -10
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Example #include <stdio. h> void foo(); int global = 0; main: global 0 local 1 foo: global 1 main: global 2 local 1 int main() { int local = 1; // local to main printf("main: global %d local %dn", global, local); global = 1; foo(); printf("main: global %d local %dn", global, local); } void foo() { int local; // local to foo local = 3; printf("foo: global %dn", global); global = 2; } 12 -11
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Agenda • Rules and usage of variables and operators in C • How C variables and operations are mapped to LC-3 • Mapping control structures to LC-3 12 -12
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Symbol Table Like assembler, compiler needs to know information associated with identifiers • in assembler, all identifiers were labels and information is address Compiler keeps more information Name (identifier) Type Name Location in memory in. Global in. Local Scope out. Local. A out. Local. B Type int int Offset 0 0 -1 -2 Scope global main 12 -13
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Local Variable Storage Local variables are stored in a stack frame. Each block has its own stack frame. Symbol table “offset” gives the distance from the base of the frame. • R 5 is the frame pointer – holds address of the base of the current frame. • A new frame is pushed on the run-time stack each time a block is entered. • In a stack frame, base is the highest R 5 address of the frame, and variable offsets are <= 0. out. Local. B out. Local. A in. Local 12 -14
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Allocating Space for Variables Global data section 0 x 0000 • All global variables stored here (actually all static variables) • R 4 points to beginning Run-time stack • • Used for local variables R 6 points to top of stack R 5 points to top frame on stack New frame for each block (goes away when block exited) Offset = distance from beginning of storage area 0 x. FFFF • Global: LDR R 1, R 4, #4 • Local: LDR R 2, R 5, #-3 instructions global data run-time stack PC R 4 R 6 R 5 12 -15
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Example: Compiling to LC-3 #include <stdio. h> int in. Global; main() { int in. Local; /* local to main */ int out. Local. A; int out. Local. B; /* initialize */ in. Local = 5; in. Global = 3; /* perform calculations */ out. Local. A = in. Local++ & ~in. Global; out. Local. B = (in. Local + in. Global) - (in. Local - in. Global); } 12 -16
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Example: Symbol Table Name Type Offset Scope in. Global int 0 global in. Local int 0 main out. Local. A int -1 main out. Local. B int -2 main 12 -17
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Example: Code Generation ; main ; initialize variables AND R 0, #0 ADD R 0, #5 ; in. Local = 5 STR R 0, R 5, #0 ; (offset = 0) AND R 0, #0 ADD R 0, #3 STR R 0, R 4, #0 ; in. Global = 3 ; (offset = 0) 12 -18
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Example (continued) ; first statement: ; out. Local. A = in. Local++ LDR R 0, R 5, #0 ; ADD R 1, R 0, #1 ; STR R 1, R 5, #0 ; LDR NOT AND STR R 1, R 2, R 4, #0 R 1 R 0, R 1 R 5, #-1 ; ; ; & ~in. Global; get in. Local increment store get in. Global ~in. Global in. Local & ~in. Global store in out. Local. A (offset = -1) 12 -19
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Example (continued) ; next statement: ; out. Local. B = (in. Local + in. Global) ; - (in. Local - in. Global); LDR R 0, R 5, #0 ; in. Local LDR R 1, R 4, #0 ; in. Global ADD R 0, R 1 ; R 0 is sum LDR R 2, R 5, #0 ; in. Local LDR R 3, R 4, #0 ; in. Global NOT R 3, R 3 ADD R 3, #1 ADD R 2, R 3 ; R 2 is difference NOT R 2, R 2 ; negate ADD R 2, #1 ADD R 0, R 2 ; R 0 = R 0 - R 2 STR R 0, R 5, #-2 ; out. Local. B (offset = -2) 12 -20
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Agenda • Rules and usage of variables and operators in C • How C variables and operations are mapped to LC-3 • Mapping control structures to LC-3 12 -21
Chapter 13 Control Structures
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Control Structures Conditional • making a decision about which code to execute, based on evaluated expression • if-else • switch Iteration • executing code multiple times, ending based on evaluated expression • while • for • do-while The Condition being tested is a C expression, which evaluates to TRUE (non-zero) or FALSE (zero). 13 -23
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Generating Code for If Statement ; int main () { ; int x, y; ; if (x == 2) y = 5; LDR R 0, R 5, #0 ; load x into R 0 ADD R 0, #-2 ; subtract 2 BRnp NOT_TRUE ; if non-zero, x is not 2 AND ADD STR NOT_TRUE. . . R 1, #0 ; store 5 to y R 1, #5 R 1, R 5, #-1 ; next statement 13 -24
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Generating Code for If-Else int x, y, z; if (x) { y++; z--; } else { y--; z++; } ELSE DONE LDR R 0, R 5, #0 ; load x BRz ELSE ; x is not zero LDR R 1, R 5, #-1 ; incr y ADD R 1, #1 STR R 1, R 5, #-1 LDR R 1, R 5, #-2 ; decr z ADD R 1, #-1 STR R 1, R 5, #-2 BRnzp DONE ; skip else code ; x is zero LDR R 1, R 5, #-1 ; decr y ADD R 1, #-1 STR R 1, R 5, #-1 LDR R 1, R 5, #-2 ; incr z ADD R 1, #1 STR R 1, R 5, #-2. . . ; next statement 13 -25
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Generating Code for While int x; x = 0; while (x < 10) { x = x + 1; } LOOP DONE AND R 0, STR R 0, R 5, ; test LDR R 0, R 5, ADD R 0, BRzp DONE ; loop body LDR R 0, R 5, ADD R 0, STR R 0, R 5, BRnzp LOOP #0 #0 ; x = 0 #0 ; load x #-10 #0 ; load x #1 ; incr x #0 ; test again ; next statement 13 -26
Copyright © The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Generating Code for For for (i = 0; i < 10; i++) ; LOOP This is the same as the while example! DONE ; init AND R 0, STR R 0, R 5, ; test LDR R 0, R 5, ADD R 0, BRzp DONE ; loop body LDR R 0, R 5, ; re-init ADD R 0, STR R 0, R 5, BRnzp LOOP #0 #0 ; i = 0 #0 ; load i #-10 #0 ; load i #1 ; incr i #0 ; test again ; next statement 13 -27
- Slides: 27