31
1 Control Transfer Instructions Suthida Chaichomchuen [email protected]

Control Transfer Instructions

  • Upload
    harris

  • View
    26

  • Download
    0

Embed Size (px)

DESCRIPTION

Control Transfer Instructions. Suthida Chaichomchuen [email protected]. 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 - PowerPoint PPT Presentation

Citation preview

Page 1: Control Transfer  Instructions

1

Control TransferInstructions

Suthida Chaichomchuen

[email protected]

Page 2: Control Transfer  Instructions

2

Types of Addresses

Short : -128 to 127 bytesNear : -32,768 to 32,767 bytes

Far : over 32K

Page 3: Control Transfer  Instructions

3

Rules on distances for operations

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 yesJnnn yes yes(80386+) yesLOOP yes no noCALL N/A yes yes

Page 4: Control Transfer  Instructions

4

Instruction Labels

ใช้�กั�บ JMP, Jnnn, LOOPตั้�งตั้ามหลั�กักัารกัารตั้�งช้��อ ตั้�องปิ�ดท้�ายด�วยเคร��องหมาย : (colon)ตั้�วอย�างเช้�น

P10: INC CX

Page 5: Control Transfer  Instructions

5

JMP Instruction

เปิลั��ยนแปิลังลั�าด�บกัารท้�างานจากัจ!ดหน"�งไปิย�งอ�กัจ!ดหน"�งโดยไม�ม�เง��อนไข

ร&ปิแบบ

[label:] JMP short, near, or far address

Page 6: Control Transfer  Instructions

6

JMP : Short & Near

- 128Short : label within to + 127 bytes

gggggggg g ggggg2 1 byte for operation [EB] 1 byte for operand [ ค�า offset ท้��จะ

น�าไปิบวกักั�บค�าของ IP จ�ากั�ดท้��00H-FFH]

Page 7: Control Transfer  Instructions

7

JMP : Short & Near

Near : label within 32Kassembler generate

1 byte for operation [E9]2 bytes for operand (8086/80286) or 4 bytes (80386 and later)

Page 8: Control Transfer  Instructions

8

JMP : Forward & Backward

Forward

assembler ย�งไม � generate code เพราะไม�ร& �ว�าจะเปิ+น แบบใด (short / near)

JMP P10. . .

P10:

Page 9: Control Transfer  Instructions

9

JMP : Forward & Backward

Backward within -128 bytes

assembler generate code 2 bytes

P10:. . .JMP P10

Page 10: Control Transfer  Instructions

10

Using the JMP Instruction

MOV AX,01MOV BX,01MOV CX,01

A20:ADD AX,01ADD BX,AXSHL CX,1JMPA20

Page 11: Control Transfer  Instructions

11

LOOP Instruction

ค�าสั่��งควบค!มกัารท้�างานท้��ซ้ำ�ากั�นเปิ+นค�าสั่��งควบค!มท้��ม�เง��อนไข ใช้� CX เปิ+นตั้�วน�บกัารท้�างานภายใน

ลั&ปิCX ถู&กัลัดค�าท้�ลัะ 1 โดยอ�ตั้โนม�ตั้0เม��อ

ค�าสั่��งถู&กั execute

Page 12: Control Transfer  Instructions

12

LOOP Instruction

ร&ปิแบบ

[label:] LOOP short-address

Page 13: Control Transfer  Instructions

13

Using the LOOP Instruction

MOV AX,01MOV BX,01MOV DX,01MOV CX,10

A20:INCAXADD BX,AXSHL DX,1LOOP A20

Page 14: Control Transfer  Instructions

14

LOOPE / LOOPZ Instruction

Loop while equal / Loop while zero ตั้รวจสั่อบค�า CX แลัะแฟลักั ZF

ลัดค�า CX ท้�ลัะ 1 เม��อค�าสั่��งถู&กัexecute

วนลั&ปิเม��อ CX เท้�ากั�บ 0 หร�อ ZF=1 ออกัจากัลั&ปิเม��อ CX<>0 หร�อ ZF=0

Page 15: Control Transfer  Instructions

15

LOOPNE / LOOPNZ Instruction

Loop while not equal / Loop while not zero ตั้รวจสั่อบค�า CX แลัะแฟลักั ZF

ลัดค�า CX ท้�ลัะ 1 เม��อค�าสั่��งถู&กัexecute

วนลั&ปิเม��อ CX ไม�เท้�ากั�บ 0 หร�อ ZF=0 ออกัจากัลั&ปิเม��อ CX=0 หร�อ ZF=1

Page 16: Control Transfer  Instructions

16

CMP Instruction เปิร�ยบเท้�ยบค�าท้��เปิ+นตั้�วเลัขใน operand 1 กั�บ operand 2

ตั้�องม� operand ตั้�วใดตั้�วหน"�งเปิ+นร�จ0สั่ เตั้อร2

