งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Introduction to Computer Organization and Architecture Flow of Control ภาษาเครื่อง สายงานของการ ควบคุม.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Introduction to Computer Organization and Architecture Flow of Control ภาษาเครื่อง สายงานของการ ควบคุม."— ใบสำเนางานนำเสนอ:

1

2 Introduction to Computer Organization and Architecture Flow of Control ภาษาเครื่อง สายงานของการ ควบคุม

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

4 3 Sequence Structure One Entry Proces s A Proces s B One Exit

5 4 IF-Then-Else Structure One Entry condi tion Tru e Proces s A Fal se Proces s B One Exit

6 5 Case Structure One Entry Proc ess Cas e 1 Proc ess 2 3... N Proc ess One Exit

7 6 Do-While Structure Pre- Test One Entry condi tion Fals e Proce ss One Exit Fals e Tru e

8 7 Do-Until Structure Post- Test One Entry Proce ss condi tion Tr ue One Exit Fal se Tr ue Repeat, For … Next

9 8 Control Flow (Flow of Control) หมายถึง ลำดับของการประมวลผลคำสั่งหรือ ชุดคำสั่งย่อยได้เปลี่ยนแปลงไปจากลำดับปกติ Control Flow Statements หมายถึง คำสั่งที่ เมื่อถูกประมวลผลแล้ว จะทำให้ลำดับการ ประมวลผลในลำดับถัดไปจากปกติ เปลี่ยนแปลงลำดับทิศทางไปจากเดิม โปรแกรมภาษาเครื่องหรือโปรแกรม ภาษาแอสเซมบลี คำสั่งควบคุมสายงาน (control flow instructions) จะเป็นการ เปลี่ยนแปลงค่าเลขที่อยู่ใน program counter. หน่วยประมวลผลกลางทั่วไป คำสั่งควบคุมสาย งานมักจัดแบ่งออกเป็นสองกลุ่มคือกระโดด แบบมีเงื่อนไข (Conditional Branches) และ แบบไม่มีเงื่อนไข (Unconditional Branches) บางครั้งอาจใช้คำสั่งว่า jumps แทน ).

10 9 ประเภทของสายงาน การควบคุม jump  continuation at a different statement (jump), choice  executing a set of statements only if some condition is met (choice), loop  executing a set of statements zero or more times, until some condition is met (loop), subroutinescoroutinescontinuations  executing a set of distant statements, after which the flow of control may possibly return (subroutines, coroutines, and continuations), halt  stopping the program, preventing any further execution (halt). Type of Control Flow แต่ละภาษาคอมพิวเตอร์อาจใช้รูปแบบ ของคำสั่งที่แตกต่างกันไป

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

12 11 Primitive Subroutines อาจใช้ชื่อเรียกเป็นอย่างอื่น เช่น routines, procedures, functions ( ส่งค่า ผลลัพธ์กลับ ) หรือ methods ( โดยเฉพาะ กับ classes หรือ type classes). ทศวรรษที่ 1950's, คอมพิวเตอร์มี หน่วยความจำที่จำกัด จึงต้องเขียนโปรแกรม หรือชุดคำสั่งย่อยด้วยขนาดที่เหมาะสม เพื่อให้สามารถเรียกใช้ซ้ำหลายๆ ครั้งได้ใน โปรแกรม. แต่ปัจจุบันชุดคำสั่งย่อยช่วยทำให้โปรแกรม มีโครงสร้างที่ดีขึ้น เช่น ช่วยซ่อนขั้นตอนวิธี ของการทำงานไว้ด้านหลัง หรือซ่อนการ เรียกใช้ข้อมูลพิเศษเฉพาะไว้ส่วนหลัง นอกจากนี้ยังช่วยให้สามารถแบ่งงานการ เขียนโปรแกรมขนาดใหญ่ โดยผู้เขียน โปรแกรมหลายๆ คนสามารถทำงานคู่ขนาน ไปพร้อมกันได้ในลักษณะของการจัดแบ่ง งานออกเป็นโมดูล (Modularity)

13 12 Control Structures No final keyword No final keyword: Algol 60, C, C++, Haskell, Java, Pascal, Perl, PHP, PL/I, Python. บางภาษาต้องการจัดกลุ่มของ คำสั่งเข้าไว้เป็นชุด 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 brackets can be used, and they can be freely mixed

14 13 Control Structures Final keyword: 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

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

