Basic Programming .

Uploaded on:
Category: News / Events
CS 345. Imperative Programming. Vitaly Shmatikov. Reading Assignment. Mitchell, Chapter 5.1-2 C Reference Manual, Chapter 8. Imperative Programming. Oldest and most popular paradigm Fortran, Algol, C, Java … Mirrors computer architecture
Slide 1

CS 345 Imperative Programming Vitaly Shmatikov

Slide 2

Reading Assignment Mitchell, Chapter 5.1-2 C Reference Manual, Chapter 8

Slide 3

Imperative Programming Oldest and most famous worldview Fortran, Algol, C, Java … Mirrors PC engineering In a von Neumann machine, memory holds guidelines and information Key operation: task Side impact: upgrading state (i.e., memory) of the machine Control-stream explanations Conditional and unrestricted (GO TO) branches, circles

Slide 4

Elements of Imperative Programs Data sort definitions Variable presentations (normally wrote) Expressions and task proclamations Control stream articulations (typically organized) Lexical extensions and squares Goal: give territory of reference Declarations and meanings of strategies and capacities (i.e., parameterized pieces)

Slide 5

Variable Declarations Typed variable announcements confine the qualities that a variable may expect amid program execution Built-in sorts (int, roast … ) or client characterized Initialization: Java whole numbers to 0. Shouldn\'t something be said about C? Variable size How much space expected to hold estimations of this variable? C on a 32-bit machine: sizeof(char) = 1 byte, sizeof(short) = 2 bytes, sizeof(int) = 4 bytes, sizeof(char*) = 4 bytes (why?) What about this client characterized datatype:

Slide 6

Variables: Locations and Values When a variable is proclaimed, it is bound to some memory area and turns into its identifier Location could be in worldwide, load, or stack stockpiling l-esteem: memory area (address) r-esteem: esteem put away at the memory area distinguished by l-esteem Assignment: An (objective) = B (expression) Destructive redesign: overwrites the memory area recognized by A with an estimation of expression B What if a variable shows up on both sides of task?

Slide 7

Copy versus Reference Semantics Copy semantics: expression is assessed to an esteem, which is duplicated to the objective Used by basic dialects Reference semantics: expression is assessed to a protest, whose pointer is replicated to the objective Used by question situated dialects

Slide 8

Variables and Assignment On the RHS of a task, utilize the variable\'s r-esteem; on the LHS, utilize its l-esteem Example: x = x+1 Meaning: "get r-estimation of x, include 1, store the outcome into the l-estimation of x" An expression that does not have a l-esteem can\'t show up on the LHS of a task What expressions don\'t have l-values? Cases: 1=x+1 , ++x++ (why?) What about a[1] = x+1 , where an is a cluster? Why?

Slide 9

l-Values and r-Values (1) Any expression or task explanation in a basic dialect can be comprehended as far as l-qualities and r-estimations of factors required In C, additionally assists with complex pointer dereferencing and pointer number-crunching Literal constants Have r-values, yet not l-values Variables Have both r-qualities and l-values Example: x=x*y signifies "process rval(x)*rval(y) and store it in lval(x)"

Slide 10

l-Values and r-Values (2) Pointer factors Their r-qualities are l-estimations of another variable Intuition: the estimation of a pointer is an address Overriding r-esteem and l-esteem calculation in C &x dependably returns l-estimation of x *p dependably return r-estimation of p If p is a pointer, this is a l-estimation of another variable What are the estimations of p and x now?

Slide 11

l-Values and r-Values (3) Declared capacities and strategies Have l-values, however no r-values

Slide 12

Turing-Complete Mini-Language Integer factors, values, operations Assignment If Go To

Slide 13

Structured Control Flow Control stream in basic dialects is regularly intended to be consecutive Instructions executed all together they are composed Some likewise bolster simultaneous execution (Java) Program is organized if control stream is clear from syntactic (static) structure of program content Big thought : software engineers can reason about element execution of a program by simply breaking down program content Eliminate many-sided quality by making dialect develops for normal control-stream "designs" Iteration, choice, systems/capacities