ท้�างานคลั�ายค�าสั่��ง SUB แตั้�จะไม�ม�กัารเกั3บค�าผลัลั�พธ์2

ม�ผลัตั้�อแฟลักั AF, CF, OF, PF, SF แลัะZF

Page 17: Control Transfer  Instructions

17

CMP Instructionร&ปิแบบ

[label:] CMP destination,source

destination : register/memorysource : register/memory/immediate

Page 18: Control Transfer  Instructions

18

Conditional Jump Instructions

เปิลั��ยนแปิลังลั�าด�บกัารท้�างานจากัจ!ดหน"�งไปิย�งอ�กัจ!ดหน"�งอย�างม�เง��อนไข

ร&ปิแบบ

[label:] Jnnn short-address

ไม�ม�กัารลัดค�า CX แบบอ�ตั้โนม�ตั้0

Page 19: Control Transfer  Instructions

19

Signed & Unsigned Data

Unsigned numeric item (Logical Data)customer number, phone number

Signed numeric item (Arithmetic Data)quantity, discount, distance

Page 20: Control Transfer  Instructions

20

Signed & Unsigned Data

Example CMP CX,DX11000110CX= ,

00010110DX= มองเปิ+น unsigned data : CX>DX มองเปิ+น signed data : CX<DX กัารกั�าหนด signed หร�อ unsigned ข"น

อย&�กั�บกัารเลั�อกัใช้�ค�าสั่��ง conditional jump

Page 21: Control Transfer  Instructions

21

Conditional Jump : unsigned data

JE/JZ : Jump Equal or Jump Zero : ZFJNE/JNZ : Jump Not Equal or Jump Not Zero : ZFJA/JNBE : Jump Above or Jump Not Below or

Equal : CF, ZFJAE/JNB : Jump Above or Equal or Jump Not

Below : CFJB/JNAE : Jump Below or Jump Not Above or Equal : CFJBE/JNA : Jump Below or Equal or Jump Not

Above : AF, ZF

Page 22: Control Transfer  Instructions

22

Conditional Jump : signed data

JE/JZ : Jump Equal or Jump Zero : ZFJNE/JNZ : Jump Not Equal or Jump Not Zero : ZFJG/JNLE : Jump Greater or Jump Not Less or Equal :

OF, SF, ZFJGE/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

Page 23: Control Transfer  Instructions

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 : OFJP/JPE : Jump Parity or Jump Parit

y Even : PF JNP/JPO : Jump No Parity or Jump Parity

Odd : PF

Page 24: Control Transfer  Instructions

24

Condition Jump : Arithmetic Tests

JS : Jump Sign (negative) : SFJNS : Jump No Sign (positive) : SF

Page 25: Control Transfer  Instructions

25

Calling Procedures

ร&ปิแบบกัารเข�ยน Procedure

proc-name PROC FAR..

proc-name ENDP

Page 26: Control Transfer  Instructions

26

Calling Procedures

ข�อด�ของกัารเข�ยน Procedure ลัดจ�านวน codeเสั่ร0มสั่ร�างกัารจ�ดกัารโปิรแกัรมท้��ด�กัว�า ง�ายตั้�อกัาร debug โปิรแกัรม ช้�วยในกัาร maintenance โปิรแกัรม

Page 27: Control Transfer  Instructions

27

CALL / RET Instructions

เปิ+นค�าสั่��งท้��เร�ยกัใช้� procedure จากั cal l i ng program

ร&ปิแบบ

[label:] CALL proc-name[label:] RET [pop-value]

Page 28: Control Transfer  Instructions

28

CALL Instruction

Transfer control จากั calling program ไปิย�ง procedure

PUSH ท้��อย&� (offset/segment) ของค�าสั่��งถู�ดไปิท้��จะ execute ไว�บน

stack

Page 29: Control Transfer  Instructions

29

RET Instruction

Transfer control จากัprocedure ไปิย�ง calling program

pop ท้��อย&�ของค�าสั่��งถู�ดไปิท้��เกั3บไว�บนstack ไปิไว�ให�กั�บ IP

Page 30: Control Transfer  Instructions

30

Near call and return

CALL เร�ยกั procedure ภายในเซ้ำกัเมนตั้2เด�ยวกั�น ลัดค�า SP ด�วย 2 push ค�า IP ปิ6จจ!บ�นไปิไว�บน stackload offset address ของ call procedure มาไว�ท้�� IP

RET/RETNpop ค�า IP เด0มจากั stack มาไว�ท้�� IP เพ0�มค�า SP ด�วย 2

Page 31: Control Transfer  Instructions

31

Far call and returnCALL

เร�ยกั procedure ในเซ้ำกัเมนตั้2อ��น push ค�า CS แลัะ IP ปิ6จจ!บ�นไปิไว�บน stack

RET/RETFload segment แลัะ offset address

ของ call procedure มาไว�ท้�� CS แลัะ IPpop ค�า IP แลัะ CS เด0มจากั stack มาไว�ท้��IP แลัะ CS