0% found this document useful (0 votes)
5 views36 pages

MP 2

The document provides an overview of the instruction set for the 8086 microprocessor, detailing various categories such as data transfer, arithmetic, logical, and control instructions. It explains specific instructions like MOV, PUSH, POP, and arithmetic operations including ADD, SUB, and their respective effects on flags. Additionally, it includes examples for each instruction to illustrate their usage in programming the x86 processor.

Uploaded by

Aneesh PK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views36 pages

MP 2

The document provides an overview of the instruction set for the 8086 microprocessor, detailing various categories such as data transfer, arithmetic, logical, and control instructions. It explains specific instructions like MOV, PUSH, POP, and arithmetic operations including ADD, SUB, and their respective effects on flags. Additionally, it includes examples for each instruction to illustrate their usage in programming the x86 processor.

Uploaded by

Aneesh PK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

MICROPROCESSORS AND INTERFACING (5131)

MODULE – II
Programming of x86 processor

Instruction Set Of 8086


The Instruction set of 8086 microprocessor is classified into 7, they are:-
1. Data transfer instructions
2. Arithmetic instructions
3. Logical instructions
4. Shift / rotate instructions
5. String instructions
6. Program control transfer instructions/Branch or Loop Instructions
7. Machine Control Instructions
8. Flag manipulation instructions

Data Transfer instructions


Data transfer instruction, as the name suggests is for the transfer of data
 from memory to internal register
 from internal register to memory
 from one register to another register
 from input port to internal register
 from internal register to output port, etc

These are also known as copy instructions.

Instructions To Transfer A Word


 MOV − Used to copy the byte or word from the provided source to the provided destination.
 PUSH − Used to put a word at the top of the stack.
 POP − Used to get a word from the top of the stack to the provided location.
 PUSHA − Used to put all the registers into the stack.
 POPA − Used to get words from the stack to all registers.
 XCHG − Used to exchange the data from two locations.
 XLAT − Used to translate a byte in AL using a table in the memory.

Instructions For Input And Output Port Transfer


 IN − Used to read a byte or word from the provided port to the accumulator.
 OUT − Used to send out a byte or word from the accumulator to the provided port.

Instructions To Transfer The Address


 LEA − Used to load the address of operand into the provided register.
 LDS − Used to load DS register and other provided register from the memory
MGM Polytechnic College, Kilimanoor (CT Department) Page 24
MICROPROCESSORS AND INTERFACING (5131)

 LES − Used to load ES register and other provided register from the memory.

Instructions To Transfer Flag Registers


 LAHF − Used to load AH with the low byte of the flag register.
 SAHF − Used to store AH register to low byte of the flag register.
 PUSHF − Used to copy the flag register at the top of the stack.
 POPF − Used to copy a word at the top of the stack to the flag register.

1. MOV instruction
It is a general purpose instruction to transfer byte or word from register to register, memory to register, register
to memory or with immediate addressing.
General Form: MOV destination, source
Here the source and destination needs to be of the same size, that is both 8 bit or both 16 bit.
MOV instruction does not affect any flags.

Example:-
MOV BX, 00F2H ; load the immediate number 00F2H in BX register
MOV CL, [2000H] ; Copy the 8 bit content of the memory location, at a displacement of
2000H from data segment base to the CL register
MOV [589H], BX ; Copy the 16 bit content of BX register on to the memory location, which
at a displacement of 589H from the data segment base.
MOV DS, CX ; Move the content of CX to DS

2. PUSH instruction
The PUSH instruction decrements the stack pointer by two and copies the word from source to the location
where stack pointer now points. Here the source must of word size data. Source can be a general purpose
register, segment register or a memory location.
The PUSH instruction first pushes the most significant byte to sp-1, then the least significant to the sp-2.
Push instruction does not affect any flags.

MGM Polytechnic College, Kilimanoor (CT Department) Page 25


MICROPROCESSORS AND INTERFACING (5131)

Example:-
PUSH CX ; Decrements SP by 2, copy content of CX to the stack (figure shows
execution of this instruction)
PUSH DS ; Decrement SP by 2 and copy DS to stack

3. POP instruction
The POP instruction copies a word from the stack location pointed by the stack pointer to the
destination. The destination can be a General purpose register, a segment register or a memory location. Here
after the content is copied the stack pointer is automatically incremented by two.
The execution pattern is similar to that of the PUSH instruction.
Example:
POP CX ; Copy a word from the top of the stack to CX and increment SP by 2

4. PUSHA instruction
Pushes all general purpose registers onto the stack in the following order: AX, CX, DX, BX, SP, BP, SI,
DI. The value of SP pushed into the stack is its value before the actual push of SP.
5. POPA instruction
This instruction pops the top 8 words off the stack into the 8 general purpose 16/32 bit registers.
Registers are popped in the following order: DI, SI, BP, SP, DX, CX and AX.
The SP value popped from the stack is actually discarded.

6. XCHG instruction
The XCHG instruction exchanges contents of the destination and source. Here destination and source can be
register and register or register and memory location, but XCHG cannot interchange the value of 2 memory
locations.
General Format
XCHG Destination, Source

Example: ; exchange word in CX with


XCHG BX, CX the word in BX
XCHG AL, CL ; exchange byte in CL with
the byte in AL
XCHG AX, SUM[BX] ; here physical address,
which is DS+SUM+[BX].
The content at physical
address and the content of
AX are interchanged.
7. XLAT Instruction
The XLAT instruction takes no operands. It is used in lookup tables. The BX register contains the offset
address of the lookup table. The AL register has a byte number. The XLAT instruction takes the byte number
from AL and load the contents of address DS: BX+AL into AL register. The syntax for this instruction is:

MGM Polytechnic College, Kilimanoor (CT Department) Page 26


MICROPROCESSORS AND INTERFACING (5131)

XLAT
or
XLAT Table_Name
First, you’ll have to store the starting offset address of table into BX register which is done by:
MOV BX, OFFSET Table_Name

8 . IN & OUT instructions


The IN instruction will copy data from a port to the accumulator. If 8 bit is read the data will go to AL and if 16
bit then to AX. Similarly OUT instruction is used to copy data from accumulator to an output port.
Both IN and OUT instructions can be done using direct and indirect addressing modes.

Example: ; Copy a byte from the port


IN AL, 0F8H 0F8H to AL
MOV DX, 30F8H ; Copy port address in DX
IN AL, DX ; Move 8 bit data from 30F8H
port
IN AX, DX ; Move 16 bit data from
30F8H port
OUT 047H, AL ; Copy contents of AL to 8 bit
port 047H
MOV DX, 30F8H ; Copy port address in DX
OUT DX, AL ; Move 8 bit data to the
30F8H port
OUT DX, AX ; Move 16 bit data to the
30F8H port

9. LEA – Load Effective Address


LEA Register, Source
This instruction determines the offset of the variable or memory location named as the source and puts
this offset in the indicated 16-bit register.
LEA does not affect any flag.

Example:
LEA BX, PRICES ;Load BX with offset of PRICE in DS

10. LDS –
LDS Register, Memory address of the first word
This instruction loads new values into the specified register and into the DS register from four
successive memory locations. The word from two memory locations is copied into the specified register and the
word from the next two memory locations is copied into the DS registers. LDS does not affect any flag.

Example:
LDS BX, [4326] ;Copy content of memory at displacement 4326H in DS to BL, content of
4327H to BH. Copy content at displacement of 4328H and 4329H in DS to DS register

11. LES –
LES Register, Memory address of the first word

MGM Polytechnic College, Kilimanoor (CT Department) Page 27


MICROPROCESSORS AND INTERFACING (5131)

