Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 12 Variables and Operators

Similar presentations


Presentation on theme: "Chapter 12 Variables and Operators"— Presentation transcript:

1 Chapter 12 Variables and Operators

2 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

3 Basic C Elements Variables Operators named, typed data items
predefined actions performed on data items combined with variables to form expressions, statements

4 Data Types C has three basic data types int integer (at least 16 bits)
double floating point (at least 32 bits) char 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 bits for most modern processors

5 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

6 Example #include <stdio.h> void foo(); int global = 0;
int main() { int local = 1; // local to main printf("main: global %d local %d\n", global, local); global = 1; foo(); } void foo() { int local; // local to foo local = 3; printf("foo: global %d\n", global); global = 2;

7 Example global variable #include <stdio.h> void foo();
int global = 0; int main() { int local = 1; // local to main printf("main: global %d local %d\n", global, local); global = 1; foo(); } void foo() { int local; // local to foo local = 3; printf("foo: global %d\n", global); global = 2; global variable

8 Example #include <stdio.h> void foo(); int global = 0;
int main() { int local = 1; // local to main printf("main: global %d local %d\n", global, local); global = 1; foo(); } void foo() { int local; // local to foo local = 3; printf("foo: global %d\n", global); global = 2;

9 Example main: global 0 local 1 #include <stdio.h> void foo();
int global = 0; int main() { int local = 1; // local to main printf("main: global %d local %d\n", global, local); global = 1; foo(); } void foo() { int local; // local to foo local = 3; printf("foo: global %d\n", global); global = 2;

10 Example main: global 0 local 1 foo: global 1 #include <stdio.h>
void foo(); int global = 0; int main() { int local = 1; // local to main printf("main: global %d local %d\n", global, local); global = 1; foo(); } void foo() { int local; // local to foo local = 3; printf("foo: global %d\n", global); global = 2;

11 Example main: global 0 local 1 foo: global 1 main: global 2 local 1
#include <stdio.h> void foo(); int global = 0; int main() { int local = 1; // local to main printf("main: global %d local %d\n", global, local); global = 1; foo(); } void foo() { int local; // local to foo local = 3; printf("foo: global %d\n", global); global = 2;

12 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

13 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 Location in memory Scope Name Type Offset Scope inGlobal int global inLocal main outLocalA -1 outLocalB -2

14 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. R5 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 address of the frame, and variable offsets are <= 0. outLocalB outLocalA inLocal R5

15 Allocating Space for Variables
Global data section All global variables stored here (actually all static variables) R4 points to beginning Run-time stack Used for local variables R6 points to top of stack R5 points to top frame on stack New frame for each block (goes away when block exited) Offset = distance from beginning of storage area Global: LDR R1, R4, #4 Local: LDR R2, R5, #-3 0x0000 instructions PC R4 global data R6 run-time stack R5 0xFFFF

16 Example: Compiling to LC-3
#include <stdio.h> int inGlobal; main() { int inLocal; /* local to main */ int outLocalA; int outLocalB; /* initialize */ inLocal = 5; inGlobal = 3; /* perform calculations */ outLocalA = inLocal++ & ~inGlobal; outLocalB = (inLocal + inGlobal) - (inLocal - inGlobal); }

17 Example: Symbol Table Name Type Offset Scope inGlobal int global
global inLocal main outLocalA -1 outLocalB -2

18 Example: Code Generation
; main ; initialize variables AND R0, R0, # ADD R0, R0, #5 ; inLocal = STR R0, R5, #0 ; (offset = 0) AND R0, R0, # ADD R0, R0, #3 ; inGlobal = STR R0, R4, #0 ; (offset = 0)

19 Example (continued) ; first statement:
; outLocalA = inLocal++ & ~inGlobal; LDR R0, R5, #0 ; get inLocal ADD R1, R0, #1 ; increment STR R1, R5, #0 ; store LDR R1, R4, #0 ; get inGlobal NOT R1, R ; ~inGlobal AND R2, R0, R1 ; inLocal & ~inGlobal STR R2, R5, #-1 ; store in outLocalA ; (offset = -1)

20 Example (continued) ; next statement:
; outLocalB = (inLocal + inGlobal) ; (inLocal - inGlobal); LDR R0, R5, #0 ; inLocal LDR R1, R4, #0 ; inGlobal ADD R0, R0, R1 ; R0 is sum LDR R2, R5, #0 ; inLocal LDR R3, R4, #0 ; inGlobal NOT R3, R ADD R3, R3, # ADD R2, R2, R3 ; R2 is difference NOT R2, R ; negate ADD R2, R2, # ADD R0, R0, R2 ; R0 = R0 - R STR R0, R5, #-2 ; outLocalB (offset = -2)

21 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

22 Chapter 13 Control Structures

23 Control Structures Conditional Iteration
making a decision about which code to execute, based on evaluated expression if 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).

24 Generating Code for If Statement
; int main () { ; int x, y; ; if (x == 2) y = 5; LDR R0, R5, #0 ; load x into R ADD R0, R0, #-2 ; subtract BRnp NOT_TRUE ; if non-zero, x is not 2 AND R1, R1, #0 ; store 5 to y ADD R1, R1, # STR R1, R5, #-1 NOT_TRUE ; next statement

25 Generating Code for If-Else
LDR R0, R5, #0 ; load x BRz ELSE ; x is not zero LDR R1, R5, #-1 ; incr y ADD R1, R1, # STR R1, R5, # LDR R1, R5, #-2 ; decr z ADD R1, R1, # STR R1, R5, # BRnzp DONE ; skip else code ; x is zero ELSE LDR R1, R5, #-1 ; decr y ADD R1, R1, # STR R1, R5, # LDR R1, R5, #-2 ; incr z ADD R1, R1, # STR R1, R5, #-2 DONE ; next statement int x, y, z; if (x) { y++; z--; } else { y--; z++; }

26 Generating Code for While
AND R0, R0, # STR R0, R5, #0 ; x = ; test LOOP LDR R0, R5, #0 ; load x ADD R0, R0, # BRzp DONE ; loop body LDR R0, R5, #0 ; load x ADD R0, R0, #1 ; incr x STR R0, R5, # BRnzp LOOP ; test again DONE ; next statement int x; x = 0; while (x < 10) { x = x + 1; }

27 Generating Code for For
; init AND R0, R0, # STR R0, R5, #0 ; i = ; test LOOP LDR R0, R5, #0 ; load i ADD R0, R0, # BRzp DONE ; loop body LDR R0, R5, #0 ; load i ; re-init ADD R0, R0, #1 ; incr i STR R0, R5, # BRnzp LOOP ; test again DONE ; next statement for (i = 0; i < 10; i++) ; This is the same as the while example!


Download ppt "Chapter 12 Variables and Operators"

Similar presentations


Ads by Google