8086 Instruction Set

94

Transcript of 8086 Instruction Set

Page 1: 8086 Instruction Set
Page 2: 8086 Instruction Set
Page 3: 8086 Instruction Set
Page 4: 8086 Instruction Set
Page 5: 8086 Instruction Set
Page 6: 8086 Instruction Set
Page 7: 8086 Instruction Set
Page 8: 8086 Instruction Set
Page 9: 8086 Instruction Set

MOV destination, sourceExamples:MOV BX,1234HMOV CL,[1000H]MOV AX,BX

Page 10: 8086 Instruction Set

PUSH sourceDecrements SP by 2 & copies a word from

source in stack.Source must a word (16-bit).Eg:

PUSH CXPUSH DS

Page 11: 8086 Instruction Set

POP destinationCopies a word from stack to destinationSP is incremented by 2.Eg:

POP CXPOP DS

Page 12: 8086 Instruction Set

XCHG destination, sourceExamples:XCHG BX,CXXCHG AL,CL

Page 13: 8086 Instruction Set

XLAT : Translate byte in ALReplaces a byte in AL register with a byte

from a lookup table in memory.AL <- DS : [BX + AL]

Page 14: 8086 Instruction Set

LEA : Load Effective AddressLEA register, sourceDetermines the offset of the source & loads

this address in the specified register.Eg:

LEA CX, TOTALLEA AX, [BX] [DI]

Page 15: 8086 Instruction Set

LDS, LES Loads DS/ES & the specified destination

register in the instruction with the contents of memory location specified as source in the instruction.

Eg: LDS BX, 5000H / LES BX,5000H

XX

YY

MM

NN

YY XX

NN MM

BX

DS/ES

5000

5001

5002

5003

Page 16: 8086 Instruction Set

LAHF: Load lower byte of flag register in AHCopies the contents of lower byte of 8086 flag

register to AH register.

SAHF: Copy AH register to lower byte of flag register

Page 17: 8086 Instruction Set

PUSHF : Decrements the SP by 2 & copies the word in

the flag register to the memory location pointed by the SP

POPF:• Copies a word from the two memory locations at the top of stack to the flag register & increments SP by 2.

Page 18: 8086 Instruction Set

IN: Input a byte or word from portCopies data from a port to the accumulator.If 8-bit port is read, the data will go to AL.IN AL,F8HIN AX,95HIn indirect addressing, the address of the port

is referred from DX register.MOV DX,30F8HIN AL,DXIN AX,DX

Page 19: 8086 Instruction Set

OUT: Send a byte or word to portCopies data from a AL or AX to a port.OUT F8H,ALOUT FBH,AXIn indirect addressing, the address of the port

is referred from DX register.MOV DX,30F8HOUT DX, ALOUT DX, AX

Page 20: 8086 Instruction Set
Page 21: 8086 Instruction Set

CBW (Convert Byte to Word)Copies the sign of a byte in AL to all the bits

in AH.AH is said to be sign extension of AL.AX=0000 0000 1001 1000 = -152 decimalCBWAX=1111 1111 1001 1000 = -152 decimal

Page 22: 8086 Instruction Set

CWD (Convert Word to Double)Copies the sign of a word in AX to all the bits of

DX.DX is said to be sign extension of AX.DX=0000 0000 0000 0000AX=1111 0000 1100 0001 = -28865 decimalCWDDX=1111 1111 1111 1111AX=1111 0000 1100 0001 = -28865 decimal

DX:AX together can be used for 32-bit operand

Page 23: 8086 Instruction Set

ADD/ADC destination, sourceADD AL,30HADC DL,BLADD CX, [2048]

Page 24: 8086 Instruction Set

INC destinationAdds 1 to specified destination.INC BXINC CL

Page 25: 8086 Instruction Set

AAA: ASCII Adjust for AdditionThe numbers from 0-9 are represented as

30H-39H in ASCII code.For addition of two decimal numbers

represented in ASCII code, it is necessary to mask upper nibble(3) from code before addition.

8086 allows you to add ASCII codes for two decimals without masking off “3”.

AAA is used after addition to get the current result in unpacked BCD form.

Page 26: 8086 Instruction Set