This instruction loads new values into the specified register and into the ES register from four
successive memory locations. The word from the first two memory locations is copied into the specified
register, and the word from the next two memory locations is copied into the ES register. LES does not affect
any flag.

Example:
LES BX, [789AH] ;Copy content of memory at displacement 789AH in DS to BL, content of 789BH
to BH, content of memory at displacement 789CH and 789DH in DS is copied to ES register

12. LAHF instruction-


This instruction copies the low byte of the flag register to AH. Only five flags are copied; the remaining bits are
undefined. After copying, AH looks like this:
AH = SF ZF xx AF xx PF xx CF
Format: LAHF
LAHF does not affect any flag

13. SAHF instruction-


SAHF- Store AH Register into Flags. Transfers bits of AH register into the Flag Register. This includes AF, CF,
PF, SF and ZF.
Format: SAHF
SAHF affects the flags AF, CF, PF, SF and ZF.

14. PUSHF instruction


This instruction transfers the Flags Register onto the stack. PUSHF saves a 16 bit value.
The stack pointer is decremented by 2.

15. POPF instruction


The POPF instruction pops a word from the stack into the Flags Register and then increments SP by 2.

Arithmetic Instructions
These instructions are used to perform arithmetic operations like addition, subtraction, multiplication, division,
etc.
Following is the list of instructions under this group –
Instructions To Perform Addition
 ADD − Used to add the provided byte to byte/word to word.
 ADC − Used to add with carry.
 INC − Used to increment the provided byte/word by 1.
 AAA − Used to adjust ASCII after addition.
 DAA − Used to adjust the decimal after the addition/subtraction operation.

MGM Polytechnic College, Kilimanoor (CT Department) Page 28


MICROPROCESSORS AND INTERFACING (5131)

Instructions To Perform Subtraction


 SUB − Used to subtract the byte from byte/word from word.
 SBB − Used to perform subtraction with borrow.
 DEC − Used to decrement the provided byte/word by 1.
 NEG − Used to negate each bit of the provided byte/word and add 1/2’s complement.
 CMP − Used to compare 2 provided byte/word.
 AAS − Used to adjust ASCII codes after subtraction.
 DAS − Used to adjust decimal after subtraction.

Instruction To Perform Multiplication


 MUL − Used to multiply unsigned byte by byte/word by word.
 IMUL − Used to multiply signed byte by byte/word by word.
 AAM − Used to adjust ASCII codes after multiplication.

Instructions To Perform Division


 DIV − Used to divide the unsigned word by byte or unsigned double word by word.
 IDIV − Used to divide the signed word by byte or signed double word by word.
 AAD − Used to adjust ASCII codes after division.
 CBW − Used to fill the upper byte of the word with the copies of sign bit of the lower byte.
 CWD − Used to fill the upper word of the double word with the sign bit of the lower word.

1. ADD instruction
Add instruction is used to add the current contents of destination with that of source and store the result in
destination. Here we can use register and/or memory locations. AF, CF, OF, PF, SF, and ZF flags are affected
General Format:
ADD Destination, Source
Example:
 ADD AL, 0FH ; Add the immediate content, 0FH to the content of AL and store
the result in AL
 ADD AX, BX ; AX <= AX+BX
 ADD AX,0100H – IMMEDIATE
 ADD AX,BX – REGISTER
 ADD AX,[SI] – REGISTER INDIRECT OR INDEXED
 ADD AX, [5000H] – DIRECT
 ADD [5000H], 0100H – IMMEDIATE
 ADD 0100H – DESTINATION AX (IMPLICT)

2. ADC : ADD WITH CARRY

MGM Polytechnic College, Kilimanoor (CT Department) Page 29


MICROPROCESSORS AND INTERFACING (5131)

This instruction performs the same operation as ADD instruction, but adds the carry flag bit (which may be set
as a result of the previous calculation) to the result. All the condition code flags are affected by this instruction.
The examples of this instruction along with the modes are as follows:
Example:
 ADC AX,BX – REGISTER
 ADC AX,[SI] – REGISTER INDIRECT OR INDEXED
 ADC AX, [5000H] – DIRECT
 ADC [5000H], 0100H – IMMEDIATE
 ADC 0100H – IMMEDIATE (AX IMPLICT)

3 . INC instruction
INC instruction adds 1 to any register or memory location, except segment register. AF, CF, OF, PF, SF, and ZF
flags are affected.
Format: INC dest
dest - mem/reg

Example:
INC BL ; BL=BL+1
INC BYTEPTR[BX] ;Adds 1 to the byte contents of data segment memory location addressed by BX

4. AAA instruction
ASCII adjust after addition. Addition of two one digit ASCII coded number will not be ASCII. To
convert the result to ASCII, we use AAA after ADD instruction. The result in AX, when added to
3030H, gives us ASCII coded sum.
Format: AAA
Affects FLAGS: AF CF (OF,PF,SF,ZF undefined)

Example:
MOV AL,31H ;mov ASCII 1 in AL
ADD AL,39H ;The ASCII result should be 10 but we get 6AH in AL
AAA ;Adjusts the result and AX now contains 0100H
ADD AX,3030H ;AX now contains ASCII result 10

5. DAA instruction
Decimal adjust after addition. This instruction follows ADD or ADC to adjust the result into BCD result.
This instruction works only with the AL registers and so must occur 8 bits at a time.

Format: DAA
Affects FLAGS: AF CF (OF,PF,SF,ZF undefined)
Example:

MGM Polytechnic College, Kilimanoor (CT Department) Page 30


MICROPROCESSORS AND INTERFACING (5131)

MOV DX,1234H ; load 1234H BCD


MOV BX,3099H ;load 3099H BCD
MOV AL,BL ;only AL register can be used for DAA
ADD AL,DL ;
DAA ;result is BCD adjusted
MOV CL,AL ;mov result into CL
MOV AL,BH ;
ADC AL,DH ;
DAA ;result is BCD adjusted
mov CH,AL ;now CX contains the sum in BCD format as 4333

6. SUB instruction
SUB instruction is used to subtract the current contents of destination with that of source and store the result in
destination. Here we can use register and/or memory locations. AF, CF, OF, PF, SF, and ZF flags are affected.
General Format:
SUB Destination, Source
Example:
 SUB AL, 0FH ; subtract the immediate content, 0FH from the content of AL and
store the result in AL
 SUB AX, BX ; AX <= AX-BX
 SUB AX,0100H – IMMEDIATE (DESTINATION AX)
 SUB AX,BX – REGISTER
 SUB AX,[5000H] – DIRECT
 SUB [5000H], 0100H – IMMEDIATE

7. SBB : SUBTRACT WITH BORROW


The subtract with borrow instruction subtracts the source operand and the borrow flag (CF) which may reflect
the result of the previous calculations, from the destination operand. Subtraction with borrow, here means
subtracting 1 from the subtraction obtained by SUB, if carry (borrow) flag is set.
The result is stored in the destination operand. All the flags are affected (condition code) by this instruction. The
examples of this instruction are as follows:
Example:
 SBB AX,0100H – IMMEDIATE (DESTINATION AX)
 SBB AX,BX – REGISTER
 SBB AX,[5000H] – DIRECT
 SBB [5000H], 0100H – IMMEDIATE

8. DEC instruction
DEC instruction subtracts 1 from any register or memory location, except segment register. AF, CF, OF, PF, SF,
and ZF flags are affected.
MGM Polytechnic College, Kilimanoor (CT Department) Page 31
MICROPROCESSORS AND INTERFACING (5131)

Format: DEC dest


dest - mem/reg

Example:
DEC BL ; BL=BL-1
DEC BYTEPTR[BX] ;Subtracts 1 from the byte contents of data segment memory location addressed by
BX

