instruction set of 8086

117

Click here to load reader

Upload: tirumalesh-nizampatnam

Post on 21-Feb-2017

235 views

Category:

Engineering


48 download

TRANSCRIPT

Page 1: Instruction set of 8086

Instruction set of 8086 MicroprocessorInstruction set of 8086 Microprocessor

N. THIRUMALESH

Assoc. Prof.

Dept. of ECE,

RISE Krishna Sai Prakasam Group of Institutions, Ongole

Page 2: Instruction set of 8086

2

• The sequence of commands used to tell a microcomputer what to do is called a program,

• Each command in a program is called an instruction• 8088 understands and performs operations for 117 basic

instructions• The native language of the IBM PC is the machine language of the

8088• A program written in machine language is referred to as machine

code• In 8088 assembly language, each of the operations is described by

alphanumeric symbols instead of 0-1s.

ADD AX, BX

(Opcode) (Destination operand) (Source operand )

SoftwareSoftware

Page 3: Instruction set of 8086

3

InstructionsInstructionsLABEL: INSTRUCTION ; COMMENT

Address identifier Does not generate any machine code

• Ex. START: MOV AX, BX ; copy BX into AX

• There is a one-to-one relationship between assembly and machine language instructions

• A compiled machine code implementation of a program written in a high-level language results in inefficient code

– More machine language instructions than an assembled version of an equivalent handwritten assembly language program

Page 4: Instruction set of 8086

4

• Two key benefits of assembly language programming

– It takes up less memory

– It executes much faster

Page 5: Instruction set of 8086

5

ApplicationsApplications

• One of the most beneficial uses of assembly language programming is real-time applications.

Real time means the task required by the application must be completed before any other input to the program that will alter its operation can occur

For example the device service routine which controls the operation of the floppy disk drive is a good example that is usually written in assembly language

Page 6: Instruction set of 8086

6

• Assembly language not only good for controlling hardware devices but also performing pure software operations

– Searching through a large table of data for a special string of characters

– Code translation from ASCII to EBCDIC– Table sort routines– Mathematical routines

Assembly language: perform real-time operations

High-level languages: used to write those parts that are not time critical

Page 7: Instruction set of 8086

7

Converting Assembly Language Instructions to Machine Code

• An instruction can be coded with 1 to 6 bytes• Byte 1 contains three kinds of information

– Opcode field (6 bits) specifies the operation (add, subtract, move)

– Register Direction Bit (D bit) Tells the register operand in REG field in byte 2 is source or destination operand1: destination 0: source

- Data Size Bit (W bit) Specifies whether the operation will be performed on 8-bit or 16-bit data 0: 8 bits 1: 16 bits

Page 8: Instruction set of 8086

8

• Byte 2 has three fields– Mode field (MOD)– Register field (REG) used to identify the register for the first operand– Register/memory field (R/M field)

Page 9: Instruction set of 8086

9

- Data Copy/ Transfer Instructions

- Arithmetic & Logical Instructions

- Shift & Rotate Instructions

- Brach Instructions

- Loop Instructions

- String Instructions

- Machine Control Instructions

- Flag Manipulation Instructions

Page 10: Instruction set of 8086

10

Mnemonic Meaning Format Operation Flags affected

MOV Move Mov D,S (S) (D) None

Data Transfer Instructions - MOVData Transfer Instructions - MOV

Destination Source Memory Accumulator Accumulator Memory

Register Register Register Memory Memory Register Register Immediate Memory ImmediateSeg reg Reg 16Seg reg Mem 16 Reg 16 Seg reg Memory Seg reg

NO MOV

MemoryImmediateSegment Register

MemorySegment RegisterSegment Register

EX: MOV AL, BL

Page 11: Instruction set of 8086

11

Data Transfer Instructions - PUSH & POPData Transfer Instructions - PUSH & POP

PUSH: Push to Stack

Ex: PUSH AX PUSH DS PUSH [5000H]

POP: Pop from Stack

Ex: POP AX POP DS POP [5000H]

55H22H

2FFFE2FFFF

2FFFD

PUSH AX

55 22

XX

55H22H

2FFFE2FFFF

2FFFD

POP AX

55 22

AH AL

AH AL

Page 12: Instruction set of 8086

12

Data Transfer Instructions - XCHGData Transfer Instructions - XCHG

Mnemonic Meaning Format Operation Flags affected

XCHG Exchange XCHG D,S (S) (D) None

Destination Source

Accumulator Reg 16

Memory Register

Register Register

Register Memory

Example: XCHG [1234h], BX

NO XCHG MEMsSEG REGs

Page 13: Instruction set of 8086

13

The XLAT InstructionThe XLAT Instruction

XLAT – TranslateReplace the byte in AL with byte from users table, addressed by BX. Original value of AL is index

into translate table.

MOV BX, OFFSET TABLEMOV AL, 00HXLAT(AL) ← 5

AL [BX+AL]

35(BX)

Base of table

Page 14: Instruction set of 8086

14

Input – Output PORT- IN AL, P8 – Byte operation (AL) ← (P8)- IN AL, DX – port no. In (DX) (AL) ← (P.DX)

IN AX, P8 – word operation(AL) ← (P8 ), (AH) ← (P8 + 1) IN AX, DX(AL) ← (P.DX), (AH) ← (P.DX + 1)

OUT P8 , AL (P8 ) ← (AL) OUT DX, AL (P.DX) ← (AL) OUT P8 , AX (P8) ← (AL), (P8 + 1) ← (AH) OUT DX, AX (P.DX) ← (AL), (P.DX + 1) ← (AH)

Data Transfer Instructions – IN & OUTData Transfer Instructions – IN & OUT

Page 15: Instruction set of 8086

15

Data Transfer Instructions – LEA, LDS, LES

Mnemonic

Meaning Format Operation Flags affected

LEA Load Effective Address

LEA Reg16,EA EA (Reg16) None

LDS Load Register And DS

LDS Reg16,MEM32 (MEM32) (Reg16)