AAA: ASCII Adjust for AdditionAL = 0011 0100 ASCII 4CL = 0011 1000 ASCII 8ADD AL,CL ; AL = 0110 1100AAA ; AL = 0000 0010 unpacked BCD for 2

; carry = 1 to indicate answer=12 decimal

The AAA works only on the AL register.

Page 27: 8086 Instruction Set

DAA : Decimal Adjust AccumulatorThis instruction is used to make sure the

result of addition of two packed BCD numbers is adjusted to be a legal BCD number.

If the value of D3-D0 in the AL > 9 , or AF is set, the instruction adds 06 to D3-D0.

If the value of D7-D4 in the AL > 9 , or CF is set, the instruction adds 06 to D7-D4.

Page 28: 8086 Instruction Set

DAA : Decimal Adjust AccumulatorAL = 0011 1001 BCD 39CL = 0001 0010 BCD 12ADD AL,CL ; AL = 0100 1011 = 4BHDAA ; add 0110 because 1011 > 9 ; AL = 0101 0001 =51 BCD

The DAA works only on the AL register.

Page 29: 8086 Instruction Set

SUB/SBB destination, sourceSUB AL,30HSBB DL,BLSUB CX, [2046]

Page 30: 8086 Instruction Set

DEC destinationSubtracts 1 to specified destination.DEC BXDEC CL

Page 31: 8086 Instruction Set

NEG: Forms 2’s complementReplaces the number in a destination with

the 2’s compliment of that number.NEG AL

Page 32: 8086 Instruction Set

CMP destination, sourceCompares a byte/word from source with a

byte/word from destination.The source & the comparison is done by

subtracting source from destination. The result is not stored in destination.

Source & destination remain unchanged, only flags are updated.

CMP AL, 04HCMP CX,BX

Page 33: 8086 Instruction Set

AAS : ASCII Adjust after SubtractionThe numbers from 0-9 are represented as 30H-

39H in ASCII code.For subtraction of two decimal numbers

represented in ASCII code, it is necessary to mask upper nibble(3) from code before subtraction.

8086 allows you to subtract ASCII codes for two decimals without masking off “3”.

AAS is used after subtraction to get the current result in unpacked BCD form.

Page 34: 8086 Instruction Set

AAS : ASCII Adjust after SubtractionAL = 0011 0010 ASCII 2CL = 0011 1000 ASCII 8SUB AL,CL ; AL = 1111 1010 = FAH CF=1AAS ; AL =0000 0110 =BCD 6

; carry = 1 to indicate -6

Page 35: 8086 Instruction Set

DAS : Decimal Adjust After SubtractionThis instruction is used to make sure the

result of subtraction of two packed BCD numbers is adjusted to be a legal BCD number.

If the value of D3-D0 in the AL > 9 , or AF is set, the instruction subtracts 06 to D3-D0.

If the value of D7-D4 in the AL > 9 , or CF is set, the instruction subtracts 06 to D7-D4.

Page 36: 8086 Instruction Set

DAS : Decimal Adjust After SubtractionAL = 0011 0010 BCD 32CL = 0001 0111 BCD 17SUB AL,CL ; AL = 0001 1011 = 1BHDAS ; subtract 0110 because 1011 > 9 ; AL = 0001 0101 =15 BCD

The DAS works only on the AL register.

Page 37: 8086 Instruction Set

MUL sourceUnsigned multiplicationWhen byte is multiplied by AL, the result is

stored in AX.When word is multiplied by AX, the MSW of

result is stored in DX & LSW of result is stored in AX.

MUL BLMUL BX

Page 38: 8086 Instruction Set

IMUL sourceSigned multiplicationWhen byte is multiplied by AL, the result is stored

in AX.When word is multiplied by AX, the MSW of result

is stored in DX & LSW of result is stored in AX.To multiply a signed byte by a signed word, it is

necessary to move the byte into a word location & fill the upper byte of the word with copies of the sign bit.

IMUL BLIMUL BX

Page 39: 8086 Instruction Set

AAM: BCD adjust after multiplyAfter the two unpacked BCD digits are

multiplied, the AAM is used to adjust the product to unpacked BCD.