9. NEG instruction
Two complements' negation
Subtracts the destination from 0 and saves the 2s complement of "dest" back into "dest".

Format: NEG dest


;dest-mem/reg

Affects FLAGS:AF CF OF PF SF ZF
Examples:
NEG AX ;AX is two's complemented
NEG BYTE PTR[BX] ; The byte contents of the data segment memory location addressed by BX
are two's complemented.

10. CMP: COMPARE


The instruction compares the source operand, which may be a register or an immediate data or a memory
location, with a destination operand that may be a register or a memory location. For comparison, it subtracts
the source operand from the destination operand but does not store the result anywhere. The flags are affected
depending upon the result of the subtraction. If both of the operands are equal, zero flag is set. If the source
operand is greater than the destination operand,carry flag is set or else, carry flag is reset. The examples of this
instruction are as follows:
Example:
 CMP BX,0100H – IMMEDIATE
 CMP AX,0100H – IMMEDIATE
 CMP [5000H], 0100H – DIRECT
 CMP BX,[SI] – REGISTER INDIRECT OR INDEXED
 CMP BX, CX – REGISTER

11. AAS instruction


Ascii Adjust For Subtraction:
Corrects result of a previous unpacked ASCII subtraction in AX.
Format: AAS

Affects FLAGS: AF, CF (OF,PF,SF,ZF undefined)


Example:

MGM Polytechnic College, Kilimanoor (CT Department) Page 32


MICROPROCESSORS AND INTERFACING (5131)

MOV AX,39H ; Load ASCII 9


SUB AL,31H ; Subtract ASCII 1
AAS ; Adjust difference
ADD AX,3030H ; Answer in ASCII

12. DAS instruction


Decimal Adjust After Subtraction
This instruction follows the SUB or SBB instruction to adjust the difference to a BCD result.DAS works
only with AL register, hence subtraction must occur 8 bits at a time.

Format: DAS
Affects FLAGS: AF, CF (OF,PF,SF,ZF undefined)

Example:

MOV DX, 1234H ; load 1234 BCD


MOV BX, 3099H ; load 3099 BCD
MOV AL,BL
SUB AL,DL ; subtract DL from BL
DAS ; decimal adjust
MOV CL,AL ; answer in CL
MOV AL,BH
SBB AL,DH ; subtract CH with borrow
DAS ; decimal adjust
MOV CH,AL ; answer in CH

13. MUL instruction


This instruction multiplies an unsigned byte (word) from some source with an unsigned byte (word) in
the AL (AX) register.
The source can be a register or a memory location.
When doing byte-multiplication, the result is stored in a 16-bit location AX since the result can be as
large as 16 bits. Word-multiplication results in the most significant word being placed in the DX register and the
least significant word placed in the AX register.
If the most significant byte (word) of a 16-bit (32-bit) result is zero, both CF and OF will be zero.

Format: MUL Source


Affects Flags: OF,CF

Example

MUL CX ;AX times CX, result high word in DX and low word in AX

14. IMUL instruction

MGM Polytechnic College, Kilimanoor (CT Department) Page 33


MICROPROCESSORS AND INTERFACING (5131)

This insruction multilplies a signed byte (word) from a source with a signed byte (word) in AL (AX).All
details of IMUL are exactly the same as that of MUL, except that if all bits of the destination are not used by the
result, the result is sign extended. The OF and the CF are both set to 1 if the upper byte (word) of a 16-bit (32-
bit) prodct contains part of the product.

Format: IMUL Source

Affects Flags: OF, CF


Example:
IMUL AX ;AX times AX, result in DX and AX

15. AAM instruction


The AAM mnemonic stands for ASCII adjust for Multiplication or BCD Adjust after Multiply. This
instruction is used in the process of multiplying two ASCII digits. The process begins with masking the upper 4
bits of each digit, leaving an unpacked BCD in each byte. These unpacked BCD digits are then multiplied and
the AAM instruction is subsequently used to adjust the product to two unpacked BCD digits in AX.
AAM works only after the multiplication of two unpacked BCD bytes, and it works only on an operand
in AL.
Format: AAM
Affects Flags: PF,SF,ZF

Example:
Multiply 9 and 5

MOV AL, 00000101


MOV BH, 00001001
MUL BH ;Result stored in AX
;AX = 00000000 00101101 = 2DH = 45 in decimals
AAM ;AX = 00000100 00000101 = 0405H = 45 in unpacked BCD
;If ASCII values are required an OR operation with 3030H can follow this step

16. DIV instruction


The DIV instruction is used to divide an unsigned word (doubleword) by a byte (word).
For word-byte division, the word must be in the AX register; the divisor can be in a reg or a mem location. After
the division, AL contains the quotient and AH contains the remainder. Division by zero or a quotient larger than
can fit into AL (FFH) raises a Type 0 Interrupt.
For a doubleword-word division, the most significant word must be in the DX register and the least
significant word in the AX register. Post-division, AX contains the quotient and DX the remainder.
Division of a byte by a byte is done by putting the dividend byte in AL and filling AH with zeros. A
similar analogy can be applied to word-word division.
If the divisor does not divide the dividend exactly, the quotient is truncated, not rounded.

Format: DIV Source


Affects Flags: None
MGM Polytechnic College, Kilimanoor (CT Department) Page 34
MICROPROCESSORS AND INTERFACING (5131)

Example
DIV BL ;Divide a word in AX with a byte in BL. Quotient in AL, remainder in AH

17. IDIV instruction


This instruction is used to divide a signed word (doubleword) by a signed byte (word). The locations of
quotients, remainders, dividends and divisors are the same as in the DIV instruction; the sign of the remainder is
the same as that of the dividend.
Division by zero and quotient values > 128 or <-127 result in a Type 0 Interrupt. (The range for 186+ is -
128 to +127).
For byte-byte (word-word) divisions, AH (DX) must be sign extended with the same sign as the AL
(AX) byte (word).

Format: IDIV Source


Affects Flags: Undefined

Example
IDIV BP ;Signed doubleword in DX and AX divided by signed word in BP

18. AAD instruction


AAD stands for ASCII Adjust before Division (or BCD-to-Binary Convert before Division). AAD
converts two unpacked BCD digits in AH and 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 the division, AL
will contain the unpacked BCD quotient and AH will contain the unpacked BCD remainder.

Format: AAD
Affects Flags: PF, SF, ZF

Example
Divide 67 by 9

MOV AX, 0607H


MOV CH, 09H

AAD ;AX = 0043 = 43H = 67

DIV CH ;Divide AX by unpacked BCD in CH, result: AL = 07 unpacked BCD, AH = 04 unpacked BCD

19. CBW instruction − Used to fill the upper byte of the word with the copies of sign bit of the lower byte.
20. CWD instruction − Used to fill the upper word of the double word with the sign bit of the lower word

MGM Polytechnic College, Kilimanoor (CT Department) Page 35


MICROPROCESSORS AND INTERFACING (5131)

Bit Manipulation Instructions


These instructions are used to perform operations where data bits are involved, i.e. operations like logical, shift,
etc.
Following is the list of instructions under this group −
Instructions To Perform Logical Operation
 NOT − Used to invert each bit of a byte or word.
 AND − Used for adding each bit in a byte/word with the corresponding bit in another byte/word.
 OR − Used to multiply each bit in a byte/word with the corresponding bit in another byte/word.
 XOR − Used to perform Exclusive-OR operation over each bit in a byte/word with the corresponding bit
in another byte/word.
 TEST − Used to add operands to update flags, without affecting operands.

Instructions To Perform Shift Operations


 SHL/SAL − Used to shift bits of a byte/word towards left and put zero(S) in LSBs.
 SHR − Used to shift bits of a byte/word towards the right and put zero(S) in MSBs.
 SAR − Used to shift bits of a byte/word towards the right and copy the old MSB into the new MSB.

