Control Transfer Instructions

Post on 11-Jan-2016

26 views 0 download

description

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 - PowerPoint PPT Presentation

Transcript of Control Transfer Instructions

1

Control TransferInstructions

Suthida Chaichomchuen

std@kmitnb.ac.th

2

Types of Addresses

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

Far : over 32K

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

4

Instruction Labels

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

P10: INC CX

5

JMP Instruction

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

ร&ปิแบบ

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

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]

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)

8

JMP : Forward & Backward

Forward

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

JMP P10. . .

P10:

9

JMP : Forward & Backward

Backward within -128 bytes

assembler generate code 2 bytes

P10:. . .JMP P10

10

Using the JMP Instruction

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

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

11

LOOP Instruction

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

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

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

12

LOOP Instruction

ร&ปิแบบ

[label:] LOOP short-address

13

Using the LOOP Instruction

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

A20:INCAXADD BX,AXSHL DX,1LOOP A20

14

LOOPE / LOOPZ Instruction

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

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

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

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

16

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

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

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

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

17

CMP Instructionร&ปิแบบ

[label:] CMP destination,source

destination : register/memorysource : register/memory/immediate

18

Conditional Jump Instructions

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

ร&ปิแบบ

[label:] Jnnn short-address

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

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,DX11000110CX= ,

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

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

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

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

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

24

Condition Jump : Arithmetic Tests

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

25

Calling Procedures

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

proc-name PROC FAR..

proc-name ENDP

26

Calling Procedures

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

27

CALL / RET Instructions

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

ร&ปิแบบ

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

28

CALL Instruction

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

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

stack

29

RET Instruction

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

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

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

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