AL= 0000 0100 = unpacked BCD 4CL= 0000 0110 = unpacked BCD 6MUL CL ; AX=0000 0000 0001 1000 = 0018HAAM ; AX=0000 0010 0000 0100 = 0204

H

Page 40: 8086 Instruction Set

DIV sourceDivide unsigned word by byte or unsigned double

word by word.When dividing word by byte, word must be in AX.

After division AL=Quotient & AH= Remainder.When dividing double word by word, the MSW of

double word must be in DX & LSW must be in AX. After division AX=Quotient & DX= Remainder.

If an attempt is made to divide by 0 or the quotient is too large to fit in AL/AX register, 8086 will execute a type 0 interrupt.

DIV CLDIV CX

Page 41: 8086 Instruction Set

IDIV sourceDivide a signed word by a signed byte or a

signed double word by a signed word.

Page 42: 8086 Instruction Set

AAD: Binary Adjust before divisionConverts two unpacked BCD digits in AH & AL

to the equivalent binary number in AL.This adjustment must be made before

dividing the two unpacked BCD digits in AX by an unpacked BCD byte.

After division AL=unpacked BCD quotient, AH=unpacked BCD remainder.

Page 43: 8086 Instruction Set

AAD: Binary Adjust before divisionAX=0403 = unpacked BCD for 43 decimalCL=07HAAD ; adjust to binary before division

AX=002BHDIV CL ; AL=quotient= 06 unpacked BCD

; AH= remainder= 01 unpacked BCD

Page 44: 8086 Instruction Set

Bit Manipulation Instructions(Logical Instructions)

Page 45: 8086 Instruction Set

NOT destinationInverts each bit of a byte or a word.AL= 0110 1100NOT AL ; AL=1001 0011

Page 46: 8086 Instruction Set

AND destination, sourceLogically ANDs each bit of the source byte or

word with the corresponding bit in the destination & stores result in the destination.

AL=1001 0011 =93HBL=0111 0101 = 75HAND BL,AL ; BL= 0001 0001 =11H

Page 47: 8086 Instruction Set

OR destination, sourceLogically ORs each bit of the source byte or

word with the corresponding bit in the destination & stores result in the destination.

AL=1001 0011 =93HBL=0111 0101 = 75HOR BL,AL ; BL= 1111 0111 =F7H

Page 48: 8086 Instruction Set

XOR destination, sourceLogically XORs each bit of the source byte or

word with the corresponding bit in the destination & stores result in the destination.

XOR BL,AL

Page 49: 8086 Instruction Set

TEST destination, sourceLogically ANDs contents of the source byte or

word with the contents of the destination.Neither operand is changed. Flags are

affected.PF, SF, ZF will be updated to show the result

of the ANDing.TEST AL,CLTEST AX,CX

Page 50: 8086 Instruction Set
Page 51: 8086 Instruction Set

SAL/SHL destination, countShifts each bit in the specified destination to the left & 0

is stored at LSB position.MSB to carry flag.Number of shifts are indicated by count.SAL AX,1

11101101

01110110

0

CY

1

0

Page 52: 8086 Instruction Set

SHR destination, countShifts each bit in the specified destination to the right &

0 is stored at MSB position.LSB to carry flag.Number of shifts are indicated by count.SHR AX,1

11101101

11011010

0

CY

1

0

Page 53: 8086 Instruction Set

SAR destination, countShifts each bit in the specified destination to the right.Copy of old MSB is put in the MSB position.LSB to carry flag.Number of shifts are indicated by count.SAR AX,1

11101101

11011011

CY

1

0

Page 54: 8086 Instruction Set
Page 55: 8086 Instruction Set

ROL destination, countRotates each bit in the specified destination to the left.MSB to new LSB & a new carry flag.Number of shifts are indicated by count.ROL AX,1

CY

11101101

11110110

0

1

Page 56: 8086 Instruction Set

ROR destination, countRotates each bit in the specified destination to the right.LSB to a new MSB & a new Carry.Number of shifts are indicated by count.ROR AX,1

11101101

11011011

CY

1

0

Page 57: 8086 Instruction Set

RCL destination, countRotates each bit in the specified destination to the left

