1 / 31

ภาษาเครื่อง

บทที่ 5. ภาษาเครื่อง. Flow of Control. สายงานของการควบคุม. Control Flow (Flow of Control). หมายถึง ลำดับของการประมวลผลคำสั่งหรือชุดคำสั่งย่อยได้เปลี่ยนแปลงไปจากลำดับปกติ

Download Presentation

ภาษาเครื่อง

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. บทที่ 5 ภาษาเครื่อง Flow of Control สายงานของการควบคุม

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

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

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

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

  6. Control Structures • No final keyword: Algol 60, C, C++, Haskell, Java, Pascal, Perl, PHP, PL/I, Python.Such languages need some way of grouping statements together: • 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

  7. Control Structures • 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

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

  9. Sequence Structure One Entry Process A Process B One Exit

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

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

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

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

  14. 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 }

  15. Condition-controlled loops • DO WHILE (Test) xxx END DO • WHILE (Test) { xxx } Repeat xxx Until test; do xxx while (test)

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

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

  18. Continuation with next Iteration • บางครั้งต้องการข้ามบรรทัดคำสั่งภายใน Loop เพื่อให้ วนซ้ำต่อไปเลย • บางภาษาจะใช้คำสั่ง 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.

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

  20. Early exit from loops Example with Ada.Text IO; with Ada.Integer Text IO; procedurePrint_Squaresis X : Integer; begin Read_Data : loop Ada.Integer Text IO.Get(X); if X = 0; then exitRead_Data; end if; Ada.Text IO.Put (X * X); Ada.Text IO.New_Line; end loop Read_Data; endPrint_Squares; Ada

  21. Early exit from loops Example 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" Python

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

  23. Conditions PL/I • PL/Ihas 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. ONconditionGOTO label

  24. Exceptions C • 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 }

  25. Exceptions C# 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 }

  26. Exceptions C# try setmyNumber to myNumber / 0 onerror e number n from f to t partial result pr if ( e = "Can't divide by zero" ) then display dialog "You idiot!" endtry

  27. Multiple early exit/exit from nested loops Ada exitwhenEventA or EventB or EventC; xxx exits EventA: actionA EventB: actionB EventC: actionC endexit;

  28. Multiple early exit/exit from nested loops Ada 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;

  29. Non-local control flow cross reference

  30. Loop system cross reference table

  31. จบบริบูรณ์ บทที่ 5 ภาษาเครื่อง Machine Language

More Related