(Mem32+2) (DS)

None

LES Load Register and ES

LES Reg16,MEM32 (MEM32) (Reg16)

(Mem32+2) (DS)

None

LEA SI DATA (or) MOV SI Offset DATA

Page 16: Instruction set of 8086

16

Page 17: Instruction set of 8086

17

Page 18: Instruction set of 8086

18

Pointer Operations- Load pointer using DS

LDS RW, DADDR RW ← (EA) Offset Segment(DS) ← (EA + 2)

- Load offset to Register LEA RW, DADDR RW ← (EA)

offset of location DADDR

Page 19: Instruction set of 8086

19

Arithmetic Instructions: ADD, ADC, INC, AAA, DAA

Mnemonic Meaning Format Operation Flags affected

ADD Addition ADD D,S (S)+(D) (D) carry (CF)

ALL

ADC Add with carry

ADC D,S (S)+(D)+(CF) (D) carry (CF)

ALL

INC Increment by one

INC D (D)+1 (D) ALL but CY

AAA ASCII adjust for addition

AAA If the sum is >9, AH is incremented by 1

AF,CF

DAA Decimal adjust for addition

DAA Adjust AL for decimalPacked BCD

ALL

Page 20: Instruction set of 8086

20

Examples:

Ex.1 ADD AX,2 ADC AX,2 Ex.2 INC BX INC WORD PTR [BX]

Ex.3 ASCII CODE 0-9 = 30-39h

MOV AX,38H ; (ASCII code for number 8) ADD AL,39H ; (ASCII code for number 9)

AL=71h AAA ; used for addition AH=01,

AL=07 ADD AX,3030H ; answer to ASCII 0107

AX=3137Ex.4 AL contains 25 (packed BCD) BL contains 56 (packed BCD)

ADD AL, BL DAA

25+ 56--------

7B 81

Page 21: Instruction set of 8086

21

Arithmetic Instructions – SUB, SBB, DEC, AAS, DAS, NEG

Mnemonic Meaning Format Operation Flags affected

SUB Subtract SUB D,S (D) - (S) (D) Borrow (CF)

All

SBB Subtract with

borrow

SBB D,S (D) - (S) - (CF) (D)

All

DEC Decrement by one

DEC D (D) - 1 (D) All but CF

NEG Negate NEG D All

DAS Decimal adjust for

subtraction

DAS Convert the result in AL to packed decimal format

All

AAS ASCII adjust for

subtraction

AAS (AL) difference(AH) dec by 1 if borrow

CY,AC

Page 22: Instruction set of 8086

22

Examples: DAS

MOV BL, 28HMOV AL, 83HSUB AL,BL ; AL=5BHDAS ; adjust as AL=55H

MOV AX, 38HSUB AL,39H; AX=00FFAAS ; AX=FF09 ten’s complement of -1 (Borrow one from AH )

OR AL,30H ; AL=39

Page 23: Instruction set of 8086

ASCII ADJUST

23

Page 24: Instruction set of 8086

INSTRUCTIONS AAA - ASCII Adjust After Addition AAS - ASCII Adjust After Subtraction AAM - ASCII Adjust After Multiply AAD - ASCII Adjust Before Division

24

Page 25: Instruction set of 8086

The PC supports BCD format, Uses of BCD

1)No loss of precision2)Simpler to perform arithmetic

operation on small values from keyboardBCD can be stored in two way:

Unpacked BCDPacked BCD

25

Page 26: Instruction set of 8086

26

Unpacked BCD representation contains only one decimal digit per byte.

The digit is stored in the least significant 4 bits; the most significant 4 bits are not relevant to the value of the represented number.

Example: Representing 1527 01 05 02 07h

Unpacked BCD Data

Page 27: Instruction set of 8086

27

Packed BCD Data

Packed BCD representation packs two Decimal digits into a single byte.

Example: Representing 1527 15 27h

Page 28: Instruction set of 8086

AAA - ASCII Adjust After Addition

Adjusts the result of the addition of two unpacked BCD values to create a unpacked BCD result.

Operation 1:In ALIf rightmost nibble is >9 (ie)A to F Or Auxiliary Flag=1ADD 6 to rightmost nibble

28

Page 29: Instruction set of 8086

Operation 2:Clear left nibble form AL.

Operation 3: In AH ADD 1

Operation 4:Set Carry and Auxiliary Carry

29

Page 30: Instruction set of 8086

.model small

.datab1 dw 38hb2 dw 34h

.codemov ax,@datamov ds,axmov ax,b1 ;moving unpacked BCD into axmov bx,b2 ;moving unpacked BCD into bxadd ax,bxaaa ;adjusting unpacked BCD after additionor ax,3030hend

30

Page 31: Instruction set of 8086

AAS - ASCII Adjust After Subtraction

Adjusts the result of the subtraction of two unpacked BCD values to create a unpacked BCD result.

Operation 1: a)AAS checks the rightmost nibble in AL b)If rightmost nibble is >9 (ie)A to F Or AuxilaryFlag=1 c)Then Subtract 6 from rightmost nibble

31

Page 32: Instruction set of 8086

Operation 2:Clear left nibble in AL.

Operation 3:Subtracts 1 from AH

Operation 4:Set Carry and AuxilaryCarry

32

Page 33: Instruction set of 8086

Example :d1 contains 34h , d2 contains 38h of byte type

Ax AFMOV AL, d1 ; 0034 Sub AL, d2; 00fc 1AAS ; ff06 1 since the rightmost digit of AL is c , subtract

6 from AL Subtract 1 from ah, set AF and CF flags The answer is -4, is ff06 in 10’s complement

33

Page 34: Instruction set of 8086

.model small

.datab1 dw 38hb2 dw 34h.codemov ax,@datamov ds,axmov ax,b1 ;moving unpacked BCD into axmov bx,b2 ;moving unpacked BCD into bxsub ax,bxaas ;adjusting unpacked BCD after subtraction

or ax,3030hend

34

Page 35: Instruction set of 8086

