8086 Architecture and Instruction Set
8086 Architecture and Instruction Set
[Link]
Assistant Professor
Department of Electronics and Communication Engineering,
Thiagarajar College of Engineering, Madurai – 15.
Chapter 3
X86
3
8086 Architecture
Architecture [1]
• 8086 supports a 16 bit ALU, a set of 16 bit registers
and provides segmented memory addressing
capability, a rich instruction set, powerful interrupt
structure, fetched instruction queue for overlapped
fetching and execution etc.
• The internal block diagram, shown in Fig. 1.2
describes the overall organization of different units
inside the chip.
• The complete architecture of 8086 can be divided
into two parts
– Bus Interface Unit (BIU) and
– Execution Unit (EU).
• The bus interface unit contains the circuit for
physical address calculations and pre-decoding
instruction byte queue (6 bytes long).
5
6
Architecture [2]
• The bus interface unit makes the system’s bus
signals available for external interfacing of the
devices. In other words, this unit is responsible for
establishing communication with external devices
and peripherals including memory via the bus.
8
Example:
Architecture [3]
• Thus, the segment addressed by the segmented value
1005H can have offset values from 0000H to FFFFH within it,
i.e. maximum 64K locations may be accommodated in the
segment.
• Thus, the segment register indicates the base address of
particular segment, while the offset indicates the distance of
the required memory location in the segment from the base
address.
• Since the offset is 16 bit number, each segment can have a
maximum of 64K locations.
• The bus interface unit has a separate adder to perform this
procedure for obtaining a physical address while addressing
memory.
• The segment address value is to be taken from appropriate
segment register, while the offset may be the content of IP,
BX, SI, DI, SP,BP or an immediate 16 bit value, depending on
addressing mode.
10
Architecture [4]
• In the case of 8085, once the opcode is fetched and
decoded, the external bus remains free for some time, while
the processor internally executes the instruction.
• This time slot is utilized in 8086 to achieve the overlapped
fetch and execution cycles.
• While the fetched instruction is executed internally, the
external bus is used to fetch the machine code of the next
instruction arrange it in a queue known as precoded
instruction byte queue.
• It is a 6 byte long, first in first out structure. The instructions
from the queue are taken for decoding sequentially.
• The execution unit contains the register set of 8086 except
segment register and IP. It has a 16 bit ALU, able to perform
arithmetic and logical operations. The 16 bit flag register
reflects the result of execution by the ALU.
11
Register organization of 8086 [1]
• 8086 has a powerful set of registers known as
general purpose and special purpose registers.
• All of them are 16 bit registers.
• The general purpose registers, can be used as
either 8 bit registers or 16 bit registers.
• These Registers can be either used for holding data,
variables and intermediate results temporarily or for
other purposes like a counter or for sorting offset
address for some particular addressing mode etc.
• The special purpose registers are used as segment
registers, pointers, index registers or as offset
storage registers for particular addressing modes.
12
Register Organization of 8086 [2]
13
General Data Register
• The registers AX, BX, CX and DX are the general
purpose 16 bit registers.
– Usually the letters L and H specify the lower and higher bytes of particular
register.
– The letter X used specify the complete 16 bit register.
15
Segment Registers [2]
• The Stack segment register is used for addressing
stack segment of memory i.e. memory which is used
to store stack data.
• While addressing any location in the any memory
bank, the physical address is calculated from two
parts.
– The first is segment address and
– second is offset.
• The segment registers contain 16 bit segment base
addresses, related to different segments.
• Any of the pointers and index registers or BX may
contain offset of the location to be addressed.
16
Segment Registers [3]
• The advantage of this scheme is that instead
of maintaining a 20 bit register for a physical
address, the processor just maintains two 16
bit registers which are within the word length
capacity of the machine.
• Thus the CS, DS, SS and ES segment
registers, respectively; contain the segment
addresses for the code, data, stack and extra
segments of memory.
• It may be noted that all these segments are
the logical segments.
17
Pointers and Index Registers
• The pointers contain offset within the particular
segments.
• The pointers IP, BP and SP usually contain offsets
within the code, and stack (BP & SP) segments.
• The index registers are used as general purpose
registers as well as for offset storage in case of
indexed, based indexed and relative based indexed
addressing modes.
• The register SI is generally used to store the offset
of source data in data segment while the register DI
is used to store the offset of destination in data or
extra segment.
18
Flag Register [1]
• The 8086 flag register contents indicate the
results of computations in the ALU.
• It also contains some flag bits to control the
CPU operations.
19
Flag Register [2]
• 8086 has a 16 bit flag register which is divided into two parts,
– Condition code or status flag and
– Machine control flags.
• This part of the flag register of 8086 reflects the results of the
operations performed by ALU.
22
Memory Segmentation
Memory Segmentation [1]
• The memory in an 8086/8088 based system is
organized as segmented memory (1Mbytes of
physical memory).
• In this scheme, the complete physically available
memory may be divided into a number of logical
segments.
• Each segment is 64K bytes in size and is addressed
by one of the segment registers.
• The 16 bit contents of the segment register actually
point to the starting location of a particular segment.
To address a specific memory location within a
segment, we need an offset address.
• The offset address is also 16 bit long so that the
maximum offset value can be FFFFH, and the
maximum size of any segment is thus 64K locations.
24
Memory Segmentation [2]
• The addresses of the segments may be assigned as
0000H to F000H respectively.
• The offset address value s from 0000H to FFFFH so
that the physical addresses range from 0000H to
FFFFH.
• In the above said case, the segments are non
overlapping segments.
• In some cases, however, the segments may be
overlapping.
• Suppose a segment starts at a particular address
and its maximum size can be 64Kbytes. But, if
another segment starts before this 64Kbytes
location of the segment, the two segments are said
to be overlapping segments.
25
Memory Segmentation [3]
• The main advantages of the segmented
memory scheme are as follows:
– Allows the memory capacity to be 1Mbytes
although the actual addresses to be handled are
of 16 bit size
– Allows the placing of code, data and stack
portions of the same program in different parts
(segments) of memory, for data and code
protection
– Permits a program and/or its data to be put into
different areas of memory each time the program
is executed, i.e. provision for relation is done.
26
Memory Segmentation [4]
28
PIN Diagram or PIN OUT or Signal description
INTEL 8086 - Pin Diagram
Signal Description Of 8086 [1]
• The microprocessor 8086 is a 16-bit CPU available in three
clock rates, i.e. 5, 8 and 10MHz, packaged in 40 pin CERDIP
or plastic package.
• The 8086 operates in single processor or multiprocessor
configuration to achieve high performance.
• Some of the pin serve a particular function in minimum mode
(single processor mode) and others function in maximum
mode (multiprocessor mode) configuration.
• The 8086 signal can be categorized in three groups.
– The first are the signals having common function in minimum as well
as maximum mode,
– The second are the signals which have special function for minimum
mode and
– The third are the signals having special function for maximum mode.
31
Signal Description Of 8086 [2]
• AD15-AD0:
– These are the time multiplexed memory I/O address and
data line. Address remains on the lines during T1 state,
while the data is available on data bus during T2,T3,Tw
and T4.
32
Signal Description Of 8086 [3]
• BHE /S7-Bus Enable/status: the bus high enable signal is
used to indicate the transfer of data over the higher order
(D15-D8) data bus shown in table 1.2. it goes low for the data
transfers over D15-D8 and is used to desire chip selects of
odd address memory bank or peripherals.
• RD bar-Read: Read signal, when low, indicates the
peripherals that the processor is performing a memory or I/O
read operation.
• INTR- Interrupt Request : This is a level triggered input. This
is sampled during the last clock cycle of each instruction to
determine the availability of the request. If any interrupt
request is pending, the processor enters the interrupt
acknowledge cycle. The can be internally masked by
resetting the interrupt enable flag.
33
Signal Description Of 8086 [4]
• TEST bar: This input is examined by a WAIT instruction. If the TEST bar
input goes low, execution will continue , else the processor remains in an
idle state.
• NMI-Non Maskable Interrupt: This is an edge-triggered input which
causes a Type 2 interrupt. The NMI is not maskable internally by software.
• RESET: This input causes the processor to terminate the current activity
and start execution from FFFF0H.
• CLK-Clock Input: The clock input provides the basic timing for processor
operation and bus control activity. It’s an asymmetric square wave 33%
duty cycle. The range of frequency for different 8086 versions is from
5MHz to 10MHz.
• Vcc : +5V power supply for the operation of the internally circuit.
• GND: ground for the internally circuit.
• MN/MX bar: The Logic level at this pin decides whether the processor is
to operate in either minimum (single processor) or maximum
(multiprocessor) mode.
34
Instruction format
Machine Language Instruction Format
• A machine language instruction format has one or more
number of fields associated with it.
36
Machine Language Instruction Format
1. One byte instruction:
− This format is only one byte long may have the implied data or
register operands.
− The least significant 3-bits of the opcode are used for specifying the
register operands, if any.
− Otherwise, all the 8-bits form an opcode and the operands are
implied.
2. Register to Register:
− This format is 2 bytes long.
− The first byte of code specifies the operation code and width of the
operand specified by w bit.
− The second byte of the code shows the register operands and R/M
field.
38
Machine Language Instruction Format
Register to Register:
39
Machine Language Instruction Format
• The MOD, R/M, REG and the W field are given as:-
40
Machine Language Instruction Format
• The opcode usually appears in the first byte, but in the few
instruction, a register destination is in the first byte and few
other instruction may have their three bits of opcode in the
second byte.
• The opcodes have the single bit indicator.
• Their definition and significance are given as follows:
– W-bit :
• This indicates whether the instruction is operate over
an 8-bit 16-bit data /operands.
• If W bit is zero, the operand is of 8-bit and if W is 1, the
operand is of 16-bits.
– D-bit:
• This is valid in case of double operand instruction.
• One of the operands must be a register specified by
REG field.
• The register specified by REG is source operand if
D=0, else, it is a destination operand.
41
Machine Language Instruction Format
• S-bit :
− This bit is called sign extension bit. The S bit is used
along with W-bit to show the type of operation.
− 8-bit operation with 8-bit immediate operand is
indicated by S=0, W=0;
− 16-bit operation with 16 bit immediate operands is
indicated by S=0, W=1 and
− 16 bit operation with a sign extended immediate data is
given by S=1, W=1
• V-bit:
− This is used in case of shift and rotate instruction.
− This bit is set to 0, if shift count is one and is set to 1,if
CL contains the shift count.
42
Machine Language Instruction Format
• Z-bit:
− This bit is used by REP instruction to control the loop.
− If Z-bit is equal to1,the instruction with REP prefix is executed until
the zero flag matches the Z-bit.
– Please note that usually all the addressing modes have DS as the default
data segment.
– However, the addressing modes using BP and SP have SS as the default
segment register.
43
Machine Language Instruction Format
• Assignment of codes with different registers
44
Addressing Modes of 8086
Addressing Modes Of 8086 [1]
• Addressing mode indicate a way of locating data or
operands.
• Depending upon data types used in the instruction and the
memory addressing modes, any instruction may belong to
one or more addressing modes, or some instruction may not
belong to any of the addressing mode.
• Thus the addressing modes describes the types of operand
and the way they are accessed foe executing an instruction.
• According to the flow of instruction execution,
• The instruction may be categorized as
– Sequential control flow instruction and
– Control transfer instruction.
• Sequential execution, transfer control to the next instruction
appearing immediately after it (in the sequence) in the
program.
• The control transfer instruction, on the other hand, transfer
control to some predefined address or the address somehow
specified in the instruction after their execution.
46
Addressing Modes Of 8086 [2]
1. Immediate In this type of addressing, immediate data is a
part of instruction ,and appears in the form of the
successive byte.
Example: MOV AX,0005H
47
Addressing Modes Of 8086 [3]
3. Register: In the register addressing mode, the data is
stored in a register and it is referred using the particular
register. All the register, except IP, may be used in this
mode.
Example: MOV BX, AX
48
Addressing Modes Of 8086 [4]
5. Indexed: In this addressing mode offset of the
operand is stored in one of the index register.
– DS is the default segment for index register SI and DI.
– In case of string instruction DS and ES are default
segment for SI and DI respectively.
Example: MOV AX,[SI]
– Here data is available at an offset address stored in SI in
DS.
5. Register Relative: In this addressing mode the
data is available at an effective address formed by
adding an 8-bit or 16-bit displacement with the
content of any one of the register BX, BP,SI and DI
in the default (either DS or ES) segment.
Example: MOV AX,50H[BX]
– Here the effective address is given as
10H*DS+50H+[BX].
49
Addressing Modes Of 8086 [5]
7. Based Indexed: The effective address of data is formed, in this addressing mode,
by adding content of base register (any on e of BX or BP) to the content of an
index register (any one of SI or DI). The default segment register may be ES or
DS.
50
Addressing Modes Of 8086 [6]
9. Intrasegment direct mode: In this mode, the address to
which the control is to be transferred lies in the same
segment in which the control transfer instruction lies and
appear directly in the instruction as an immediate
displacement value.
– In this addressing mode, the displacement is computed relative to the
content of the instruction pointer IP.
– The effective address to which the control will be transferred is given
by the sum of 8 or 16 bit displacement and current content of IP.
– In case of jump instruction,
• If the signed displacement (d) is of 8 bits (i. e. -128<d<+127), it as short
jump and
• If it is of 16 bit (i. e. -32767<d<+32767), it is termed as long jump.
– JMP SHORT LABLE lies within -128 to +127 from the current IP
content.
– Thus SHORT LABLE is 8-bit signed displacement.
51
Addressing Modes Of 8086 [7]
10. Intrasegment Indirect Mode: In this mode, the displacement to which the
control is to be transferred, is in the same segment in which the control transfer
instruction lies ,but it is passed to the instruction indirectly. Here the branch
address is found as the content of a register or memory location. This addressing
mode may be used in the unconditional branch instruction.
11. Intersegment Direct: In this mode, the address to which the control is to be
transferred is in a different segment. This addressing mode provides a means of
branching from one code segment to another code segment. The CS and IP of the
destination address are specified directly in the instruction.
12. Intersegment indirect: In this mode the address to which the control is to be
transferred lies in a different segment and it is passed to the instruction indirectly,
i. e. IP(LSB) and CS (MSB) sequentially.
55
Instruction Set of 8086
Instruction Set Of 8086/8088
• The categories of 8086/8088 instructions are.
SS
61
Data copy/Transfer Instruction
• POP: Pop from stack
– This instruction when executed, loads the specified register/ memory location with the
content of the memory location of which the address is formed using the current stack
segment and stack pointer as usual.
– The stack pointer is incremented by 2.
– The POP instruction serves exactly opposite to PUSH instruction.
• The sequence of operation is as below:
– Contents of stack memory location is stored in AL and SP incremented by one.
– Further contents of memory location pointed by SP are copied to AH and SP is again
incremented by 1.
– Effectively SP is incremented by 2 and points to next stack top.
• The example of these instructions are shown follows
– POPAX
– POP DS
– POP [5000H]
SP
SP+1
AH AL SP+2
62
Data copy/Transfer Instruction
• XCHG: Exchange
– This instruction exchanges the contents of the specified source and
destination operands, which may be registers or one of them may be a
memory location.
– However, exchange of contents of two memory locations is not permitted.
Immediate data also not allowed in these instructions .
63
Data copy/Transfer Instruction
• OUT: Output to the Port
– This instruction is used for writing to an output port.
– The address of the output port may be specified in the instruction
directly or implicitly in DX.
– Contents of AX or AL are transferred to a directly or indirectly
addressed port after execution of this instruction.
– The data to an odd addressed port is transferred on D8- D15 while
that to an even addressed port is transferred on D0-D7.
– The registers AL and AX are the allowed source operands for 8 bit and
16 bit operations respectively.
– If the address is of 16 bits it must be in DX.
– OUT 03H, AL; This sends data available in AL to a port whose address
is 03H.
– OUT DX,AX; This sends data available in AX to a port whose address
is specified implicitly in DX.
• XLAT: Translate
– The translate instruction is used for finding out the address codes in
case of code conversion problem, using look up table technique.
64
Data copy/Transfer Instruction
• LEA: Load effective address
– The load effective address instruction loads the effective address
formed by destination operand into the specified source register.
• LEA BX, ADR; Effective address of label ADR i.e. offset of ADR will
be transferred to Register BX.
• LEA SI, ADR [BX]; offset label ADR will be added to content of BX
to form effective address and it will be loaded in SI.
• LDS/LES: Load pointer to DS/ES
– This instruction loads the DS or ES register and the specified destination
register in the instruction with the content of memory location specified as
source in the instruction.
– Ex: LDS BX, 5000H/LES BX, 5000H
65
Data copy/Transfer Instruction
• LAHF: Load AH from lower byte of flag
– This instruction loads the AH register with the lower byte of the flag register.
– This command may be used to observe the status of all the condition code flags
(except overflow) at a time.
66
Arithmetic instructions
• These instructions perform the arithmetic operations, like addition,
subtraction, multiplication and division along with the respective ASCII
and decimal adjust instructions.
• The increment and decrement also belong to this type of instructions.
• ADD: Add
– This instruction adds the immediate data or contents of the memory locations
specified in the instruction or a register (source ) to the contents of the
another register (destination) or memory location.
– The result is in the destination operand. However, both the source and
destination operands can not be memory operands.
– That means memory to memory addition is not possible. Also contents of
segment register can not be added using this instruction.
– Flag register entries will get affected due to this istruction.
– Examples:
• INC : Increment
– This instruction increases the contents of the specified register or
memory location by 1.
– All the condition code flags are affected except the carry flag CF.
– This instruction adds 1 to the contents of the operand.
– Immediate data cannot be operand of this instruction.
– INC AX Register
– INC [BX] Register Indirect
– INC [5000H] Direct 82
Arithmetic instructions
• DEC: Decrement :
– The decrement instruction subtracts 1 from the contents of the specified
register or memory location.
– All the condition code flags, except the carry flag, are affected depending
upon the results.
– Immediate data cannot be operand of this instruction.
– DECAX Register
– DEC [5000H] Direct
• SUB: Subtract
– The subtract instruction subtracts the source operand from the destination
operand and result is left in destination operand.
– Source operand may be a register, memory location or immediate data and
the destination operand may be a register or a memory location, but source
and destination operands both must not be memory operands.
– Destination operand can not be an immediate data.
– All the condition code flags are effected by this instruction.
71
AAA
OR AX, 3030H
Arithmetic instructions
– If the lower digit of AL is between 0 and 9 and AF is set, 06 is added to AL.
– The upper 4 bits of AL are cleared and AH is incremented by one.
– If the value in lower nibble of AL is greater than 9 then the AL is incremented
by 06,AH is incremented by 1 the AF and CF flags are set to 1, and the higher
4 bits of AL are cleared to 0.
– The remaining flags are unaffected.
– The AH is modified as sum of previous content (usually 00) and carry from the
adjustment.
– This instruction does not give exact ASCII codes of the sum, but they can be
obtained by adding 3030H to AX.
– MOV AL , 04 ; AL ← 04
– MOV BL, 09 ; BL ← 09
– MUL BL ; AH-AL ← 24H(9*4)
– AAM ; AH ←03; AL ← 06
78
Arithmetic instructions
• AAD:ASCII Adjust before Division
– Though the names of these two instruction (AAM and
AAD) appear to be similar there is a lot of difference
between their function.
– The AAD instruction converts two unpacked BCD digits in
AH and AL to the equivalent binary number inAL.
– This adjustment must be made before dividing the two
unpacked BCD digits in AX by an unpacked BCD byte.
– PF, SF, ZF are modified while AF, CF, OF are undefined,
after the execution of instruction AAD.
– In the instruction sequence this instruction appears before
DIV instruction unlike AAM appears after MUL.
– The examples explains the execution of the instruction.
– Let AX contents as 0508H
– AAD; Result in AX will be 003AH
79
Arithmetic instructions
• DAA: Decimal Adjust Accumulator
– This instruction is used to convert the result of addition of two packed
BCD number to a valid BCD number.
– The result has to be only in AL, if the lower nibble is greater than 9,
after addition or if AF is set, it will add 06 to the lower nibble inAL.
– After addition 06 in the lower nibble of AL if the upper nibble of AL is
greater than 9 or if carry flag is set, DAA instruction adds 60H to AL.
– The instruction DAA affects AF, CF ,PF and ZF flags. The OFis
undefined.
• DAS: Decimal Adjust after Subtraction
– This instruction converts the result of subtraction of two packed BCD
numbers to a valid BCD number.
– The subtraction has to be in AL only, if the lower nibble of AL is greater
than 9 this instruction will subtract 06 from lower nibble ofAL.
– If the result of subtraction sets the carry flag or if upper nibble is
greater than 9 it subtracts 60H fromAL.
– This instruction modifies the AF, CF, SF, PF and ZF flags. The OF is
undefined after DAS instruction.
80
Arithmetic instructions
• NEG: Negate
– The negate instruction forms 2’s complement of the specified destination in
the instruction.
– For obtaining 2’s complement it subtracts the component of destination from
zero.
– The result is stored back in the destination operand which may be a register
or a memory location.
– If OF is set it indicates that the operation could not be completed successfully.
This instruction affects all the condition code flags.
81
Arithmetic instructions
• IMUL: Signed multiplication
– This instruction multiplies a signed byte in source operand by a signed byte in
AL or a signed word in source operand by a signed word in AX.
– The source can be a general purpose register, memory operand, index
register or base register, but it cannot be immediate data.
– In case of 32 bit result , the higher order word (MSW) is stored in DX and
lower order word is stored in AX.
– The AF, PF, SF and ZF flags are undefined after IMUL.
– If AH and DX contain parts of 16 and 32 bit result respectively CF and OF
both will be set.
– The AL and AX are the implicit operand in case of 8bits and 16bits
multiplication respectively.
– Example
– IMUL BH
– IMUL CX
– IMUL [SI]
• CBW: Convert Signed Byte or Word
– This instruction converts a signed byte to a signed word.
– In other words, it copies the sign bit of a byte to be converted to all bits in the
higher byte of the result word.
• CWD: Convert Signed Word to Double word
– This instruction copies the sign bit of AX to all the bits of DX register.
82
Arithmetic instructions
• DIV: Unsigned Division
– This instruction performs unsigned division.
– It divides an unsigned words or double word by a 16bit or 8bit operand.
– The dividend must be in AX for 16-bit operation and divisor may be specified
using any one of the addressing modes except immediate.
– The result will be in AL while AH will contain the remainder.
– In case of double word dividend (32-bit) the higher word should be in DX and
lower word should be in AX.
– The divisor may be specified as already explained.
– The quotient and the remainder, in this case will be in AX and DX respectively.
– This instruction does not affect any flag.
• OR: Logical OR
– The OR instruction carries out the OR operation in the same way as described
in case of AND operation.
– The limitation on source and destination operands are also the same as in the
case of AND operation.
– Examples
– OR AX, 0098H
– OR AX, BX
– OR AX, [5000H]
84
Logical instructions
• NOT: Logical Invert
– The NOT instruction complements the content of an operand register or a
memory location, bit by bit.
• NOT AX
• NOT [5000H]
87
Logical instructions
88
Logical instructions
• ROL: Rotate Left without Carry
• This instruction rotates the content of destination operand to the left by the specified
count (bit-wise) excluding carry.
• The most significant bit is pushed in to the carry flag as well as the least significant
bit position at each operation.
• The remaining bits are shifted left subsequently by the specified count position.
• The PF, SF and ZF flags are left unchanged in this rotate operation.
• The operand may be a register or a memory location.
89
Logical instructions
ROR: Rotate Right
without Carry
90
String Manipulation Instruction
• A series of data bytes or words available in memory at consecutive
location, to be referred to collectively or individually, are called as byte
string or word string.
• For example a string of a character may be or located in consecutive
memory location where each character may be represented by itsASCII
equivalent.
• For referring to string, two parameters are required (a) starting or end
address of string (b) length of string.
• The length of string usually stored as a count in CX register.
• The incrementing or decrementing of pointer in case of 8086 string
instruction depends upon the direction flag status.
• If it is a byte string operation the index register are updated by one.
• If it is word string operation the index register are updated by two.
• The counter in both cases, is decremented by one.
– After the MOVS instruction is executed once, the index register are
automatically updated and CX is decremented.
– The incrementing or decrementing of pointer i.e. SI and DI depend upon the
direction flag DF.
– If DF is 0, the index register are incremented, otherwise they are decremented
in case of all the string manipulation instruction.
92
String Manipulation Instruction
1. Example for MOVSB 2. Example for MOVSB
– MOV AX, 2000H – MOV AX, 5000H
– MOV DS, AX – MOV DS, AX
– MOV AX, 3000H – MOV AX, 3000H
– MOV ES,AX – MOV ES,AX
– MOV CX, 00FFH – MOV CX, 00FFH
– MOV SI, 1000H – LEA SI, SOURCE_STRING
– MOV DI, 2000H – LEA DI, DESTIN_STRING
– CLD – CLD
– REP MOVSB – REP MOVSB
93
String Manipulation Instruction
• CMPS: Compare String Byte or String
– The CMPS instruction can be used to compare two string of bytes or
words.
– The length of the string must be stored in a register CX.
– If both the byte or word string are equal, zero flag is set.
– The flags are affected in same way as CMP instruction.
– The DS:SI point to the two string.
– The REP instruction prefix is used to repeat the operation till CX
becomes zero
• SCAS: Scan String Byte or String Word
– This instruction scans string of byte or words for an operand byte or
word specified in the register AL orAX.
– The string is pointed to by ES; DI register pair.
– The length of the string is stored in CX.
– The DF controls the mode for scanning of the string.
– Whenever a matched to the specified operand, is found in the string,
execution stops and zero flag is set.
– If no match is found, the zero flag is reset.
94
String Manipulation Instruction
• Example for CMPSB 2. Example for SCAS
– MOV AX, 2000H
– MOV DS, AX – MOV AX, 5000H
– MOV AX, 3000H – MOV ES, AX
– MOV ES,AX – MOV CX, 00FFH
– MOV CX, 00FFH – LEA DI, DESTIN_STRING
– MOV SI, 1000H – CLD
– MOV DI, 2000H – REPNE SCASW
– CLD
– REP CMPSB
95
String Manipulation Instruction
• LODS: Load String Byte or String Word
– The LODS instruction loads the AL /AX register by the content of a string pointed to by
DS;SI register pair.
– The SI is modified automatically depending upon DF.
– The DF plays exactly to same role as in case of MOVSB/MOVSW instruction.
– If it is a byte transfer (LODSB), the SI is modified by one and if it is a word transfer
(LODSW ), the SI is modified bye two.
– No other flags are affected by this instruction.
96
Control transfer or Branching Instruction
• The control transfer instructions transfer the flow of execution of the
program to a new address specified in the instruction directly or indirectly.
97
Control transfer or Branching Instruction
• Unconditional Branch Instructions
– CALL Unconditional Call
• This instruction is used to call a subroutine from a main program.
• There are again two types of procedures depending upon whether
it is available in the same segment (Near CALL, i.e. +/- 32K
displacement) or in other segment (FAR CALL, i.e. anywhere
outside the segment).
• The modes for them are called as intrasegment and intersegment
addressing modes respectively.
• On execution, this instruction stores the incremented IP (i.e.
address of the next instruction) and CS onto the stack and loads
the CS and IP registers, respectively, with the segment and offset
addresses of the procedure to be called.
• In case of NEAR CALL it pushes only IP register and in case of
FAR CALL it pushes IP and CS both onto the stack.
• The NEAR and FAR CALLS are discriminated using opcode.
98
Control transfer or Branching Instruction
• RET: Return From the Procedure
– At each CALL instruction, the IP and CS of the next instruction is
pushed onto stack, before the control is transferred to the procedure.
– At the end of the procedure, the RET instruction must be executed.
– When it is executed, the previously stored content of IP and CS along
with flags are retrieved into the CS, IP and flag register from the stack
and execution of the main program continues further.
– The procedure may be a near or far procedure. In case of a FAR
procedure, the current contents of the SP points to IP and CS atthe
time of return.
– While in case of a NEAR procedure, it points to only IP.
99
Control transfer or Branching Instruction
• INT: Interrupt Type N
– In the interrupt structure of 8086/8088, 256 interrupts are
defined corresponding to the types from 00H to FFH.
– When an INT N instruction is executed, the TYPE byte N
is multiplied BY 4 and the contents of IP and CS of the
interrupt service routine will be taken from the
hexadecimal multiplication (N * 4) as offset address and
0000 as segment address.
102
Control transfer or Branching Instruction
103
Control transfer or Branching Instruction
104
Flag Manipulation and Processor Control Instructions
• These instructions control the functioning of the available hardware inside
the processor chip.
• These are categorized into two types;
105
Control transfer or Branching Instruction
Flag manipulation Instructions
Sr. No. Instruction Function
1 CLC Clear carry flag
2 CMC Complement carry flag
3 STC Set carry flag
4 CLD Clear direction flag
5 STD Set direction flag
6 CLI Clear interrupt flag
7 STI Set interrupt flag
106
Assembler Directives and Operators
Assembler Directives and Operators
• To make the programming on microprocessor simpler,
assemblers are used. The main role of the assembler is to
convert the assembly language program in to the machine
language program and in 8086 since the hand coding is a
tedious job mostly people follow the assembler for
programming the microprocessor.
• For completing the task of the assembly, assembler needs
some hints from the programmer i.e. the required storage of
particular constant or variable, logical names of the
segments, types of different subroutines or modules, end of
file etc.
• Such hints are given by the assembler directives.
• Another type of hint which helps the assembler to assign a
particular constant with a label or initialize particular memory
locations or labels with constants is an operator. 108
Assembler Directives and Operators
• DB: Define Byte
– It is used to reserve byte or bytes of memory locations in the available memory
space.
• DW: Define Word (1 – word = 2 – bytes)
– It is used to reserve word or words of memory locations in the available memory
space.
• DD: Define Double Word (1 – double word = 4 – bytes)
– It is used to reserve word or words of memory locations in the available memory
space.
• DQ: Define Quad Word (1 – quad word = 8 – bytes)
– It is used to reserve two words of memory locations in the available memory
space.
• DT: Define Ten Bytes
– It is used to reserve ten bytes of memory locations in the available memory
space.
• Usage:
– RANKS DB 01H,02H,03H
– WORDS DB 1234H,0250H,03ABH
– LABEL1 DQ 1234567887654321H
– PLAYERS_RANKS DT 01H,02H,03H, 31H,22H,03H 21H,32H,07H 10H,
109
Assembler Directives and Operators
• Assume: Assume logical segment name
– It is used to indicate the start of a logical segment
– ASSUME DS: DATA it implies that the start of a new segment named
DATA and its base address is to be initialized to DS.
• END - END directive is placed after the last statement of a
program to tell the assembler that this is the end of the
program module. The assembler will ignore any statement
after an END directive. Carriage return is required after the
END directive.
• ENDP - ENDP directive is used along with the name of the
procedure to indicate the end of a procedure to the
assembler
• Example:
– SQUARE_NUM PROCE ; It start the procedure
– ;Some steps to find the square root of a number
– SQUARE_NUM ENDP ;Hear it is the End for the procedure
110
Assembler Directives and Operators
• ENDS - This ENDS directive is used with name of
the segment to indicate the end of that logic
segment.
– Example:
– CODE SEGMENT ;Hear it Start the logic
– ;segment containing code
– ; Some instructions statements to perform the logical operation
– CODE ENDS ;End of segment named as CODE
112
Assembler Directives and Operators
• EVEN - This EVEN directive instructs the assembler to
increment the location of the counter to the next even
address if it is not already in the even address.
– If the word is at even address 8086 can read a memory in 1 bus cycle.
– If the word starts at an odd address, the 8086 will take 2 bus cycles to
get the data.
– A series of words can be read much more quickly if they are ateven
address.
– When EVEN is used the location counter will simply incremented to
next address and NOP instruction is inserted in that incremented
location.
• Example:
– DATA1 SEGMENT
– ; Location counter will point to 0009 after assembler reads next
statement
– SALES DB 9 DUP(?) ;declare an array of 9 bytes
– EVEN ; increment location counter to 000AH
– RECORD DW 100 DUP( 0 ) ;Array of 100 words will start from an
even address for quicker read
– DATA1 ENDS
113
Assembler Directives and Operators
• EXTERN: External and PUBLIC: public
– The directive EXTERN informs the assembler that the names,
procedures and labels declared after this directive have already been
defined in some other assembly language modules.
• GROUP: Group the Related Segment
– This directive is used to form logical groups of segments with similar
purpose or type. This directive is used to inform the assembler to form
a logical group of the following segment names.
• PROGRAM GROUP CODE, DATA, STACK
• LABEL: label
– The Label directive is used to assign a name to the current content of
the location counter. When the assembly process starts, the
assembler initializes a location counter to keep track of memory
location assigned to the program.
• LENGTH: Byte Length of a label
– This directive is not available in MASM. This is used to refer to the
length of data array or string.
– MOV CX, LENGTH ARRAY
114
Assembler Directives and Operators
• LOCAL
– The labels, variables, constants or procedures declared LOCAL in a
module are to be used only by the particular module.
– LOCAL a, b, DATA, ARRAY, ROUTINE
• NAME: Logical Name of a Module
– The NAME directive is used to assign a name to an assembly
language program module. The module, may now referred to by its
declared name.
• OFFSET: Offset of a Label
– When the assembler comes across the OFFSET operator along with a
label, it first computes the 16-bit displacement (also called as offset
interchangeably) of the particular label, and replaces the string
OFFSET LABEL by the computed displacement. This operator is used
with arrays, strings, labels and procedures to decide their offset in
their default segments.
• CODE SEGMENT
• MOV SI, OFFSET LIST
• CODE ENDS
• DATA SEGMENTS
• LIST DB 10H
• DATA ENDS 123
Assembler Directives and Operators
• ORG: Origin
– The ORG directives directs the assembler to start the memory
allotment for the particular segments, block or code from the declared
address in the ORG statement.
– If an ORG 200H address in code segment is present at the starting of
the code segment of that module then the code will start from 200H
address in code segment.
• PROC: Procedure
– The PROC directives marks the start of named procedure in the
statement.
– Also the types NEAR or FAR specify the type the procedure i.e.
whether it is to be called by the main program located within 64k of
physical memory or not.
• PTR: Pointer
– The POINTER operator is used to declare the type of a label, variable
or memory operand.
– The operator PTR prefixed by either BYTE or WORD.
– If prefix is byte then the particular label, variable or memory operand is
treated as an 8-bit quantity, while if word is the prefix, then it is treated
as a 16-bit quantity.
116
Assembler Directives and Operators
• PUBLIC
– The PUBLIC directive is used along with the Extern directive.
– This informs the assembler that the labels, variables constants, or procedures
declared PUBLIC may be accessed by other assembly modules to form their
codes, but while using the PUBLIC declared labels, variables, constants or
procedures the user must declare them externals using the extern directive.
• SHORT
– The SHORT operator indicates to the assembler that only one byte is required
to code the displacement for a jump (i.e. displacement is within -128 to +127
bytes from address of the byte next to the jump opcode).
• JMP SHORT LABEL
117
Assembler Directives and Operators
• TYPE
– The TYPE operator directs the assembler to decide the data type of
the specified label and replaces the ‘TYPE’ label by the decided data
type.
• GLOBAL
– The labels, variables, constants or procedures declared GLOBAL may
be used by other modules of the program. Once a variable is declared
GLOBAL, it can be used by any module in the program.
• FAR PTR
– This directive indicates the assembler that the label following FAR
PYTR is not available within the same segment and the address of the
label is of 32- bits i.e. 2-bytes offset followed two byte segment
address.
• NEAR PTR
– This directive indicates that the label following NEAR PTR is in the
same segment and needs only 16-bit i.e. 2-byte offset to address it.
118
Interrupts
In 8086 microprocessor following tasks are performed when
microprocessor encounters an interrupt:
[Link] value of flag register is pushed into the stack. It means that
first the value of SP (Stack Pointer) is decremented by 2 then the
value of flag register is pushed to the memory address of stack
segment.
[Link] value of starting memory address of CS (Code Segment) is
pushed into the stack.
[Link] value of IP (Instruction Pointer) is pushed into the stack.
[Link] is loaded from word location (Interrupt type) * 04.
[Link] is loaded from the next word location.
[Link] and Trap flag are reset to 0.
Types of Interrupt
Hardware Interrupts –
(A) NMI (Non Maskable Interrupt) – It is a single pin non maskable hardware interrupt
which cannot be disabled. It is the highest priority interrupt in 8086
microprocessor. After its execution, this interrupt generates a TYPE 2 interrupt. IP
is loaded from word location 00008 H and CS is loaded from the word location
0000A H.
(B) INTR (Interrupt Request) – It provides a single interrupt request and is activated by
I/O port. This interrupt can be masked or delayed. It is a level triggered interrupt. It can
receive any interrupt type, so the value of IP and CS will change on the interrupt type
received.
Software Interrupts –
These are instructions that are inserted within the program to
generate interrupts. There are 256 software interrupts in 8086
microprocessor. The instructions are of the format INT type
where type ranges from 00 to FF. The starting address ranges
from 00000 H to 003FF H. These are 2 byte instructions. IP is
loaded from type * 04 H and CS is loaded from the next
address give by (type * 04) + 02 H.
Some important software interrupts are:
(A) TYPE 0 corresponds to division by zero(0).
(B) TYPE 1 is used for single step execution for debugging of
program.
(C) TYPE 2 represents NMI and is used in power failure
conditions.
(D) TYPE 3 represents a break-point interrupt.
(E) TYPE 4 is the overflow interrupt.
8086 Emulator for Lab Experiments
Address Generation in 8086 Emulator
12
6