1 / 15

โปรแกรมย่อย

โปรแกรมย่อย. ธนาวินท์ รักธรรมานนท์ fengtwr@ku.ac.th. หัวข้อในวันนี้. โปรแกรมย่อย การเรียกใช้งานโปรแกรมย่อย การทำงานของแสต็กซ์ขณะเรียกใช้งานโปรแกรมย่อย. Procedure Operation (Pascal). Program draw(input, output); Var I:integer; Procedure writeBox; Begin writeln(‘------------’);

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. โปรแกรมย่อย ธนาวินท์ รักธรรมานนท์ fengtwr@ku.ac.th

  2. หัวข้อในวันนี้ • โปรแกรมย่อย • การเรียกใช้งานโปรแกรมย่อย • การทำงานของแสต็กซ์ขณะเรียกใช้งานโปรแกรมย่อย

  3. Procedure Operation (Pascal) Program draw(input, output); Var I:integer; Procedure writeBox; Begin writeln(‘------------’); writeln(‘- -’); writeln(‘------------’); End; Begin { main program} writeln(‘Hello…..’); for I:= 1 to 10 do writeBox; writeln(‘Bye’); End.

  4. โปรแกรมย่อยขั้นต้น • การประกาศ procnamePROCNEAR … ;program RET procnameENDP • การเรียกใช้ CALL procname

  5. ตัวอย่างโปรแกรมย่อย - การประกาศ ;print Hex digit ;input al<-digit ;affect : dl,ah printhexdigitproc near mov ah , 2 mov dl , al cmp al , 10 add dl , ’0’ jb printit add dl , ’A’-’0’-10 printit: int 21h ret printhexdigitendp

  6. ตัวอย่างโปรแกรมย่อย - การเรียกใช้ ; print a 2-digit hex number ; the digits is stored in bh,bl ... mov al,bh call printhexdigit mov al,bl call printhexdigit ... • ข้อเสียหลักคือรีจิสเตอร์ที่ถูกใช้งานในโปรแกรมย่อยทั้งหมดจะถูกเปลี่ยนค่าไปด้วย • แก้โดยใช้สแต็กซ์มาเก็บค่าของรีจิสเตอร์แต่ละตัวไว้ก่อนการทำงาน

  7. โจทย์เกี่ยวกับโปรแกรมย่อยโจทย์เกี่ยวกับโปรแกรมย่อย 1. จงเขียนโปรแกรมย่อยเพื่อรับค่าอักษร 1 ตัวจากผู้ใช้และเก็บเป็นตัวเลขไว้ใน AX และถ้าอักษรที่ผู้ใช้ระบุไม่ใช่ตัวเลขให้ AX=-1 call readInt ; ผู้ใช้ใส่ ‘5’ แล้ว AX = 5h 2. จงเขียนโปรแกรมย่อยเพื่อแสดงค่าของตัวเลขที่เก็บไว้ใน DX (ไม่เกิน 255) mov dx , C8h call printInt ; แสดงเลข 200 บนหน้าจอ

  8. โจทย์เกี่ยวกับโปรแกรมย่อยโจทย์เกี่ยวกับโปรแกรมย่อย 3. จงเขียนโปรแกรมย่อยเพื่อคำนวณผลรวมตั้งแต่ 1+2+3+..+AX และเก็บคำตอบสุดท้ายไว้ใน AX mov ax,10 call sigma ; ax = 55 4. จงเขียนโปรแกรมย่อยเพื่อหาหรม.ของ BX และ CX โดยเก็บค่าไว้ใน AX mov bx , 100 mov cx , 225 call gcd ; ax = 25

  9. การใช้งานแสต็กซ์ • คำสั่งเกี่ยวกับแสต็กซ์ • PUSH reg : ใส่ค่าลงใน stack • POP reg : อ่านค่าขึ้นมาจาก stack • โดยปกติรีจิสเตอร์ SS:SP จะเก็บตำแหน่งบนสุดของแสต็ก. เมื่อเราสั่ง PUSH หรือ POP กับสแต็กซ์หน่วยประมวลผลจะปรับค่าของรีจิสเตอร์ SP ให้มีค่าชี้ตำแหน่งบนสุดของแสต็กอีกครั้ง

  10. DX = 034A PUSH DX SS : SP 034A 1234 ??? ??? SS : SP AX = 1234 034A POP BX PUSH AX SS : SP SS : SP 1234 1234 ??? ??? BX = 034A ตัวอย่างการใช้งานแสต็กซ์ Mov AX,1234h Mov DX,034Ah Push AX Push DX Pop BX

  11. push ax push dx ;print Hex digit ;input al<-digit printhexdigitproc near mov ah,2 mov dl,al add dl,’0’ cmp al,10 jb printit add dl,’A’-’0’-10 printit: int 21h ret printhexdigitendp pop dx pop ax

  12. call printhexdigit กระโดดไปทำงานที่โปรแกรมย่อย ? push ax push dx DX IP AX ? IP ? DX AX pop dx pop ax IP ? ret IP ? กระโดดกลับไป โปรแกรมหลัก การเปลี่ยนไปของแสต็กซ์กับโปรแกรมย่อย

  13. โจทย์เกี่ยวกับโปรแกรมย่อยโจทย์เกี่ยวกับโปรแกรมย่อย 5.จงเขียนโปรแกรมเพื่อแสดงจำนวนเฉพาะที่อยู่ระหว่าง 1 ถึง 999 โดยแสดงผลลัพธ์ทางหน้าจอ คำแนะนำ: ใช้โปรแกรมย่อยช่วยจะง่ายขึ้นมาก - โปรแกรมย่อยแสดงตัวเลข - โปรแกรมย่อยในการตรวจสอบจำนวนเฉพาะ

  14. โจทย์เกี่ยวกับโปรแกรมย่อย (การบ้าน) จงเขียนโปรแกรมย่อยเพื่อทำงานแทนแสต็ก โดยให้เขียน 2 โปรแกรมย่อย ได้แก่ myPush และ myPop กำหนดให้ หน่วยความจำตำแหน่ง 100 เก็บจำนวนข้อมูลที่มีในแสต็ก และหน่วยความจำตำแหน่ง 101 เป็นต้นไปเก็บค่าที่ต้องการ myPush – เป็นโปรแกรมย่อยที่ใช้ในการเก็บข้อมูลใน AX ลงแสต็กที่เรากำหนด myPop – ใช้สำหรับนำข้อมูลออกจาก stack และนำมาเก็บใน AX

  15. Question ?

More Related