Documente Academic
Documente Profesional
Documente Cultură
Introduction
8086 Addressing Modes
Introduction to Assembly Language
Programming
Instruction Set of 8086
DTM 2
Introduction
The 8086 has about 117 different instructions with about
300 opcodes.
The 8086 instruction sets can contain no operand, single
operand, and two operand instructions.
The 8086 instructions do not permit memory to memory
operations except for string instructions which involve
array operations.
The processor can access memory in different ways that
are collectively called addressing mode.
DTM 3
8086 Addressing Modes
The addressing modes describe the types of operands and
the way they are accessed for executing an instruction.
The number of addressing modes is determined when the
microprocessor is designed and cannot be changed. The
8086 provides a total of seven distinct addressing modes:
a) Register e) Based relative
b) Immediate f) Indexed relative
c) Direct g) Based indexed
d) Register indirect relative
MOV instructions are used to explain addressing modes.
DTM 4
8086 Addressing Modes
DTM 5
8086 Addressing Modes
DTM 6
8086 Addressing Modes
Solution: The contents of AX are moved into memory locations with logical
address DS: SI and DS: SI + 1;
therefore, the physical address starts at DS (shifted left) + SI = 13698. According
to the little endian convention, low address I3698H contains FE, the low byte,
and high address 13699H will contain 17, the high byte.
DTM 9
8086 Addressing Modes
Examples:
MOV CX, [BX]+10 ; move DS:BX+10 and
DS:BX+10+1 into CX. PA = DS*10H+ BX + 10,
MOV AL, [BP]+5 ;PA = SS*10H + BP + 5
Alternative codings for MOV reg1, [reg2]+const is MOV reg1,
[reg2+const] or MOV reg1, const[reg2] for instance, MOV CX,
[BX]+10 is same as "MOV CX, [BX+10]" or "MOV CX, 10[BX]"
DTM 11
8086 Addressing Modes
F) Indexed relative Addressing Mode: MOV reg1,
[reg2]+const or MOV [reg2]+const, reg1; const is an 8-bit
displacement value.
reg1 can be any general purpose register and reg2 can only
be either of DI or SI
PA= DS*10H+DI+const; EA=DI+ const or
PA=DS*10H+SI+const and EA= SI + const
Examples: MOV DX, [SI]+5
;PA = DS (shifted left) + SI + 5
MOV CL, [DI]+20 ;PA = DS (shifted left) + DI + 20
DTM 12
8086 Addressing Modes
Exercise 3-3: Assume that DS = 4500, SS = 2000, BX = 2100, SI =
1486, DI = 8500, BP = 7814, and AX = 2512. Show the exact physical
memory location where AX is stored in each of the following. All values
are in hex.
a)MOV [BX]+20,AX c)MOV [DI]+4,AX
b)MOV [SI]+10,AX d)MOV [BP]+12,AX
Solution: In each case PA = segment register (shifted left) + offset register +
displacement.
(a) DS:BX+20 location 47120 = (12) and 47121 =(25)
(b) DS:SI+10 location 46496 = (12) and 46497 = (25)
(c) DS:DI+4 location 4D504 = (12) and 4D505 = (25)
(d) SS:BP+12 location 27826 = (12) and 27827 = (25)
DTM 13
8086 Addressing Modes
G) Based Indexed relative Addressing Mode: MOV reg1,
[reg2][reg3]+const or MOV [reg2][reg3]+const, reg1; const is an 8-bit
displacement value.
reg1 can be any general purpose register and reg2 can only be
either of DI or SI and reg3 can only be either of BX or BP.
PA= DS*10H+BX+DI+const; EA=DI+BX+ const or
PA=SS*10H+BP+SI+const and EA= SI +BP+ const
Examples:
MOV CL,[BX][DI]+8 ;PA = DS (shifted left) + BX + DI + 8
MOV CH,[BX][SI]+20 ;PA = DS (shifted left) + BX + SI + 20
MOV AH,[BP][SI]+29 ;PA = SS (shifted left) + BP + SI + 29
Note that "MOV AX, [SI][DI]+displacement" is illegal.
DTM 14
8086 Addressing Modes
Summary of addressing modes
DTM 15
8086 Addressing Modes
Summary of addressing modes
DTM 16
8086 Addressing Modes
Summary of addressing modes
DTM 17
Quiz #1
Assume that the registers have the following values (all in hex) and that
CS= 1000, DS = 2000, SS = 3000, SI= 4000, DI= 5000, BX = 6080,
BP= 7000, AX= 25FF, CX = 8791, and DX= 1299. Give the
addressing mode for each of the following, calculate the physical
address of the memory where the operand is stored and the contents
of the memory locations in each of the following addressing examples.
1. MOV [SI],AL
2. MOV [DI+6],BX
3. MOV [BP][SI]+ 10,DX
4. MOV [3600],AX
5. MOV [BP]+200,AX
6. MOV [BX+SI+100],BX
DTM 18
Introduction to Assembly Language
Programming
Program execution in any microprocessor system consists
of fetching binary information from memory and
decoding that information to determine the instruction
represented.
For us it is much easier to remember the mnemonic SUB
AX,AX than the corresponding machine code 2BC0.
For this reason, we write source files containing all the
instruction mnemonics needed to execute a program.
DTM 19
Introduction to Assembly Language
Programming
The source file is converted into an object file, containing
the actual binary information the machine will
understand, by a special program called an assembler.
Some assemblers allow the entire source file to be written
and assembled at one time.
Other assemblers, called single-line assemblers, work with
one source line at a time and are restricted in operation.
The DEBUG utility that comes with DOS and Windows
(up to Windows XP) contains a built-in, single-line
assembler.
DTM 20
Introduction to Assembly Language
Programming
We use a cross-assembler instead of single-line assembler.
Figure 3.1 shows source program assembling process.
The source file in the example, TOTAL.ASM, is presented as
input to the assembler. The assembler will convert all source
statements into the correct binary codes and place these into
the object file TOTAL.OBJ.
A second file created by the assembler is the list file,
TOTAL.LST, which contains all the original source file text plus
the additional code generated by the assembler. The list file may
be displayed on the screen, or printed. The object file may not
be printed or displayed, since it is just code. next
DTM 21
Introduction to Assembly Language
Programming
DTM 22
Introduction to Assembly Language
Programming
The sample source file, a subroutine designed to find the
sum of 16 bytes stored in memory.
DTM 23
Introduction to Assembly Language
Programming
The list file for our example subroutine looks like this:
DTM 24
Introduction to Assembly Language
Programming
Assembler Directives ORG, SEGMENT, ENDS, ASSUME,
and END
When writing 8086 source files, we separate code areas
from data areas.
We may even have a separate area reserved for the stack.
The new source file for TOTAL, which includes
separate areas called segments, contains many new
pseudo codes to help the assembler generate the correct
machine code for the object file.
DTM 25
Introduction to Assembly Language
Programming
DTM 26
Introduction to Assembly Language
Programming
DTM 27
Introduction to Assembly Language
Programming
DTM 28
Introduction to Assembly Language
Programming
The first directive, .MODEL, instructs the assembler that
the type of program being created falls into a category
called SMALL.
All of the programs in this course are small programs.
Other models are as indicated in Table 3.4.
The .DATA directive indicates the beginning of a data
segment. There is no need to indicate the end of the data
segment. This is automatically assumed when the .CODE
directive is encountered, which begins the code segment.
DTM 29
Introduction to Assembly Language
Programming
DTM 30
Introduction to Assembly Language
Programming
When a large program must be written by a team of people, each
person will be assigned a few subroutines to write.
They must all assemble and test their individual sections to ensure
the code executes correctly.
When all portions of the program (called modules, after a
technique called modular programming) are assembled and tested,
their object files are combined into one large object file via a
program called a linker.
Figure 3.2 represents this process. The linker examines each object
file, determining its length in bytes, its proper place in the final
object file, and what modifications should be made to it.
In addition, a special collection of object files is sometimes
available in a library file.
DTM 31
Introduction to Assembly Language
Programming
DTM 32
Introduction to Assembly Language
Programming
When the linker is through, the final code is written to a file called
the load module. Another program called a loader takes care of
loading the program into memory.
Usually the linker and loader are combined into a single program
called a link-loader.
So, writing the source file is actually only the first step in a long
process.
But even before a source file can be written, the programmer must
understand the instructions that will be used in the source file.
The remaining sections will begin coverage of this important topic,
Instruction Set of 8086.
DTM 33
Instruction Set of 8086
The instruction set of the 8086 microprocessor is divided
into seven different groups:
i. Data transfer
ii. Strings
iii. Arithmetic
iv. Bit manipulation
v. Loops and jumps
vi. Subroutine and interrupt
vii. Processor control
DTM 34
Instruction Set of 8086
i. Data transfer Instructions: This group of instructions makes it
possible to move (copy) data around inside the processor and between
the processor and its memory.
a) MOV; MOV Destination, Source:
Transfer can be from register to register, register to memory or from memory
to register but not from memory to memory.
The source and destination must be of same type i.e. either both must be byte
or word.
MOV instruction does not affect any flags.
DTM 35
Instruction Set of 8086
DTM 36
Instruction Set of 8086
DTM 37
Instruction Set of 8086
DTM 39
Instruction Set of 8086
EXAMPLES:
PUSH BX; Decrement SP by 2, copy BX to stack
PUSH DS; Decrement SP by 2, copy DS to stack
PUSH AL; Illegal, must push a word
PUSH TABLE [BX]; Decrement SP by 2, copy word from memory in DS
at EA = TABLE + [BX] to stack
PUSHA ; Save all 16-bit registers onto the stack in the following order: AX,
CX, DX, BX, SP, BP, SI, DI. The value of the SP is that before the PUSHA
instruction.
PUSHF; copies the contents of the flag register to the stack.
DTM 40
Instruction Set of 8086
DTM 41
Instruction Set of 8086
DTM 42
Instruction Set of 8086
i. Data transfer Instructions:
d) POP; POP Destination: copies a word from the stack location
pointed to by the stack pointer to a destination specified in the
Instruction.
The destination can be a general-purpose register, a segment register,
or a memory location. The data in the stack is not changed.
After the word is copied to the specified destination, the stack
pointer is automatically incremented by 2 to point to the next word
on the stack.
No flags are affected by the POP instruction.
DTM 43
Instruction Set of 8086
EXAMPLES:
POP DX; Copy a word from top of stack to DX Increment SP by 2
POP DS; Copy a word from top of stack to DS Increment SP by 2
POP TABLE [BX]; Copy a word from top of stack to memory in DS
with EA = TABLE +[BX]
NOTE: POP CS Is illegal.
DTM 44
Instruction Set of 8086
DTM 45
Instruction Set of 8086
POPA Destination (Pop All Registers).
All general purpose registers are popped from the stack in the order indicated
in Table 3-6.
Note that the contents of the SP are not loaded with the data popped off the
stack.
This is necessary to prevent the stack from changing locations halfway through
the execution.
DTM 46
Instruction Set of 8086
DTM 47
Instruction Set of 8086
IN Accumulator, Port (Input Byte or word from Port):
Data read from an input port always ends up in the accumulator.
IN: copy data from a port. Port can be direct or indirect.
Indirect: If a full 16-bit port address must be specified, the port address is loaded into
register DX, and IN AL,DX or IN AX, DX is used to read the input port.
Direct; If the port number is between 00 and FFH, for instance, to input from port 80H
we would use IN AL,80H or IN AX,80H. Using AL in the operand field causes 8 bits of
data to be read. Two bytes can be input by using AX in the operand field.
DTM 48
Instruction Set of 8086
OUT Port, Accumulator (Output Byte or Word to Port):
With OUT, we can send 8 or 16 bits of data to an output port.
The port address may be loaded into DX for use with OUT DX, AL or
OUT DX, AX, or specified within the instruction, as in OUT 80H,AL or
OUT 80H, AX.
DTM 49
Instruction Set of 8086
i. Data transfer Instructions:
LEA Destination, Source (Load Effective Address):
Determines the offset of the variable or memory location named as the source
and loads this address in the specified 16-bit register.
Flags are not affected by LEA instruction.
DTM 50
Instruction Set of 8086
i. Data transfer Instructions:
LEA Destination, Source (Load Effective Address):
This instruction is used to load the offset of the source memory operand into
one of the processor's registers.
The memory operand may be specified by any number of addressing modes.
The destination may not be a segment register.
MOV BX, 35h
MOV DI, 12h
LEA SI, [BX+DI] ; SI = 35h + 12h = 47h
Note: The integrated 8086 assembler automatically replaces LEA with a more
efficient MOV where possible. For example:
org 100h
LEA AX, m ; AX = offset of m
RET
DTM m dw 1234h 51
END
Instruction Set of 8086
i. Data transfer Instructions:
XCHG Destination, Source (Exchange Data):
used to swap the contents of two 8-, or 16-bit operands.
One operand must be a processor register (excluding the segment registers). The other
operand may be a register or a memory location.
If a memory location is used as an operand it is assumed to be within a data segment.
Example: Registers AL and BL contain 30H and 40H, respectively. What is the result
of XCHG AL, BL?
Solution: After execution, AL contains 40 and BL contains 30.
The machine code for XCHG AL, BL is 86 C3. It may be interesting to note that the
machine code for XCHG BL. AL (which performs the same operation as XCHG AL,
BL) is 86D8.
DTM 52
Instruction Set of 8086
i. Data transfer Instructions:
XLAT Translate-Table (Translate Byte):
XLAT assumes that a 256-byte data table has been written into
memory at the starting address contained in register BX.
The number in register AL at the beginning of execution is used as an
index into the translation table.
The byte stored at the address formed by the addition of BX and AL
is then copied into AL.
The translation table is assumed to be in the data segment.
AL AL+BX+DS*10H
DTM o Only instruction adding an 8-bit to a 16-bit number. 53
Instruction Set of 8086
Example for XLAT Instruction
Assume DS=0300H, BX=0100h and AL=0DH, ODH represents the ASCII code
of the character CR.
The execution of XLAT replaces the contents of AL by the content of the
memory location with the physical address: Ph. A = DS*10H+BX
+AL=03000+0100+0DH=0310DH; Thus AL [0310DH].
Assuming this memory locations contains 52H (EBCDIC code for CR) this
value is placed in AL; Thus AL= 52H
Therefore, XLAT Performs the direct table lookup technique often used to
convert one code to another .
Extended Binary Coded Decimal Interchange Code (EBCDIC) is an 8-bit character
encoding used mainly on IBM mainframe and IBM midrange computer operating systems
DTM 54
Instruction Set of 8086
i. Data transfer Instructions:
Reading Assignment:
LDS
LES
LAHF and
SAHF
DTM 55
Instruction Set of 8086
ii. String Instructions:
A particularly nice feature of the 8086 is its ability to handle strings.
A string is a collection of bytes, or words that can be up to 64KB in
length.
An example of a string might be a sequence of ASCII character codes
that constitute a password, or the ASCII codes for Good Morning!.
The common operations that we can perform on any string are
copying, comparing and scanning.
DTM 56
Instruction Set of 8086
ii. String Instructions:
A special instruction called the repeat prefix can be used to repeat the copy,
compare, or scan.
Register CX contains the repeat count necessary for the repeat prefix. CX is
decremented during the string operation, which terminates when CX reaches 0.
SI register points to the first element of the source string, which must be located
in the data segment. The destination string is located in a similar way via the DI
register and must reside in the extra segment.
Direction flag (DF) is used to control the way SI and DI are adjusted during a
string instruction. They are automatically incremented or decremented by 1, or 2,
based on the value of the direction flag and the size of the string elements.
DTM 57
Instruction Set of 8086
.
DTM 58
Instruction Set of 8086
Initializing the String Pointers
Before we can use any string instruction, we have to set up the SI, DS, DI, and
ES registers. The source string (SHOPPER) in Figure 3-8 could be pointed to
by these instructions:
MOV AX, 510H ; string segment-address
MOV DS, AX
MOV SI, 0 ; string offset within segment
When the contents of SI and DS are combined to form an effective address,
05100H will be the first byte accessed in the data segment.
A similar technique is used to initialize the destination string (SHOPPING):
MOV AX, 4A8H ; string segment - address
MOV ES, AX
MOV DI, 0 ; string offset within segment
DTM 59
Instruction Set of 8086
REP/ REPE/ REPZ/ REPNE/ REPNZ: are available for use by the
programmer to control the way a string operation is repeated. They are all
recognized by the assembler as prefix instructions for string operations.
MOVS (move string) and STOS (store string) make use of the REP prefix.
When preceded by REP, these string operations repeat until CX
decrements to 0.
REPE and REPZ operate the same way, but are used for SCAS (scan
string) and CMPS (compare string). Each time SCAS or CMPS completes
its operation, the zero flag is tested and execution continues (repeats) as
long as the zero flag is set.
REPNE and REPNZ also repeat as long as CX does not equal 0 but
require that the zero flag be cleared to continue.
DTM 60
Instruction Set of 8086
DTM 61
Instruction Set of 8086
In each of the string instructions, the operand can be a byte or a word.
They are distinguished by the letters B (byte) and W (word) in the
instruction mnemonic.
DTM 62
Instruction Set of 8086
Example: Using string instructions, write a program that transfers a
block of 20 bytes of data.
.MODEL SMALL
.DATA
DATA1 DB ABCDEFGHIJKLMNOPQRST
DATA2 DB 20 DUP(?)
.CODE
;WRITE THE CODE
DTM 63
Instruction Set of 8086
Example: write a program that uses STOSB to store byte AAH into
100 memory locations. and uses STOSB to change 10th memory
location to 0DH.
.MODEL SMALL
.DATA
mem_location DB 100 (?)
.CODE
;WRITE THE CODE
DTM 64
Instruction Set of 8086
Assuming that there is a spelling of teff" in an electronic dictionary
and a user types in teaf", write a program that compares these two
and displays the following message, depending on the result:
1 . If they are equal, display "The spelling is correct".
2. If they are not equal, display "Wrong spelling".
.MODEL SMALL
.DATA
DATA_DICT DB "teff"
DATA_TYPED DB "teef"
MESSAGE1 DB "The spelling is correct",'$'
MESSAGE2 DB "Wrong spelling",'$'
.CODE
DTM 65
;write the code here
Instruction Set of 8086
iii. Arithmetic Instruction
This group of instructions provides the 8086 with its basic
integer math skills.
Floating point math operations are handled by a separate
group of instructions.
Addition, subtraction, multiplication, and division can all be
performed on different sizes and types of numbers.
DTM 66
Instruction Set of 8086
DTM 67
Instruction Set of 8086
ADD/ADC Instruction: ADD destination, source /ADC
destination, source.
These instructions add a number from some source to a number from
some destination and put the result in the specified destination.
ADC also adds the status of the carry flag into the result.
The source may be an immediate number, a register, or a memory
location. The destination may also be a register or a memory location.
The source and the destination in an instruction cannot both be
memory locations.
The source and the destination must be of the same type.
All Flags are affected: AF, CF, OF, PF, SF, ZF.
DTM 68
Instruction Set of 8086
DTM 69
Instruction Set of 8086
DTM 71
Instruction Set of 8086
Example:
Write a program that adds the following two multiword
numbers and saves the result: DATA1 =
548FB9963CE7H and DATA2 = 3FCD4FA23B8DH.
The result should be saved in DATA3.
DTM 72
Instruction Set of 8086
DTM 73
Instruction Set of 8086
DTM 74
Instruction Set of 8086
DTM 75
Instruction Set of 8086
DTM 76
Instruction Set of 8086
NEG This Instruction: Form 2s complement; NEG
Destination
This instruction replaces the number in a destination with the
2s complement of that number.
The destination can be a register or a memory location.
instruction can be implemented by inverting each bit and
adding 1 to it.
The flags that can be affected are: AF, CF, SF, PF, ZF and OF.
DTM 77
Instruction Set of 8086
DTM 78
Instruction Set of 8086
DTM 79
Instruction Set of 8086
DTM 80
Instruction Set of 8086
DTM 81
Instruction Set of 8086
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 the 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 affect AF, PF, SF, and ZF flags.
DTM 82
Instruction Set of 8086
DTM 83
Instruction Set of 8086
IMUL Instruction: IMUL source
This instruction multiplies an signed byte from source and
signed byte in AL register or signed word from source and
signed word in AX register.
The source can be a register or a memory location.
When the signed byte is multiplied by AL, the signed result is
stored in AX. The most significant byte is stored in AH and
least significant byte is stored in AL.
When the signed word is multiplied by AX, the most significant
word of result is stored in DX and least significant word of
result is stored in AX.
DTM 84
Instruction Set of 8086
If the magnitude of the product does not require all the bits
of the destination, the, unused bits will be filled with copies
of the sign bit.
If the upper byte of a 16-bit result or the upper word of a
32-bit result contains only copies of the sign bit (all 0's or
all 1's), then CF and the OF will both be 0.
If the upper byte of a 16-bit result or the upper word of a
32-bit result contains part of the product, CF and OF will
both be 1.
You can use the status of these flags to determine whether
the upper byte or word of the product needs to be kept.
AF, PF, SF, and ZF are undefined after IMUL.
DTM 85
Instruction Set of 8086
DTM 86
Instruction Set of 8086
[READING ASSINGEMENT]
DTM 87
Instruction Set of 8086
DTM 88
Instruction Set of 8086
DTM 91
Instruction Set of 8086
DTM 93
Instruction Set of 8086
DTM 95
Instruction Set of 8086
DTM 96
Instruction Set of 8086
AAS: ASCII adjust after subtraction
Used to subtract the ASCII codes for two decimal digits without masking the "3"
in the upper nibble of each.
The AAS instruction is then used to make sure the result is the correct unpacked
BCD. Some simple numerical examples will show how this works.
The AAS instruction leaves the correct unpacked BCD result in the low nibble
of AL and resets the upper nibble of AH to all 0's.
If you want to send the result back to a CRT terminal, you can OR AL with 30H
to produce the correct ASCII code for the result.
If multiple-digit numbers are being subtracted, the CF can be taken into account
by using the SBB instruction when subtracting the next digits.
The AAS instruction works only on the AL register. It updates AF and CF, but OF, PF, SF, and
ZF are left undefined.
DTM 97
Instruction Set of 8086
DTM 98
Instruction Set of 8086
DTM 99
Instruction Set of 8086
AAD: ASCII adjust before division
AAD converts two unpacked BCD digits in AH
and AL to the equivalent binary number in
This adjustment must be made before
dividing the two unpacked BCD digits in AX
unpacked BCD byte.
After the division, AL will contain the
unpacked BCD quotient and AH will contain
the unpacked BCD remainder.
PF, SF, and ZF are updated. AF, CF, and OF are
undefined after AAD.
DTM 100
Instruction Set of 8086
DTM 101
Instruction Set of 8086
DTM 102
Instruction Set of 8086
NOT Instruction; NOT Destination
o The NOT instruction inverts each bit (forms the 1's
complement) of the byte or word at the specified destination.
o The destination can be a register or a memory location.
o No flags are affected by the NOT Instruction.
EXAMPLES:
NOT BX ; Complement contents of BX register
NOT BYTE PTR [BX] ;Complement memory byte at offset [BX] in
data segment
DTM 103
Instruction Set of 8086
AND Instruction: AND Destination, Source
ANDs each bit in a source byte or word with the same number bit in a destination
byte or word. The result is put in the specified destination.
The contents of the specified source will not be changed.
A bit can be masked (reset) by ANDing it with 0.
The source operand can be an immediate number, the contents of a register, or the
contents of a memory location.
The destination can be a register or a memory location.
The source and the destination cannot both be memory locations in the same
instruction.
Flags: CF and OF are both 0 after AND. PF, SF, and ZF are updated by AND. AF
is undefined. Note that PF has meaning only for an 8-bit operand.
DTM 104
Instruction Set of 8086
EXAMPLES :
AND CX, [SI] ;AND word in DS at offset [SI] with word in CX register
;Result in CX register
AND BH, CL ;AND byte in CL with byte in BH Result in BH
AND BX, 00FFH ;AND word in BX with immediate 00FFH.
Masks upper byte, leaves lower byte unchanged
If BX = 10110011 01011110 then AND BX, 00FFH Mask out upper 8 bits
of BX and the Result: BX = 00000000 01011110 CF, OF, PF, SF, ZF = 0
DTM 105
Instruction Set of 8086
OR Instruction; OR Destination, Source
This instruction ORs each bit in a source byte or word with the corresponding bit in a
destination byte or word. The result is put in the specified destination.
The contents of the specified source will not be changed.
A bit in the destination operand can be set to a 1 by simply ORing that bit with a 1 in the same
bit of the source operand. A bit ORed with 0 is not changed.
The source operand can be an immediate number, the contents of a register, or the contents of
a memory location .
The destination can be a register or a memory location.
The source and the destination cannot both be memory locations in the same instruction.
Flags: CF and OF are both 0 after OR. PF, SF, and ZF are updated by the OR instruction. AF is
undefined after OR.
DTM 106
Instruction Set of 8086
EXAMPLES (SYNTAX):
OR AH, CL ; CL ORed with AH, result in AH. CL not changed
OR BP, SI ; SI ORed with BP, result in BP. SI not changed
OR SI, BP ; BP ORed with SI, result in SI. BP not changed
OR BL, 80H ; BL ORed with immediate 80H. Set MSB of BL to a 1
OR CX, TABLE [BX][SI] ; CX ORed with word from effective address TABLE[BX][SI] in
;data segment.
OR CX, 0FF00H ; If CX = 00111101 10100101 then OR CX with immediate
;FF00H, Result in CX = 11111111 10100101
;CF=0,OF=0,PF= 1,SF= 1,ZF=0.
DTM 107
Instruction Set of 8086
XOR Instruction; XOR Destination, source
This instruction Exclusive-ORs each bit in a source byte or word with the same
number bit in a destination byte or word.
The result replaces the contents of the specified destination. The contents of the
specified source will not be changed.
A bit Exclusive-ORed with a 1 will be inverted. A bit Exclusive-ORed with a 0
will not be changed.
The source operand can be an immediate number, the contents of a register, or
the contents of a memory .
The destination can be a register or a memory location. The source and
destination cannot both be memory locations in the same instruction.
Flags: CF and OF are both 0 after XOR. PF, SF, and ZF are updated. PF has
meaning only for an 8-bit operand. AF is undefined after XOR.
DTM 108
Instruction Set of 8086
EXAMPLES:
XOR CL,BH ;Byte in BH Exclusive-ORed with byte in CL. Result in CL.
;BH not chanced
XOR BP,DL ;Word in DI Exclusive-ORed with word in BP. Result
;in BP. DI not changed
XOR WORD PTR [BX], 00FFH ;Exclusive-OR immediate number
;OOFFH with word at offset [BXI in data
;segment. Result in memory location [BX]
; If BX = 0011110 01101001 , CX = 00000000 11111111
XOR BX,CX ; Result: BX = 0011110110010110 Note bits in lower
;byte are inverted CF,OF,SF,ZF = 0, PF = 1, AF
DTM 109
Instruction Set of 8086
TEST Instruction; TEST Destination, source
This instruction ANDs the contents of a source byte or word with the
contents of the specified destination word.
Flags are updated, but neither operand is changed.
The TEST instruction is often used to set flags before a Conditional Jump
instruction.
The source operand can be an immediate number, the contents of a register,
or the contents of a memory .
The destination operand can be in a register or in a memory location. The
source and the destination cannot both be memory locations in an
instruction.
Flags: CF and OF are both 0's after TEST. PF, SF, and ZF will be updated to
show the results of the ANDing. PF has meaning only for the lower 8 bits
DTM of the destination. AF will be undefined. 110
Instruction Set of 8086
DTM 111
Instruction Set of 8086
Shift and rotate instructions
PF, SF and ZF flags are affected by shift instructions but left unchanged
by the rotate instructions
Shift Instructions: (SHL, SHR, SAL, SAR)
SHL Shift Logical Left Instruction : SHL Target, Count ;
o SHL Target, 1 ;
o SHL Target, C L;
SHL mu lt ipl ies the nu mber sig ned o r unsigned by 2 on each
shift
The target can be a register or a memory
112 DTM
SHR Shift Logical Right Instruction : SHR Target , Count;
o SHR Target, 1;
o SHR Target, CL;
SHL di v ides t he n u mber s ig ned o r unsign ed by 2 on each
shif t
Th e t a r g e t c an b e a r e g i s t er o r a m e m o ry
Carry
SHR 0
Target
Ex: MOV AL,8
SHR AL,1
MOV BL,16
MOV CL,3
SHR BL,CL ;
113 DTM
Shift Instruction
SAL 0
Ex: MOV AL,8 Target
SAL AL,1
MOV BL,16
MOV CL,3
SAL BL,CL ;
11
4
Shift Instruction
Carry
SAR
Ex: MOV AL,6 Target
SAR AL,1
MOV BL,-16
MOV CL,3
SAR BL,CL ;
115 DTM
Example: A 16-bit unsigned number
X is present in AX register. Write a
program to compute 10*X (ten
multiplied by X), assuming that final
result can be filled in 16 bits.
116 DTM
Rotate Instruction
Carry Carry
ROR
ROL
Target Target
117 DTM
Rotate Instruction
RCL/RCR Rotate Through Carry Left (Right) Instruction :
o RCL/RC R Target, 1;
o RCL/RC R Target, CL;
Al l exc ep t i mm edia t e an d s e gment re g ister s.
Carry Carry
RCL RCR
Target Target
118 DTM
Rotate Instruction
Co u nt m ust b e 1 or CL .
Ex: MOV AL,6
SAR AL,1
MOV BL,-16
MOV CL,3
SAR BL,CL ;
119 DTM
Instruction Set of 8086
DTM 121
Instruction Set of 8086
JMP-Unconditional jump to Specified Destination
This instruction will always cause the 8086 to fetch its next
Instruction from the location specified in the instruction rather than
from the next location after the JMP instruction.
If the destination is in the same code segment as the JMP instruction,
then only the instruction pointer will be changed to get to the
destination location. This is referred to as a near jump.
If the destination for the jump instruction is in a segment with a
name different from that of the segment containing the JMP
instruction, then both the instruction pointer and the code segment
register contents will be changed to get to the destination location.
This is referred to as a far jump.
The JMP instruction affects no flags.
DTM 122
Instruction Set of 8086
DTM 123
Instruction Set of 8086
DTM 124
Instruction Set of 8086
Conditional Jump Instructions
Conditional jumps are always short jumps in the 8086.
These instructions will cause a jump to a label given in the
instruction if the desired conditions occurs in the program
before the execution of the instruction.
The destination must be in the range of -128 bytes to +127
bytes from the address of the instruction after the
conditional transfer instruction.
If the jump is not taken (jump condition is not fulfilled),
execution simply goes on to the next instruction.
DTM 125
Instruction Set of 8086
DTM 126
Instruction Set of 8086
DTM 127
Start
N1+N2
Store
Result<0 Yes result in
1103H
No
Store
Result=0 result in
Yes 1104H
No
Store
result in
1102H
DTM 128
End
Instruction Set of 8086
Solution:
MOV AL, [1100H] ; AL=N1
ADD AL, [1101H] ; AL=N1+N2
JS NEGATIVE
JZ NULL
MOV [1102H],AL; POSITIVE
; RESULT
JMP END
NEGATIVE: MOV [1103H], AL;
NEGATIVE ;RESULT
JUMP END
NULL: MOV [1104H], AL; NULL
RESULT
DTM
END: HLT 129
Instruction Set of 8086
DTM 131
Instruction Set of 8086
Examples
MOV BX, OFFSET PRICE ;Point BX at first element in array
MOV CX, 40 ;Load CX with number of elements in array
NEXT: MOV AL, [BX] ; Get elements from array
ADD AL, 07H ;Add correction factor
DAA ;decimal adjust result
MOV [BX], AL ; Put result back in array
LOOP NEXT ; Repeat until all elements adjusted.
DTM 133
Instruction Set of 8086
A near call is a call to a procedure, which is in the same
code segment as the CALL instruction.
When the 8086 executes a near CALL instruction, it
decrements the stack pointer by 2 and copies the offset of
the next instruction after the CALL onto the stack. This
offset saved on the stack is referred to as the return address.
A near CALL instruction will also load the instruction
pointer with the offset of the first instruction in the
procedure.
A RET instruction at the end of the procedure will return
execution to the instruction after the call by copying the
offset saved on the stack back to IP.
DTM 134
Instruction Set of 8086
A far call is a call to a procedure, which is in a different segment
from the one that contains the CALL instruction.
When the 8086 executes a far call, it decrements the stack
pointer by 2 and copies the contents of the CS register to the
stack. It then decrements the stack pointer by 2 again and copies
the offset of the instruction after the CALL instruction to the
stack.
Finally, it loads CS with the segment base of the segment, which
contains the procedure, and loads IP with the offset of the first
instruction of the procedure in that segment.
A RET instruction at the end of the procedure will return
execution to the next Instruction after the CALL by restoring
DTM
the saved values of CS and IP from the stack. 135
Instruction Set of 8086
DTM 136
Instruction Set of 8086
DTM 141
Instruction Set of 8086
Special Instruction (software Interrupt)
Intel 8086 supports special instruction for interrupt,
called INT, to execute special program.
Example: INT 21H, INT 10H
Condition Produced by Instruction (software Interrupt)
Intel 8086 can be interrupted by some condition
produced by execution of an instruction.
Example: MOV BL, 00H then DIV BX ; divide by
zero ISR is executed
DTM 142
Instruction Set of 8086
DTM 144
Instruction Set of 8086
DTM 145
Instruction Set of 8086
Interrupt Vector
This vector gives us information about the
location of an I.S.R. in the memory.
An interrupt vector is a 4-byte number. The first
two bytes give us the value of IP (offset) for the
I.S.R, where the last two bytes contain the value
of CS register.
The physical address (20 bits) is defined by :
CS*10h + IP
DTM 146
Instruction Set of 8086
DTM 147
Instruction Set of 8086
DTM 148
Instruction Set of 8086
Examples:
INT 35; New IP from 0008CH, new CS from 0008EH
Example of MS-DOS interrupts
INT 21h /AH=1 read character from standard
input, with echo, result is stored in AL. If there is no
character in the keyboard buffer, the function waits
until any key is pressed.
Example: MOV AH,1
INT 21h
DTM 149
Instruction Set of 8086
DTM 151
Instruction Set of 8086
Example of BIOS interrupt
INT 16H/ AH=00H get keystroke from keyboard (no echo).
AH= BIOS scan code.
AL= ASCII character.
(if a keystroke is present, it is removed from the
keyboard buffer).
DTM 153
Instruction Set of 8086
STD: sets the direction flag. It doesnt affect any other flag.
CLD: reset the DF. It doesnt affect any other flag.
STI: sets the interrupt flag to one. This enables INTR
interrupt of the 8086. It doesnt affect any other flag.
CLI: resets the interrupt flag to zero. Due to this 8086 will
not respond to an interrupt signal on its INTR input. It
doesnt affect any other flag.
DTM 154
Instruction Set of 8086
DTM 156
Instruction Set of 8086
ESC-Escape
This instruction is used to pass instructions to a coprocessor,
such as the 8087-math coprocessor, which shares the address
and data bus with an 8086.
Instructions for the coprocessor are represented by a 6-bit
code embedded in the escape instruction.
As the 8086 fetches instruction bytes, the coprocessor also
catches these bytes from the data bus and puts them in its
queue. However, the coprocessor treats all the normal 8086
instructions as NOPs.
DTM 157
Instruction Set of 8086
DTM 158
Instruction Set of 8086
DTM 159
Instruction Set of 8086
DTM 160
Instruction Set of 8086
NOP-Perform No Operation
This instruction simply uses up three clock cycles
and Increments the instruction pointer to point
to the next instruction.
NOP affects no flags. The NOP instruction can
be used to increase the delay of a delay loop.
When hand coding a NOP can also be used to
hold a place in a program for an instruction that
will be added later.
DTM 161
DTM 162