16 15 IF Expression 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' ; }

17 16 Arithmetic IF Fortran 77 Fortran 77 has an "arithmetic if" statement which is halfway between a computed IF and a case statement, based on the trichotomy x 0 IF (e) label1, label2, label3 IF (e 0) GOTO label3

18 17 Case or Switch Structure case someChar of 'a': actionOnA; 'x': actionOnX; 'y','z':actionOnYandZ; end; switch (someChar) { case 'a': actionOnA; break; case 'x': actionOnX; break; case 'y': case 'z': actionOnYandZ; break; default: actionOnNoMatch; }

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

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

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

22 21 Collection-controlled loops บางภาษาคอมพิวเตอร์ (e.g. Ada, Smalltalk, Perl, Java, C#, Visual Basic) มีโครงสร้าง การวนซ้ำแบบ พิเศษกับแต่ละสมาชิกภายในเซ็ตหรือ แถวลำดับได้ someCollection do: [ :eachElement |xxx ]. foreach someArray { xxx } Collection coll; for (String s : coll) {} foreach (string s in myStringCollection) { xxx }

23 22 Infinite Loops 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. หรือบางครั้งหากไม่เกิดการเปลี่ยนแปลง ค่าใดๆ เลยในการวนซ้ำ ก็สามารถ นำมาใช้เป็นเหตุให้ยกเลิกการวนซ้ำได้

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

25 24 Early exit from loops คำสั่ง break หรือคำสั่ง exit สั่งให้ หยุดการวนซ้ำในทันทีแล้วไปทำงาน ที่คำสั่งถัดไปจากชุดของคำสั่งวนซ้ำ ต่อไป จึงนิยมนำไปประยุกต์ใช้กับงาน ค้นหาข้อมูลในตาราง เมื่อค้นหา พบแล้วโดยชุดคำสั่งวนซ้ำ จะ กระโดดออกจาก การวนซ้ำในทันทีโดยไม่ต้อง เสียเวลาการวนซ้ำจนจบรอบ

26 25 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;

27 26 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 " Early exit from loops

28 27 Structured non-local control flow 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.

29 28 Conditions PL/I 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. 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. ON condition GOTO label

30 29 Exceptions Modern languages have a structured construct for exception handling which does not rely on the use of GOTO: try { xxx1 // Somewhere in here xxx2 // use: '''throw''' someValue; xxx3 } catch (someClass & someId) { // catch value of someClass actionForSomeClass } catch (someType & anotherId) { // catch value of someType actionForSomeType } catch (...) { // catch anything not already caught actionForAnythingElse }

31 30 Exceptions FileStream stm = null; // C# example try { stm = new FileStream ("logfile.txt", FileMode. Create); return ProcessStuff(stm); // may throw an exception } finally { if (stm != null) stm. Close(); } using (FileStream stm = new FileStream ("logfile.txt", FileMode. Create)) { return ProcessStuff(stm); // may throw an exception }

32 31 Exceptions try set myNumber to myNumber / 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

33 32 Multiple early exit/ exit from nested loops exitwhen EventA or EventB or EventC ; xxx exits EventA: actionA EventB: actionB EventC: actionC endexit;

34 33 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; Multiple early exit/ exit from nested loops

35 34 Non-local control flow cross reference Programming languageconditionsexceptions AdaNoYes CNo C++NoYes C#NoYes DNoYes HaskellNoYes JavaNoYes Objective CNoYes PHPNoYes PL/1YesNo PythonNoYes RubyNoYes

36 Loop system cross reference table Programming language conditionalloop early exit Conti Nua tion begin Mid Dle end count Collec tion Gener al Infi nite Ada Yes arraysNoYes Deep nested No C YesNoYes No YesNo Deep nested Yes C++ YesNoYes No YesNo Deep nested Yes FORTRAN 77YesNo YesNo one levelYes Fortran 90 YesNo YesNo Yes Deep nested Yes Java YesNoYes No Yes No Deep nested Yes PHP YesNoYes NoYes No Deep nested Yes Python YesNo YesNo Deep nested Yes

37 Introduction to Computer Organization and Architecture Machine Language ภาษาเครื่อ ง


ดาวน์โหลด ppt Introduction to Computer Organization and Architecture Flow of Control ภาษาเครื่อง สายงานของการ ควบคุม.

งานนำเสนอที่คล้ายกัน


Ads by Google