Unit 2 Mpi
Unit 2 Mpi
Addressing Modes of 8086 The way of specifying data to be operated by an instruction is known
as addressing modes. This specifies that the given data is an immediate data or an address. It also specifies whether
the given operand is register or register pair.
Types of addressing modes:
Register mode – In this type of addressing mode both the operands are registers.
Example: MOV AX, BX XOR AX, DX ADD AL, BL
Immediate mode – In this type of addressing mode the source operand is a 8 bit or 16 bit data. Destination operand
can never be immediate data.
Example: MOV AX, 2000 MOV CL, 0A ADD AL, 45 AND AX, 0000
Note that to initialize the value of segment register an register is required. MOV AX, 2000 MOV CS, AX
Displacement or direct mode – In this type of addressing mode the effective address is directly given in the
instruction as displacement.
Example: MOV AX, [DISP] MOV AX, [0500]
Register indirect mode – In this addressing mode the effective address is in SI, DI or BX.
Example: Physical Address = Segment Address + Effective Address
MOV AX, [DI] ADD AL, [BX] MOV AX, [SI]
Based indexed mode – In this the effective address is sum of base register and index register.
Base register: BX, BP Index register: SI, DI
The physical memory address is calculated according to the base register.
Example: MOV AL, [BP+SI] MOV AX, [BX+DI]
Indexed mode – In this type of addressing mode the effective address is sum of index register and displacement.
Example:MOV AX, [SI+2000] MOV AL, [DI+3000]
Based mode – In this the effective address is the sum of base register and displacement.
Example: MOV AL, [BP+ 0100]
Based indexed displacement mode – In this type of addressing mode the effective address is the sum of index
register, base register and displacement.
Example: MOV AL, [SI+BP+2000]
String mode – This addressing mode is related to string instructions. In this the value of SI and DI are auto
incremented and decremented depending upon the value of directional flag.
Example: MOVS B MOVS W
Input/Output mode – This addressing mode is related with input output operations.
Example: IN A, 45 OUT A, 50
Relative mode – In this the effective address is calculated with reference to instruction pointer.
Example: JNZ 8 bit address IP=IP+8 bit address
Addressing modes of 8085 –
Immediate Addressing Mode –
In immediate addressing mode the source operand is always data. If the data is 8-bit, then the instruction will be of
2 bytes, if the data is of 16-bit then the instruction will be of 3 bytes.
Examples: MVI B 45 (move the data 45H immediately to register B)
LXI H 3050 (load the H-L pair with the operand 3050H immediately)
JMP address (jump to the operand address immediately)
Register Addressing Mode – In register addressing mode, the data to be operated is available inside the register(s)
and register(s) is(are) operands. Therefore the operation is performed within various registers of the
microprocessor. Examples: MOV A, B (move the contents of register B to register A)
ADD B (add contents of registers A and B and store the result in register A)
INR A (increment the contents of register A by one)
Direct Addressing Mode – In direct addressing mode, the data to be operated is available inside a memory
location and that memory location is directly specified as an operand. The operand is directly available in the
instruction itself. Examples: LDA 2050 (load the contents of memory location into accumulator A)
LHLD address (load contents of 16-bit memory location into H-L register pair)
IN 35 (read the data from port whose address is 35)
Register Indirect Addressing Mode – In register indirect addressing mode, the data to be operated is available
inside a memory location and that memory location is indirectly specified by a register pair.
Examples: MOV A, M (move the contents of the memory location pointed by the H-L pair to the accumulator)
LDAX B (move contents of B-C register to the accumulator)
STAX B (store accumulator contents in memory pointed by register pair B-C)
Implied/Implicit Addressing Mode – In implied/implicit addressing mode the operand is hidden and the data to be
operated is available in the instruction itself.
Examples: CMA (finds and stores the 1’s complement of the contents of accumulator A in A)
RRC (rotate accumulator A right by one bit)
RLC (rotate accumulator A left by one bit)
Relative Addressing Mode: In this mode, the operand is a memory location specified by the contents of the
program counter plus a constant value. Example: MOV R0,#05H
8086 Instruction Format:The 8086 Instruction 8086 Instruction Format vary from 1 to 6 bytes in length. Fig.
6.8 shows the instruction formats for 1 to 6 bytes instructions. As shown in the Fig. 6.8, displacements and operands
may be either 8-bits or 16-bits long depending on the instruction. The opcode and the addressing mode is specified
using first two bytes of an instruction.
As seen from the Fig. 6.8 if an instruction has two opcode/addressing mode bytes, then the second byte is of one of the
following two forms .where Mod, Reg and R/M fields specify operand as described in the following tables.
5. String instructions: These instructions are used for manipulating strings of data, such as moving, copying, or
comparing strings. They operate on consecutive bytes or words in memory, and can be used for fast and efficient
string processing. Some examples of string instructions include:
• MOVS: Moves a byte or word from a source location to a destination location, and updates the index
registers to point to the next byte or word.
• CMPS: Compares a byte or word in memory to a byte or word in a register, and updates the index
registers accordingly.
• LODS: Loads a byte or word from a memory location into a register, and updates the index registers to
point to the next byte or word.
• STOS: Stores a byte or word from a register into a memory location, and updates the index registers to
point to the next byte or word.
• OPCODE OPERAND EXPLANATION EXAMPLE
PUSHA none put all the registers into the stack PUSHA
POPA none gets words from the stack to all registers POPA
LAHF none loads AH with the lower byte of the flag register LAHF
SAHF none stores AH register to lower byte of the flag register SAHF
PUSHF none copies the flag register at the top of the stack PUSHF
POPF none copies a word at the top of the stack to the flag register POPF
ADD/ADC Instruction : ADD destination, source / ADC destination, source. These instructions add a number from
source to a number from destination and put the result in the destination. The ADC, instruction also adds the status of
carry flag into the result. The source may be an immediate number, a register, or a memory location. The source and
the destination in an instruction cannot both be memory locations. The source and destination both must be a word or
byte. If you want to add a byte to a word, you must copy the byte to a word location and fill the upper byte of the word
with zeroes before adding. Flags affected : AF, CF, OF, PF, SF, ZF.
INC Instruction : Increment destination. The INC instruction adds 1 to the specified destination. The destination may
be a register or memory location. The AF, OF, PF, SF and ZF flags are affected.
AAA Instruction : ASCII Adjust for Addition. The numbers from 0-9 are represented as 30H-39H in ASCII code.
When you want to add two decimal digits which are represented in ASCII code, it is necessary to mask upper nibble
(3) from the code before addition. The Arithmetic Instructions in 8086 allows you to add the ASCII codes for two
decimal digits without masking off the “3” in the upper nibble of each digit. The AAA instruction can be used after
addition to get the current result in unpacked BCD form.
DAA Instruction : Decimal Adjust Accumulator. This instruction is used to make sure the result of adding two
packed BCD numbers is adjusted to be a legal BCD number.
1. If the value of the low-order four bits (D3-D0) in the AL is greater than 9 or if AF is set, the instruction
adds 6 (06) to the low-order four bits.
2. If the value of the high-order four bits (D7-D4) in the AL is greater than 9 or if carry flag is set, the
instruction adds 6 (60) to the high-order four bits.
b) Subtraction Instructions: SUB SBB DEC NEG CMP AAS DAS
1. If the value of the low-order four bits (D3-D0) in the AL is greater than 9 o AF is set; the instruction
subtracts 6 (06) from the low-order four bits.
2. If the value of the high-order four bits (D7-D4) in the AL is greater than 9 or if carry flag is set, the
instruction subtracts 6 (60) from the high-order four bits.
c) Multiplication Instructions: MUL IMUL AAM
MUL Instruction : MUL source. This instruction multiplies an unsigned byte from source and unsigned byte in AL
register or unsigned word from source and unsigned word in AX register. The source can be a register or a memory
location. When the byte is multiplied by the contents of AL, the result is stored in AX. The most significant byte is
stored in AH and least significant byte is stored in AL. When a word is multiplied by the contents of AX, the most
significant word of result is stored in DX and least significant word of result is stored in AX.
Flags : MUL instruction affect AF, PF, SF, and ZF flags.
IMUL Instruction : This instruction multiplies a signed byte from some source and a signed byte in AL, or a signed
word from some source and a signed word in AX. The source can be register or memory location. When a signed byte
is multiplied by AL a signed result will be put in AX. When a signed word is multiplied by AX, the high-order word
of the signed result is put in DX and the low-order word of the signed result is put in AX. If the upper byte of a 16-bit
result or the upper word of 32-bit result contains only copies of the sign bit (all 0’s or all 1’s), then the CF and the OF
flags will both be 0’s. The AF, PF, SF, and ZF flags are undefined after IMUL. To multiply a signed byte by a signed
word it is necessary to move the byte into a word location and fill the upper byte of the word with copies of the sign
bit. This can be done using CBW instruction.
AAM Instruction : BCD Adjust After Multiply. After the two unpacked BCD digits are multiplied, the AAM
instruction is used to adjust the product to two unpacked BCD digits in AX.
DIV Instruction : DIV source This instruction is used to divide an unsigned word by a byte or to divide an unsigned
double word by a word When dividing a word by a byte, the word must be in AX register. After the division AL will
contain an 8-bit quotient and AH will contain an 8-bit remainder. If an attempt is made to divide by 0 or the quotient is
too large to fit in AL (greater than FFH), the Arithmetic Instructions in 8086 will automatically execute a type 0
interrupt. 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 must be in AX. After the division AX will contain a 16-bit quotient and DX will contain a
16-bit remainder. Again, if an attempt is made to divide by 0 or the quotient is too large to fit in AX register (greater
than FFFFH), the Arithmetic Instructions in 8086 will do a type 0 interrupt. For DIV instruction source may be a
register or memory location. To divide a byte by a byte, it is necessary to put the dividend byte in AL and fill AH with
all 0’s. Similarly, to divide a word by a word, it is necessary to put the dividend word in AX and fill DX with all 0’s.
Flags : All flags are undefined after a DIV instruction.
IDIV Instruction : IDIV source. This instruction is used to divide a signed word by a signed byte, or to divide
a signed double word (32-bits) by a signed word. Rest all is similar to DIV instruction.
AAD Instruction : Binary Adjust 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. The PF, SF and ZF are updated. The AF, CF and OF are undefined after AAD.
In the 8086 microprocessor, branch instructions are used for conditional and unconditional branching, while
looping instructions are used for implementing loops. Here are some examples of branch and looping instructions in
the 8086 microprocessor:
Branch Instructions:
- JMP: Unconditional Jump. Transfers program control to the specified destination. Example: JMP Label ; Jumps
to the instruction labeled "Label".
- JZ / JE: Jump if Zero / Jump if Equal. Jumps to the specified destination if the Zero/Equal flag is set. Example:
JZ Label ; Jumps to the instruction labeled "Label" if the Zero flag is set.
- JNZ / JNE: Jump if Not Zero / Jump if Not Equal. Jumps to the specified destination if the Zero/Equal flag is not
set. Example: JNZ Label ; Jumps to the instruction labeled "Label" if the Zero flag is not set.
- JC / JB: Jump if Carry / Jump if Below. Jumps to the specified destination if the Carry flag is set.
Example: JC Label ; Jumps to the instruction labeled "Label" if the Carry flag is set.
- JNC / JNB: Jump if Not Carry / Jump if Not Below. Jumps to the specified destination if the Carry flag is not set.
Example: JNC Label ; Jumps to the instruction labeled "Label" if the Carry flag is not set.
- JA: Jump if Above. Jumps to the specified destination if the Carry and Zero flags are not set (unsigned
comparison). Example: JA Label ; Jumps to the instruction labeled "Label" if the Carry and Zero flags are not set.
- JAE: Jump if Above or Equal. Jumps to the specified destination if the Carry flag is not set (unsigned
comparison). Example: JAE Label ; Jumps to the instruction labeled "Label" if the Carry flag is not set.
- JB / JC: Jump if Below / Jump if Carry. Jumps to the specified destination if the Carry flag is set (unsigned
comparison). Example: JB Label ; Jumps to the instruction labeled "Label" if the Carry flag is set.
- JBE: Jump if Below or Equal. Jumps to the specified destination if the Carry or Zero flag is set (unsigned
comparison). Example: JBE Label ; Jumps to the instruction labeled "Label" if the Carry or Zero flag is set.
Looping Instructions:
- LOOP: Decrement the CX register by 1 and jumps to the specified destination if CX is not zero.
Example: LOOP Label ; Decrements CX and jumps to the instruction labeled "Label" if CX is not zero.
- LOOPE / LOOPZ: Loop if Equal / Loop if Zero. Decrement CX by 1 and jumps to the specified destination if
CX is not zero and the Zero flag is set. Example: LOOPE Label ; Decrements CX and jumps to the instruction
labeled "Label" if CX is not zero and the Zero flag is set.
- LOOPNE / LOOPNZ: Loop if Not Equal / Loop if Not Zero. Decrement CX by 1 and jumps to the specified
destination if CX is not zero and the Zero flag is not set. Example: LOOPNE Label ; Decrements CX and
jumps to the instruction labeled "Label" if CX is not zero and the Zero flag is not set.
Logical instructions Logical instructions in the 8086 microprocessor are instructions that perform logical
operations on data stored in registers or memory locations. These instructions can manipulate bits within a byte, set
or clear individual bits, or perform Boolean operations such as AND, OR, XOR, and NOT.
Some of the commonly used logical instructions in the 8086 microprocessor include:
1. AND – Performs a bitwise logical AND operation between two operands and stores the result in the
destination operand.
2. OR – Performs a bitwise logical OR operation between two operands and stores the result in the
destination operand.
3. XOR – Performs a bitwise logical XOR (exclusive OR) operation between two operands and stores the
result in the destination operand.
4. NOT – Performs a bitwise logical NOT (negation) operation on the operand and stores the result in the
destination operand.
5. TEST – Performs a bitwise logical AND operation between two operands, but does not store the result.
Instead, it sets the condition code flags based on the result of the operation.
Logical instructions are used in many applications, including bit manipulation, data encryption, and data
compression. They are an important part of the instruction set of the 8086 microprocessor and are used extensively
in assembly language programming. Logical instructions are the instructions which perform basic logical
operations such as AND, OR, etc. In 8086 microprocessor, the destination operand need not be the accumulator.
Following is the table showing the list of logical instructions:
OPCODE OPERAND DESTINATION EXAMPLE
AND D, S D = D AND S AND AX, 0010
OR D, S D = D OR S OR AX, BX
NOT D D = NOT of D NOT AL
XOR D, S D = D XOR S XOR AL, BL
TEST D, S performs bit-wise AND operation and affects the flag register TEST [0250], 06
SHR D, C shifts each bit in D to the right C times and 0 is stored at MSB position SHR AL,
04
SHL D, C shifts each bit in D to the left C times and 0 is stored at LSB position SHL AX, BL
ROR D, C rotates all bits in D to the right C times ROR BL, CL
ROL R, C rotates all bits in D to the left C times ROL BX, 06
RCR D, C rotates all bits in D to the right along with carry flag C times RCR BL, CL
RCL R, C rotates all bits in D to the left along with carry flag C times RCL BX, 06
Here D stands for destination, S stands for source and C stands for count. They can either be register, data or
memory address.
Flag Manipulation and Processor Control Instructions
Instruction Description
CLC Clear Carry Flag: This instruction resets the carry flag CF to 0.
CLD Clear Direction Flag: This instruction resets the direction flag DF to 0.
CLI Clear Interrupt Flag: This instruction resets the interrupt flag IF to 0.
CMC This instruction take complement of carry flag CF.
STC Set carry flag CF to 1.
STD Set direction flag to 1.
STI Set interrupt flag IF to 1.
HLT Halt processing. It stops program execution.
NOP Performs no operation.
ESC Escape: makes bus free for external master like a coprocessor or peripheral device.
WAIT When WAIT instruction is executed, the processor enters an idle state in which the processor does no processing.
LOCK It is a prefix instruction. It makes the LOCK pin low till the execution of the next instruction.
NOP (No operation) : NOP is used when no operation is performed. It is commonly used to fill in time delay or to
delete and insert instructions while troubleshooting. During the execution of NOP, no flags are affected.
Opcode- 00 Operand- None Length- 1 byte M-Cycles- 1 T-states- 4 Hex code- 00
HLT (Halt) HLT is used to stop the execution of the program temporarily. The microprocessor finishes executing
the current instruction and halts any further execution. The contents of the registers are unaffected during the HLT
state. HLT can be used to enter a wait state in which the microprocessor waits for a specific event to occur before
resuming execution. Opcode- 76 Operand- None Length- 1 byte
M-Cycles- 2 or more T-states- 5 or more Hex code- 76
Assembler Directives of 8086 Microprocessor It control the organization if the program and provide
necessary information to the assembler to understand the assembly language programs to generate necessary machine
codes. They indicate how an operand or a section of the program is to be processed by the assembler.
An assembler supports directives to define data, to organise segments to control procedure, to define macros. It
consists of two types of statements: instructions and directives. The instructions are translated to the machine code by
the assembler whereas directives are not translated to the machine codes.
Assembler Directives of the 8086 Microprocessor (a) The DB directive (b) The DW directive (c) The DD
directive (d) The STRUCT (or STRUC) and ENDS directives (counted as one) (e)The EQU Directive (f)The
COMMENT directive (g)ASSUME (h) EXTERN (i) GLOBAL (j) SEGMENT (k)OFFSET (l) PROC
(m)GROUP (n) INCLUDE
Data declaration directives:
1. DB – The DB directive is used to declare a BYTE -2-BYTE variable – A BYTE is made up of 8 bits.
Declaration examples:Byte1 DB 10h Byte2 DB 255 ; 0FFh, the max. possible for a BYTE
2. DW – The DW directive is used to declare a WORD type variable – A WORD occupies 16 bits or (2 BYTE).
Declaration examples:Word DW 1234h Word2 DW 65535; 0FFFFh, (the max. possible for a WORD)
3. DD – The DD directive is used to declare a DWORD – A DWORD double word is made up of 32 bits =2 Word’s
or 4 BYTE. Declaration examples: Dword1 DW 12345678h Dword2 DW 4294967295 ;0FFFFFFFFh.
4. STRUCT and ENDS directives to define a structure template for grouping data items.
(1) The STRUCT directive tells the assembler that a user defined uninitialized data structure follows. The uninitialized
data structure consists of a combination of the three supported data types. DB, DW, and DD. The labels serve as zero-
based offsets into the structure. The first element’s offset for any structure is 0. A structure element is referenced with
the base “+” operator before the element’s name. A Structure ends by using the ENDS directive meaning END of
Structure. DECLARATION: STRUCT Byte1 DB? Byte2 DB? Word1 DW? Word2 DW?
Dword1DW? Dword2 DW? ENDS
Use OF STRUCT: The STRUCT directive enables us to change the order of items in the structure when, we reform a
file header and shuffle the data. Shuffle the data items in the file header and reformat the sequence of data declaration
in the STRUCT and off you go. No change in the code we write that processes the file header is necessary unless you
inserted an extra data element.
6.The EQU Directive The EQU directive is used to give name to some value or symbol. Each time the assembler
finds the given names in the program, it will replace the name with the value or a symbol. The value can be in the
range 0 through 65535 and it can be another Equate declared anywhere above or below.
The following operators can also be used to declare an Equate: THIS BYTE THIS WORD THIS DWORD
A variable – declared with a DB, DW, or DD directive – has an address and has space reserved at that address for it in
the .COM file. But an Equate does not have an address or space reserved for it in the .COM file.
7. Extern: It is used to tell the assembler that the name or label following the directive are I some other assembly
module. For example: if you call a procedure which is in program module assembled at a different time from that
which contains the CALL instructions ,you must tell the assembler that the procedure is external the assembler will
put information in the object code file so that the linker can connect the two module together.
Example: PROCEDURE -HERE SEGMENT EXTERN SMART-DIVIDE: FAR ; found in the segment;
PROCEDURES-HERE PROCEDURES-HERE ENDS
(8) GLOBAL: The GLOBAL directive can be used in place of PUBLIC directive .for a name defined in the current
assembly module; the GLOBAL directive is used to make the symbol available to the other modules. Example:
GLOBAL DIVISOR:WORD tells the assembler that DIVISOR is a variable of type of word which is in another
assembly module or EXTERN.
(9) SEGMENT: It is used to indicate the start of a logical segment. It is the name given to the the segment. Example:
the code segment is used to indicate to the assembler the start of logical segment.
(10) PROC: (PROCEDURE) It is used to identify the start of a procedure. It follows a name we give the procedure.
After the procedure the term NEAR and FAR is used to specify the procedure Example: SMART-DIVIDE PROC
FAR identifies the start of procedure named SMART-DIVIDE and tells the assembler that the procedure is far.
(11) NAME:It is used to give a specific name to each assembly module when program consists of several
modules.Example: PC-BOARD used to name an assembly module which contains the instructions for controlling a
printed circuit board.
(12) INCLUDE:It is used to tell the assembler to insert a block of source code from the named file into the current
source module. This shortens the source module. An alternative is use of editor block command to cop the file into the
current source module.
(13) OFFSET:It is an operator which tells the assembler to determine the offset or displacement of a named data item
from the start of the segment which contains it. It is used to load the offset of a variable into a register so that variable
can be accessed with one of the addressed modes. Example: when the assembler read MOV BX.OFFSET PRICES, it
will determine the offset of the prices.
(14) GROUP:It can be used to tell the assembler to group the logical segments named after the directive into one
logical group. This allows the contents of all he segments to be accessed from the same group. Example: SMALL-
SYSTEM GROUP CODE, DATA, STACK-SEG.
Operators in 8086- Operator can be applied in the operand which uses the immediate data/address.
- Being active during assembling and no machine language code is generated. - Different types of operators are:
1) Arithmetic: + , - , * , / 2) Logical : AND, OR, XOR, NOT 3) SHL and SHR: Shift during assembly
4) [ ]: index 5) HIGH: returns higher byte of an expression 6) LOW: returns lower byte of an
expression. E.g. NUM EQU 1374 H MOV AL HIGH Num ; ( [AL] 13 )
7) OFFSET: returns offset address of a variable 8) SEG: returns segment address of a variable
9) PTR: used with type specifications BYTE, WORD,
RWORD, DWORD, QWORD
E.g. INC BYTE PTR [BX]
10) Segment override MOV AH, ES: [BX] 11) LENGTH:
returns the size of the referred variable
12) SIZE: returns length times type E.g.: BYTE VAR DB?
WTABLE DW 10 DUP (?)
MOV AX, TYPE BYTEVAR ; AX = 0001H
In 8086 there are 4 different flags which are used to enable or disable some basic operations of the microprocessor.
These flags and their functions are listed below.
Flag Bit Function
O The overflow flag is set to 1 when the result of a signed operation is too large to fit.
D This is directional flag. This is used in string related operations. D = 1, then the string will be
accessed from higher memory address to lower memory address, and if D = 0, it will do the reverse.
I This is interrupt flag. If I = 1, then MPU will recognize the interrupts from peripherals. For I = 0, the
interrupts will be ignored
T This trap flag is used for on-chip debugging. When T = 1, it will work in a single step mode. After
each instruction, one internal interrupt is generated. It helps to execute some program instruction by instruction.