AAM - ASCII Adjust After Multiplication

For multiplication and Division of ASCII numbers require that the numbers first be converted into unpacked BCD format.

Before Multiplication First clear the leftmost nibble in each Byte.

After Multiplication AAM performs the following operations 1) Divides AL value by 10 (0AH) 2) Stores Quotient in AH 3) Store Remainder in AL

35

Page 36: Instruction set of 8086

Example:AL contains 35H and CL contains 39H Instruction comment AX CLand CL, 0Fh; Convert CL to 09 0035 39and AL,0Fh; Convert AL to 05 0005 09mul CL; Multiply AL by CL 002DAAM ; Convert to unpacked 0405 BCDOr AX,3030h; covert to ASCII 3435

36

Page 37: Instruction set of 8086

MUL operation generates 45 (002Dh) in AX

AAM divides this value by 10, so quotient of 04 in AH and remainder of 05 in AL

OR instruction converts the unpacked BCD to ASCII format

37

Page 38: Instruction set of 8086

.model small

.datab1 dw 39h ; 9 in ASCII Formatb2 dw 35h ; 5 in ASCII Format.codemov ax,@datamov ds,axmov ax,b1 ;AX=0039hand AL,0fh ;AX=0009hmov bx,b2 ;BX=0035hand bl,0fh ;BX=0005hmul bx ;AX=002Dhaam or ax,3030hend

38

Page 39: Instruction set of 8086

AAD - ASCII Adjust Before Division AAD allows for a 2-Byte Dividend in AX. The

divisor can be only a single Byte(0-9) Before Division First clear the leftmost nibble in

each Byte. Operations done by AAD instruction 1) AAD multiplies the AH by 10(0Ah).

2) Then adds the product to AL and clears the AH

After AAD , division is performed.

39

Page 40: Instruction set of 8086

Example:AX contains 3238 (28) - DividendCL contains 37 (07) – DivisorInstruction comment AX CLand CL,0Fh; convert to unpacked 3238 07 BCDand AX,0F0Fh; convert to unpacked 0208 07 BCDAAD; convert to binary 001Cdiv CL; divide by 7 0004

40

Page 41: Instruction set of 8086

AAD multiplies the AH by 10(0Ah) Adds the product 20(14h) to the AL and

clears the AH The result is 001Ch, is hex representation

of decimal 28 Then division is performed. Remainder stored in AH, Quotient stored

in AL

41

Page 42: Instruction set of 8086

model small.datab1 dw 3238h ; 28 in ASCII Formatb2 db 37h ; 7 in ASCII Format.codemov ax,@datamov ds,axmov ax,b1 ;AX=3238hand ax,0f0fh ;AX=0208hmov cl,b2 ;CL=37hand cl,0fh ;CL=07h aad ; AX= 001cdiv cl ; AX=0004or ax,3030h; AX=3034end

42

Page 43: Instruction set of 8086

43

Multiplication and Division

Page 44: Instruction set of 8086

44

Multiplication and Division

Page 45: Instruction set of 8086

45

Multiplication(MUL or IMUL)

Multiplicand Operand(Multiplier)

Result

Byte*Byte AL Register or memory AX

Word*Word AX Register or memory DX :AX

Dword*Dword EAX Register or memory EAX :EDX

Division (DIV or IDIV)

Dividend Operand (Divisor)

Quotient: Remainder

Word/Byte AX Register or Memory AL : AH

Dword/Word DX:AX Register or Memory AX : DX

Qword/Dword EDX: EAX Register or Memory EAX : EDX

Multiplication and Division

Page 46: Instruction set of 8086

46

Multiplication and Division Examples

Ex1: Assume that each instruction starts from these values:AL = 85H, BL = 35H, AH = 0H

1. MUL BL → AL . BL = 85H * 35H = 1B89H → AX = 1B89H 2. IMUL BL → AL . BL = 2’S AL * BL = 2’S (85H) * 35H = 7BH * 35H = 1977H→ 2’s comp → E689H → AX.

• DIV BL → = = 02 (85-02*35=1B) →

4. IDIV BL → = =

1BHH

350085

02AH AL

BLAX

BLAX

HH

350085

1B 02AH AL

Page 47: Instruction set of 8086

47

Ex2: AL = F3H, BL = 91H, AH = 00H

1. MUL BL → AL * BL = F3H * 91H = 89A3H → AX = 89A3H

2. IMUL BL → AL * BL = 2’S AL * 2’S BL = 2’S (F3H) * 2’S(91H) = 0DH * 6FH = 05A3H → AX.

 3.IDIV BL → = = = 2→ (00F3 – 2*6F=15H)