along with carry.MSB to new carry & carry to new LSB.Number of shifts are indicated by count.RCL AX,CL

CY

11101101

01110110

0

1

Page 58: 8086 Instruction Set

RCR destination, countRotates each bit in the specified destination to the right

along with carry flag.LSB to a new carry & carry to a new MSB.Number of shifts are indicated by count.RCR AX,1

11101101

11011010

CY

1

0

Page 59: 8086 Instruction Set
Page 60: 8086 Instruction Set

STC: Sets the carry flag

CLC: Resets the carry flag to zero

CMC: Complements the carry flag

Page 61: 8086 Instruction Set

STDThis is used to set the direction flag to one so

that SI/DI can be decremented automatically after execution of string instructions.

Page 62: 8086 Instruction Set

CLDThis is used to reset the direction flag to zero

so that SI/DI can be incremented automatically after execution of string instructions.

Page 63: 8086 Instruction Set

STI: Sets the interrupt flag to one. This enables INTR interrupt of 8086.

CLI:Resets the interrupt flag to zero.Due to this 8086 will not respond to an interrupt signal on its INTR input.

Page 64: 8086 Instruction Set
Page 65: 8086 Instruction Set

Iteration control instructionsUsed to execute a series of instructions some

number of times.The number is specified in CX.CX is automatically decremented by one,

each time after execute of LOOP instruction.Until CX=0, execution will jump to a

destination specified by a label in the instruction.

Page 66: 8086 Instruction Set

Instruction Code Description Condition for Exit

1. LOOP Loop through a sequence of instructions

CX=0

2. LOOPE/LOOPZ Loop through a sequence of instructions

CX=0 or ZF=0

3. LOOPNE/LOOPNZ Loop through a sequence of instructions

CX=0 or ZF=1

Page 67: 8086 Instruction Set
Page 68: 8086 Instruction Set

Unconditional transfer instructionsCALLRETJMP

Page 69: 8086 Instruction Set

CALLUsed to transfer execution to a subprogram or

procedure.Two types: near & farA near CALL is a call to a procedure which is in

the same code segment as the CALL instruction.When 8086 executes a near CALL, it decrements

the SP by 2 & copies the offset of the next instruction after the CALL on the stack.

IP is loaded with the offset of the first instruction of the procedure.

The near CALL is known as intra segment CALL.

Page 70: 8086 Instruction Set

CALLA far CALL is a call to a procedure which is in

a different segment from that which contains the CALL instruction.

When 8086 executes a far CALL, it decrements the SP by 2 & copies the contents of the CS register to the stack.

It then decrements the SP by 2 again & copies the offset of the next instruction after the CALL on the stack.

Finally, it loads CS with the segment base of the segment which contains the procedure & IP is loaded with the offset of the first instruction of the procedure.

The far CALL is known as inter segment CALL.

Page 71: 8086 Instruction Set

CALLCALL PROCALL DWORD PTR[BX]

Page 72: 8086 Instruction Set

RETIt will return execution from a procedure to

the next instruction after the CALL instruction in the calling program.

Page 73: 8086 Instruction Set

JMPIt will cause 8086 to fetch its next instruction

from the location specified in the instruction.Two types: near & far.A near JMP is a jump where destination

location is in the same code segment.A near JMP is known as intra segment JMP.A far JMP is a jump where destination

location is from different segment.A far JMP is known as inter segment JMP.JMP NEXTJMP WORD PTR [BX]

Page 74: 8086 Instruction Set

J cond – Conditional Transfer InstructionsThese instructions will cause a jump to a

label given in the instruction if the desired condition occurs in the program before execution of the instruction.

If the jump is not taken, execution simply goes on to the next instruction.

Page 75: 8086 Instruction Set
Page 76: 8086 Instruction Set
Page 77: 8086 Instruction Set
Page 78: 8086 Instruction Set

Interrupt InstructionsInterrupt Instructions

Page 79: 8086 Instruction Set

INT typeType refers to a number between 0 – 255

which identifies the interrupt.This instruction causes the 8086 to call a far

procedure.The address of the procedure is taken from

the memory whose address is four times the type number.

Page 80: 8086 Instruction Set

