1 / 31

Control Transfer Instructions

Control Transfer Instructions. Suthida Chaichomchuen std@kmitnb.ac.th. Types of Addresses. Short : -128 to 127 bytes Near : -32,768 to 32,767 bytes Far : over 32K. Short Near Far -128 to 127 -32,768 to 32,767 Over 32K or in

harris
Download Presentation

Control Transfer Instructions

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. Control Transfer Instructions Suthida Chaichomchuen std@kmitnb.ac.th

  2. Types of Addresses • Short : -128 to 127 bytes • Near : -32,768 to 32,767 bytes • Far : over 32K

  3. Short Near Far -128 to 127 -32,768 to 32,767 Over 32K or in Instructions same segment same segment another segment JMP yes yes yes Jnnn yes yes(80386+) yes LOOP yes no no CALL N/A yes yes Rules on distances for operations

  4. Instruction Labels • ใช้กับ JMP, Jnnn, LOOP • ตั้งตามหลักการการตั้งชื่อ • ต้องปิดท้ายด้วยเครื่องหมาย : (colon) • ตัวอย่างเช่น P10: INC CX

  5. [label:] JMP short, near, or far address JMP Instruction • เปลี่ยนแปลงลำดับการทำงานจากจุดหนึ่งไปยังอีกจุดหนึ่งโดยไม่มีเงื่อนไข • รูปแบบ

  6. JMP : Short & Near • Short : label within -128 to +127 bytes • assembler generate 2 bytes • 1 byte for operation [EB] • 1 byte for operand [ค่า offset ที่จะนำไปบวกกับค่าของ IP จำกัดที่ 00H-FFH]

  7. JMP : Short & Near • Near : label within 32K • assembler generate • 1 byte for operation [E9] • 2 bytes for operand (8086/80286) or 4 bytes (80386 and later)

  8. JMP P10 . . . P10: JMP : Forward & Backward • Forward • assembler ยังไม่ generate code เพราะไม่รู้ว่าจะเป็นแบบใด (short / near)

  9. Backward within -128 bytes • assembler generate code 2 bytes P10: . . . JMP P10 JMP : Forward & Backward

  10. MOV AX,01 MOV BX,01 MOV CX,01 A20: ADD AX,01 ADD BX,AX SHL CX,1 JMP A20 Using the JMP Instruction

  11. LOOP Instruction • คำสั่งควบคุมการทำงานที่ซ้ำกัน • เป็นคำสั่งควบคุมที่มีเงื่อนไข • ใช้ CX เป็นตัวนับการทำงานภายในลูป • CX ถูกลดค่าทีละ 1 โดยอัตโนมัติเมื่อคำสั่งถูก execute

  12. [label:] LOOP short-address LOOP Instruction • รูปแบบ

  13. MOV AX,01 MOV BX,01 MOV DX,01 MOV CX,10 A20: INC AX ADD BX,AX SHL DX,1 LOOP A20 Using the LOOP Instruction

  14. LOOPE / LOOPZ InstructionLoop while equal / Loop while zero • ตรวจสอบค่า CX และแฟลก ZF • ลดค่า CX ทีละ 1 เมื่อคำสั่งถูก execute • วนลูปเมื่อ CX เท่ากับ 0 หรือ ZF=1 • ออกจากลูปเมื่อ CX<>0 หรือ ZF=0

  15. LOOPNE / LOOPNZ InstructionLoop while not equal / Loop while not zero • ตรวจสอบค่า CX และแฟลก ZF • ลดค่า CX ทีละ 1 เมื่อคำสั่งถูก execute • วนลูปเมื่อ CX ไม่เท่ากับ 0 หรือ ZF=0 • ออกจากลูปเมื่อ CX=0 หรือ ZF=1

  16. CMP Instruction • เปรียบเทียบค่าที่เป็นตัวเลขใน operand 1 กับ operand2 • ต้องมี operand ตัวใดตัวหนึ่งเป็นรีจิสเตอร์ • ทำงานคล้ายคำสั่ง SUB แต่จะไม่มีการเก็บค่าผลลัพธ์ • มีผลต่อแฟลก AF, CF, OF, PF, SF และ ZF

  17. [label:] CMP destination,source destination : register/memory source : register/memory/immediate CMP Instruction • รูปแบบ

  18. [label:] Jnnn short-address Conditional Jump Instructions • เปลี่ยนแปลงลำดับการทำงานจากจุดหนึ่งไปยังอีกจุดหนึ่งอย่างมีเงื่อนไข • รูปแบบ • ไม่มีการลดค่า CX แบบอัตโนมัติ

  19. Signed & Unsigned Data • Unsigned numeric item (Logical Data) • customer number, phone number • Signed numeric item (Arithmetic Data) • quantity, discount, distance

  20. Signed & Unsigned Data • Example CMP CX,DX CX=11000110, DX=00010110 • มองเป็น unsigned data : CX>DX • มองเป็น signed data : CX<DX • การกำหนด signed หรือ unsigned ขึ้นอยู่กับการเลือกใช้คำสั่ง conditional jump

  21. Conditional Jump : unsigned data • JE/JZ : Jump Equal or Jump Zero: ZF • JNE/JNZ : Jump Not Equal or Jump Not Zero: ZF • JA/JNBE : Jump Above or Jump Not Below or Equal: CF, ZF • JAE/JNB : Jump Above or Equal or Jump Not Below: CF • JB/JNAE : Jump Below or Jump Not Above or Equal: CF • JBE/JNA : Jump Below or Equal or Jump Not Above: AF, ZF

  22. Conditional Jump : signed data • JE/JZ : Jump Equal or Jump Zero: ZF • JNE/JNZ : Jump Not Equal or Jump Not Zero : ZF • JG/JNLE : Jump Greater or Jump Not Less or Equal : OF, SF, ZF • JGE/JNL : Jump Greater or Equal or Jump Not Less : OF, SF • JL/JNGE : Jump Less or Jump Not Greater or Equal : OF, SF • JLE/JNG : Jump Less or Equal or Jump Not Greater : OF, SF, ZF

  23. Conditional Jump : special arithmetic tests • JCXZ : Jump if CX is Zero: None • JC : Jump Carry : CF • JNC : Jump No Carry : CF • JO : Jump Overflow : OF • JNO : Jump No Overflow : OF • JP/JPE : Jump Parity or Jump Parity Even : PF • JNP/JPO : Jump No Parity or Jump Parity Odd : PF

  24. Condition Jump : Arithmetic Tests • JS : Jump Sign (negative): SF • JNS : Jump No Sign (positive): SF

  25. proc-name PROC FAR . . proc-name ENDP Calling Procedures • รูปแบบการเขียน Procedure

  26. Calling Procedures • ข้อดีของการเขียน Procedure • ลดจำนวน code • เสริมสร้างการจัดการโปรแกรมที่ดีกว่า • ง่ายต่อการ debug โปรแกรม • ช่วยในการ maintenance โปรแกรม

  27. [label:] CALL proc-name [label:] RET [pop-value] CALL / RET Instructions • เป็นคำสั่งที่เรียกใช้ procedure จาก calling program • รูปแบบ

  28. CALL Instruction • Transfer control จาก calling program ไปยัง procedure • PUSH ที่อยู่ (offset/segment) ของคำสั่งถัดไปที่จะ execute ไว้บน stack

  29. RET Instruction • Transfer control จาก procedure ไปยัง calling program • pop ที่อยู่ของคำสั่งถัดไปที่เก็บไว้บน stack ไปไว้ให้กับ IP

  30. Near call and return • CALL • เรียก procedure ภายในเซกเมนต์เดียวกัน • ลดค่า SP ด้วย 2 • push ค่า IP ปัจจุบันไปไว้บน stack • load offset address ของ call procedure มาไว้ที่ IP • RET/RETN • pop ค่า IP เดิมจาก stack มาไว้ที่ IP • เพิ่มค่า SP ด้วย 2

  31. Far call and return • CALL • เรียก procedure ในเซกเมนต์อื่น • push ค่า CS และ IP ปัจจุบันไปไว้บน stack • RET/RETF • load segment และ offset address ของ call procedure มาไว้ที่ CS และ IP • pop ค่า IP และ CS เดิมจาก stack มาไว้ที่ IP และ CS

More Related