BLAX

)91('2300HSHF

FHHF

6300

AH AL15 02

R Q

NEGNEGPOS

→ 2’s(02) = FEH→ AH AL15 FE→

4. DIV BL → = = 01→(F3-1*91=62) → BLAX

HHF

91300 AH AL

62 01

R Q

Page 48: Instruction set of 8086

48

Ex3: AX= F000H, BX= 9015H, DX= 0000H

1. MUL BX = F000H * 9015H =

DX AX8713 B000

2. IMUL BX = 2’S(F000H) * 2’S(9015H) = 1000 * 6FEB =DX AX

06FE B000

3. DIV BL = = B6DH → More than FFH → Divide Error. HHF

15000

4. IDIV BL → = = C3H > 7F → Divide Error. HHFS

15)000('2

HH

151000

Page 49: Instruction set of 8086

49

Ex4: AX= 1250H, BL= 90H

1.    IDIV BL → = = = = = BLAX

HH

901250

NEGPOS

sNEGPOS'2 )90('2

1250HsH

HH

701250

= 29H (Q) → (1250 – 29 * 70) = 60H (REM)

29H ( POS) → 2’S (29H) = D7H → R Q

60H D7H

2. DIV BL → = = 20H→1250-20*90 =50H → BLAX

HH

901250 R Q

50H 20H

AH AL

Page 50: Instruction set of 8086

50

Mnemonic Meaning Format Operation Flags AffectedAND

OR

XOR

NOT

Logical AND

Logical Inclusive OR

Logical Exclusive OR

LOGICAL NOT

AND D,S

OR D,S

XOR D,S

NOT D

(S) · (D) → (D)

(S)+(D) → (D)

(S) (D)→(D)

_ (D) → (D)

OF, SF, ZF, PF, CF

AF undefinedOF, SF, ZF, PF,

CFAF undefined

OF, SF, ZF, PF, CF

AF undefinedNone

+

Logical Instructions

Destination Source

RegisterRegisterMemoryRegisterMemory

Accumulator

RegisterMemoryRegister

ImmediateImmediateImmediate

Destination

Register Memory

Page 51: Instruction set of 8086

51

• AND– Uses any addressing mode except memory-to-memory and

segment registers– Especially used in clearing certain bits (masking) xxxx xxxx AND 0000 1111 = 0000 xxxx

(clear the first four bits)– Examples: AND BL, 0FH

AND AL, [345H]

• OR– Used in setting certain bits

xxxx xxxx OR 0000 1111 = xxxx 1111(Set the upper four bits)

Logical Instructions

Page 52: Instruction set of 8086

52

• XOR– Used in Inverting bits

xxxx xxxx XOR 0000 1111 = xxxxx’x’x’x’

-Example: Clear bits 0 and 1, set bits 6 and 7, invert bit 5 of register CL:

AND CL, OFCH ; 1111 1100BOR CL, 0C0H ; 1100 0000BXOR CL, 020H ; 0010 0000B

Logical Instructions

Page 53: Instruction set of 8086

53

Shift and Rotate InstructionsSHL/SAL: shift logical left/shift arithmetic left SHR: shift logical right SAR: shift arithmetic right ROL: rotate left ROR: rotate right RCL: rotate left through carry RCR: rotate right through carry

Page 54: Instruction set of 8086

54

Logical vs Arithmetic Shifts• A logical shift fills the newly created bit position with zero:

• An arithmetic shift fills the newly created bit position with a copy of the number’s sign bit:

Page 55: Instruction set of 8086

55

Mnemonic Meaning Format Operation Flags Affected

SAL/SHL

SHR

SAR

Shift arithmeticLeft/shiftLogical left

Shift logical right

Shift arithmeticright

SAL/SHL D, Count

SHR D, Count

SAR D, Count

Shift the (D) left by the number of bit positions equal to count and fill the vacated bits positions on the right with zeros

Shift the (D) right by the number of bit positions equal to count and fill the vacated bits positions on the left with zeros

Shift the (D) right by the number of bit positions equal to count and fill the vacated bits positions on the left with the original most significant bit

CF,PF,SF,ZFAF undefinedOF undefined if count ≠1

CF,PF,SF,ZFAF undefinedOF undefined if count ≠1

CF,PF,SF,ZFAF undefinedOF undefined if count ≠1

Shift Instructions

Page 56: Instruction set of 8086

56

Allowed operands

Destination Count

Register

Register Memory Memory

1

CL

1

CL

Page 57: Instruction set of 8086

57

Page 58: Instruction set of 8086

58

SHL Instruction

• The SHL (shift left) instruction performs a logical left shift on the destination operand, filling the lowest bit with 0.

• Operand types:SHL reg,imm8SHL mem,imm8SHL reg,CLSHL mem,CL

Page 59: Instruction set of 8086

59

Fast Multiplication

mov dl,5

shl dl,1

Shifting left 1 bit multiplies a number by 2

mov dl,5shl dl,2 ; DL = 20

Shifting left n bits multiplies the operand by

2n

For example, 5 * 22 = 20

Page 60: Instruction set of 8086

60

SHR Instruction

• The SHR (shift right) instruction performs a logical right shift on the destination operand. The highest bit position is filled with a zero.

MOV DL,80SHR DL,1 ; DL = 40SHR DL,2 ; DL = 10

Shifting right n bits divides the operand by 2n

Page 61: Instruction set of 8086

61

SAR Instruction

• SAR (shift arithmetic right) performs a right arithmetic shift on the destination operand.

An arithmetic shift preserves the number's sign.

MOV DL,-80SAR DL,1 ; DL = -40SAR DL,2 ; DL = -10

Page 62: Instruction set of 8086

62

Rotate InstructionsRotate InstructionsMnem-onic

Meaning Format Operation Flags Affected

ROL Rotate Left

ROL D,Count Rotate the (D) left by the number of bit positions equal to Count. Each bit shifted out from the left most bit goes back into the rightmost bit position.

CF OF undefined if count ≠ 1

ROR Rotate Right

ROR D,Count Rotate the (D) right by the number of bit positions equal to Count. Each bit shifted out from the rightmost bit goes back into the leftmost bit position.

CF OF undefined if count ≠ 1

RCL Rotate Left through Carry

RCL D,Count Same as ROL except carry is attached to (D) for rotation.

CF OF undefined if count ≠ 1

RCR Rotate right through Carry

RCR D,Count Same as ROR except carry is attached to (D) for rotation.

CF OF undefined if count ≠ 1

Page 63: Instruction set of 8086

63

ROL Instruction

• ROL (rotate) shifts each bit to the left• The highest bit is copied into both the Carry

flag and into the lowest bit• No bits are lost

MOV Al,11110000bROL Al,1 ; AL = 11100001b

MOV Dl,3FhROL Dl,4 ; DL = F3h

Page 64: Instruction set of 8086

64

ROR Instruction

• ROR (rotate right) shifts each bit to the right• The lowest bit is copied into both the Carry flag and

into the highest bit• No bits are lost

MOV AL,11110000bROR AL,1 ; AL = 01111000b

MOV DL,3FhROR DL,4 ; DL = F3h

Page 65: Instruction set of 8086

65

RCL Instruction• RCL (rotate carry left) shifts each bit to the left• Copies the Carry flag to the least significant bit• Copies the most significant bit to the Carry flag

CF

CLC ; CF = 0MOV BL,88H ; CF,BL = 0 10001000bRCL BL,1 ; CF,BL = 1 00010000bRCL BL,1 ; CF,BL = 0 00100001b

Page 66: Instruction set of 8086

66

RCR Instruction• RCR (rotate carry right) shifts each bit to the right• Copies the Carry flag to the most significant bit• Copies the least significant bit to the Carry flag

STC ; CF = 1MOV AH,10H ; CF,AH = 00010000 1RCR AH,1 ; CF,AH = 10001000 0

Page 67: Instruction set of 8086

67

Destination Count

Register

Register Memory Memory

1

CL

1

CL

Rotate Instructions

Page 68: Instruction set of 8086

68

Compare Instruction, CMPMnemonic

Meaning Format Operation Flags Affected

CMP Compare CMP D,S (D) – (S) is used in setting or resetting the flags

CF, AF, OF, PF, SF, ZF

(D) = (S) ; ZF=0

(D) > (S) ; ZF=0, CF=0

(D) < (S) ; ZF=0, CF=1

Allowed OperandsDestination Source

RegisterRegister

RegisterMemory

Memory Register

RegisterImmediate

Memory Immediate

Accumulator Immediate

Page 69: Instruction set of 8086

69

Branch InstructionsBranch Instructions

Branch instructions provide lot of convenience to the programmer to perform operations selectively, repetitively etc.

Branch group of instructions

Conditional jumps

Uncondi-tional jump

Iteration instructions

CALL instructions

Return instructions

Page 70: Instruction set of 8086

70

SUBROUTINE & SUBROUTINE HANDILING INSTRUCTIONS

Call subroutine ANext instruction

Call subroutine ANext instruction

Main program

Subroutine A

First Instruction

Return

Page 71: Instruction set of 8086

71

A subroutine is a special segment of program that can be called for execution from any point in a program.

An assembly language subroutine is also referred to as a “procedure”. Whenever we need the subroutine, a single instruction is inserted in to

the main body of the program to call subroutine. To branch a subroutine the value in the IP or CS and IP must be

modified. After execution, we want to return the control to the instruction that

immediately follows the one called the subroutine i.e., the original value of IP or CS and IP must be preserved.

Execution of the instruction causes the contents of IP to be saved on the stack. (this time (SP) (SP) -2 )

A new 16-bit (near-proc, mem16, reg16 i.e., Intra Segment) value which is specified by the instructions operand is loaded into IP.

Examples: CALL 1234HCALL BXCALL [BX]

Page 72: Instruction set of 8086

72

• Inter Segment

– At starting CS and IP placed in a stack.– New values are loaded in to CS and IP given by the

operand.– After execution original CS, IP values placed as it is.

Far-procMemptr32

These two words (32 bits) are loaded directly into IP and CS with execution at CALL instruction.

First 16 IP

Next 16 CS

Page 73: Instruction set of 8086

73

Mnem-onic

Meaning Format Operation Flags Affected

CALL Subroutine call

CALL operand Execution continues from the address of the subroutine specified by the operand. Information required to return back to the main program such as IP and CS are saved on the stack.

none

OperandOperand

Near-proc

Far – proc

Memptr 16

Regptr 16

Memptr 32

Page 74: Instruction set of 8086

74

RETURNRETURN• Every subroutine must end by executing an instruction that returns control to

the main program. This is the return (RET) instruction.

• By execution the value of IP or IP and CS that were saved in the stack to be returned back to their corresponding regs. (this time (SP) (SP)+2 )

Mnem-onic

Meaning Format Operation Flags Affected

RET Return RET orRET operand

Return to the main program by restoring IP (and CS for far-proc). If operands is present, it is added to the contents of SP.

None

OperandOperand

None

Disp16

Page 75: Instruction set of 8086

75

Loop InstructionsLoop Instructions• These instructions are used to repeat a set of instructions several

times.• Format: LOOP Short-Label• Operation: (CX) (CX)-1• Jump is initialized to location defined by short label if CX≠0.

otherwise, execute next sequential instruction.• Instruction LOOP works w.r.t contents of CX. CX must be

preloaded with a count that represents the number of times the loop is to be repeat.

• Whenever the loop is executed, contents at CX are first decremented then checked to determine if they are equal to zero.

• If CX=0, loop is complete and the instruction following loop is executed.

• If CX ≠ 0, content return to the instruction at the label specified in the loop instruction.

Page 76: Instruction set of 8086

76

General format : LOOP r8 ; r8 is 8-bit signed value.

It is a 2 byte instruction.

Used for backward jump only.

Maximum distance for backward jump is only 128 bytes.

LOOP AGAIN is almost same as: DEC CXJNZ AGAIN

LOOP instruction does not affect any flags.

LOOP Instruction contd.LOOP Instruction contd.

Page 77: Instruction set of 8086

77

Mnemonic meaning format Operation

LOOP Loop Loop short-label (CX) (CX) – 1Jump to location given by short-label if CX ≠ 0

LOOPE/ LOOPZ

Loop while equal/ loop while zero

LOOPE/LOOPZ short-label

(CX) (CX) – 1Jump to location given by short-label if CX ≠ 0 and ZF=1

LOOPNE/ LOOPNZ

Loop while not equal/ loop while not zero

LOOPNE/LOOPNZ short-label

(CX) (CX) – 1Jump to location given by short-label if CX ≠ 0 and ZF=0

Page 78: Instruction set of 8086

78

Unconditional JumpUnconditional Jump

Part 1

JMP AA Unconditional JMP

Skipped part

Part 3

AA XXXX

Part 2

Next instruction

Control flow and JUMP instructionsControl flow and JUMP instructions

JMP unconditional jump

JMP Operand

Page 79: Instruction set of 8086

79

Unconditional JumpUnconditional Jump

Unconditional Jump Instruction

Near Jump or Far Jump orIntra segment Jump Inter segment Jump

(Jump within the segment) (Jump to a different segment)Is limited to the address with in the current segment. It is achieved by modifying value in IP

Permits jumps from one code segment to another. It is achieved by modifying CS and IP

OperandsOperandsShort label

Near label

Far label

Memptr16

Regptr16

memptr32

Inter Segment Jump

Inter Segment Jump

Page 80: Instruction set of 8086

80

Conditional JumpConditional Jump

Part 1

Jcc AA Conditional Jump

Skipped part

Part 2

XXXX

Part 3

AA XXXX

condition

YES

NO

Next instruction

Page 81: Instruction set of 8086

81

Conditional Jump instructionsConditional Jump instructions

Conditional Jump instructions in 8086 are just 2 bytes long. 1-byte opcode followed by 1-byte signed displacement (range of –128 to +127).

Conditional Jump Instructions

Jumps based on a single flag

Jumps based on more than one flag

Page 82: Instruction set of 8086

82

Conditional Jump InstructionsConditional Jump Instructions

Mnemonic : Jcc

Meaning : Conditional Jump

Format : Jcc operand

Operation : If condition is true jump to the address specified by operand. Otherwise the next instruction is executed.

Flags affected : None

Page 83: Instruction set of 8086

83

Mnemonic meaning condition

JA Above CF=0 and ZF=0

JB Above or Equal CF=0

JB Below CF=1

JBE Below or Equal CF=1 or ZF=1

JC Carry CF=1

JCXZ CX register is Zero (CF or ZF)=0

JE Equal ZF=1

JG Greater ZF=0 and SF=OF

JGE Greater or Equal SF=OF

JL Less (SF XOR OF) = 1

TYPESTYPES

Page 84: Instruction set of 8086

84

Mnemonic meaning condition

JLE Less or Equal ((SF XOR OF) or ZF) = 1

JNA Not Above CF =1 or Zf=1

JNAE Not Above nor Equal CF = 1

JNB Not Below CF = 0

JNBE Not Below nor Equal CF = 0 and ZF = 0

JNC Not Carry CF = 0

JNE Not Equal ZF = 0

JNG Not Greater ((SF XOR OF) or ZF)=1

JNGE Not Greater nor Equal

(SF XOR OF) = 1

JNL Not Less SF = OF

Page 85: Instruction set of 8086

85

Mnemonic meaning conditionJNLE Not Less nor Equal ZF = 0 and SF = OFJNO Not Overflow OF = 0JNP Not Parity PF = 0JNZ Not Zero ZF = 0JNS Not Sign SF = 0JO Overflow OF = 1JP Parity PF = 1JPE Parity Even PF = 1JPO Parity Odd PF = 0JS Sign SF = 1JZ Zero ZF = 1

Page 86: Instruction set of 8086

86

Jumps Based on a single flagJumps Based on a single flagJZ r8 ;Jump if zero flag set to 1 (Jump if result is zero)

JNZ r8 ;Jump if Not Zero (Z flag = 0 i.e. result is nonzero)

JS r8 ;Jump if Sign flag set to 1 (result is negative)

JNS r8 ;Jump if Not Sign (result is positive)

JC r8 ;Jump if Carry flag set to 1

JNC r8 ;Jump if No Carry

JP r8 ;Jump if Parity flag set to 1 (Parity is even)

JNP r8 ;Jump if No Parity (Parity is odd)

JO r8 ;Jump if Overflow flag set to 1 (result is wrong)

JNO r8 ;Jump if No Overflow (result is correct)

There is no jump based on AC flag

Page 87: Instruction set of 8086

87

JZ r8 ; JE (Jump if Equal) also means same.JNZ r8 ; JNE (Jump if Not Equal) also means same.JC r8 ;JB (Jump if below) and JNAE (Jump if Not Above or Equal) also mean same.

JNC r8 ;JAE (Jump if Above or Equal) and JNB (Jump if Not Above) also mean same.

JZ, JNZ, JC and JNC used after arithmetic operationJE, JNE, JB, JNAE, JAE and JNB are used after a compare operation.

JP r8 ; JPE (Jump if Parity Even) also means same. JNP r8 ; JPO (Jump if Parity Odd) also means same.

Page 88: Instruction set of 8086

88

Examples for JE or JZ instruction

Ex. for forward jump (Only examples for JE given)

CMP SI, DI

JE SAME

Should be <=127 bytes

ADD CX, DX ;Executed if Z = 0

: (if SI not equal to DI)

:

SAME: SUB BX, AX ;Executed if Z = 1

(if SI = DI)

Page 89: Instruction set of 8086

89

Examples for JE or JZ instruction

Ex. for backward jumpBACK: SUB BX, AX ; executed if Z = 1

Should be <= 128 bytes

: (if SI = DI):

CMP SI, DIJE BACKADD CX, DX ;executed if Z = 0

(if SI not equal to DI)

Page 90: Instruction set of 8086

90

Jumping beyond -128 to +127?

Requirement Then do this!

CMP SI, DI CMP SI, DI

JE SAME JNE NEXT

What if >127 bytes

ADD CX, DX JMP SAME

: NEXT: ADD CX, DX

: :

SAME: SUB BX, AX :

SAME: SUB BX, AX

Range for JMP (unconditional jump) can be +215 = + 32K JMP instruction discussed in detail later

Page 91: Instruction set of 8086

91

Terms used in comparison

Above and Below used for comparing Unsigned nos.Greater than and less than used with signed numbers.All Intel microprocessors use this convention.

95H is above 65H Unsigned comparison - True95H is less than 65H Signed comparison - True

95H is negative, 65H is positive

65H is below 95H Unsigned comparison - True65H is greater than 95H Signed comparison - True

Page 92: Instruction set of 8086

92

Jump on multiple flagsJump on multiple flagsConditional Jumps based on more than one flag are used after a CMP (compare) instruction.

JBE or JNA

Jump if Below or EqualJump if Not Above

Jump if No Jump if Ex.

Cy = 1 OR Z= 1 Cy = 0 AND Z = 0 CMP BX, CX

Below OR Equal Surely Above JBE BX_BE

BX_BE (BX is Below or Equal) is a symbolic location

Page 93: Instruction set of 8086

93

Jump on multiple flags contdJump on multiple flags contd..

JNBE or JA

Jump if Not (Below or Equal)Jump if Above

Jump if No Jump if Ex.

Cy = 0 AND Z= 0 Cy = 1 OR Z = 1 CMP BX, CXSurely Above Below OR Equal JA BXabove

BXabove (BX is above) is a symbolic location

Page 94: Instruction set of 8086

94

Jump on multiple flags contdJump on multiple flags contd.

JLE or JNG

Jump if Less than OR EqualJump if Not Greater than

Jump if No Jump if

S = 1 AND V = 0(surely negative)

OR (S = 0 AND V = 1)(wrong answer positive!)

OR Z = 1 (equal)

i.e. S XOR V = 1 OR Z = 1

S = 0 AND V = 0(surely positive)

OR (S = 1 AND V = 1)(wrong answer negative!)

AND Z = 0 (not equal)

i.e. S XOR V = 0 AND Z = 0

Page 95: Instruction set of 8086

95

Jump on multiple flags contd.Jump on multiple flags contd.JNLE or JG

Jump if Not (Less than OR Equal)Jump if Greater than

Jump if No Jump if

S = 0 AND V = 0(surely positive)

OR (S = 1 AND V = 1)(wrong answer negative!)

AND Z = 0 (not equal)

i.e. S XOR V = 0 AND Z = 0

S = 1 AND V = 0(surely negative)

OR (S = 0 AND V = 1)(wrong answer positive!)

OR Z = 1 (equal)

i.e. S XOR V = 1 OR Z = 1

Page 96: Instruction set of 8086

96

Jump on multiple flags Jump on multiple flags contd.contd.

JL or JNGE

Jump if Less thanJump if Not (Greater than OR Equal)

Jump if No Jump if

S = 1 AND V = 0(surely negative)

OR (S = 0 AND V = 1)(wrong answer positive!)

i.e. S XOR V = 1When S = 1, result cannot be 0

S = 0 AND V = 0(surely positive)

OR (S = 1 AND V = 1)(wrong answer negative!)

i.e. S XOR V = 0When S = 0, result can be 0

Page 97: Instruction set of 8086

97

Jump on multiple flags Jump on multiple flags contd.contd.

JNL or JGE

Jump if Not Less thanJump if Greater than OR Equal

Jump if No Jump if

S = 0 AND V = 0(surely positive)

OR (S = 1 AND V = 1)(wrong answer negative!)

i.e. S XOR V = 0When S = 0, result can be 0

S = 1 AND V = 0(surely negative)

OR (S = 0 AND V = 1)(wrong answer positive!)

i.e. S XOR V = 1When S = 1, result cannot be 0

Page 98: Instruction set of 8086

98

Near JumpNear Jump

Near Jump

Direct Jump(common)

Indirect Jump(uncommon)

Short Jump Long Jump2 or more bytes

Starting with FFHRange: complete

segment

2 bytes 3 bytes

EB r8 E9 r16

range + 27 range +215

3 Near Jump and 2 Far Jump instructions have the same mnemonic JMP but different opcodes

Page 99: Instruction set of 8086

99

Short JumpShort Jump

2 byte (EB r8) instruction Range: -128 to +127 bytes

Backward jump: Assembler knows the quantum of jump.Generates Short Jump code if <=128 bytes is the required jumpGenerates code for Long Jump if >128 bytes is the required jump

Forward jump: Assembler doesn’t know jump quantum in pass 1.Assembler reserves 3 bytes for the forward jump instruction.If jump distance turns out to be >128 bytes, the instruction is coded as E9 r16 (E9H = Long jump code).If jump distance becomes <=128 bytes, the instruction is coded as EB r8 followed by code for NOP (E8H = Short jump code).

Page 100: Instruction set of 8086

100

Short Jump contd.Short Jump contd.

SHORT Assembler DirectiveAssembler generates only 2 byte Short Jump code for forward jump, if the SHORT assembler directive is used.

JMP SHORT SAMEProgrammer should ensure that the Jump distance is <=127 bytes

::

SAME: MOV CX, DX

Page 101: Instruction set of 8086

101

Long JumpLong Jump

3-byte (E9 r16) instruction Range: -32768 to +32767 bytes

Long Jump can cover entire 64K bytes of Code segmentCS:0000H

Long Jump can handle it as jump quantum is <=32767

CS:8000H JMP FRWD::

FRWD = CS:FFFFH

Page 102: Instruction set of 8086

102

Long Jump contd.Long Jump contd.

It can cover entire 64K bytes of Code segment

Long Jump can handle it as jump quantum is <=32768

BKWD = CS:0000H

CS:8000H JMP BKWD::

CS:FFFFH

Page 103: Instruction set of 8086

103

Long Jump or Short Jump?Long Jump or Short Jump?

Can be treated as a small (20H) backward branch!

CS:0000H :

: Jump distance =FFE0H. Too

very long forward jump

CS:000DH JMP FRWD

CS:0010H :

:

FRWD= CS:FFF0H

CS:FFFFH

Page 104: Instruction set of 8086

104

Long Jump or Short Jump?Long Jump or Short Jump?

Can be treated as a small (20H) forward branch!

CS:0000H :

: Jump distance =FFE0H. Too

very long backward jump

BKWD= CS:0010H :

:

JMP BKWD

CS:FFF0H

CS:FFFFH

Page 105: Instruction set of 8086

105

Intra segment indirect JumpIntra segment indirect Jump

Near Indirect Jump is uncommon.Instruction length: 2 or more bytesRange: complete segment

Ex.1: JMP DX

If DX = 1234H, branches to CS:1234H1234H is not signed relative displacement

Ex. 2: JMP wordptr 2000H[BX]

BX 1234H DS:3234H 5678H Branches to

DS:3236H AB22H CS:5678H

Page 106: Instruction set of 8086

106

Far JumpFar Jump

Far Jump

Direct Jump(common)

Indirect Jump(uncommon)

5 bytes

2 or more bytesStarting with FFHRange: anywhere

EA,2 byte offset, 2 byte segment

Range: anywhere

3 Near Jump and 2 Far Jump instructions have the same mnemonic JMP but different opcodes

Page 107: Instruction set of 8086

107

Inter segment Direct JumpInter segment Direct Jump

Also called Far Direct JumpIt is the common inter segment jump scheme

It is a 5 byte instruction1 byte opcode (EAH)2 byte offset value2 byte segment value

Ex. JMP Far ptr LOC

Page 108: Instruction set of 8086

108

Inter segment Indirect JumpInter segment Indirect Jump

Instruction length depends on the way jump location is specifiedIt can be a minimum of 2 bytes

Ex. JMP DWORD PTR 2000H[BX]

Page 109: Instruction set of 8086

109

Inter segment Indirect JumpInter segment Indirect Jump

Also called Far Indirect JumpIt is not commonly usedInstruction length is a minimum of 2 bytes.It depends on the way jump location is specifiedEx. JMP DWORD PTR 2000H[BX]

BX 1234H Branches to

ABCDH:5678H

DS:3234H 5678H It is a 4-byte instruction

DS:3236H ABCDH

Page 110: Instruction set of 8086

110

String?

• An array of bytes or words located in memory

• Supported String Operations– Copy (move, load)– Search (scan)– Store– Compare

Page 111: Instruction set of 8086

111

String Instruction Basics

• Source DS:SI, Destination ES:DI

– You must ensure DS and ES are correct– You must ensure SI and DI are offsets into DS and ES

respectively

• Direction Flag (0 = Up, 1 = Down)

– CLD - Increment addresses (left to right)– STD - Decrement addresses (right to left)

Page 112: Instruction set of 8086

112

String InstructionsString InstructionsInstruction prefixes

Prefix Used with Meaning

REP

REPE/REPZ

REPNE/REPNZ

MOVSSTOS

CMPSSCAS

CMPSSCAS

Repeat while not end of stringCX ≠ 0

Repeat while not end of string and strings are equal. CX ≠ 0 and ZF = 1

Repeat while not end of string and strings are not equal. CX ≠ 0 and ZF = 0

Page 113: Instruction set of 8086

113

InstructionsInstructions

Mnemo-Nic

meaning format Operation Flags effect

-edMOVS Move string

DS:SI ES:DI

MOVSB/MOVSW

((ES)0+(DI)) ((DS)0+(SI))(SI) (SI) ± 1 or 2(DI) (DI) ± 1 or 2

none

CMPS Compare stringDS:SI ES:DI

CMPSB/CMPSW

Set flags as per((DS)0+(SI)) - ((ES)0+(DI))(SI) (SI) ± 1 or 2(DI) (DI) ± 1 or 2

All status flags

Page 114: Instruction set of 8086

114

Mnemo-Nic

meaning format Operation

SCAS Scan stringAX – ES:DI

SCASB/SCASW

Set flags as per(AL or AX) - ((ES)0+(DI))(DI) (DI) ± 1 or 2

LODS Load stringDS:SI AX

LODSB/ LODSW

(AL or AX) ((DS)0+(SI))(SI) (SI) ± 1 or 2

STOS Store stringES:DI AX

STOSB/ STOSW

((ES)0+(DI)) (AL or AX) ± 1 or 2(DI) (DI) ± 1 or 2

Page 115: Instruction set of 8086

115

Machine control instructionsMachine control instructionsHLT instruction – HALT processing

the HLT instruction will cause the 8086 to stop fetching and executing instructions. The 8086 will enter a halt state. The only way to get the processor out of the halt state are with an interrupt signal on the INTR pin or an interrupt signal on NMI pin or a reset signal on the RESET input.

NOP instructionthis instruction simply takes up three clock cycles and does no

processing. After this, it will execute the next instruction. This instruction is normally used to provide delays in between instructions.

ESC instructionwhenever this instruction executes, the microprocessor does NOP or

access a data from memory for coprocessor. This instruction passes the information to 8087 math processor. Six bits of ESC instruction provide the opcode to coprocessor.

when 8086 fetches instruction bytes, co-processor also picks up these bytes and puts in its queue. The co-processor will treat normal 8086 instructions as NOP. Floating point instructions are executed by 8087 and during this 8086 will be in WAIT.

Page 116: Instruction set of 8086

116

Machine control instructions Machine control instructions contdcontd

LOCK instructionthis is a prefix to an instruction. This prefix makes sure that during

execution of the instruction, control of system bus is not taken by other microprocessor.

in multiprocessor systems, individual microprocessors are connected together by a system bus. This is to share the common resources. Each processor will take control of this bus only when it needs to use common resource.

the lock prefix will ensure that in the middle of an instruction, system bus is not taken by other processors. This is achieved by hardware signal ‘LOCK’ available on one of the CPU pin. This signal will be made active during this instruction and it is used by the bus control logic to prevent others from taking the bus.

once this instruction is completed, lock signal becomes inactive and microprocessors can take the system bus.

WAIT instructionthis instruction takes 8086 to an idle condition. The CPU will not do

any processing during this. It will continue to be in idle state until TEST pin of 8086 becomes low or an interrupt signal is received on INTR or NMI. On valid interrupt, ISR is executed and processor enters the idle state again.

Page 117: Instruction set of 8086

117

Flag Manipulation instructionsMNEM-ONIC

MEANING OPERATION Flags Affected

CLC Clear Carry Flag (CF) 0 CF

STC Set Carry Flag (CF) 1 CF

CMC Complement Carry Flag

(CF) (CF)l CF

CLD Clear Direction Flag

(DF) 0SI & DI will be auto incremented while string instructions are executed.

DF

STD Set Direction Flag

(DF) 1SI & DI will be auto decremented while string instructions are executed.

DF

CLI Clear Interrupt Flag

(IF) 0 IF

STI Set Interrupt Flag

(IF) 1 IF