INTOIf the overflow flag is set, this instruction will

cause the 8086 to do an indirect far call to a procedure you write to handle overflow condition.

Page 81: 8086 Instruction Set

IRETIt is used at the end of the interrupt service

routine to return execution to the interrupted program.

Page 82: 8086 Instruction Set
Page 83: 8086 Instruction Set

REP/REPE/REPZ/REPNE/REPNZREP is a prefix which is written before one of

the string instructionsThese instructions repeat until specified

condition exists.REPZ CMPSB ; compare string bytes until

CX=0.Instruction Code Condition for Exit

REP CX=0

REPE/REPZ CX=0 OR ZF=0

REPNE/REPNZ CX=0 OR ZF=1

Page 84: 8086 Instruction Set

MOVS/MOVSB/MOVSWIt copies a byte or word from a location in the data segment

to a location in the extra segment.The offset of the source byte or word in the DS must be in the

SI register.The offset of the source byte or word in the ES must be in the

DI register.Count in CX register.After byte or word is moved, SI & DI are automatically

adjusted to point to the next source & destination.If DF=0, then SI & DI are incremented; and if DF=1, SI & DI are

decremented.

Page 85: 8086 Instruction Set

CMPS/CMPSB/CMPSWThis instruction is used to compare a byte in one

string with a byte in another string.SI is used to hold the offset of a byte or word in

the source string & DI is used to hold the offset of a byte or word in the another string.

Comparison is done by subtracting the byte or word pointed to by DI from the byte or word pointed to by SI.

If both the byte or word strings are equal, zero flag is set.

AF,CF,OF,PF,SF,ZF are affected.

Page 86: 8086 Instruction Set

SCAS/SCASB/SCASWCompares a string byte with a byte in AL or a

string word with word in AX .This instruction affects the flags. Neither

operand is changed.The string to be scanned must be in extra

segment & DI must contain the offset of a byte or word to be compared .

After comparison DI will be automatically incremented or decremented according to DF to point to the next element in the two strings .

Whenever a match to the specified operand is found in the string, execution stops & ZF is set.

AF,CF,OF,PF,SF,ZF are affected.

Page 87: 8086 Instruction Set

LODS/LODSB/LODSWIt copies a byte from a string location pointed

to by SI to AL, or a word from a string location pointed to by SI to AX.

It does not affect any flag.

Page 88: 8086 Instruction Set

STOS/STOSB/STOSWIt copies a byte from AL or a word from AX to

a memory location in the extra segment.DI is used to hold the offset of the memory

location in ES.After the copy DI will be automatically

incremented or decremented according to DF to point to the next element in the memory .

It does not affect any flag.

Page 89: 8086 Instruction Set

External Hardware Synchronization Instructions

Page 90: 8086 Instruction Set

HLTIt will cause the 8086 to stop fetching &

executing instructions.The 8086 will enter a halt state.The only ways to get the processor out of the

halt state are with an interrupt signal on the INTR pin, an interrupt signal on the NMI pin, or a reset signal on the RESET input.

Page 91: 8086 Instruction Set

WAITWhen this instruction executes, the 8086 enters

an idle condition where it is doing no processing.The 8086 will stay in this idle state until a signal

is asserted on the 8086 TEST input pin, or until a valid interrupt is received on the INTR or NMI pins.

If interrupt occurs while the 8086 is in this idle state, the 8086 will return to the idle state after the execution of the ISR.

WAIT is used to synchronize the 8086 with external hardware such as 8087.

Page 92: 8086 Instruction Set

ESCIt is used to pass instructions to a

coprocessor such as 8087, which shares the address & data bus with 8086.

Instructions for the coprocessor are represented by a 6-bit code embedded in the escape instruction.

In most cases, 8086 treats the ESC as NOP.

Page 93: 8086 Instruction Set

LOCKMultiprocessor systemLOCK prefix allows a microprocessor to make

sure that another processor does not take control of the system bus while it is in the middle of a critical instruction which uses the system bus.

Page 94: 8086 Instruction Set

NOPNo OperationSimply uses three clock cyclesAffects no flagsUsed to increase delay of a loopCan be used to hold a place in a program for

an instruction that will be added later.