Instructions To Perform Rotate Operations


 ROL − Used to rotate bits of byte/word towards the left, i.e. MSB to LSB and to Carry Flag [CF].
 ROR − Used to rotate bits of byte/word towards the right, i.e. LSB to MSB and to Carry Flag [CF].
 RCR − Used to rotate bits of byte/word towards the right, i.e. LSB to CF and CF to MSB.
 RCL − Used to rotate bits of byte/word towards the left, i.e. MSB to CF and CF to LSB.

Logical Instructions
1. AND instruction
This instruction logically ANDs each bit of the source byte/word with the corresponding bit in the destination
and stores the result in destination. The source can be an immediate number, register or memory location,
register can be a register or memory location.
The CF and OF flags are both made zero, PF, ZF, SF are affected by the operation and AF is undefined.
General Format:
AND Destination, Source
Example:
 AND BL, AL ; suppose BL=1000 0110 and AL = 1100 1010 then after the
operation BL would be BL= 1000 0010.
 AND CX, AX ; CX <= CX AND AX
 AND CL, 08 ; CL<= CL AND (0000 1000)

2. OR instruction

MGM Polytechnic College, Kilimanoor (CT Department) Page 36


MICROPROCESSORS AND INTERFACING (5131)

This instruction logically ORs each bit of the source byte/word with the corresponding bit in the destination and
stores the result in destination. The source can be an immediate number, register or memory location, register
can be a register or memory location.
The CF and OF flags are both made zero, PF, ZF, SF are affected by the operation and AF is undefined.
General Format:
OR Destination, Source
Example:

