Introduction to Computer Organization and Architecture Flow of

  • Slides: 36
Download presentation
Introduction to Computer Organization and Architecture ภาษาเครอง Flow of Control �����

Introduction to Computer Organization and Architecture ภาษาเครอง Flow of Control �����

Structure Programming • Sequence Structure • Selection Structure • IF-Then-Else Structure • Switch or

Structure Programming • Sequence Structure • Selection Structure • IF-Then-Else Structure • Switch or Case Structure • Iterative Structure (Loop( • Do-While Structure • Do-Until Structure 2

Sequence Structure One Entry Process A Process B One Exit 3

Sequence Structure One Entry Process A Process B One Exit 3

IF-Then-Else Structure One Entry True condition Process A False Process B One Exit 4

IF-Then-Else Structure One Entry True condition Process A False Process B One Exit 4

Case Structure One Entry Case 1 2 3 N Process . . . One

Case Structure One Entry Case 1 2 3 N Process . . . One Exit Process 5

Do-While Structure Pre-Test One Entry Process False condition True One Exit 6

Do-While Structure Pre-Test One Entry Process False condition True One Exit 6

Do-Until Structure Post-Test One Entry Repeat, For…Next Process True condition False One Exit 7

Do-Until Structure Post-Test One Entry Repeat, For…Next Process True condition False One Exit 7

����� Type of Control Flow vcontinuation at a different statement (jump(, jump vexecuting a

����� Type of Control Flow vcontinuation at a different statement (jump(, jump vexecuting a set of statements only if some condition is met (choice , ( vexecuting a set of statements zero or more times, until some condition is met (loop(, loop vexecuting a set of distant statements, after which the flow of control may possibly return (subroutines , coroutines , and continuations , ( vstopping the program, preventing any further execution (halt. ( ��������� 9

Primitive n Labels n Line Numbers ในภาษา Fortran, BASIC 10 LET X = 3

Primitive n Labels n Line Numbers ในภาษา Fortran, BASIC 10 LET X = 3 20 PRINT X n Identifier ในภาษา C, Ada Success: printf ("The operation was successful. n"); n ภาษา Algol 60 อนญาตใหใชไดทงสองอยาง n GOTO n goto unconditional transfer of control. label 10

Control Structures n No final keyword : Algol 60, C, C++, Haskell, Java, Pascal,

Control Structures n No final keyword : Algol 60, C, C++, Haskell, Java, Pascal, Perl, PHP, PL/I, Python. บางภาษาตองการจดกลมของคำสงเ ขาไวเปนชด n n n Algol 60 and Pascal : begin. . . end C, C++, Java, Perl, and PHP: curly brackets {. . . } PL/1: DO. . . END Python: uses indentation level (see Off-side rule) Haskell: either indentation level or curly 12

Control Structures n n n Final keyword: Ada, Algol 68, Modula-2, Fortran 77, Visual

Control Structures n n n Final keyword: Ada, Algol 68, Modula-2, Fortran 77, Visual Basic. The forms of the final keyword vary: Ada: final keyword is end + space + initial keyword e. g. if. . . end if, loop. . . end loop Algol 68: initial keyword spelled backwards e. g. if. . . fi, case. . . esac Fortran 77: final keyword is end + initial keyword e. g. IF. . . ENDIF, DO. . . ENDDO Modula-2: same final keyword end for everything Visual Basic: every control structure has its own keyword. If. . . End If; For. . . Next; Do. . . Loop 13

IF-THEN-ELSE Structure Pseudo Form If (condition (Then (statements) Else (statements) End If ELSEIF Blocks

IF-THEN-ELSE Structure Pseudo Form If (condition (Then (statements) Else (statements) End If ELSEIF Blocks if condition then -- statements; elseif condition then -- more statements; else condition then --other statements; end if ; . . . 14

IF Expression n Many languages support if expressions, which are similar to if statements,

IF Expression n Many languages support if expressions, which are similar to if statements, but return a value as a result. Thus, they are true expressions (which evaluate to a value), not statements (which just perform an action). )condition)? (evaluate if condition was true): (evaluate if condition was false) //Invalid my_variable = if(x > 10) { "foo" } else { "bar" }; //Valid my_variable = (x > 10)? "foo": "bar"; if (x > 10) { my_variable = 'foo'; { else { my_variable = 'bar'; { 15

Arithmetic IF n Fortran 77 has an "arithmetic if" statement which is halfway between

Arithmetic IF n Fortran 77 has an "arithmetic if" statement which is halfway between a computed IF and a case statement, based on the trichotomy x 0 > , x = 0, x 0 < IF (e) label 1, label 2, label 3 IF) e < 0) GOTO label 1 IF (e = 0) GOTO label 2 IF) e > 0) GOTO label 3 16

Case or Switch Structure switch) some. Char) { case' a': action. On. A; break;

Case or Switch Structure switch) some. Char) { case' a': action. On. A; break; case' x': action. On. X; break; case' y': case' z': action. On. Yand. Z; break; default : action. On. No. Match; { case some. Char of 'a': action. On. A; 'x': action. On. X; 'y', 'z': action. On. Yand. Z; end; 17

Choice system cross reference Programming language Structured if Constant choice Arithmeticif Pattern matching then

Choice system cross reference Programming language Structured if Constant choice Arithmeticif Pattern matching then else-if Ada Yes Yes No ? C and C++ Yes Not needed fall-through No No Fortran Yes Yes Yes ? Perl Yes Yes No No ? PHP Yes Yes fall-through No ? Haskell Yes required Not needed not needed No Yes Visual Basic. NET Yes No No Yes, Yes 18

Count-controlled loops n FOR I = 1 TO N for I : = 1

Count-controlled loops n FOR I = 1 TO N for I : = 1 to N do begin xxx NEXT I end; n DO I = 1, N xxx END DO for ( I=1; I<=N; ++I ){ xxx } 19

Condition-controlled loops n DO WHILE (Test) xxx END DO n WHILE } xxx (Test)

Condition-controlled loops n DO WHILE (Test) xxx END DO n WHILE } xxx (Test) { Repeat xxx Until test; do xxx while (test) 20

Collection-controlled loops (e. g. Ada, Smalltalk, Perl, Java, C#, Visual Basic ( มโครงสราง การวนซำแบบพเศษกบแตละสมาชกภายในเซตหร

Collection-controlled loops (e. g. Ada, Smalltalk, Perl, Java, C#, Visual Basic ( มโครงสราง การวนซำแบบพเศษกบแตละสมาชกภายในเซตหร some. Collection do: : ]each. Element |xxx. [ อแถวลำดบได n บางภาษาคอมพวเตอร foreach some. Array { xxx } Collection<String> coll; for (String s : coll) {} foreach (string s in my. String. Collection) { xxx } 21

Infinite Loops n Sometimes it is desirable for a program to loop forever, or

Infinite Loops n Sometimes it is desirable for a program to loop forever, or until an exceptional condition such as an error arises. For instance, an event-driven program may be intended to loop forever handling events as they occur, only stopping when the process is killed by the operator. n หรอบางครงหากไมเกดการเปลยนแปลงคาใดๆ เลยในการวนซำ กสามารถนำมาใชเปนเหตใหยกเลกการวนซำได 22

Continuation with next Iteration n บางครงตองการขามบรรทดคำสงภายใน Loop เพอให วนซำตอไปเลย n บางภาษาจะใชคำสง continue หรอคำสง skip

Continuation with next Iteration n บางครงตองการขามบรรทดคำสงภายใน Loop เพอให วนซำตอไปเลย n บางภาษาจะใชคำสง continue หรอคำสง skip n The effect is to prematurely terminate the innermost loop body and then resume as normal with the next iteration. n If the iteration is the last one in the loop, the effect is to terminate the entire loop early. 23

Early exit from loops with Ada. Text IO; with Ada. Integer Text IO; procedure

Early exit from loops with Ada. Text IO; with Ada. Integer Text IO; procedure Print_Squares is X : Integer; begin Read_Data : loop Ada. Integer Text IO. Get(X); if X = 0; then exit Read_Data; end if; Ada. Text IO. Put (X * X); Ada. Text IO. New_Line; end loop Read_Data; end Print_Squares; 25

Early exit from loops for n in set_of_numbers : if isprime(n : ( print"

Early exit from loops for n in set_of_numbers : if isprime(n : ( print" Set contains a prime number " break else : print" Set did not contain any prime numbers " 26

Structured non-local control flow n Many programming languages, particularly those which favor more dynamic

Structured non-local control flow n Many programming languages, particularly those which favor more dynamic styles of programming, offer constructs for non-local control flow. These cause the flow of execution to jump out of a given context and resume at some predeclared point. Exceptions, conditions, and continuations are three common sorts of non-local control constructs. 27

Conditions PL/I has some 22 standard conditions (e. g. ZERODIVIDE SUBSCRIPTRANGE ENDFILE) which can

Conditions PL/I has some 22 standard conditions (e. g. ZERODIVIDE SUBSCRIPTRANGE ENDFILE) which can be RAISEd and which can be intercepted by: ON condition action; Programmers can also define and use their own named conditions. n Like the unstructured if only one statement can be specified so in many cases a GOTO is needed to decide where flow of control should resume. n ON condition GOTO label 28

Exceptions Modern languages have a structured construct for exception handling which does not rely

Exceptions Modern languages have a structured construct for exception handling which does not rely on the use of GOTO: try { xxx 1 //Somewhere in here xxx 2 //use: '''throw''' some. Value; xxx 3 } catch (some. Class & some. Id) { n // catch value of some. Class // catch value of some. Type action. For. Some. Class } catch (some. Type & another. Id) { action. For. Some. Type } catch (. . . ) { // catch anything not already caught action. For. Anything. Else } 29

Exceptions File. Stream stm = null; try { //C# example stm = new File.

Exceptions File. Stream stm = null; try { //C# example stm = new File. Stream ("logfile. txt , "File. Mode. Create(; return Process. Stuff(stm); { finally } if (stm != null ( stm. Close ; () } //may throw an exception using) File. Stream stm = new File. Stream ("logfile. txt", File. Mode. Create (( } return Process. Stuff(stm); { //may throw an exception 30

Exceptions try set my. Number to my. Number / 0 on error e number

Exceptions try set my. Number to my. Number / 0 on error e number n from f to t partial result pr if ) e = "Can't divide by zero ( " then display dialog "You idiot! " end try 31

Multiple early exit/ exit from nested loops exitwhen Event. A or Event. B or

Multiple early exit/ exit from nested loops exitwhen Event. A or Event. B or Event. C ; xxx exits Event. A: action. A Event. B: action. B Event. C: action. C endexit ; 32

Multiple early exit/ exit from nested loops exitwhen found or missing; for I :

Multiple early exit/ exit from nested loops exitwhen found or missing; for I : = 1 to N do for J : = 1 to M do if table[I, J] = target then found; missing ; exits found: print ("item is in table; (" missing: print ("item is not in table; (" endexit ; 33

Non-local control flow cross reference Programming language Ada C C++ C# D Haskell Java

Non-local control flow cross reference Programming language Ada C C++ C# D Haskell Java Objective C PHP PL/1 Python Ruby conditions No No No exceptions Yes No No Yes No No Yes Yes Yes No Yes 34

Loop system cross reference table Programming language conditional loop begin Mid end Dle count

Loop system cross reference table Programming language conditional loop begin Mid end Dle count Collec tion Gener al Infi nite Ada Yes Yes arrays No Yes C Yes No No Yes No C++ Yes No No Yes No FORTRAN 77 Yes No No Fortran 90 Yes No No Yes No Java Yes No PHP Yes No Yes Python Yes No No early exit Conti Nua tion Deep nested Yes No one level Yes No Yes Yes No No Yes No No Deep nested No Yes Yes

Introduction to Computer Organization and Architecture ������ Machine �� Language

Introduction to Computer Organization and Architecture ������ Machine �� Language