5th 11th Lecture Instruction Set of 8086
5th 11th Lecture Instruction Set of 8086
⚫POP Des:
⚫It pops the operand from top of stack to Des.
⚫Des can be a general purpose register,
segment register (except CS) or memory
location.
⚫E.g.: POP AX
Data Transfer Instructions
Data Transfer Instructions
Note :- MOV, PUSH, POP are the only
instructions that use the Segment Registers as
operands except CS.
Data Transfer Instructions
⚫XCHG Des, Src:
⚫This instruction exchanges Src with Des.
⚫It cannot exchange two memory locations
directly.
⚫E.g.: XCHG DX, AX
XCHG BL,CH
Data Transfer Instructions
⚫ IN Accumulator, Port Address:
⚫ SAHF:
⚫ It copies the contents of AH to lower byte of flag register.
⚫ PUSHF:
⚫ Pushes flag register to top of stack.
⚫ POPF:
⚫ Pops the stack top to flag register.
XLAT
⚫Moves into AL, Content of mem.
Loc. In DS.
⚫ whose EA is formed by sum of
BX and AL.
Instruction Set of 8086
Dr. Manju Khurana
Assistant Professor,
CSED
TIET, Patiala
manju.khurana@thap
ar.edu
Arithmetic Instructions
⚫ADD Des, Src:
⚫It adds a byte to byte or a word to word.
⚫It effects AF, CF, OF, PF, SF, ZF flags.
Arithmetic Instructions
⚫ADC Des, Src:
⚫It adds the two operands with CF.
⚫It effects AF, CF, OF, PF, SF, ZF flags.
⚫E.g.:
⚫ADC AL, 74H
⚫ADC DX, AX
⚫ADC AX, [BX]
Arithmetic Instructions
⚫SUB Des, Src:
⚫It subtracts a byte from byte or a word
from word.
⚫It effects AF, CF, OF, PF, SF, ZF flags.
⚫For subtraction, CF acts as borrow flag.
⚫E.g.:
⚫ SUB AL, 74H
⚫ SUB DX, AX
⚫ SUB AX, [BX]
Arithmetic Instructions
⚫SBB Des, Src:
⚫It subtracts the two operands and also
the borrow from the result.
⚫It effects AF, CF, OF, PF, SF, ZF flags.
⚫E.g.:
⚫SBB AL, 74H
⚫SBB DX, AX
⚫SBB AX, [BX]
Arithmetic Instructions
⚫INC Src:
⚫It increments the byte or word by one.
⚫The operand can be a register or
memory location.
⚫It effects AF, OF, PF, SF, ZF flags.
⚫CF is not effected.
Arithmetic Instructions
⚫DEC Src:
⚫It decrements the byte or word by one.
⚫The operand can be a register or
memory location.
⚫It effects AF, OF, PF, SF, ZF flags.
⚫CF is not effected.
⚫E.g.: DEC AX
Arithmetic Instructions
⚫ MUL Src:
⚫ It is an unsigned multiplication instruction.
⚫ It multiplies two bytes to produce a word or two
words to produce a double word.
⚫ AX = AL * Src
⚫ DX : AX = AX * Src
⚫ This instruction assumes one of the operand in AL
or AX.
⚫ Src can be a register or memory location.
⚫ IMUL Src:
⚫ It is a signed multiplication instruction.
Arithmetic Instructions
⚫ MUL – MUL Source
⚫ This instruction multiplies an unsigned byte in
some source with an unsigned byte in AL register
or an unsigned word in some source with an
unsigned word in AX register.
⚫ The source can be a register or a memory
location. When a byte is multiplied by the
content of AL, the result (product) is put in
AX. When a word is multiplied by the content
of AX, the result is put in DX and AX
registers.
⚫ If the most significant byte of a 16-bit result
or the most significant word of a 32-bit
result is 0, CF and OF will both be 0’s. AF, PF,
SF and ZF are undefined after a MUL instruction.
Arithmetic Instructions
⚫ MUL – MUL Source
⚫ If you want to multiply a byte with a word, you
must first move the byte to a word location such
as an extended register and fill the upper byte
of the word with all 0’s.
⚫ You cannot use the CBW instruction for this,
because the CBW instruction fills the upper byte
with copies of the most significant bit of the
lower byte.
Arithmetic Instructions
⚫ IMUL – IMUL Source
⚫ multiplies a signed byte from source with a
signed byte in AL or a signed word from some
source with a signed word in AX.
⚫ If the magnitude of the product does not require
all the bits of the destination, the unused byte /
word will be filled with copies of the sign bit.
⚫ If you move the byte into AL, you can use the
CBW instruction to do this.
Arithmetic Instructions
⚫DIV Src:
⚫ It is an unsigned division instruction.
⚫ To divide an unsigned word by a byte or to divide an
unsigned double word (32 bits) by a word. The operand
is stored in AX, divisor is Src and the result is stored as:
⚫ AH = remainder AL = quotient
Arithmetic Instructions
⚫DIV – DIV Source
⚫When a word is divided by a byte, the
word must be in the AX register. The
divisor can be in a register or a memory
location.
⚫When a double word is divided by a word,
the most significant word of the double
word must be in DX, and the least
significant word of the double word must
be in AX. After the division, AX will
contain the 16-bit quotient and DX will
contain the 16-bit remainder.
Arithmetic Instructions
⚫DIV – DIV Source
⚫If an attempt is made to divide by 0 or if
the quotient is too large to fit in the
destination (greater than FFH / FFFFH),
the 8086 will generate a type 0 interrupt.
All flags are undefined after a DIV
instruction.
Arithmetic Instructions
⚫DIV – DIV Source
⚫If you want to divide a byte by a byte, you
must first put the dividend byte in AL and
fill AH with all 0’s.
⚫Likewise, if you want to divide a word by
another word, then put the dividend word
in AX and fill DX with all 0’s.
Arithmetic Instructions
⚫IDIV – IDIV Source
⚫This instruction is used to divide a signed
word by a signed byte, or to divide a
signed double word by a signed word.
⚫When dividing a signed word by a signed
byte, the word must be in the AX
register.
Arithmetic Instructions
⚫IDIV – IDIV Source
⚫When dividing a signed word by a
signed byte, the word must be in the AX
register. The divisor can be in an 8-bit
register or a memory location.
⚫After the division, AL will contain the
signed quotient, and AH will contain
the signed remainder.
⚫The sign of the remainder will be the
same as the sign of the dividend. If an
attempt is made to divide by 0, the
quotient is greater than 127 (7FH) or less
than –127 (81H), the 8086 will
Arithmetic Instructions
⚫IDIV – IDIV Source
⚫If you want to divide a signed byte by a
signed byte, you must first put the
dividend byte in AL and sign-extend AL
into AH.
⚫The CBW instruction can be used for
this purpose.
⚫Likewise, if you want to divide a signed
word by a signed word, you must put the
dividend word in AX and extend the sign
of AX to all the bits of DX.
⚫The CWD instruction can be used for
this purpose.
Arithmetic Instructions
⚫IDIV – IDIV Source
⚫When dividing a signed double word by
a signed word, the most significant
word of the dividend (numerator) must be
in the DX register, and the least
significant word of the dividend must be
in the AX register.
Arithmetic Instructions
⚫IDIV – IDIV Source
⚫The divisor can be in any other 16-bit
register or memory location.
⚫After the division, AX will contain a
signed 16-bit quotient, and DX will
contain a signed 16-bit remainder. The
sign of the remainder will be the same as
the sign of the dividend.
⚫Again, if an attempt is made to divide by
0, the quotient is greater than +32,767
(7FFFH) or less than –32,767 (8001H), the
8086 will automatically generate a type 0
interrupt.
Arithmetic Instructions
⚫NEG Src:
⚫It creates 2’s complement of a given number.
⚫That means, it changes the sign of a number.
Arithmetic Instructions
⚫CMP Des, Src:
⚫ It compares two specified bytes or words.
⚫ The Src and Des can be a constant, register or memory location.
⚫ Both operands cannot be a memory location at the same time.
⚫ The comparison is done simply by internally subtracting the source
from destination.
⚫ The value of source and destination does not change, but the flags are
modified to indicate the result.
Arithmetic Instructions
⚫CBW (Convert Byte to Word):
⚫This instruction converts byte in AL to word in
AX.
⚫The conversion is done by extending the sign
bit of AL throughout AH.
⚫CWD (Convert Word to Double Word):
⚫This instruction converts word in AX to double
word in DX : AX.
⚫The conversion is done by extending the sign
bit of AX throughout DX.
CBW
CWD
Instruction Set of 8086
Dr. Manju Khurana
Assistant Professor,
CSED
TIET, Patiala
manju.khurana@thap
ar.edu
DAA (Decimal Adjust for Addition)
• The daa instruction works as follows:
* If the least significant four bits in AL are > 9 or if AF
=1, it adds 06H to AL and sets AF.
* If the most significant four bits in AL are > 9 or if CF
=1, it adds 60H to AL and sets CF.
Example:
mov AL,71H
; AL := B4H
add AL,43H ; AL := 14H and CF := 1
* The result including the carry (i.e., 114H) is the
daa
correct answer
DAS (Decimal Adjust for Subtraction)
⚫Packed BCD subtraction
• The das instruction works as follows:
* If the least significant four bits in AL are > 9 or if AF =1, it
subtracts 6 from AL and sets AF.
* If the most significant four bits in AL are > 9 or if CF =1, it
subtracts 60H from AL and sets CF.
⚫Example:
mov AL,71H
; AL := 2EH
sub AL,43H ; AL := 28H
das
Representation of Numbers
• ASCII representation
* Numbers are stored as a string of ASCII characters
» Example: 1234 is stored as 31 32 33 34H
>ASCII for 1 is 31H, for 2 is 32H, etc.
• BCD representation
* Unpacked BCD
» Example: 1234 is stored as 01 02 03 04H
– Additional byte is used for sign
>Sign byte: 00H for + and 80H for −
* Packed BCD
» Saves space by packing two digits into a byte
– Example: 1234 is stored as 12 34H
AAA (ASCII Adjust After Addition)
• If lower nibble of AL > 9 or AF = 1,then AL<- AL+6.
• AH <- AH+1 Set AF - 1, CY- 1.
• else
• AF and CY <- Reset i.e. 0.
ASCII subtraction
• If lower nibble of AL > 9 or AF = 1,then AL<- AL-6.
• AH <- AH-1 Set AF - 1, CY- 1.
• else
• AF and CY <- Reset i.e. 0.
• In both cases, Clear the higher nibble of AL.
• eg.
• MOV AX,020FH ;
• AAS;
• HLT
AAS (ASCII Adjust After Subtraction) contd.
ASCII subtraction
• If lower nibble of AL > 9 or AF = 1,then AL<- AL-6.
• AH <- AH-1 Set AF - 1, CY- 1.
• else
• AF and CY <- Reset i.e. 0.
• In both cases, Clear the higher nibble of AL.
• eg.
• MOV AX,020FH ; AX<- 020F, AH<- 02, AL <- 0F
• AAS; AL<- F(15) - 6 = 09 , AH <- AH-1, ->01
AF and CY <- 1. 0109 ANS
• HLT
AAS (ASCII Adjust After Subtraction) contd.
ASCII multiplication
• eg.
• MOV AL,5H
• MOV BL,7H
• MUL BL
• AAM
• HLT
AAM (ASCII Adjust After Multiplication) contd.
ASCII multiplication
• eg.
• MOV AL,5H
• MOV BL,7H
• MUL BL 5*7 = 23H (35)-----(35/16 = 2(Q), 3(R))
Result will go in AX(16 bit)
• AAM 35/10 = 3(Q), 5(R) , AH<- 03 AL <- 05
• HLT
AAM (ASCII Adjust After Multiplication) contd.
• Example 1
mov AL,3 ;
mov BL,9 ;
mul BL ;
aam ;
or AX,3030H ;
• Example 2
mov AL,'3' ;
mov BL,'9' ;
and AL,0FH ;
and BL,0FH ;
mul BL ;
aam ;
AL,30H ;
or
AAM (ASCII Adjust After Multiplication) contd.
• Example 1
mov AL,3 ; multiplier in unpacked BCD form
mov BL,9 ; multiplicand in unpacked BCD form
mul BL ; result 001BH is in AX
aam ; AX := 0207H
or AX,3030H ; AX := 3237H
• Example 2
mov AL,'3' ; multiplier in ASCII
mov BL,'9' ; multiplicand in ASCII
and AL,0FH ; multiplier in unpacked BCD form
and BL,0FH ; multiplicand in unpacked BCD form
mul BL ; result 001BH is in AX
aam ; AX := 0207H
AL,30H ; AL := 37H
or
AAD (ASCII Adjust Before Division)
• The aad instruction works as follows
* Multiplies AH by 10 and adds it to AL and sets AH to 0
* Example:
» If AX is 0207H before AAD
» AX is changed to 001BH after AAD
• AAD instruction reverses the changes done by AAM
AAD (ASCII Adjust Before Division) contd.
ASCII division
• MOV AX,0205H ; AX<- 0205, AH<- 02, AL <- 05
• Example: Divide 27 by 5
mov AX,0207H ; dividend in unpacked BCD form
mov BL,05H ; divisor in unpacked BCD form
aad ; AX := 001BH
div BL ; AX := 0205H
• AL <- 05 AH <- 02
⚫INC DL
⚫JMP L3
⚫L2: INC BL
⚫L3: INC SI
WAP to find out the count of positive numbers
and negative numbers from a series of signed
numbers in 8086. 1000-01
1001-02
⚫DEC CL 1002-03
⚫JNZ L1 1003-04
1004-80
⚫MOV [100AH],BL 1005-81
1006-82
⚫MOV [100BH],DL 1007-83
1008-84
⚫HLT 1009-85
100A-04 O/P
1020-06 O/P
Fibonacci Series:-
⚫MOV SI,3000H ; SI=3000
⚫MOV CX,0AH ; CX=00 0A
⚫XOR AL,AL ; AL=00
⚫MOV [SI],0AH ; [3000]=0A
⚫INC SI ;3001
⚫MOV [SI],00H ;[3001]=00
⚫ADD AL,01H ; AL=01
⚫INC SI ; 3002
⚫MOV [SI],AL ; 3002= 01
⚫INC SI ; 3003
⚫MOV [SI],AL ;3003= 01
⚫Back: ADD AL,[SI] ; 01,01 =02
⚫INC SI ; 3004
⚫MOV [SI],AL ; 3004=02
⚫DEC SI ; 3003
⚫MOV AL,[SI] ; AL=01
⚫INC SI ; 3004
⚫LOOP Back
⚫HLT ; halt!
Bit Manipulation Instructions
or
Logical Instructions
Dr. Manju Khurana
Assistant Professor,
CSED
TIET, Patiala
manju.khurana@thap
ar.edu
Bit Manipulation Instructions
⚫These instructions are used at the bit level.
⚫These instructions can be used for:
⚫Testing a zero bit
⚫Set or reset a bit
⚫Shift bits across registers
Bit Manipulation Instructions
⚫NOT Src:
⚫ The NOT instruction inverts each bit (forms the
1’s complement) of a byte or word in the
specified destination. The destination can be a
register or a memory location. This instruction
does not affect any flag.
⚫ NOT BX Complement content or BX register
⚫ NOT BYTE PTR [BX] Complement memory byte at offset
[BX] in data segment.
Bit Manipulation Instructions
⚫ AND Des, Src:
⚫ It performs AND operation of Des and Src.
⚫ Src can be immediate number, register or memory
location.
⚫ Des can be register or memory location.
⚫ Both operands cannot be memory locations at the same
time.
⚫ CF and OF become zero after the operation.
⚫ PF, SF and ZF are updated.
Bit Manipulation Instructions
⚫ OR Des, Src:
⚫ It performs OR operation of Des and Src.
⚫ Src can be immediate number, register or memory
location.
⚫ Des can be register or memory location.
⚫ Both operands cannot be memory locations at the same
time.
⚫ CF and OF become zero after the operation.
⚫ PF, SF and ZF are updated.
Bit Manipulation Instructions
⚫ XOR Des, Src:
⚫ It performs XOR operation of Des and Src.
⚫ Src can be immediate number, register or memory
location.
⚫ Des can be register or memory location.
⚫ Both operands cannot be memory locations at the same
time.
⚫ CF and OF become zero after the operation.
⚫ PF, SF and ZF are updated.
Bit Manipulation Instructions
⚫NEG Destination:
⚫ This instruction replaces the number in a
destination with its 2’s complement. The
destination can be a register or a memory location.
It gives the same result as the invert each bit and
add one algorithm. The NEG instruction updates
AF, AF, PF, ZF, and OF.
⚫ NEG AL Replace number in AL with its 2’s
complement
⚫ NEG BX Replace number in BX with its 2’s
complement
⚫ NEG BYTE PTR [BX] Replace byte at offset BX in
DX with its 2’s complement
⚫ NEG WORD PTR [BP] Replace word at offset BP in
SS with its 2’s complement
Bit Manipulation
⚫TEST des,src:
Instructions
⚫ This instruction ANDs the byte / word in the
specified source with the byte / word in the
specified destination.
⚫ Result is not stored anywhere.
⚫ Flags are updated, but neither operand is
changed.
⚫ The test instruction is often used to set flags
before a Conditional jump instruction.
⚫ The source can be an immediate number, the
content of a register, or the content of a memory
location. The destination can be a register or a
memory location. The source and the destination
cannot both be memory locations.
⚫ CF and OF are both 0’s after TEST. PF, SF and
Bit Manipulation Instructions
⚫TEST des,src:
⚫ TEST AL, BH ;AND BH with AL. No result stored;
Update PF, SF, ZF.
⚫ TEST CX, 0001H ;AND CX with immediate
number 0001H;
No result stored; Update PF, SF, ZF
⚫ TEST BP, [BX][DI] ;AND word are offset [BX][DI]
in DS with word in BP. No result stored. Update
PF, SF, and ZF
Bit Manipulation Instructions
⚫SHL/SAL Des, Count: Shift
Logic/Arithmatic left
⚫It shift bits of byte or word left, by count.
⚫It puts zero(s) in LSBs.
⚫MSB is shifted into carry flag.
⚫If the number of bits desired to be shifted is 1,
then the immediate number 1 can be written
in Count.
⚫However, if the number of bits to be shifted is
more than 1, then the count is put in CL
register.
Bit Manipulation Instructions
⚫SHL/SAL Des, Count: Shift
Logic/Arithmatic left
⚫.
Bit Manipulation Instructions
⚫SHR Des, Count:
⚫This instruction shifts each bit in the specified
destination to the right.
⚫As a bit is shifted out of the MSB position, a 0
is put in its place. The bit shifted out of the
LSB position goes to CF.
⚫ In the case of multi-bit shifts, CF will contain
the bit most recently shifted out from the LSB.
⚫Bits shifted into CF previously will be lost.
Bit Manipulation Instructions
⚫SHR Des, Count:
Bit Manipulation Instructions
⚫SHR Des, Count:
⚫If you want to shift the operand by one bit
position, you can specify this by putting a 1 in
the count position of the instruction.
⚫For shifts of more than 1 bit position, load
the desired number of shifts into the CL
register, and put “CL” in the count position of
the instruction.
Bit Manipulation Instructions
⚫SAR Des, Count:
⚫This instruction shifts each bit in the specified
destination to the right.
⚫As a bit is shifted out of the MSB position, a
copy of the old MSB is put in the MSB
position. In other words, the sign bit is copied
into the MSB.
⚫The LSB will be shifted into CF. In the case of
multiple-bit shift, CF will contain the bit most
recently shifted out from the LSB. Bits shifted
into CF previously will be lost.
Bit Manipulation Instructions
⚫ SAR Des, Count:
⚫ SAR DX, 1 ;Shift word in DI one bit position right, new
MSB = old MSB
⚫ MOV CL, 02H ;Load desired number of shifts in CL
⚫ SAR WORD PTR [BP], CL ;Shift word at offset [BP] in
stack segment right by two bit positions, the two MSBs are
now copies of original MSB
Bit Manipulation Instructions
⚫ROL Des, Count:rotate left without carry
⚫It rotates bits of byte or word left, by count.
⚫MSB is transferred to LSB and also to CF.
⚫If the number of bits desired to be shifted is 1,
then the immediate number 1 can be written in
Count.
⚫However, if the number of bits to be shifted is
more than 1, then the count is put in CL
register.
Bit Manipulation Instructions
⚫ROL Des, Count:
Bit Manipulation Instructions
⚫ROR Des, Count: rotate right without
carry
⚫It rotates bits of byte or word right, by count.
⚫LSB is transferred to MSB and also to CF.
⚫If the number of bits desired to be shifted is 1,
then the immediate number 1 can be written in
Count.
⚫However, if the number of bits to be shifted is
more than 1, then the count is put in CL
register.
Bit Manipulation Instructions
⚫ROR Des, Count:
Bit Manipulation Instructions
⚫RCL Des, Count: rotate left through carry
⚫This instruction rotates all the bits in a
specified word or byte some number of bit
positions to the left.
⚫The operation circular because the MSB of
the operand is rotated into the carry flag
and the bit in the carry flag is rotated around
into LSB of the operand.
Bit Manipulation Instructions
⚫RCR Des, Count: rotate right through
carry
⚫This instruction rotates all the bits in a
specified word or byte some number of bit
positions to the right.
⚫ The operation circular because the LSB of the
operand is rotated into the carry flag and the
bit in the carry flag is rotate around into MSB
of the operand.
Program Execution Transfer
Instructions
Dr. Manju Khurana
Assistant Professor,
CSED
TIET, Patiala
manju.khurana@thap
ar.edu
Program Execution Transfer Instructions
⚫These instructions cause change in the
sequence of the execution of instruction.
⚫This change can be through a condition or
sometimes unconditional.
⚫The conditions are represented by flags.
Program Execution Transfer Instructions
⚫2 main type of branching:
⚫ Near :- Intra-segment(branch to new location
within current segment only)
⚫ Only IP needs to be changed.
⚫ Range of -128 to 127 🡪 short branch.