3. NOT instruction
The NOT instruction complements (inverts) the contents of an operand register or a memory location, bit by bit.
The examples are as follows:
Example:
 NOT AX ;(BEFORE AX= 1011= B= 16 AFTER EXECUTION AX = 0100 = 4 = 16.
 NOT [5000H]

4. XOR instruction
The XOR operation is again carried out in a similar way to the AND and OR operation. The constraints on the
operands are also similar. The XOR operation gives a high output, when the 2 input bits are dissimilar.
Otherwise, the output is zero. The example instructions are as follows:
Example:
 XOR AX,0098H
 XOR AX,BX
 XOR AX,[5000H]

5. TEST instruction
Performs a logical AND of the two operands updating the flags
register without saving the result.

Format: TEST dest,src


Affects FLAGS: CF OF PF SF ZF (AF undefined)

Shift Instructions
Shift instructions move the binary data to the left or right by shifting them within the register or memory
location. They also can perform multiplication of powers of 2^+n and division of powers of 2^-n.
There are two type of shifts
 logical shifting

MGM Polytechnic College, Kilimanoor (CT Department) Page 37


MICROPROCESSORS AND INTERFACING (5131)

 arithmetic shifting
later is used with signed numbers while former with unsigned.

1. SHL/SAL instruction
Both the instruction shifts each bit to left, and places the MSB in CF and LSB is made 0. The destination
can be of byte size or of word size, also it can be a register or a memory location. Number of shifts is indicated
by the count.
All flags are affected
General Format:
SAL/SHL destination, count

After the execution


CY B7 B6 B5 B4 B3 B2 B1 B0
1 0 1 1 0 1 1 1 0
SHL/SAL CX,10 : CX is logically shifted left 10 places
SHL/SAL CX,CL ;CX is logically shifted left by the number of spaces specified by CL

2. SHR instruction
This instruction shifts each bit in the specified destination to the right and 0 is stored in the MSB
position. The LSB is shifted into the carry flag. The destination can be of byte size or of word size, also it can be
a register or a memory location. Number of shifts is indicated by the count.
All flags are affected
General Format:
SHR destination, count
Example:
MOV BL, B7H ; BL is made B7H
SHR BL, 1 ; shift the content of BL register one place to the right. Before execution,

B7 B6 B5 B4 B3 B2 B1 B0 CY
1 0 1 1 0 1 1 1 0

After execution,

MGM Polytechnic College, Kilimanoor (CT Department) Page 38


MICROPROCESSORS AND INTERFACING (5131)

B7 B6 B5 B4 B3 B2 B1 B0 CY
0 1 0 1 1 0 1 1 1

3. SAR instruction
Shifts the destination right by "count" bits with the current sign
bit replicated in the leftmost bit. The Carry Flag contains the last bit shifted out.
Format:SAR dest,count
dest-mem/reg
Affects FLAGS:CF OF PF SF ZF (AF undefined)

Examples:
SAR SI,2 ;SI is arithmetically shifted 2 places to the right
SAR DATA1,CL ;The contents of data segment addressed by DATA1 is right shifted arithmetically by the
number specified by CL.

Rotate Instructions
Rotate on the other hand rotates the information in a register or memory either from one end to another
or through the carry flag.

1. ROL instruction
This instruction rotates all the bits in a specified byte or word to the left some number of bit positions.
MSB is placed as a new LSB and a new CF. The destination can be of byte size or of word size, also it can be a
register or a memory location. Number of shifts is indicated by the count.
MGM Polytechnic College, Kilimanoor (CT Department) Page 39
MICROPROCESSORS AND INTERFACING (5131)

All flags are affected


General Format:
ROL destination, count
Example:
MOV BL, B7H ; BL is made B7H
ROL BL, 1 ; rotates the content of BL register one place to the left. Before
execution,

After the execution,


CY B7 B6 B5 B4 B3 B2 B1 B0
1 0 1 1 0 1 1 1 1

2. ROR instruction
This instruction rotates all the bits in a specified byte or word to the right some number of bit positions.
LSB is placed as a new MSB and a new CF. The destination can be of byte size or of word size, also it can be a
register or a memory location. Number of shifts is indicated by the count.
All flags are affected
General Format:
ROR destination, count
Example:
MOV BL, B7H ; BL is made B7H
ROR BL, 1 ; shift the content of BL register one place to the right.

Before execution,
B7 B6 B5 B4 B3 B2 B1 B0 CY
1 0 1 1 0 1 1 1 0

After execution,
B7 B6 B5 B4 B3 B2 B1 B0 CY
1 1 0 1 1 0 1 1 1

3. RCR instruction
This instruction rotates all the bits in a specified byte or word to the right some number of bit positions
along with the carry flag. LSB is placed in a new CF and previous carry is placed in the new MSB. The
destination can be of byte size or of word size, also it can be a register or a memory location. Number of shifts is
indicated by the count.
All flags are affected
MGM Polytechnic College, Kilimanoor (CT Department) Page 40
MICROPROCESSORS AND INTERFACING (5131)

General Format:
RCR destination, count
Example:
MOV BL, B7H ; BL is made B7H
RCR BL, 1 ; shift the content of BL register one place to the right.

Before execution,
B7 B6 B5 B4 B3 B2 B1 B0 CY
1 0 1 1 0 1 1 1 0

After execution,
B7 B6 B5 B4 B3 B2 B1 B0 CY
0 1 0 1 1 0 1 1 1

4. RCL instruction
Rotate Through Carry Left
Rotates the bits in the destination to the left "count" times with
all data pushed out the left side reentering on the right. The Carry Flag holds the last bit rotated out.

Format: RCL dest,count


dest-mem/reg

Affects FLAGS: CF, OF

Examples:
RCL,BX,6 ;BX rotates left through carry 6 places
RCL AH,CL ;AH rotates left through carry by the number specified in CL

MGM Polytechnic College, Kilimanoor (CT Department) Page 41


MICROPROCESSORS AND INTERFACING (5131)

String Instructions
String is a group of bytes/words and their memory is always allocated in a sequential order.
Following is the list of instructions under this group −
 REP − Used to repeat the given instruction till CX ≠ 0.
 REPE/REPZ − Used to repeat the given instruction until CX = 0 or zero flag ZF = 1.
 REPNE/REPNZ − Used to repeat the given instruction until CX = 0 or zero flag ZF = 1.
 MOVS/MOVSB/MOVSW − Used to move the byte/word from one string to another.
 CMPS/CMPSB/CMPSW − Used to compare two string bytes/words.
 INS/INSB/INSW − Used as an input string/byte/word from the I/O port to the provided memory
location.
 OUTS/OUTSB/OUTSW − Used as an output string/byte/word from the provided memory location to
the I/O port.
 SCAS/SCASB/SCASW − Used to scan a string and compare its byte with a byte in AL or string word
with a word in AX.
 LODS/LODSB/LODSW − Used to store the string byte into AL or string word into AX.

1. MOVS/MOVSB/MOVSW
These instructions copy a word or byte from a location in the data segment to a location in the extra
segment. The offset of the source is in SI and that of destination is in DI. For multiple word/byte transfers the
count is stored in the CX register.
When direction flag is 0, SI and DI are incremented and when it is 1, SI and DI are decremented.
MOVS affect no flags. MOVSB is used for byte sized movements while MOVSW is for word sized.

Example:
CLD ; clear the direction flag to auto increment SI and DI MOV AX, 0000H ;
MOV DS, AX ; initialize data segment register to 0 MOV ES, AX ; initialize extra segment register to 0
MOV SI, 2000H ; Load the offset of the string1 in SI MOV DI, 2400H ; Load the offset of the string2 in DI
MOV CX, 04H ; load length of the string in CX

REP MOVSB ; decrement CX and MOVSB until CX will be 0

2. REP/REPE/REPZ/REPNE/REPNZ
REP is used with string instruction; it repeats an instruction until the specified condition becomes false.

Example:
REP => CX=0
REPE/REPZ => CX=0 OR
ZF=0
REPNE/REP => CX=0 OR
NZ ZF=1
.
3. CMPS/CMPSB/CMPSW
CMPS is used to compare the strings, byte wise or word wise. The comparison is affected by subtraction
of content pointed by DI from that pointed by SI. The AF, CF, OF, PF, SF and ZF flags are affected by this
instruction, but neither operand is affected.
MGM Polytechnic College, Kilimanoor (CT Department) Page 42
MICROPROCESSORS AND INTERFACING (5131)

Example:
MOV SI, OFFSET F_STRING ; point first string

MOV DI, OFFSET S_STRING ; point second string


MOV CX, 0AH ; set the counter as 0AH
CLD ; clear direction flag to auto
REPE CMPSB ; increment repeatedly compare
till unequal or counter =0

4. LODS/LODSB/LODSW
This instruction copies a byte (word) of string from the location pointed to by SI. The SI value is
automatically incremented (depending on the DF value) after each movement by 1 byte (2 bytes) for a byte
string (word string).
This instruction is preferred over MOV for string manipulation due to its treatment of the SI pointer
and the ability to use it with REP prefixes.

Format: LODS <String>


LODSB
LODSW

Affects Flags: None


Example
CLD
MOV SI, Offset STR
LODSB

5.STOS/STOSB/STOSW
he STOS instruction copies a byte (word) from AL (AX) to a memory location stored in [ES:DI]. DI is
automatically incremented after the copy, the DF determines the increment/decrement.
In effect, it replaces a string element with a byte (word) from AL (AX).
STOS does not affect any flags. After copying the content DI is automatically incremented or
decremented, based on the value of direction flag.

Example:
MOV DL, OFFSET D_STRING ; assign DI with destination address.
STOS D_STRING ; assembler uses string name to determine byte or
word, if byte then AL is used and if of word size, AX is used

6. SCAS/SCASB/SCASW
SCAS(/B/W) compares a byte in AL or a word in AX with a byte or word pointed to by DI in ES. The
direction flag determines the direction of scan.
Used with the REP prefix to find the first occurrence of a specified byte(word) in a string.
MGM Polytechnic College, Kilimanoor (CT Department) Page 43
MICROPROCESSORS AND INTERFACING (5131)

Format: SCAS <String>


SCASB
SCASW

Affects Flags: AF,PF,OF,PF,SF,ZF


Example
Scan a string STR of 100 characters for the 'space' character, 20H

MOV CX, 100


MOV DI, offset STR
MOV AL, 20H
REPNE SCASB

7. INS/INSB/INSW instructions
Loads data from a port to the destination [ES:DI]. DI is incremented or decremented depending on the
value of DF.

Format: INS dest, port


INSB
INSW
Affects Flags: None

Example
Move a word from <port address> to STR

MOV DX, <port address>


MOV DI, offset STR
INSW

8. OUTS/OUTSB/OUTSW
Transfers a data byte/word from [DS:SI] to a port specified in the DX register. SI is
incremented/decremented depending on the value of the DF.

Format: OUTS port, src


OUTSB
OUTSW
Affects Flags: None

Example
Move a byte from STR to the port PORT

MOV SI, offset STR


MOV DX, <addr. PORT>
OUTSB

MGM Polytechnic College, Kilimanoor (CT Department) Page 44


MICROPROCESSORS AND INTERFACING (5131)

Program control transfer instructions/ Branch Instructions


(Branch and Loop Instructions)

There are 2 types of such instructions. They are:


1. Unconditional transfer instructions – CALL, RET, JMP
2. Conditional transfer instructions – J condition

1. CALL instruction
The CALL instruction is used to transfer execution to a subprogram or procedure. There are two types of
CALL instructions, near and far.
A near CALL is a call to a procedure which is in the same code segment as the CALL instruction. 8086
when encountered a near call, it decrements the SP by 2 and copies the offset of the next instruction after the
CALL on the stack. It loads the IP with the offset of the procedure then to start the execution of the procedure.
A far CALL is the call to a procedure residing in a different segment.
Here value of CS and offset of the next instruction both are backed up in the stack. And then branches to
the procedure by changing the content of CS with the segment base containing procedure and IP with the offset
of the first instruction of the procedure.
Example:
Near call
CALL PRO ; PRO is the name of the procedure
CALL CX ; Here CX contains the offset of the first instruction of
the procedure, that is replaces the content of IP with the content of CX
Far call
CALL DWORD PTR[8X] ; New values for CS and IP are fetched from four memory
locations in the DS. The new value for CS is fetched from [8X] and
[8X+1], the new IP is fetched from [8X+2] and [8X+3].
2. RET instruction
RET instruction will return execution from a procedure to the next instruction after the CALL instruction
in the calling program. If it was a near call, then IP is replaced with the value at the top of the stack, if it had
been a far call, then another POP of the stack is required. This second popped data from the stack is put in the
CS, thus resuming the execution of the calling program. RET instruction can be followed by a number, to
specify the parameters passed.
RET instruction does not affect any flags.
General format:
RET

Example:
p1 PROC ; procedure
declaration.
MOV AX, 1234h ;

MGM Polytechnic College, Kilimanoor (CT Department) Page 45


MICROPROCESSORS AND INTERFACING (5131)

RET ; return to
caller.
p1 ENDP

3. JMP Instruction
This is also called as unconditional jump instruction, because the processor jumps to the specified
location rather than the instruction after the JMP instruction. Jumps can be short jumps when the target address
is in the same segment as the JMP instruction or far jumps when it is in a different segment.

General Format:
JMP <targetaddress>
Example:
MOV AL,05H
JMP label1 ; jump over to label
MOV AL, 00H
label1: MOV [2000H], AL
RET

CONDITIONAL JUMP (J cond)


Conditional jumps are always short jumps in 8086. Here jump is done only if the condition specified is
true/false. If the condition is not satisfied, then the execution proceeds in the normal way.

There are many conditional jump instructions like JC : Jump on carry (CF=set)
 JA/JNBE − Used to jump if above/not below/equal instruction satisfies.
 JAE/JNB − Used to jump if above/not below instruction satisfies.
 JBE/JNA − Used to jump if below/equal/ not above instruction satisfies.
 JC − Used to jump if carry flag CF = 1
 JE/JZ − Used to jump if equal/zero flag ZF = 1
 JG/JNLE − Used to jump if greater/not less than/equal instruction satisfies.
 JGE/JNL − Used to jump if greater than/equal/not less than instruction satisfies.
 JL/JNGE − Used to jump if less than/not greater than/equal instruction satisfies.
 JLE/JNG − Used to jump if less than/equal/if not greater than instruction satisfies.
 JNC − Used to jump if no carry flag (CF = 0)
 JNE/JNZ − Used to jump if not equal/zero flag ZF = 0
 JNO − Used to jump if no overflow flag OF = 0
 JNP/JPO − Used to jump if not parity/parity odd PF = 0
 JNS − Used to jump if not sign SF = 0
 JO − Used to jump if overflow flag OF = 1
 JP/JPE − Used to jump if parity/parity even PF = 1
 JS − Used to jump if sign flag SF = 1

JA/JNBE
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes both CF and ZF to have value equal to 0, else no action is taken.

Format: JA dest, JNBE dest


MGM Polytechnic College, Kilimanoor (CT Department) Page 46
MICROPROCESSORS AND INTERFACING (5131)

[dest: addressin the range of -128 bytes to +127 bytes from the address of instruction after JA/JNBE]

Flags: the instruction has no effect on any flags.

Example:
CMP AX, 0030H; compares by subtracting 0030H from the value in AX regsiter
JA LABEL1; jumps to the address specified by LABEL1 if value in register AX is above the value 0030H

JAE/JNB
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes CF to have value equal to 0, else no action is taken.

Format: JAE dest, JNB dest


[dest: address in the range of -128 bytes to +127 bytes from the address of instruction after JAE/JNB]

Flags: the instruction has no effect on any flags.

Example:
CMP AX, 0030H; compares by subtracting 0030H from the value in AX regsiter
JAE LABEL1; jumps to the address specified by LABEL1 if value in register AX is above or equal to the
value 0030H

JBE/JNA
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes either the CF or ZF to have value equal to 1, else no action is taken.

Format: JBE dest, JNA dest


[dest: address in the range of -128 bytes to +127 bytes from the address of instruction after JBE/JNA]

Flags: the instruction has no effect on any flags.

Example:
CMP AX, 0030H; compares by subtracting 0030H from the value in AX regsiter
JBE LABEL1; jumps to the address specified by LABEL1 if value in register AX is below or equal to the
value 0030H

JC
Jumps to the destination label mentioned in the instruction if the value of CF is 1, else no action is taken.

Format: JC dest
[dest: address in the range of -128 bytes to +127 bytes from the address of instruction after JC]

Flags: the instruction has no effect on any flags.

Example:
JC LABEL1; jumps to the address specified by LABEL1 if CF is set

MGM Polytechnic College, Kilimanoor (CT Department) Page 47


MICROPROCESSORS AND INTERFACING (5131)

JE/JZ
Jumps to the destination label mentioned in the instruction if the ZF is set, else no action is taken.

Format: JE dest, JZ dest


[dest: address in the range of -128 bytes to +127 bytes from the address of instruction after JE/JZ]

Flags: the instruction has no effect on any flags.

Example:
JZ LABEL1; jumps to the address specified by LABEL1 if ZF=1

JG/JNLE
Jump if greater/ Jump if not less than or equal
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes ZF to have value equal to 0 and CF and OF to have same values, else no action is taken.

Format: JG dest, JNLE dest


[dest: addressin the range of -128 bytes to +127 bytes from the address of instruction after JG/JNLE]

Flags: the instruction has no effect on any flags.

Example:
CMP AX, 0030H; compares by subtracting 0030H from the value in AX regsiter
JG LABEL1; jumps to the address specified by LABEL1 if value in register AX is more positive than the
value 0030H

JGE/JNL
Jump if greater than or equal/ Jump if not less than
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes CF to have value equal to OF, else no action is taken.

Format: JGE dest, JNL dest


[dest: address in the range of -128 bytes to +127 bytes from the address of instruction after JGE/JNL]

Flags: the instruction has no effect on any flags.

Example:
CMP AX, 0030H; compares by subtracting 0030H from the value in AX regsiter
JGE LABEL1; jumps to the address specified by LABEL1 if value in register AX is more positive or equal to
the value 0030H

JL/JNGE
Jump if less than/ Jump if not greater than or equal
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes CF to have value not equal to OF, else no action is taken.

Format: JL dest, JNGE dest


[dest: address in the range of -128 bytes to +127 bytes from the address of instruction after JL/JNGE]
MGM Polytechnic College, Kilimanoor (CT Department) Page 48
MICROPROCESSORS AND INTERFACING (5131)

Flags: the instruction has no effect on any flags.

Example:
CMP AX, 0030H; compares by subtracting 0030H from the value in AX regsiter
JL LABEL1; jumps to the address specified by LABEL1 if value in register AX is more negative than the
value 0030H

JLE/JNG
Jump if less than or equal/ Jump if not greater than
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes either the ZF to have value equal to 1 or OF to have value unequal to SF, else no action is
taken.

Format: JLE dest, JNG dest


[dest: address in the range of -128 bytes to +127 bytes from the address of instruction after JLE/JNG]

Flags: the instruction has no effect on any flags.

Example:
CMP AX, 0030H; compares by subtracting 0030H from the value in AX regsiter
JLE LABEL1; jumps to the address specified by LABEL1 if value in register AX is more negative than or
equal to the value 0030H

JNE/JNZ
Jump if not equal/ Jump if not zero
Jumps to the destination label mentioned in the instruction if the ZF is 0, else no action is taken.

Format: JNZ dest, JNE dest


[dest: address in the range of -128 bytes to +127 bytes from the address of instruction after JNZ/JNE]

Flags: the instruction has no effect on any flags.

Example:
JNZ LABEL1; jumps to the address specified by LABEL1 if ZF=0

JNO
Jump if no overflow
Jumps to the destination label mentioned in the instruction if the OF is 0, else no action is taken. The
overflow flag is set when a signed arithmetic operation produces a result too large for the destination register or
memory location to store.

Format: JNO dest


[dest: address in the range of -128 bytes to +127 bytes from the address of instruction after JNO]

Flags: the instruction has no effect on any flags.

Example:
MGM Polytechnic College, Kilimanoor (CT Department) Page 49
MICROPROCESSORS AND INTERFACING (5131)

ADD AL,BL; add signed bytes in AL and BL


JNZ LABEL1; jumps to the address specified by LABEL1 if OF=0 after to the add instruction above

JNP/JPO
Jump if no parity/ Jump if parity odd
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes PF to have value equal to 0, else no action is taken.

Format: JNP dest, JPO dest


[dest: address in the range of -128 bytes to +127 bytes from the address of instruction after JNP/JPO]

Flags: the instruction has no effect on any flags.

Example:
OR AX, AX; Set flags
JNP LABEL1; even parity is expected, jumps to the address specified by LABEL1 if parity is found to be odd

JNS
Jump if not signed (Jump if positive)
Jumps to the destination label mentioned in the instruction if the SF is set, else no action is taken. If
the sign flag is 0 it indicates a positive signed number. Hence the instruction causes a jump if the result of
previous instruction is positive.

Format: JNS dest


[dest: address in the range of -128 bytes to +127 bytes from the address of instruction after JNS]

Flags: the instruction has no effect on any flags.

Example:
ADD AL,BL; add signed bytes in AL and BL
JNS LABEL1; jumps to the address specified by LABEL1 if SF=0 due to the add instruction above

JO
Jump if overflow
Jumps to the destination label mentioned in the instruction if the OF is set, else no action is taken. The
overflow flag is set when a signed arithmetic operation produces a result too large for the destination register or
memory location to store.

Format: JO dest
[dest: address in the range of -128 bytes to +127 bytes from the address of instruction after JO]

Flags: the instruction has no effect on any flags.

Example:
ADD AL,BL; add signed bytes in AL and BL
JZ LABEL1; jumps to the address specified by LABEL1 if OF=1 due to the add instruction above

MGM Polytechnic College, Kilimanoor (CT Department) Page 50


MICROPROCESSORS AND INTERFACING (5131)

JP/JPE
Jump if parity/ Jump if parity even
Jumps to the destination label mentioned in the instruction if the result of previous instruction (generally
compare) causes PF to have value equal to 1, else no action is taken.

Format: JP dest, JPE dest


[dest: address in the range of -128 bytes to +127 bytes from the address of instruction after JP/JPE]

Flags: the instruction has no effect on any flags.

Example:
OR AX, AX; Set flags
JNP LABEL1; odd parity is expected, jumps to the address specified by LABEL1 if parity is found to be even

JS
Jump if signed (Jump if negative)
Jumps to the destination label mentioned in the instruction if the SF is 0, else no action is taken. If
the sign flag is 0 it indicates a positive signed number. Hence the instruction causes a jump if the result of
previous instruction is negative.

Format: JS dest
[dest: address in the range of -128 bytes to +127 bytes from the address of instruction after JS]

Flags: the instruction has no effect on any flags.

Example:
ADD AL,BL; add signed bytes in AL and BL
JS LABEL1; jumps to the address specified by LABEL1 if SF=1 due to the add instruction above

ITERATION CONTROL INSTRUCTIONS (Loop Instructions)


These instructions are used to execute a series of instructions some number of times. The number is
specified in the CX register, which will be automatically decremented in course of iteration. But here the
destination address for the jump must be in the range of -128 to 127 bytes.
Following is the list of instructions under this group −

LOOP
Contents of CX register are decremented by 1 and if the new value in CX register is non-zero, a jump is
taken to the destination label mentioned in the instruction, else no action is taken.
Format: LOOP dest
[dest: addressin the range of -128 bytes to +127 bytes from the address of instruction after LOOP]

Flags: the instruction has no effect on any flags.


Example:
LOOP LABEL1; jumps to the address specified by LABEL1 if value in register CX is non-zero after it has
been decremented

MGM Polytechnic College, Kilimanoor (CT Department) Page 51


MICROPROCESSORS AND INTERFACING (5131)

LOOPE/LOOPZ
Contents of CX register are decremented by 1 and if the new value in CX register is non-zero and the ZF
is set, a jump is taken to the destination label mentioned in the instruction, else no action is taken.
Format: LOOPE dest, LOOPZ dest
[dest: addressin the range of -128 bytes to +127 bytes from the address of instruction after LOOPE/LOOPZ]

Flags: the instruction has no effect on any flags.


Example:
LOOPE LABEL1; jumps to the address specified by LABEL1 if value in register CX is non-zero after it has
been decremented and ZF is set due to previous instruction

LOOPNE/LOOPNZ
Contents of CX register are decremented by 1 and if the new value in CX register is non-zero and the ZF
is 0, a jump is taken to the destination label mentioned in the instruction, else no action is taken.
Format: LOOPNE dest, LOOPNZ dest
[dest: addressin the range of -128 bytes to +127 bytes from the address of instruction after LOOPNE/LOOPNZ]

Flags: the instruction has no effect on any flags.


Example:
LOOPNE LABEL1; jumps to the address specified by LABEL1 if value in register CX is non-zero after it has
been decremented and ZF is 0 due to previous instruction

JCXZ
The Jump if CX is Zero causes a jump to a label given in the instruction if CX contains all zeros;
unsigned comparison is used.

JCXZ does not look at ZF when deciding to jump. Destination label must be in the range of -128 to 127 bytes
from the address of the instruction after the JCXZ instruction.

Format: JCXZ Label


JECXZ Label (386+)

Flags: None

Machine Control Instructions


1. HLT instruction
The HLT instruction will cause the 8086 microprocessor to fetching and executing instructions.
The 8086 will enter a halt state. The processor gets out of this Halt signal upon an interrupt signal in INTR
pin/NMI pin or a reset signal on RESET input.
General form:-
HLT

5. NOP instruction

MGM Polytechnic College, Kilimanoor (CT Department) Page 52


MICROPROCESSORS AND INTERFACING (5131)

At the end of NOP instruction, no operation is done other than the fetching and decoding of the
instruction. It takes 3 clock cycles. NOP is used to fill in time delays or to provide space for instructions while
trouble shooting. NOP affects no flags.

Flag manipulation instructions (Processor Control Instructions)

1. STC instruction
This instruction sets the carry flag. It does not affect any other flag.
2. CLC instruction
This instruction resets the carry flag to zero. CLC does not affect any other flag.
3. CMC instruction
This instruction complements the carry flag. CMC does not affect any other flag.
4. STD instruction
This instruction is used to set the direction flag to one so that SI and/or DI can be decremented
automatically after execution of string instruction. STD does not affect any other flag.
5. CLD instruction
This instruction is used to reset the direction flag to zero so that SI and/or DI can be incremented
automatically after execution of string instruction. CLD does not affect any other flag.
6. STI instruction
This instruction sets the interrupt flag to 1. This enables INTR interrupt of the 8086. STI does not affect
any other flag.
7. CLI instruction
This instruction resets the interrupt flag to 0. Due to this the 8086 will not respond to an interrupt signal
on its INTR input. CLI does not affect any other flag.

Interrupt Instructions
These instructions are used to call the interrupt during program execution.
 INT − Used to interrupt the program during execution and calling service specified.
 INTO − Used to interrupt the program during execution if OF = 1
 IRET − Used to return from interrupt service to the main program

PRCEDURES

A procedure is group of instructions that usually performs one task. It is a reusable section of a software
program which is stored in memory once but can be used as often as necessary. A procedure can be of two
types.
1) Near Procedure
2) Far Procedure

