Chapter 12 Variables and Operators

Slides:



Advertisements
Similar presentations
Chapter 7 Introduction to LC-3 Assembly Language Assembly Language Assembly Process Using the Editor & Simulator for Assembly Language Programming.
Advertisements

Programming Languages and Paradigms The C Programming Language.
The University of Adelaide, School of Computer Science
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Chapter 11-14, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3 Please return breadboards.
1 Introduction to “C” Control Loops and Functions Patt and Patel Ch. 13 & 14.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Chapter 11-12, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3.
Programming C/C++ on Eclipe C Training Trình bày : Ths HungNM.
TCSS 372A Computer Architecture. Getting Started Get acquainted (take pictures) Review Web Page (
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
1 C Programming Language Review and Dissection I: Overview, Static and Automatic Variables, and Program Control Flow These lecture notes created by Dr.
Chapter 12 Variables and Operators. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Basic C Elements.
Chapter 11 Programming in C Compilation vs. Interpretation Different ways of translating high-level language Compilation translates code into machine.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
CMPE13Cyrus Bazeghi Chapter 13 C Control Structures.
Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional making.
Chapter 11 Introduction to Programming in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Compilation.
CPS120: Introduction to Computer Science Decision Making in Programs.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
Chapter 12 Variables and Operators. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Basic C Elements.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
CMPE13Cyrus Bazeghi 1 Chapter 12 Variables and Operators.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Variables. A variable is a space in your robot’s memory where you can store data, such as whole numbers, decimal numbers, and words. Variable names follow.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 14 Functions.
Stack and Heap Memory Stack resident variables include:
C Programming Language Review and Dissection
Chapter 17 Recursion.
Chapter 12 Variables and Operators
Programming Languages and Paradigms
Lecture 13 & 14.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Programming Paradigms
Chapter 14 Functions.
Chapter 12 Variables and Operators
Chapter 12 Variables and Operators
Chapter 13 Control Structures
Chapter 16 Pointers and Arrays
Arrays, For loop While loop Do while loop
Chapter 14 Functions.
Chapter 13 Control Structures
More C Stack Frames / Pointer variables
Chapter 17 Recursion.
Computer Science 210 Computer Organization
The University of Adelaide, School of Computer Science
Chapter 14 Functions.
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Chapter 16 Pointers and Arrays
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Chapter 17 Recursion.
Homework Any Questions?.
ECE 103 Engineering Programming Chapter 18 Iteration
Chapter 11 Programming in C
Programming Languages and Paradigms
Chapter 14 Functions.
Chapter 12 Variables and Operators
Chapter 16 Pointers and Arrays
Chapter 17 Recursion.
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

Chapter 12 Variables and Operators

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

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

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 -- 32 bits for most modern processors

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

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;

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

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;

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;

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;

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;

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

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

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

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

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); }

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

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

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, R1 ; ~inGlobal AND R2, R0, R1 ; inLocal & ~inGlobal STR R2, R5, #-1 ; store in outLocalA ; (offset = -1)

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, R3 ADD R3, R3, #1 ADD R2, R2, R3 ; R2 is difference NOT R2, R2 ; negate ADD R2, R2, #1 ADD R0, R0, R2 ; R0 = R0 - R2 STR R0, R5, #-2 ; outLocalB (offset = -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

Chapter 13 Control Structures

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).

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

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, #1 STR R1, R5, #-1 LDR R1, R5, #-2 ; decr z ADD R1, R1, #-1 STR R1, R5, #-2 BRnzp DONE ; skip else code ; x is zero ELSE LDR R1, R5, #-1 ; decr y ADD R1, R1, #-1 STR R1, R5, #-1 LDR R1, R5, #-2 ; incr z ADD R1, R1, #1 STR R1, R5, #-2 DONE ... ; next statement int x, y, z; if (x) { y++; z--; } else { y--; z++; }

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

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