Slide 14

Fortran Control Structure 10 IF (X .GT. 0.000001) GO TO 20 11 X = - X IF (X .LT. 0.000001) GO TO 50 20 IF (X*Y .LT. 0.00001) GO TO 30 X = X-Y-Y 30 X = X+Y ... 50 CONTINUE X = A Y = B-A GO TO 11 … Similar structure may happen in gathering code

Slide 15

Historical Debate Dijkstra, "GO TO Statement Considered Harmful" Letter to Editor, Comm. ACM, March 1968 Linked from the course site Knuth, "Organized Prog. with Go To Statements" You can utilize goto, yet do as such in organized way … Continued examination Welch, "GOTO (Considered Harmful) n , n is Odd" General inquiries Do syntactic guidelines compel great programming style? Could they offer assistance?

Slide 16

Modern Style Standard builds that structure hops if … then … else … end while … do … end for … { … } case … Group code in legitimate squares Avoid express bounced (aside from capacity return) Cannot hop into the center of a piece or capacity body

Slide 17

Iteration Definite Indefinite Termination relies on upon a powerfully figured esteem How would we know statically (i.e., before we run the program) that the circle will end , i.e., that n will in the end turn out to be not exactly or equivalent to 0?

Slide 18

Iteration Constructs in C while (condition) stmt; while (condition) { stmt; stmt; … ; } do stmt while (condition); do { stmt; stmt; … ; } while (condition); for (<initialize>; <test>; <step>) stmt; Restricted type of "while" circle – same as <initialize>; while (<test>) { stmt; <step> } for (<initialize>; <test>; <step>) { stmt; stmt; … ; }

Slide 19

"Breaking Out" Of A Loop in C

Slide 20

Forced Loop Re-Entry in C

Slide 21

Block-Structured Languages Nested hinders with nearby factors { int x = 2; { int y = 3; x = y+2; } Storage administration Enter square: apportion space for factors Exit obstruct: a few or all space might be deallocated new factors proclaimed in settled pieces external piece internal piece neighborhood variable worldwide variable

Slide 22

Blocks in Common Languages Examples C, JavaScript * { … } Algol start … end ML let … in … end Two types of pieces Inline pieces Blocks connected with capacities or methodology We\'ll discuss these later * JavaScript capacities gives squares

Slide 23

Simplified Machine Model Registers Code Data Stack Program counter Heap Environment pointer

Slide 24

Memory Management Registers, Code portion, Program counter Ignore registers (for our motivations) and subtle elements of guideline set Data fragment Stack contains information identified with piece section/leave Heap contains information of differing lifetime Environment pointer focuses to current stack position Block passage: add new initiation record to stack Block leave: evacuate latest enactment record

Slide 25

Scope and Lifetime Scope Region of program content where revelation is unmistakable Lifetime Period of time when area is assigned to program { int x = … ; { int y = … ; { int x = … ; … . }; }; }; Inner presentation of x stows away external one ("gap in extension") Lifetime of external x incorporates time when inward piece is executed Lifetime  scope

Slide 26

Inline Blocks Activation record Data structure put away on run-time stack Contains space for neighborhood factors Push record with space for x, y Set estimations of x, y Push record for internal square Set estimation of z Pop record for internal piece Pop record for external piece { int x=0; int y=x+1; { int z=(x+y)*(x-y); }; }; May require space for factors and moderate results like (x+y), (x-y )

Slide 27

Activation Record For Inline Block Control interface Pointer to past record on stack Push record on stack Set new control connection to indicate old env ptr Set env ptr to new record Pop record off stack Follow control connection of current record to reset environment pointer Control interface Control connect Local factors Local factors Intermediate results Intermediate results Environment pointer by and by, can be streamlined away

Slide 28

Example Control connect x 0 y 1 Control connect Push record with space for x, y Set estimations of x, y Push record for inward square Set estimation of z Pop record for inward square Pop record for external square { int x=0; int y=x+1; { int z=(x+y)*(x-y); }; }; z - 1 x+y 1 x-y - 1 Environment pointer

View more...