Near Procedure: A procedure is known as NEAR procedure if it is written (defined) in the same code segment
which is calling that procedure. Only Instruction Pointer (IP register) contents will be changed in NEAR
procedure.

MGM Polytechnic College, Kilimanoor (CT Department) Page 53


MICROPROCESSORS AND INTERFACING (5131)

FAR procedure : A procedure is known as FAR procedure if it is written (defined) in the different code
segment than the calling segment. In this case both Instruction Pointer (IP) and the Code Segment (CS) register
content will be changed.

The Syntax for a procedure is as follows:


Procedure_name PROC [near / far]
Instruction 1
Instruction 2
-----------
-----------
Instruction n
Procedure_name ENDP

The CALL to a procedure can be made in the following way,


CALL procedure_name

Directives used for procedure :


PROC directive: The PROC directive is used to identify the start of a procedure. The PROC directive follows a
name given to the procedure.After that the term FAR and NEAR is used to specify the type of the procedure.
ENDP Directive: This directive is used along with the name of the procedure to indicate the end of a procedure
to the assembler. The PROC and ENDP directive are used to bracket a procedure.

CALL instruction and RET instruction :


CALL instruction : The CALL instruction is used to transfer execution to a procedure.It performs two
operation. When it executes, first it stores the address of instruction after the CALL instruction on the stack.
Second it changes the content of IP register in case of Near call and changes the content of IP register and cs
register in case of FAR call. There are two types of calls.
1) Near Call or Intra segment call.
2) Far call or Inter Segment call

RET instruction : The RET instruction will return execution from a procedure to the next instruction after call
in the main program. At the end of every procedure RET instruction must be executed.
Example:-
MOV AL, 1
MOV BL, 2

CALL m2
CALL m2
CALL m2
CALL m2

m2 PROC
MUL BL ; AX = AL * BL.
RET ; return to caller.
m2 ENDP

MGM Polytechnic College, Kilimanoor (CT Department) Page 54


MICROPROCESSORS AND INTERFACING (5131)

END

Advantages of using procedures

 Reusability of code
 Less usage of memory
 Development becomes easier
 Reduced development time
 easier

Disadvantages of using procedures

 Extra code is required to integrate the procedures


 Extra time required to link the procedures.
 Not efficient way to use for a small set of instructions
 Extra load on the processor

MACROS

Macro is a group of instruction. The macro assembler generates the code in the program each time where
the macro is “called”.

Macros can be defined by MACRO and ENDM assembler directives. Creating macro is very similar to
creating a new opcode that can used in the program, as shown below.

To use a macro it is only necessary to call it by its name, as if it were another assembler instruction,
since directives are no longer necessary as in the case of the procedures

It is important to note that macro sequences execute faster than procedures because there is no CALL
and RET instructions to execute. The assembler places the macro instructions in the program each time when it
is invoked. This procedure is known as Macro expansion.

Syntax:
Name-of_macro MACRO [ARGUMENT 1,……….ARGUMENT N]
Instructions
ENDM

Example:-

DisplayMsg MACRO msg ; define macro to display message


lea dx,msg ; standard message display code
mov ah,9
int 21h
ENDM

MGM Polytechnic College, Kilimanoor (CT Department) Page 55


MICROPROCESSORS AND INTERFACING (5131)

Advantages and disadvantages of MACRO :

Advantages:
1) Program written with macro is more readable.
2) Macro can be called just writing by its name along with parameters, hence no extra code is required like
CALL & RET.
3) Execution time is less bcz of no linking and returning
4) Finding errors during debugging is easier.

Disadvantages :
1) object code is generated every time a macro is called hence object file becomes lengthy.
2) For large group of instructions macro cannot be preferred.

Difference between Procedure and Macro

Number Format Conversions


Packed BCD to Unpacked BCD Conversion

BCD is Binary Coded Decimal. Its 2 versions are Packed BCD and Unpacked BCD
Eg: 64 is packed BCD Number. Its Unpacked BCD is 06, 04

The steps for converting a Packed BCD number into Unpacked BCD is:-
1. ANDing the BCD number with F0
st
2. The result obtained from the 1 step is shifted right 4 times.
3. ANDing the BCD number with 0F.
MGM Polytechnic College, Kilimanoor (CT Department) Page 56
MICROPROCESSORS AND INTERFACING (5131)

Eg:- 64  Packed BCD

1. 64 AND F0  60
2. Shift right 4 times  06
3. 64 AND 0F  04

Unpacked BCD to Packed BCD Conversion

The different steps are:


1. Shift the 1st unpacked number 4 times left
2. Add the 2nd unpacked BCD number with the result obtained after 1st step.

BCD Calculations

BCD Addition : DAA


BCD Subtraction : DAS

The DAA (Decimal Adjust after Addition) instruction allows addition of numbers represented in 8-bit
packed BCD code. It is used immediately after normal addition instruction operating on BCD codes. This
instruction assumes the AL register as the source and the destination, and hence it requires no operand.
The effect of DAS (Decimal Adjust after Subtraction) instruction is similar to that of DAA, except
that it is used after a subtraction instruction.

For example in the following program, that NUM1 and NUM2 are decimal numbers coded in BCD format, the
result should be 61

NUM1 DB 27H
NUM2 DB 35H

MOV AL, NUM1 ;load AX with number NUM1


ADD AL, NUM2 ;AL = AL + NUM2 i.e. AL = 5CH = 92 in decimal
;The expected result is 62 in decimal
DAA ; AL = 62
.

DAA operation:
For the processor there is no difference between a BCD and a hexadecimal number, all numbers are seen as
hexadecimal numbers. After performing an addition and the result is saved in the AL register, conversion to
decimal is carried out as follows:

 if the digit in the lower four nibbles of AL is greater than 10 (decimal),


 then subtract 10 and
 add 1 to the digit in the higher four nibbles of AL.

MGM Polytechnic College, Kilimanoor (CT Department) Page 57


MICROPROCESSORS AND INTERFACING (5131)

Example:
Suppose that the result obtained after adding 27 to 35, is 5CH. To convert this to the value that we would expect
after a decimal addition, the DAA instruction is used.

 assume result = AL = 5CH


 digit in the low four nibbles of AL = C = 12
 then 12 - 10 = 2
 hence keep the 2 and
 add 1 to the digit in the higher four nibbles of AL 5 + 1 = 6
 The result is thus: 62
The DAS instruction works in a similar fashion after a SUB instruction.

ASCII OPERATIONS
The ASCII arithmetic instructions function with ASCII-coded numbers. These numbers range in value from
30H to 39H for the numbers 0 to 9. There are four instructions used with ASCII arithmetic operations:
1. AAA (ASCII Adjust after Addition)
2. AAD (ASCII Adjust before Division)
3. AAM (ASCII Adjust after Multiplication)
4. AAS (ASCII Adjust after Subtraction)

These instructions use register AX as the source and as destination.


1. AAA Instruction
 Adjust the sum of two unpacked BCD values to create an unpacked BCD result.
 The AL register is the implied source and destination operand for this instruction.
 The AAA instruction is only useful when its follow an ADD instruction that adds (binary addition) two
unpacked BCD values.
 The AAA instruction then adjusts the contents of AL register to contain the correct 1-digit unpacked
BCD result.
 If the addition produces a decimal carry, the AH register is incremented by 1, and the CF and AF flags
are set.

2. AAD Instruction
 Unlike all other adjustment instructions, the AAD instruction appears before a division.
 The AAD instruction requires that the AX register contain a two-digit unpacked BCD number (not
ASCII) before executing.
 After adjusting the AX register with AAD, it is divided by an unpacked BCD number to generate a
single-digit result in AL with any remainder in AH.
 Example:
MOV BL, 9H
MOV AX, 702H
AAD
DIV BL
 The above example show how 72 is unpacked BCD is divided by 9 to produce a quotient of 8. The
0702H loaded into AX register is adjusted by the AAD instruction to 0048H.
MGM Polytechnic College, Kilimanoor (CT Department) Page 58
MICROPROCESSORS AND INTERFACING (5131)

3. AAM Instruction
 Adjust the result of the multiplication of two unpacked BCD values to create a pair of unpacked BCD
values.
 The AX register is the implied source and destination operand for this instruction.
 The AAM instruction is only useful when it follows an MUL instruction that multiplies (binary
multiplication) two unpacked BCD values and stores a word result in AX registers.

4. AAS Instruction
 Adjust the result of the subtraction of two unpacked BCD values to create a unpacked BCD result.
 The AL register is the implied source and destination for this instructions.
 The AAS instruction is only useful when it follows a SUB instruction that subtracts (binary subtraction)
one unpacked BCD valued from another and stores a byte result in the AL register.
 If the subtraction produced a decimal carry, the AH register is decremented by 1, and the CF and AF
flags are set.
 If no decimal carry occurred, the CF and AF flags are cleared, and the AH register is unchanged

MGM Polytechnic College, Kilimanoor (CT Department) Page 59

You might also like