Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Microprocessor- 8086
MICROPROCESSOR
IA Marks : 25
Exam Hours : 03
Exam Marks : 100
PART - A
UNIT - 1
8086 PROCESSORS: Historical background, The microprocessor-based personal computer system, 8086 CPU
Architecture, Machine language instructions, Instruction execution timing, The 8088
7 Hours
UNIT - 2
INSTRUCTION SET OF 8086: Assembler instruction format, data transfer and arithmetic, branch type, loop,
NOP & HALT, flag manipulation, logical and shift and rotate instructions. Illustration of these instructions with
example programs, Directives and operators
7 Hours
UNIT - 3
BYTE AND STRING MANIPULATION: String instructions, REP Prefix, Table translation, Number format
conversions, Procedures, Macros, Programming using keyboard and video display
6 Hours
UNIT - 4
8086 INTERRUPTS: 8086 Interrupts and interrupt responses, Hardware interrupt applications, Software interrupt
applications, Interrupt examples
6 Hours
PART - B
UNIT - 5
8086 INTERFACING: Interfacing microprocessor to keyboard (keyboard types, keyboard circuit connections and
interfacing, software keyboard interfacing, keyboard interfacing with hardware), Interfacing to alphanumeric
displays (interfacing LED displays to microcomputer), Interfacing a microcomputer to a stepper motor
6 Hours
UNIT - 6
8086 BASED MULTIPROCESSING SYSTEMS: Coprocessor configurations, The 8087 numeric data processor:
data types, processor architecture, instruction set and examples
6 Hours
UNIT - 7
SYSTEM BUS STRUCTURE: Basic 8086 configurations: minimum mode, maximum mode, Bus Interface:
peripheral component interconnect (PCI) bus, the parallel printer interface (LPT), the universal serial bus (USB)
7 Hours
UNIT - 8
80386, 80486 AND PENTIUM PROCESSORS: Introduction to the 80386 microprocessor, Special 80386
registers, Introduction to the 80486 microprocessor, Introduction to the Pentium microprocessor.
7 Hours
TEXT BOOKS:
1. Microcomputer systems-The 8086 / 8088 Family Y.C. Liu and G. A. Gibson, 2E PHI -2003
2. The Intel Microprocessor, Architecture, Programming and Interfacing-Barry B. Brey, 6e, Pearson Education
/ PHI, 2003
REFERENCE BOOKS:
1. Microprocessor and Interfacing- Programming & Hardware, Douglas hall, 2e TMH, 1991
2. Advanced Microprocessors and Peripherals - A.K. Ray and K.M. Bhurchandi, TMH, 2001
3. 8088 and 8086 Microprocessors - Programming, Interfacing, Software, Hardware & Applications - Triebel
and Avtar Singh,4e, Pearson Education, 2003.
MANOJKUMAR S.B
Page 1
Microprocessor- 8086
Unit - I
8086- PROCESSORS
In December 1970, Gilbert Hyatt filed a patent application entitled Single Chip Integrated
Circuit Computer Architecture, the first basic patent on the microprocessor.
The microprocessor was invented in the year 1971 in the Intel labs. The first processor was a 4
bit processor and was called 4004.The following table gives chronologically the microprocessor
revolution.
Microprocess Year of
Word
Memory
ors
Length
Addressi
Introduct
ion
Pins
Clock
Remarks
ng
4004
1971
4 bits
1KB
16
750KHz
Intels 1st P
8008
1972
8 bits
16KB
18
800KHz
Mark-8 used
this;
1st computer for
the home.
8080
1973
8 bits
64KB
40
2 MHz
8085
1976
8 bits
64KB
40
3-6 MHz
8086
1978
16 bits
1 MB
40
5-10 MHz
8088
1980
8/16 bits
1MB
40
5-8MHz
80186
1982
16 bits
1 MB
68
5-8MHz
80286
1982
16 bits
16 MB
68
60-
real,
12.5MHz
4GBv
80386DX
80386SX
1985
1988
MANOJKUMAR S.B
32 bits
16/32
4GB real,
132
64TBv
PGA
16MB
100
6000trs, Altair1st PC
Popular
IBM PC, Intel
became one of
fortune 500
companies.
PC/XT
More a
Microcontroller
PC/AT, 15
million PCs
sold in 6 years
20-33MHz
2,75,000
transistors
20MHz
32b int
16b ext
Page 2
Microprocessor- 8086
bits
real,
64TBv
80486DX
Pentium
1989
1993
32 bits
64 bits
4 GB real,
168
25-66MHz
64TBv
PGA
4 GB, 16
237
60-200
KB cache
PGA
MHz
64Gb,
256K/512
K L2
Cache
64Gb
387
PGA
150MHz
242
400MHz
Pentium Pro
1995
64 bits
Pentium II
1997
64 bits
Pentium II
Xeon
1998
64 bits
512k/1M/
2M L2
cache
528
pins
LGA
400MHz
Pentium III
Xeon
1999
64 bits
370
PGA
1GHz
Pentium 4
2000
64 bits
16 k L1
data + 16
k L1 instr;
512 kB/1
MB/2 MB
L2
514,864
KB
423
PGA
1.3 - 2GHz
Xeon
2001
64 bits
Itanium
2001
64 bits
Itanium 2
2002
64 bits
MANOJKUMAR S.B
8 MB iL3
cache
2MB/
4MB L3
cache
1.5
9MB L3
cache
3.33 GHz
418
pins
FCP
GA
611
pins
FCP
GA
800 MHz
200 MHz
Flaot pt cop,
Command line
to point and
click
2 intr. At a time,
Process real
world data like
sound, hand
written and
photo images.
Speedy CAD
1.5 GHz,
Professional
quality movies,
rendering 3D
graphics.
Choice of
operating system
Enabling ecommerce
security
transactions
Business
applications
Page 3
Microprocessor- 8086
Centrino
mobile
2003
64 bits
Pentium 4
processor
extreme
Centrino M
(mobile)
2003
64 bits
2004
64 bits
2 MB L2
cache
423
pins
PGA
3.80 GHz
Mobile specific,
increased battery
life.
Hyper threading
technology,
games
90nm,2MB L2
cache400MHz
power-system
optimized
system bus
Apart from Intel, Motorola, Zylog Corporation, Fairchild and National (Hitachi, Japan) are some
of the other microprocessor manufacturers.
Microprocessors are used in all modern appliances, which are Intelligent, meaning that they are
capable of different modes of working. For example an automatic washing machine has different
wash options, one for woolen and the other for nylon etc., Also in a printing Industry right from
type setting to page lay out to color photo scanning and printing and cutting and folding are also
taken care of by microprocessors.
The applications of microprocessors can be sub divided into three categories. The first and most
important one is the computer applications. The second one is the control application (micro
controllers, embedded controllers etc.) and the third is in Communication (DSP processors, Cell
phones etc.).
The basis of working of all the microprocessors is binary arithmetic and Boolean logic. The
number system used is Hexadecimal (base 16) and the character code used is ASCII. Many
assemblers are available to interface the machine code savvy processor to English language like
programs of the users.(CP/M, MASM, TASM etc.).
For Games we have joysticks, electronic guns and touch screens. Nowadays laptop and palmtop
computers are proliferating and in future nano computing, bio computing, molecular and optical
computing also are contemplated.
MANOJKUMAR S.B
Page 4
Microprocessor- 8086
DATA BUS
INPUT
DEVICE
I/O PORTS
CONTROL
BUS
CENTRAL
PROCESSING
UNIT (CPU)
CONTROL
BUS
MEMORY
(RAM AND
ROM)
OUTPUT
DEVICE
ADDRESS BUS
Microprocessor 8086, 8088, 80186, 80188, 80286, 80386, 80486, Pentium, Pentium
Pro, Pentium II, Pentium III, Pentium IV
Memory System DRAM, SRAM, Cache, ROM, Flash Memory, EEPROM, SDRAM,
RAMBUS
I/O System Printer, Serial communications, Floppy Disk Drive, Hard Disk Drive,
Mouse, CD-ROM drive, Plotter, Keyboard, Monitor, Scanner, DVD, Pen Drive
Page 5
Microprocessor- 8086
The block diagram of 8086 is as shown. This can be subdivided into two parts, namely the Bus
Interface Unit and Execution Unit. The Bus Interface Unit consists of segment registers, adder to
generate 20 bit address and instruction prefetch queue.
Once this address is sent out of BIU, the instruction and data bytes are fetched from memory and
they fill a First In First Out 6 byte queue.
Execution Unit:
The execution unit consists of scratch pad registers such as 16-bit AX, BX, CX and DX and
pointers like SP (Stack Pointer), BP (Base Pointer) and finally index registers such as source
index and destination index registers. The 16-bit scratch pad registers can be split into two 8-bit
registers. For example, AX can be split into AH and AL registers. The segment registers and
their default offsets are given below.
MANOJKUMAR S.B
Segment Register
Default Offset
CS
IP (Instruction Pointer)
DS
SI, DI
SS
SP, BP
ES
DI
Page 6
Microprocessor- 8086
The Arithmetic and Logic Unit adjacent to these registers perform all the operations. The results
of these operations can affect the condition flags.
Different registers and their operations are listed below:
Register
Operations
AX
AL
AH
BX
Translate
CX
CL
DX
IP
Instruction Pointer
CS
DS
SS
ES
AX
AH
AL
BX
BE
BL
CX
CE
CL
DX
DH
DL
SP
BP
SI
DI
SR
Status Register
MANOJKUMAR S.B
MEMORY
00000016
Page 7
Microprocessor- 8086
0000
ADDER
15
14
13
12
11
10
0F
DF
IF
TF
SF
ZF
AF
PF
CF
U= UNDEFINED
(a)
(b)
(c)
(d)
(e)
(f)
(g)
(h)
(i)
(a)
(b)
(c)
(d)
(e)
(f)
(g)
(h)
(i)
MANOJKUMAR S.B
Page 8
Microprocessor- 8086
There are three internal buses, namely A bus, B bus and C bus, which interconnect the various
blocks inside 8086.The execution of instruction in 8086 is as follows:
The microprocessor unit (MPU) sends out a 20-bit physical address to the memory and fetches
the first instruction of a program from the memory. Subsequent addresses are sent out and the
queue is filled upto 6 bytes. The instructions are decoded and further data (if necessary) are
fetched from memory. After the execution of the instruction, the results may go back to memory
or to the output peripheral devices as the case may be.
PHYSICAL
ADDRESS
7FFFFH
MEMORY
HIGHEST ADDRESS
TOP OF EXTRA SEGMENT
64K
70000H
5FFFFH
50000H
4489FH
348A0H
2FFFFH
of an 8086
20000H
MANOJKUMAR S.B
Page 9
Microprocessor- 8086
4489FH
PHYSICAL
ADDRESS
MEMORY
TOP OF CODE SEGMENT
38AB4H
CODE BYTE
IP=4214H
348A0H
(a) Diagram
CS
IP +
PHYSICAL ADDRESS
3 4 8 A 0
4 2 1 4
3 8 A B 4
HARDWIRED ZERO
(b) Computation
Segment Over Ride Prefix
SOP is used when a particular offset register is not used with its default base segment register,
but with a different base register. This is a byte put before the OPCODE byte.
0
SR
Segment Register
00
ES
01
CS
10
SS
11
DS
MANOJKUMAR S.B
Page 10
Microprocessor- 8086
Here SR is the new base register. To use DS as the new register 3EH should be prefix.
Operand Register
Default
IP (Code address)
CS
Never
SP(Stack address)
SS
Never
BP(Stack Address)
SS
BP+DS or ES or CS
DS
ES, SS or CS
DS
ES
Never
strings)
DI (Implicit Destination
Address for strings)
Examples: MOV AX, DS: [BP], LODS ES: DATA1
S4
S3
Indications
Alternate data
Stack
Code or none
Data
BHE
A0
Indications
Whole word
None
Segmentation:
The 8086 microprocessor has 20 bit address pins. These are capable of addressing 220 = 1Mega
Byte memory.
To generate this 20 bit physical address from 2 sixteen bit registers, the following procedure is
adopted.
MANOJKUMAR S.B
Page 11
Microprocessor- 8086
The 20 bit address is generated from two 16-bit registers. The first 16-bit register is called the
segment base register. These are code segment registers to hold programs, data segment register
to keep data, stack segment register for stack operations and extra segment register to keep
strings of data. The contents of the segment registers are shifted left four times with zeroes (0s)
filling on the right hand side. This is similar to multiplying four hex numbers by the base 16.
This multiplication process takes place in the adder and thus a 20 bit number is generated. This is
called the base address. To this a 16-bit offset is added to generate the 20-bit physical address.
Segmentation helps in the following way. The program is stored in code segment area. The data
is stored in data segment area. In many cases the program is optimized and kept unaltered for the
specific application. Normally the data is variable. So in order to test the program with a
different set of data, one need not change the program but only have to alter the data. Same is the
case with stack and extra segments also, which are only different type of data storage facilities.
Generally, the program does not know the exact physical address of an instruction. The
assembler, a software which converts the Assembly Language Program (MOV, ADD etc.) into
machine code (3EH, 4CH etc) takes care of address generation and location.
Sr. No
1.
2.
3.
4.
5.
8088
Its has only eight data lines.
Therefore, it has AD0 AD7
and A8 A15 signals.
As data bus is 8-bit wide, it
does not have BHE signal.
It has 4 byte instruction queue.
Due to 8-bit data bus
instruction fetching is slow
and 4 bytes are sufficient for
queue.
Its pin number 34 is SSO. It
acts as S0 in the minimum
mode. In maximum mode,
SSO pin is always high.
80086
It has sixteen data lines. Therefore it has AD0
AD15 signals.
It has BHE signal to access higher byte.
It has 6 byte instrucyion queue.
MANOJKUMAR S.B
Page 12
Microprocessor- 8086
Addressing Modes
Addressing modes of 8086
When 8086 executes an instruction, it performs the specified function on data. These data are
called its operands and may be part of the instruction, reside in one of the internal registers of the
microprocessor, stored at an address in memory or held at an I/O port, to access these different
types of operands, the 8086 is provided with various addressing modes (Data Addressing
Modes).
Data Addressing Modes of 8086
The 8086 has 12 addressing modes. The various 8086 addressing modes can be classified into
five groups.
A.
Addressing modes for accessing immediate and register data (register and immediate
modes).
B.
Addressing modes for accessing data in memory (memory modes)
C.
Addressing modes for accessing I/O ports (I/O modes)
D.
Relative addressing mode
E.
Implied addressing mode
A.
Immediate addressing mode:
In this mode, 8 or 16 bit data can be specified as part of the instruction.
OP Code
Example 1 : MOV CL, 03 H
Moves the 8 bit data 03 H into CL
Immediate Operand
Example 2 :
Page 13
Register
Accumulator
Base
Count
Data
Stack pointer
Base pointer
Source index
Destination index
Code Segment
Data Segment
Stack Segment
Extra Segment
Microprocessor- 8086
Byte (Reg 8)
AL, AH
BL, BH
CL, CH
DL, DH
-
Operand sizes
MOV CL, DL
Moves 8 bit contents of DL into CL
CS
DS
:
Direct Address
SS
ES
The Execution Unit (EU) has direct access to all registers and data for register and immediate
operands. However the EU cannot directly access the memory operands. It must use the BIU, in
order to access memory operands.
In the direct addressing mode, the 16 bit effective address (EA) is taken directly from the
displacement field of the instruction.
MANOJKUMAR S.B
Page 14
Microprocessor- 8086
CS
DS
SS
ES
BX
or
BP
+ displacement
when memory is accessed PA is computed from BX and DS when the stack is accessed PA is
computed from BP and SS.
Example : MOV AL, START [BX]
or
MOV AL, [START + BX]
based mode
EA : [START] + [BX]
PA : [DS] + [EA]
The 8 bit content of this memory location is moved to AL.
MANOJKUMAR S.B
Page 15
Microprocessor- 8086
SI
or
DI
+ 8 or 16bit displacement
Page 16
Microprocessor- 8086
Example 1 :
INAL, DX
If [DX] = 5040
8 bit content by port 5040 is moved into AL.
Example 2 : IN AX, DX
Inputs 8 bit content of ports 5040 and 5041 into AL and AH respectively.
D. Relative addressing mode:
Example : JNC START
If CY=O, then PC is loaded with current PC contents plus 8 bit signed value of START,
otherwise the next instruction is executed.
E. Implied addressing mode:
Instruction using this mode have no operands.
Example : CLC which clears carry flag to zero.
SINGLE INDEX
DOUBLE INDEX
BX
OR
Encoded
in the
instruction
BP
OR
BX
SI
OR
OR
BP
DI
EU
SI
OR
DI
Explicit in the
instruction
DISPLACEMENT
CS
0000
OR
Assumed
unless
over
ridden
by prefix
DS
0000
OR
SS
OR
ES
BIU
0000
0000
PHYSICAL ADDRESS
Page 17
Microprocessor- 8086
Instruction Format :
The 8086 instruction sizes vary from one to six bytes. The OP code occupies six bytes and it
defines the operation to be carried out by the instruction.
Register Direct bit (D) occupies one bit. It defines whether the register operand in byte 2 is the
source or destination operand.
D=1 Specifies that the register operand is the destination operand.
D=0 indicates that the register is a source operand.
Data size bit (W) defines whether the operation to be performed is an 8 bit or 16 bit data
W=0 indicates 8 bit operation
W=1 indicates 16 bit operation
MANOJKUMAR S.B
Byte 3
Byte 4
Page 18
Opcode
0
D
7
W
Byte 1
Microprocessor- 8086
MOD
REG
1 0
R/M
Low Disp/
DATA
High Disp/
DATA
OR
Byte 2
DIRECT
ADDRESS LOW
BYTE
DIRECT
ADDRESS HIGH
BYTE
The second byte of the instruction usually identifies whether one of the operands is in memory or
whether both are registers.
This byte contains 3 fields. These are the mode (MOD) field, the register (REG) field and the
Register/Memory (R/M) field.
MOD (2 bits)
00
01
10
11
Interpretation
Memory mode with no displacement follows except for 16 bit
displacement when R/M=110
Memory mode with 8 bit displacement
Memory mode with 16 bit displacement
Register mode (no displacement)
Register field occupies 3 bits. It defines the register for the first operand which is specified as
source or destination by the D bit.
REG
W=0
W=1
000
AL
AX
001
CL
CX
010
DL
DX
011
BL
BX
100
AH
SP
101
CH
BP
110
DH
SI
111
BH
DI
The R/M field occupies 3 bits. The R/M field along with the MOD field defines the second
operand as shown below.
MANOJKUMAR S.B
Page 19
Microprocessor- 8086
MOD 11
R/M
000
001
010
011
100
101
110
111
W=0
AL
CL
DL
BL
AH
CH
DH
BH
W=1
AX
CX
DX
BX
SP
BP
SI
DI
MOD=00
(BX) + (SI)
(BX)+(DI)
(BP)+(SI)
(BP)+(DI)
(SI)
(DI)
Direct address
(BX)
MOD 01
(BX)+(SI)+D8
(BX)+(DI)+D8
(BP)+(SI)+D8
(BP)+(DI)+D8
(SI) + D8
(DI) + D8
(BP) + D8
(BX) + D8
MOD 10
(BX)+(SI)+D16
(BX)+(DI)+D16
(BP)+(SI)+D16
(BP)+(DI)+D10
(SI) + D16
(DI) + D16
(BP) + D16
(BX) + D16
In the above, encoding of the R/M field depends on how the mode field is set. If MOD=11
(register to register mode), this R/M identifies the second register operand.
MOD selects memory mode, then R/M indicates how the effective address of the memory
operand is to be calculated. Bytes 3 through 6 of an instruction are optional fields that normally
contain the displacement value of a memory operand and / or the actual value of an immediate
constant operand.
Example 1 : MOV CH, BL
This instruction transfers 8 bit content of BL
Into CH
The 6 bit Opcode for this instruction is 1000102 D bit indicates whether the register specified by
the REG field of byte 2 is a source or destination operand.
D=0 indicates BL is a source operand.
W=0 byte operation
In byte 2, since the second operand is a register MOD field is 112.
The R/M field = 101 (CH)
Register (REG) field = 011 (BL)
Hence the machine code for MOV CH, BL is
10001000 11 011 101
Byte 1
Byte2
= 88DD16
MANOJKUMAR S.B
Page 20
Microprocessor- 8086
000
001
010
011
100
101
110
111
Register Mode
W=0
W=1
AL
AX
CL
CX
DL
DX
BL
BX
AH
SP
CH
BP
DH
SI
BH
DI
MOD
REG
R/M
LB displacement
HB displacement
100010
10
010
110
34H
12H
MANOJKUMAR S.B
Page 21
Microprocessor- 8086
SR
Segment register
00
ES
01
CS
10
SS
11
DS
To specify DS register, the SOP byte would be 001 11 110 = 3E H. Thus the 5 byte code for this
instruction would be 3E 89 96 45 23 H.
SOP
Opcode
MOD
REG
R/M
LB disp.
HD disp.
3EH
1000 10
10
010
110
45
23
Suppose we want to code MOV SS : 2345 (BP), DX. This generates only a 4 byte code, without
SOP byte, as SS is already the default segment register in this case.
Example 5 :
Give the instruction template and generate code for the instruction ADD OFABE [BX], [DI], DX
(code for ADD instruction is 000000)
ADD OFABE [BX] [DI], DX
Here we have to specify DX using REG field. The bit D is 0, indicating that DX is the source
register. The REG field must be 010 to indicate DX register. The w must be 1 to indicate it is a
word operation. FABE (BX + DI) is specified using MOD value of 10 and R/M value of 001
(from the summary table). The 4 byte code for this instruction would be
Opcode D
W MOD REG R/M
16 bit disp.
=01 91 BE FAH
000000 0
1
10
010
001
BEH FAH
Example 6 :
Give the instruction template and generate the code for the instruction MOV AX, [BX]
(Code for MOV instruction is 100010)
AX destination register with D=1 and code for AX is 000 [BX] is specified using 00 Mode and
R/M value 111
It is a word operation
Opcode
100010
D
1
W
1
Mod
00
REG
000
R/M
111
=8B 07H
Questions :
1.
2.
3.
4.
5.
6.
7.
8.
MANOJKUMAR S.B
Page 22
Microprocessor- 8086
Unit - 2
2.
ARITHMETIC INSTRUCTIONS
3.
4.
STRING INSTRUCTIONS
5.
6.
MOV
PUSH
POP
XCHG
XLAT
SPECIAL ADDRESS
TRANSFER INSTRUCTION
FLAG TRANSFER
INSTRUCTIONS
LEA
LDS
LES
LAHF
SAHF
PUSHF
POPF
IN
OUT
2.ARITHMETIC INSTRUCTIONS
These instructions are those which are useful to perform Arithmetic calculations, such as addition, subtraction,
multiplication and division. They are again classified into four groups. They are:
ADDITION INSTRUCTIONS
SUBTRACTION
INSTRUCTIONS
MULTIPLICATION
INSTRUCTIONS
DIVISION INSTRUCTIONS
ADD
ADC
INC
AAA
DAA
SUB
SBB
DEC
NEG
CMP
AAS
DAS
MUL
IMUL
AAM
DIV
IDIV
AAD
CBW
CWD
MANOJKUMAR S.B
Page 23
Microprocessor- 8086
SHIFT INSTRUCTIONS
ROTATE INSTRUCTIONS
NOT
AND
OR
XOR
TEST
SHL / SAL
SHR
SAR
ROL
ROR
RCL
RCR
4. STRING INSTRUCTIONS
The string instructions function easily on blocks of memory. They are user friendly instructions, which help for easy
program writing and execution. They can speed up the manipulating code. They are useful in array handling, tables
and records.
STRING INSTRUCTIONS
REP
REPE
/
REPZ
REPNE
/
REPNZ
MOVS / MOVSB / MOVSW
COMPS / COMPSB / COMPSW
SCAS
/
SCASB
/
SCASW
LODS / LODSB / LODSW
STOS / STOSB / STOSW
MANOJKUMAR S.B
CONDITIONAL TRANSFER
INSTRUCTIONS
JA / JNBE
JAE / JNB
JB / JNAE
JBE / JNA
JC
JE / JZ
JG / JNLE
JGE / JNL
JL / JNGE
JLE / JNG
JNC
JNE / JNZ
JNO
JNP / JPO
JNS
JO
ITERATION CONTROL
INSTRUCTIONS
INTERRUPT
INSTRUCTIONS
LOOP
LOOPE / LOOPZ
LOOPNE / LOOPNZ
JCXZ
INT
INTO
IRET
JP / JPE
JS
Page 24
Microprocessor- 8086
These instructions are used to change the process of the Microprocessor. They change the process with the stored
information. They are again classified into Two groups. They are:
FLAG SET / CLEAR INSTRUCTIONS
EXTERNAL HARDWARE
SYNCHRONIZATION INSTRUCTIONS
STC
CLC
CMC
STD
CLD
STI
CLI
HLT
WAIT
ESC
LOCK
NOP
Addition:
There are two instructions ADD and ADC
Register Addition:
ADD AL,BL AL=AL+BL
ADD CX,DI CX=CX+DI
ADD CL,10H CL=CL+10
ADD [BX],AL the contents of AL are added with the contents of a memory location
addressed by BX and the result is stored in the same memory location
Example
ADD AL,BL AL=10H BL=30H the result AL=40H
ADD AX,[SI+2] the word content of the data segment memory location addressed by sum of
SI+2 is added with AX and the result is stored in AX
Example
AX=1234H SI=2000 SI+2=2002 and let the word stored in memory location 2002 be
1122H The result AX=2356H
ADD BYTE PTR [DI],3 3 is added to the byte contents of the data segment memory
location addressed by DI
Example
DI=2000 and the contents of that memory location is 11H
The contents of address 2000 will be 14H after the execution of this instruction
The contents of the flag register change after the addition operation. The flags affected are
SIGN,CARRY,ZERO, AUX CARRY,PARITY,OVERFLOW
MANOJKUMAR S.B
Page 25
Microprocessor- 8086
Immediate Addition
An 8 bit immediate data is added.
Example
MOV AL,10H
ADD AL,30H
The result AL=40H
Offset
addr
2000
10H
2001
2002
2003
2004
11H
22H
33H
44H
Page 26
Microprocessor- 8086
Increment addition
ADC AL,BH
AL=AL+BH+CARRY
ADC CX,AX
CX=CX+AX+CARRY
ADC BX,[BP+2] the word contents of the stack segment memory location addressed by
BP+2 is added to BX with carry and the result is stored in BX.
Subtraction
Many forms of subtraction appears to use with any addressing mode 8 16 and 32 bit data
SUB
SBB subtract with borrow
Register Subtraction:
SUB AL,BL
AL=AL-BL
SUB CL,10H CL=CL-10
The carry flag holds the borrow.
Decrement
DEC AX
DEC BYTE PTR [DI]
DEC CL
MANOJKUMAR S.B
Page 27
Microprocessor- 8086
DEC BL
Subtracts 1 to from a register or a memory location
CMP
This changes only the flag the destination operand never changes
This instruction is usually followed by conditional jump instructions
and tests the condition against the flags
Multiplication
The multiplication is performed on bytes words or double words and can be a signed integer or
unsigned integer
MUL: unsigned
IMUL: signed
Flags CARRY,OVERFLOW
8 Bit multiplication
Example
MOV BL,05H
MOV AL,10H
MUL BL
The multiplicand is in AL
The multiplier is in BL (even a memory location can be used)
8 Bit multiplication
Example
IMUL BYTE PTR [BX]
AL is multiplied by the byte contents of the data segment memory location addressed by BX the
signed product is placed in AX
For signed multiplication the product is in true binary form if positive and in twos complement
form if negative
Example
AL
00000010
BL 10000100
AL contains +2 and BL contains -4
IMUL BL
The product is -8
The product is in twos complement form stored in AX
AX 11111000
Division
DIV,IDIV
The dividend is always a double width dividend that is divided by the operand
An 8 bit division devides a 16 bit number by a 8 bit number
MANOJKUMAR S.B
Page 28
Microprocessor- 8086
AX register stores the dividend that is divided by contents of any 8 bit register or memory
location.
the Quotient(result) moves to AL and AH has the remainder.
For signed division the remainder always assumes sign of dividend and is an integer
AX=0010H equivalent to +16
BL=FDH equivalent to -3
DIV BL
AL=05H and AH=-1 11111111H
AX=1111111100000101H
AX=0010H equivalent to +16
BL=FDH equivalent to -3
DIV BL
AL=-5 11111011 and AH=1
AX=0000000111111011H
BCD Arithmetic:
The microprocessor allows manipulation of BCD and ASCII data
BCD used in Cash registers and ASCII used by many programs
There are two instructions
DAA decimal adjust after addition
DAS decimal adjust after subtraction
Both instructions correct the result. The BCD number is stored as packed form 2 digits/byte and
if unpacked form means 1 digit/byte it functions with AL only.
DAA decimal adjust after addition
The result is in AL
The Logic of this instruction
If lower nibble>9 or AF=1 add 06
After adding 06 if upper nibble>9 or CF=1 add 60
DAA instruction follows ADD or ADC
Example1
ADD AL,CL
DAA
Let AL=53 and CL=29
AL=53+29
AL=7C
AL=7C+06 (as C>9)
AL=82
Example 2
MANOJKUMAR S.B
Page 29
Microprocessor- 8086
MANOJKUMAR S.B
Page 30
AL=2F-6(F>9)
AL=29
Microprocessor- 8086
Example 2
SUB AL,CH
DAS
AL=38 CH=61
AL=38-61=D7 CF=1(borrow)
AL=D7-60(D>9)
AL=77 CF=1(borrow)
Example 3
MOV DX,1234H
MOV BX,3099H
MOV AL,BL
SUB AL,DL
DAS
MOV CL,AL
MOV AL,BH
SBB AL,DH
DAS
MOV CH,AL
AL=99-34=65
DAS will not have affect
AL=30-12=1E
AL=1E-06(E>9)
AL=18
The result is 1865 placed in CX
ASCII Arithmetic
Functions with ASCII coded numbers
The numbers range from 30-39H for 0-9
AAA
AAD
AAM
AAS use AX as source and destination
AAA
Example
add 31H and 39H the result is 6AH it should have been 10 decimal which is 31H and 30H
AAA is used to correct the answer
Converts resulting contents of AL to unpacked decimal digits
MANOJKUMAR S.B
Page 31
Microprocessor- 8086
AAA instruction examines the lower 4 bits of AL for valid BCD numbers and checks AF=0 sets
the 4 high order bits to 0
AH cleared before addition
If lower digit of AL is between 0-9 and AF=1 06 is added
The upper 4 digits are cleared and incremented by 1
If the lower value of the lower nibble is greater than 9 then increment AL by 06 AH by 1
AF and CF set
The higher 4 bits of AL are cleared to 0
AH modified
To get the exact sum add 3030H to AX
AAS
Correct result in AL after subtracting two unpacked ASCII operands
The result is in unpacked decimal format
If the lower 4 bits of AL are>9 or if AF=1 then AL=AL-6 and AH=AH-1 CF and AF set
otherwise CF and AF set to 0 no correction
result the upper nibble of AL is 00 and the lower nibble may be any number from 0-9
AAM
Follows multiplication instruction after multiplying two unpacked BCD numbers
Converts the product available in AL into unpacked BCD
Lower byte of result is in AL and upper in AH
Example
let the product is 5D in AL
D>9 so add 6 =13H
LSD of 13H is lower unpacked byte
Increment AH, AH=5+1=6 upper unpacked byte
After execution AH=06 and AL=03
MOV AL,5
MOV CL,5
MUL CL
AAM
Accomplishes conversion by dividing AX by 10
Benefit of AAM converts from binary to unpacked BCD
use of AAM for conversion
XOR DX,DX
MOV CX,100
DIV CX
AAM
ADD AX,3030H
XCHG AX,DX
AAM
ADD AX,3030H
MANOJKUMAR S.B
Page 32
Microprocessor- 8086
AAD
Appears before division
requires AX to contain two digit unpacked BCD number(not ASCII) before executing
After adjusting AX with AAD it is divided by an unpacked BCD number to generate a single
digit result in AL with remainder in AH
Example
.MODEL
.CODE
.STARTUP
MOV AL,48H
MOV AH,0
AAM
ADD AX,3030H
MOV DL,AH
MOV AH,2
PUSH AX
INT 21H
POP AX
MOV DL,AL
INT 21H
.EXIT
END
Logic instructions
AND
OR
Exclusive OR
NOT
TEST
The above instructions perform bitwise operation and the src and destination could be register or
memory location. Their function is same as logic opeartions
JUMP Group of Instructions
Introduction:
In almost any meaningful program, we need to alter the sequential flow of execution.
Examples:
Instruction at reset CS:IP: At rest, 8086 begins execution at the address FFFF:0000H
(absolute address of FFFF0H). There are only 16 bytes from this location to the end of
the memory space (FFFFH)! It is unlikely that any meaningful program can be written
within this space. Thus the instruction at this reset location is usually a long jump
MANOJKUMAR S.B
Page 33
Microprocessor- 8086
instruction that transfers control to some suitable lower address based on the available
memory.
A Sorting Program: A comparison-based sorting program would need to swap or not
swap two elements based on the outcome of the comparison of the two elements. This
would mean that we need an instruction to conditionally jump to some other location in
the program.
Any number of such examples can be given to illustrate the need for instructions that
alter the linear flow of control, either unconditionally or conditionally.
Unconditional and Conditional Jump Instructions allow such a control over the
execution flow.
Unconditional Jump:
After:
CS=FFFF; IP=0000
CS=F000; IP=8000
FFFF0
F0
00
80
00
EA
Unconditionally
Jump here
F8000
01
00
B8
Target
Location
The instructions used for such unconditional jumps are discussed in detail later.
Conditional Jump:
Values of one or more flags are used in deciding whether a jump is to be executed or not.
Thus, a jump may or may not occur depending on the values of such flag bits
MANOJKUMAR S.B
Page 34
Microprocessor- 8086
The instructions used for such conditional jumps are discussed in detail later.
Unconditional Jump Instructions:
All have similar behavior; but differ in where the target instruction is allowed to be and
consequently, the instruction lengths also differ. And that is the main advantage.
Both Short Jump and Near Jump are called intra-segment jumps also because in both
the cases, there is no change in the CS value and only IP is changed. In other words, the
jump is to a location that is within the same code segment.
MANOJKUMAR S.B
Page 35
Microprocessor- 8086
For these jumps, the target IP value is not specified as absolute value. Instead,
displacement (relative distance) from the current IP is specified.
Thus both are relative jumps. (Called Relative Program Memory Addressing in earlier
sessions)
Jump can be forward (to a higher address) or backwards (to a lower address).
Short and Near Jumps differ in the way the displacement is specified.
This instruction occupies 2 bytes; the first byte specifies the opcode and the second byte
specifies the relative displacement as a signed 8-bit quantity. The format is shown below:
Displacement
Opcode
EB
A short jump instruction with positive relative displacement is illustrated in the following
figure:
Jmp Target
10002
10001
10000
MANOJKUMAR S.B
1A
EB
Page 36
Microprocessor- 8086
A similar situation but with a negative relative displacement is illustrated in the following
figure:
10005
10004
10003
F2
EB
Jmp Target
In a typical Assembly Language Program, we use labels for branch targets and the
Assembler will automatically compute the displacement.
Anyway, most Assemblers choose the short form if possible (that is, if the displacement
is in the range of -128 to +127)
($ stands
Page 37
Microprocessor- 8086
Example: JMP $ + 2 jumps over the next 2 memory locations following the JMP
instruction. Thus if the above instruction starts at 1000:0010, the after the jump, control is
transferred to the instruction at 1000: 0014
This is quite similar to Short Jump except that the displacement is specified as 16 Bit
signed integer rather than as 8-bit integer.
Instruction is 3 Byte long. The first byte specifies the opcode and the next two bytes
specify the displacement. The format is shown in the following figure:
Opcode
E9
Displacement
Low Byte
High Byte
A near jump instruction with positive relative displacement is illustrated in the following
figure:
10003
10002
10001
10000
20
1A
E9
Page 38
Microprocessor- 8086
FF
F2
E9
10006
10005
10004
Jmp Target
FAR Jump is the only inter-segment jump instruction. It is called inter-segment because
the target location can be to any other code segment! Thus it is possible that change
occurs in the IP value as well as in the CS value.
MANOJKUMAR S.B
Page 39
Microprocessor- 8086
This instruction is 5 Byte long. The first byte specifies the opcode, the next 2 bytes
specify the new IP value and the last 2 bytes specify the new CS value. The format is
shown in the following figure:
IP
Opcode
Low
Byte
EA
CS
Low
Byte
Low
Byte
Low
Byte
10
00
00
04
EA
Jmp Target
In such cases, the Linker will fill the CS and IP values at the link time.
MANOJKUMAR S.B
Page 40
Microprocessor- 8086
EXTRN L1:FAR
ST1: MOV AX, 1
JMP L1
Jump with Register Operands:
A 16-Bit register may be used as the operand for the jump instruction. (Indirect Jump)
Contents of the register are transferred directly into IP (no concept of relative
displacement).
FAR PTR directive indicates a far jump ( jump table is assumed to contain double words
giving CS, IP values)
Otherwise a near jump is indicated. ( jump table is assumed to contain words giving IP
values)
MANOJKUMAR S.B
Page 41
Microprocessor- 8086
Conditional Jump
Conditional jumps are always short jumps (relative displacement is -128 to +127).
Based on the values of one or more flags, jump to target address may occur or execution
may continue with the next sequential instruction.
Usually preceded by instructions like CMP, SUB, TEST, AND etc which affect the
flags.
Example:
CMP AX, BX
JZ
LAB1
MOV AX, BX
LAB1: MOV CX, AX
In the above program, equality of the operands is tested based on the Z flag.
Thus, we have one set of conditional jump instructions that are to be used if the numbers
are to be interpreted as
instructions that are to be used if the numbers are to be interpreted as Signed Integers.
Mnemonic
Condition
JA
Jump if above
JAE
C=0
JB
C=1
Jump if below
JBE
Z=1 or C=1
MANOJKUMAR S.B
Operation
Page 42
Microprocessor- 8086
Mnemonic
Condition
Operation
JG
JGE
S=O
JL
S< >O
JLE
(Both S and O Flags are required to test the condition when comparing signed numbers!)
Mnemonic
Condition
Operation
JE or JZ
Z=1
JNE or JNZ
Z=0
Mnemonic
Condition
Operation
JC
C=1
JNC
C=0
Jump if no carry
JO
O=1
Jump if overflow
JNO
O=0
Jump if no overflow
JS
S=1
JNS
S=0
Jump if no sign
Mnemonic
JP or JPE
MANOJKUMAR S.B
Condition
P=1
Operation
Jump if parity set or Jump if parity is even
Page 43
JNP or JPO
P=0
JCXZ
CX = 0
Microprocessor- 8086
(Note that the last instruction, JCXZ is some what different from the rest in the sense that it
tests the contents of CX register rather than flags. This instruction, generally used in loops, is
illustrated in later sessions. Programs illustrating other conditional jump instructions are also
discussed in later sessions.)
LOOP Instruction:
Program loops are quite common. Most of the counting loops have a typical structure that
is shown below:
MOV CX, 10H ; Initialize the count that determines the number of
; times the loop is to be executed.
Start1: Instructions constituting the loop body
DEC CX
; Decrement counter
If the pair of instructions that test whether the loop body is to be executed again or not,
that is the instructions, DEC CX and JNZ Start1, could be combined in to one
instruction, we would get more elegant and clearer program.
These instructions are similar to LOOP instruction except that equality (Z flag) is also
tested. This allows a loop to be controlled by a count as well as a comparison test (like in
the case of String instructions).
MANOJKUMAR S.B
Page 44
Microprocessor- 8086
This instruction is related to 8087 Numeric Data Coprocessor. Details of 8087 are
discussed in later sessions. Briefly, 8087 provides support for floating point operations
and works as a coprocessor to 8086. It
The program has instructions for 8086 as well as 8087. How does 8087 know the
instruction is for itself? The solution to this problem is the ESC instruction.
ESC indicates that it is 8087 instruction. Opcode has 11011 as the higher-order 5 bits.
Thus ESC is never used by itself.
TEST/ is sampled during leading edge of CLK in each clock cycle during waiting.
WAIT instruction allows synchronization between the concurrently executing 8086 and
8087.
When 8086 needs the result from 8087, it executes the WAIT instruction.
MANOJKUMAR S.B
Page 45
Microprocessor- 8086
When 8087 is busy executing its instruction, it sets its BUSY pin = HIGH. Thus TEST/
of 8086 will also be HIGH. This forces 8086 to wait for the completion of 8087
activity.
When 8087 is not executing its instruction, its BUSY pin = LOW and thus the TEST/ of
8086 will also be LOW. This allows 8086 to continue its execution.
Used in early days to provide for manual code patches. Some NOP instructions would be
written every 100 bytes or so. When code was to be patched, the space occupied by the
NOP instructions was used. However, this is irrelevant in modern times because the
program development usually is based on Assemblers nad manual coding is no more
common.
Another use for this instruction is that it could be used for producing short time delays if
delay accuracy is not of concern.
HLT Instruction
As the name implies, it halts the program; the processor enters the HALT state.
An interrupt or a hardware reset will force the 8086 out of the HALT state.
May be used when the program has to wait for an interrupt to occur; but rarely used so in
practice.
In the early days, this instruction was used in the trainer kits as the last instruction of a
user program. It is no more used in this fashion. In fact, presently it is rarely used for any
other purpose either!
MANOJKUMAR S.B
Page 46
Microprocessor- 8086
LOCK Prefix
When such an instruction is executed, the LOCK/ pin of 8086 is activated (forced LOW).
Now, another bus master can not gain control of the bus until the end of the bus lock.
Thus the Lock prefixed instruction executes as an indivisible instruction even if it has
several memory cycles. (Without the LOCK prefix, the bus could be taken over by
another bus master after a memory cycle, even if the current instruction is not
completed!)
Shift instructions
They manipulate binary numbers
Used to control I/O Devices. Shift operation moves the number either to left or right within
memory location or a register. There are four instructions.There are two types of shift (1)
arithmetic and (2) logical. The shift left operation is equivalent to multiply operation and shift
right is divide operation. The data is shifted to left or right only by one position.
Shift left operation
Logical left: The contents of the register or memory location are shifted left by one position the
MSB bit moves to Carry flag bit and a zero is added to the LSB position
Example
SHL AX,1
AX=0000 1111 0000 1111 and Carry=1
After the execution of the instruction
AX=0001 1110 0001 1110 and Carry =0
Example
MOV CL,3
SHL DX,CL
The contents of the DX register are shifted left by three postions
Arithmetical Left: It is same as logical left shift.
MANOJKUMAR S.B
Page 47
Microprocessor- 8086
Logical right: The contents of the register or memory location are shifted right by one position
the LSB bit moves to Carry flag bit and a zero is added to the MSB position
Example
SHR AX,1
AX=0000 1111 0000 1111 and Carry=0
Result
AX=0000 0111 1000 0111 and carry=1
Arithmetic right: The contents of the register or memory location are shifted right by one
position the LSB bit moves to Carry flag bit and the sign bit is copied through the MSB position
Example
SAL AX,1
AX=1000 0000 0000 1111 and carry=0
Result
AX=1100 0000 0000 0111 and carry=1
Example
SAR SI,3
SI= 1010 1100 1010 0101 C=0
After first shift SI= 1101 0110 0101 0010 C=1
second shift SI=1110 1011 0010 1001 C=0
third shift SI= 1111 0101 1001 0100 C=1
All condition flags are affected
Rotation instructions
There are four rotate instructions.
Rotate left: The contents of the memory location or the register are rotated left by the no of
positions indicated in the instruction through the carry or without the carry.
ROL BL,4
Let BL=0001 0110 C=0
After first rotate
After second rotate
After third rotate
After fourth rotate
MANOJKUMAR S.B
C= 0
C=0
C=0
C=1
Page 48
Microprocessor- 8086
Rotate right
The contents of the memory location or the register are rotated right by the no of positions
indicated in the instruction through the carry or without the carry.
Assembly Language programming
Assembler: is a program that accepts an assembly language program as input and converts it
into an object module and prepares for loading the program into memory for execution.
Loader (linker) further converts the object module prepared by the assembler into executable
form, by linking it with other object modules and library modules.
The final executable map of the assembly language program is prepared by the loader at the
time of loading into the primary memory for actual execution.
The assembler prepares the relocation and linkages information (subroutine, ISR) for loader.
The operating system that actually has the control of the memory, which is to be allotted to
the program for execution, passes the memory address at which the program is to be loaded
for execution and the map of the available memory to the loader.
Based on this information and the information generated by the assembler, the loader
generates an executable map of the program and further physically loads it into the memory
and transfers control to for execution.
Thus the basic task of an assembler is to generate the object module and prepare the loading
and linking information.
Directives
MANOJKUMAR S.B
Page 49
Microprocessor- 8086
Each model defines the way that a program is stored in the memory system.
Tiny: data fits into one segment written in .COM format
Small: has two segments data and memory.
There are several other models too.
define byte
define word
define double word
define 10 bytes
Data1 DB
Data2 DW
10H,11H,12H
1234H
SEGMENT: statement to indicate the start of the program and its symbolic name.
Example
Name SEGMENT
Variable_name
DB
.
Variable_name
DW .
Name ENDS
Data
Data
SEGMENT
Data1
Data2
ENDS
DB
DW
.
.
Code SEGMENT
START: MOV AX,BX
Code ENDS
Similarly the stack segment is also declared.
MANOJKUMAR S.B
.DATA
ENDS
The ENDS directive indicates the end of the segment.
Page 50
Microprocessor- 8086
XX
YY
XX
ORG: Changes the starting offset address of the data in the data segment
Example
ORG 100H
100 data1 DB
10H
it can be used for code too.
PROC & ENDP: indicate the start and end of the procedure. They require a label to indicate
the name of the procedure.
NEAR: the procedure resides in the same code segment. (Local)
MANOJKUMAR S.B
Page 51
Microprocessor- 8086
Data
Example
SEGMENT
Num1 DB
10H
Num2 DB
20H
Num3 EQU 30H
ENDS
ASSUME CS:Code,DS:Data
Code SEGMENT
START: MOV
AX,Data
MOV DS,AX
MOV AX,Num1
MOV CX,Num2
ADD AX,CX
Code ENDS
A sample for small model
. MODEL SMALL
.Data
.Code
MANOJKUMAR S.B
Num1 DB
10H
Num2 DB
20H
Num3 EQU 30H
HERE: MOV
MOV
MOV
MOV
ADD
AX,@Data
DS,AX
AX,Num1
CX,Num2
AX,CX
Page 52
Microprocessor- 8086
UNIT - 3
BYTE AND STRING MANIPULATION
String instructions
REP it is a prefix used with instruction
REPE/REPZ
REPNE/REPNZ
These are used with CMPS and SCAS instructions
These instructions are used in the program as prefix.
CMPS
Compare string byte or string word
Only Flags affected
Zero flag set if strings match otherwise reset
DS:SI and ES:DI are used to point to the two strings
SCAS
Scans the string of bytes or words for an operand byte or word specified in register AL or AX
When match found the ZF=1 otherwise it is reset
LODS
Load string byte or string word
Loads the AL/AX register by the contents of a string pointed by DS:SI No flag affected
STOS
Stores contents of AL/AX register to a location in a string pointed by ES:DI
No flag affected
Strings and String Handling Instructions :
The 8086 microprocessor is equipped with special instructions to handle string operations. By
string we mean a series of data words or bytes that reside in consecutive memory locations. The
string instructions of the 8086 permit a programmer to implement operations such as to move
data from one block of memory to a block elsewhere in memory. A second type of operation
that is easily performed is to scan a string and data elements stored in memory looking for a
specific value. Other examples are to compare the elements and two strings together in order to
determine whether they are the same or different.
Move String : MOV SB, MOV SW:
An element of the string specified by the source index (SI) register with respect to the current
data segment (DS) register is moved to the location specified by the destination index (DI)
register with respect to the current extra segment (ES) register.
The move can be performed on a byte (MOV SB) or a word (MOV SW) of data. After the move
is complete, the contents of both SI & DI are automatically incremented or decremented by 1 for
a byte move and by 2 for a word move. Address pointers SI and DI increment or decrement
depends on how the direction flag DF is set.
MANOJKUMAR S.B
Page 53
Microprocessor- 8086
MOV SB
LOOP NEXT
HLT
Load and store strings : (LOD SB/LOD SW and STO SB/STO SW)
LOD SB: Loads a byte from a string in memory into AL. The address in SI is used relative to
DS to determine the address of the memory location of the string element.
(AL) [(DS) + (SI)]
(SI) (SI) + 1
LOD SW : The word string element at the physical address derived from DS and SI is to be
loaded into AX. SI is automatically incremented by 2.
(AX) [(DS) + (SI)]
(SI) (SI) + 2
STO SB : Stores a byte from AL into a string location in memory. This time the contents of ES
and DI are used to form the address of the storage location in memory
[(ES) + (DI)] (AL)
(DI) (DI) + 1
STO SW : [(ES) + (DI)] (AX)
(DI) (DI) + 2
Mnemonic Meaning
Move
MOV SB
String
Byte
MOV SW
Move
String
Word
LOD SB /
LOD SW
Load
String
STOSB/
STOSW
Store
String
MANOJKUMAR S.B
Format
MOV
SB
MOV
SW
LOD
SB/
LOD
SW
STOSB/
STOSW
Operation
((ES)+(DI))((DS)+(SI))
(SI)(SI) 1
(DI) 1
((ES)+(DI))((DS)+(SI))
((ES)+(DI)+1)(DS)+(SI)+1)
(SI) (SI) 2
(DI) (DI) 2
Flags affected
None
None
None
((ES)+(DI))(AL) or (AX)
(DI) (DI) 71 or 2
None
Page 54
Example :
AGAIN :
NEXT :
Microprocessor- 8086
MANOJKUMAR S.B
MOVS
STOS
CMPS
SCAS
CMPS
SCAS
Used with
Meaning
Repeat while not end of
string CX0
CX0 & ZF=1
CX0 & ZF=0
Page 55
Example :
Microprocessor- 8086
CLD
; DF =0
MOV AX, DATA SEGMENT ADDR
MOV DS, AX
MOV AX, EXTRA SEGMENT ADDR
MOV ES, AX
MOV CX, 20
MOV SI, OFFSET MASTER
MOV DI, OFFSET COPY
REP MOVSB
Moves a block of 32 consecutive bytes from the block of memory locations starting at offset
address MASTER with respect to the current data segment (DS) to a block of locations starting
at offset address copy with respect to the current extra segment (ES).
Auto Indexing for String Instructions :
SI & DI addresses are either automatically incremented or decremented based on the setting of
the direction flag DF.
When CLD (Clear Direction Flag) is executed DF=0 permits auto increment by 1.
When STD (Set Direction Flag) is executed DF=1 permits auto decrement by 1.
Mnemonic
CLD
STD
Meaning
Clear DF
Set DF
Format
CLD
STD
Operation
(DF) 0
(DF) 1
Flags affected
DF
DF
1. LDS Instruction:
LDS register, memory (Loads register and DS with words from memory)
This instruction copies a word from two memory locations into the register specified in the
instruction. It then copies a word from the next two memory locations into the DS register. LDS
is useful for pointing SI and DS at the start of the string before using one of the string
instructions. LDS affects no flags.
Example 1 :LDS BX [1234]
Copy contents of memory at displacement 1234 in DS to BL. Contents of 1235H to BH. Copy
contents at displacement of 1236H and 1237H is DS to DS register.
Example 2 : LDS, SI String Pointer
(SI) [String Pointer]
(DS) [String Pointer +2]
DS, SI now points at start and desired string
2.
LEA Instruction :
Load Effective Address (LEA register, source)
This instruction determines the offset of the variable or memory location named as the source
and puts this offset in the indicated 16 bit register.
LEA will not affect the flags.
Examples :
MANOJKUMAR S.B
Page 56
Microprocessor- 8086
LES instruction :
LES register, memory
Example 1: LES BX, [789A H]
(BX) [789A] in DS
(ES) [789C] in DS
Example 2 : LES DI, [BX]
(DI) [BX] in DS
(ES) [BX+2] in DS
Modular Programming
Concepts:
The basic idea is the classical Divide & Conquer approach. Program is composed from
several smaller modules. Modules could be developed by separate teams concurrently.
The modules are only assembled producing .OBJ modules (Object modules). Each
module is produced from a separate Assembly Language program.
The ML command of MASM typically used for smaller programs actually consists of the
two distinct steps of assembling to produce the object file and then Linking the object
module to produce the .EXE file.
MANOJKUMAR S.B
Page 57
Microprocessor- 8086
Concepts - 2
ASM File #1
Assembler
ASM File #2
Assembler
.OBJ file
.OBJ file
Linker
ASM File #n
Assembler
.EXE
.OBJ file
A further concept that is extremely useful is that of a Library of Object Modules. Frequently
used procedures could be assembled in to object modules and these object modules are placed in
a Library file that is linked into the application. Note that only the required object modules are
pulled from the Library to be linked in to the final application. The concept of a Library is
explored in detail later.
This idea is illustrated in the following figure:
Concepts - 3
ASM File
Assembler
ASM File
Assembler
.OBJ file
.OBJ file
Linker
.EXE
Page 58
Microprocessor- 8086
In Module A , we defined
BUF1 is not defined in Module B. Thus, when assembling Module B, we will get the
assembly error of Undefined Symbol. Note that the symbol is actually defined in
Module A. But, the two modules are assembled independently!
Solution:
What happens if BUF1 is declared as external in one module; but Linker does not
find its definition in any of the modules being linked together? This will be a
Link Error, something like: Globally Unresolved Symbol
Properly used, Public and External facilities allow modules to communicate with each
other, which is essential for Modular Programming.
Page 59
PUBLIC
Microprocessor- 8086
BUF1
PUBLIC
BUF2
BUF1 DB
10 DUP (?)
BUF2 DW
10 DUP (?)
.CODE
.STARTUP
PUBLIC
RDKEY PROC
RDKEY
FAR
MOV AH, 1
INT 21H
RDKEY
ENDP
END
In another program PROG2.ASM, we make use of the symbols BUF1, BUF2 and RDKEY.
We declare them as EXTRN. Such a declaration indicates that the definitions for these
symbols are not in this module; how ever this is not to be treated as an error. These symbols
are expected to be defined in other modules which will be specified during LINK time.
Further, note that the type of the EXTRN data items must be declared for proper assembly.
Similarly, the FAR / NEAR nature of an EXTRN procedure also must be declared for the
Assembler to generate correct code.
File 2: PROG2.ASM
.MODEL
SMALL
.DATA
EXTRN
BUF1:BYTE
EXTRN
BUF2:WORD
.CODE
EXTRN
RDKEY:FAR
.STARTUP
MOV DX, OFFSET BUF1
MOV CX, 10
L1:
CALL RDKEY
STOSB
MANOJKUMAR S.B
Page 60
LOOP L1
Microprocessor- 8086
MOV BUF2, AX
.EXIT
END
Another way that more clearly illustrates the Modular Programming approach is as
follows :
Only assemble (not link) the two programs separately, getting the two .OBJ files.
Run LINK utility and specify the two .OBJ files as inputs.
LIBRARIES:
Frequently used procedures for a given application domain may be placed in a Library
File as .OBJ files.
Only the required .OBJ files are extracted from the Library File and linked in to the
program.
LIB Command:
The LIB command provides the following facilities:
Replace an existing .OBJ file in the Library with another .OBJ file with the same name
(equivalent to Delete followed by Add)
If the named Library file does not exist, the system prompts whether to create? Type
Y
MANOJKUMAR S.B
Page 61
Microprocessor- 8086
It prompts for operation (operation could be specified on command line also). The
Operation can be:
Examples
Create a Library file called MYIO1.LIB and add the module PROG1 to the library:
>LIB MYIO1.LIB
Copyright messages etc from the utility
Library file does not exist. Create? Y
Operations: +PROG1
List File: MYIO1
To the Library file called MYIO1.LIB add the module PROG2:
> LIB MYIO1.LIB
Copyright messages etc from the utility
Operations: +PROG2
>
To the Library file called MYIO1.LIB add the module PROG3:
>LIB MYIO1.LIB
Copyright messages etc from the utility
Operations: +PROG3
>
Alternatively, the 3 modules could be added to the Library file as shown below:
>LIB MYIO1.LIB
Copyright messages etc from the utility
Library file does not exist. Create? Y
Operations: PROG1 + PROG2 + PROG3
List File: MYIO1
>
From the Library file called MYIO1.LIB, remove the module PROG2:
> LIB MYIO1.LIB
Copyright messages etc from the utility
Operations: -PROG2
MANOJKUMAR S.B
Page 62
>
Microprocessor- 8086
Once we create Library files, we can use them to link the required modules in to the
application program by specifying the Library files to the Linker. With ML or with
LINK, specify the Library files required in response to the prompt
Libraries [.lib]:
Only one copy of the procedure is stored in the memory; and it can be called as many
times as needed.
As only one copy is stored, it saves memory; but has execution time overhead for the
call and return operations.
CALL transfers control to the procedure like with a jump; but unlike a jump, procedure
has a RETURN instruction which returns control to the instruction following the CALL
instruction! In order to implement such a return, the necessary information is stored on a
stack, before transferring control to the procedure.
Further, nested procedures calls are possible. In other words, Procedure A can call
Procedure B which in turn calls Procedure C. After completing Procedure C, control
returns to Procedure B and after completing Procedure B, control returns to Procedure A.
Logically, such a nesting of calls can be to any level, though in practice Assemblers
impose implementation-dependent limits on the nesting depth.
MANOJKUMAR S.B
Page 63
Microprocessor- 8086
It is also possible for a Procedure to call itself (a recursive procedure). Of course, to avoid
infinite regress, the procedure would have an alternative that would not involve recursion.
Examples of recursive procedures are described in later sessions.
Procedure
JMP
CALL
RETURN
Nesting of Procedures
CALL
CALL
CALL
RETURN
RETURN
RETURN
MANOJKUMAR S.B
Page 64
Microprocessor- 8086
From the above figure we see that return addresses are known in one order and are used
for implementing return in exactly the reverse order. Thus a Stack would be the most
convenient data structure for storing return addresses
Call can be to a procedure in the same code segment. In such a case, we specify only IP
as relative distance or indirectly as actual value. This is known as NEAR CALL.
Call can be to a procedure in a different code segment. I such a case, we need to specify
both IP and CS (directly or indirectly). This is known as a FAR CALL
In ALP, procedure starts with PROC directive and ends with ENDP directive.
Similar to Near Jump except that current IP is saved on the stack before transferring
control to the new IP with CS remaining the same.
opcode
E8
Displacement
Low Byte
High Byte
MANOJKUMAR S.B
Page 65
Microprocessor- 8086
10003
10002
10001
10000
20
1A
E9
00
03
Saving IP:
Before CALL:
SS = A000 H ; SP = A000 H
IP = 0003
10006
10005
10004
MANOJKUMAR S.B
Page 66
Microprocessor- 8086
In ALP, NEAR indicates near procedure. The following program fragment illustrates the coding
and using of a near procedure:
ST1
ST1
ENDP
CALL ST1
FAR CALL Instruction:
FAR CALL is like FAR JMP in the sense that it can call a procedure available any where
in the code space.
Both current IP and CS are saved on the stack and then control is transferred to the new
CS:IP
The instruction is 5 Byte long. The first byte specifies the opcode, the next two bytes
specify the new IP value and the next two bytes specify the new CS value. The format is
shown below:
opcode
E8
MANOJKUMAR S.B
IP
Low
Byte
CS
High
Byte
Low
Byte
High
Byte
Page 67
Microprocessor- 8086
The operation of the Far Call instruction is illustrated in the following figure:
10
00
00
20011
20010
04
9A
10005
10004
10003
Far Procedure
Example:
SUM1 PROC FAR
SUM1 ENDP
Now, a CALL to SUM1 is assembled as FAR CALL.
CALL with Register Operands:
As with JMP, 16-Bit register may be used as the operand for the CALL instruction.
(Indirect CALL)
Again, as with JMP, contents of the register are transferred directly into IP (no concept
of relative displacement).
MANOJKUMAR S.B
Page 68
Microprocessor- 8086
Like the corresponding JMP instruction, this instruction uses [] form of addressing to
directly access a table of CALL addresses.
FAR PTR directive indicates a far CALL ( the table is assumed to contain double words
giving CS, IP values)
Otherwise a near CALL is indicated. ( the table is assumed to contain words giving IP
values)
The control returns to the instruction following the CALL instruction in the calling
program. Corresponding to the two varieties of CALL instructions (near & far), two
forms of RET instructions (near & far) exist.
Near RET instruction pops a 16-Bit word from the stack and places it in the IP.
Far RET instruction pops two 16-Bit words from the stack and places them in IP & CS.
In ALP, RET is written within the procedure, before the ENDP directive and the
Assembler will automatically select the proper RET instruction!
MANOJKUMAR S.B
Page 69
Microprocessor- 8086
Macros
Macros provide several powerful mechanisms useful for the development of generic
programs.
When a macro is invoked, the associated set of instructions is inserted in place in to the
source, replacing the macro name. This macro expansion is done by a Macro
Preprocessor and it happens before assembly. Thus the actual Assembler sees the
expanded source!
We could consider the macro as shorthand for a piece of text; somewhat like a new
pseudo-code instruction.
Macros are similar to procedures in some respects, yet are quite different in many other
respects.
Procedure:
Execution time overhead is present because of the call and return instructions.
Macro:
However, there is no execution overhead because there are no additional call and
return instructions. The code is in-place.
MANOJKUMAR S.B
Page 70
Microprocessor- 8086
CALL
Macro invoked
MACRO
Inserted in
place
RETURN
MACRO Definition:
A macro has a name. The body of the macro is defined between a pair of directives, MACRO
and ENDM. Two macros are defined in the example given below.
Examples of Macro Definitions:
; Definition of a Macro named PA2C
PA2C
MACRO
PUSH AX
PUSH BX
PUSH CX
ENDM
MACRO
POP CX
POP BX
POP AX
ENDM
MANOJKUMAR S.B
Page 71
Microprocessor- 8086
The following examples illustrate the use of macros. We first show the source with macro
invocation and then show how the expanded source looks.
Program with macro invocations:
PA2C
MOV CX, DA1
MOV BX, DA2
ADD AX, BX
ADD AX, CX
MOV DA2, AX
POPA2C
When the Macro Preprocessor expands the macros in the above source, the expanded source
looks as shown below:
PUSH AX
PUSH BX
PUSH CX
MOV CX, DA1
MOV BX, DA2
ADD AX, BX
ADD AX, CX
MOV DA2, AX
POP CX
POP BX
POP AX
Note how the macro name is replaced by the associated set of instructions. Thus, macro name
does not appear in the expanded source code. In other words, the actual Assembler does not
see the macros. What gets assembled is the expanded source. This process is illustrated in
the following figure:
MANOJKUMAR S.B
Page 72
Microprocessor- 8086
MACROS (continued)
Macro
Preproc
essor
.ASM file
with Macros
Assemble &
Link
.EXE file
A,B
PUSH AX
MOV AX, B
MOV A, AX
POP
AX
ENDM
MANOJKUMAR S.B
Page 73
Microprocessor- 8086
The macro is invoked in the following code with actual parameters as VAR1 and VAR2.
Thus during the macro expansion, the parameter A is replaced by VAR1 and the parameter B
is replaced by VAR2.
COPY
VAR1, VAR2
AX
Assume that a macro definition includes a label RD1 as in the following example:
READ MACRO
PUSH
RD1: MOV
A
DX
AH, 06
MOV
DL, 0FFH
INT
21H
JE
RD1
MOV
A, AL
POP
DX
ENDM
The problem is that the label RD1 appears in the expansion of READ VAR1 as well as
in the expansion of READ VAR2. Hence, the label RD1 appears in both the expansions.
In other words, the Assembler sees the label RD1 at two different places and this results
in the Multiple Definition error!
MANOJKUMAR S.B
Page 74
READ MACRO
Microprocessor- 8086
A
LOCAL
RD1
PUSH
DX
RD1: MOV
AH, 06
MOV
DL, 0FFH
INT
21H
JE
RD1
MOV
A, AL
POP
DX
ENDM
Now, in each invocation of READ, the label RD1 will be replaced, automatically, with a
unique label of the form ??xxxx ; where xxxx is a unique number generated by
Assembler. This eliminates the problem of multiple definitions in the expanded source.
VAR1
DX
AH, 06
MOV
DL, 0FFH
INT
21H
JE
MOV
VAR1, AL
POP
DX
Subsequently, if we write
READ
VAR2
DX
AH, 06
MOV
DL, 0FFH
INT
21H
MANOJKUMAR S.B
Page 75
Microprocessor- 8086
JE
MOV
VAR2, AL
POP
DX
Note how each invocation of the READ macro gets expanded with a new and unique
label, generated automatically by the Assembler, in place of the local variable RD1. Further,
note that LOCAL directive must immediately follow the MACRO directive. Another feature
to note is that Comments in Macros are preceded by ;; (two semicolons) , and not as usual by
; (a single semicolon).
File of Macros:
We can place all the required Macros in a file of its own and then include the file into the
source.
Advanced Features:
Conditional Assembly
Conditional Assembly:
A set of statements enclosed by IF and ENDIF are assembled if the condition stated with
IF is true; otherwise, the statements are not assembled; no code is generated.
Allows development of generic programs. From such a generic program, we can produce
specific source programs for specific application contexts.
Example: Assume that our generic program has the following statements:
IF WIDT
WIDE
DB
72
DB
80
ELSE
WIDE
ENDIF
Now the assembly language program that is generated depends on the value of WIDT.
Assume the block is preceded by
MANOJKUMAR S.B
Page 76
Microprocessor- 8086
WIDT EQU 1
72
It is important to note that the Assembler sees a source file that has only the above
statement.
Another case:
WIDT EQU 0
IF WIDT
WIDE DB 72
ELSE
WIDE DB 80
ENDIF
80
There are several other directives that can be used for Conditional Assembly as listed
below:
IF
IFB
IFNB
IFDEF
IFNDEF
IFIDN
IFDIF
With each of the above constructs, the code that follows gets assembled only if the stated
condition is true.
REPEAT Statement:
This statement allows a block of code to be repeated the specified number of times. This
avoids repetitive typing and is much more elegant than Editor-level Copy-and-Paste
operation.
Example:
REPEAT 3
INT 21H
MANOJKUMAR S.B
Page 77
INC
DL
Microprocessor- 8086
ENDM
The generated code would be 3 repetitions of the block of 2 statements enclosed within
REPEAT and ENDM as shown below:
INT 21H
INC DL
INT 21H
INC DL
INT 21H
INC DL
WHILE Statement:
This statement allows a block of code to be repeated while the condition specified with the
WHILE is true.
Example: Consider the following code
SQ LABEL BYTE
SEED = 1
RES = SEED * SEED
WHILE RES LE 9
DB
RES
SEED = SEED + 1
RES = SEED * SEED
ENDM
Note that SEED and the arithmetic statements involving SEED and RES are all Assembly
time actions. Apart from the initial label SQ, the only statement to actually get repeated is
DB RES.
The logic is follows: Initially the label SQ is generated. SEED is initialized to 1 and RES is
computed as 1 * 1 = 1. Now RES LE 9 is true as the value of RES is 1 which is less than 9.
So the code DB 1 is generated. The next statement within the scope of WHILE, SEED =
SEED + 1 is executed making SEED assume the value of 2. The next statement within the
scope of WHILE is RES = SEED * SEED. This is also executed and RES assumes the value
of 4. This completes one pass of execution of the WHILE block. So, the condition associated
MANOJKUMAR S.B
Page 78
Microprocessor- 8086
with WHILE is again evaluated. This is again TRUE as 4 is less than 9. So again DB 9 is
generated. Reasoning as before, we see that DB 9 is also generated. However, in the next
pass SEED is 4 and RES is 16. So the condition RES LE 9 evaluates to FALSE and WHILE
loop is exited!
Thus the generated code is:
SQ
DB
01
DB
04
DB
09
FOR Statement:
This is very similar to the FOR of languages like PERL. With the FOR statement, a control
variable and a list of values are specified. The control variable is successively assigned
values from the specified list and for each such value, the following block of statements is
repeated.
Example:
DISP MACRO CHR:VARARG
MOV AH, 2
FOR ARG, <CHR>
MOV DL, ARG
INT
21H
ENDM
ENDM
The outer Macro has one parameter which is specified as sequence of characters of variable
length. The inner FOR statement has two enclosed statements which will be repeated for
each value in the list <CHR>. Thus in the following illustration, DISP is invoked with 3
characters as parameters. The two statements within FOR scope are thus repeated 3 times
with ARG successively assuming the 3 characters.
Thus, the statement
DISP S,B,M
gets expanded as
MOV
AH, 2
MOV
DL,S
MANOJKUMAR S.B
Page 79
Microprocessor- 8086
INT
21H
MOV
DL, B
INT
21H
MOV
DL, M
INT
21H
Summary:
Used properly, they can reduce effort required to develop large and complex programs.
Further, Macros make it easy to develop Generic Programs which can be easily adapted
for specific applications.
Expertise with Modular programming techniques and Macros is essential for developing
industry-strength large scale software in Assembly Language.
Often Data available in one format needs to be converted in to some other format.
Examples:
ASCII to Binary
Binary to ASCII
Algorithm
Look Up Table
Page 80
Microprocessor- 8086
To display this on the screen, we need to convert this binary number in to Two ASCII
characters, 6 and 7.
ASCII code for character 6 is 36H and
ASCII code for character 7 is 37H.
So, we need to produce 36H and 37H as output given 43H as input.
Another Example: Binary number: 0000 0010 0100 0011 = 0243H = 579 D
To display this on the screen, we need Three ASCII characters, 5, 7 and 9.
ASCII code for character 5 is 35H,
ASCII code for character 7 is 37H, and
ASCII code for character 9 is 39H
So, we need to produce 35H, 37H and 39H as output given 0243H as input
Divide 57 by 10;
Divide 5 by 10;
While retrieving, add 30H to convert the digit to ASCII code and then display it (or print
it, or save it)
MANOJKUMAR S.B
Page 81
Microprocessor- 8086
; Test value
CALL B2A
.EXIT
B2A
PROC
NEAR
PUSH DX
PUSH CX
PUSH BX
B2A1:
MOV
CX, 0
MOV
BX, 10
; Divisor is 10
MOV DX, 0
DIV
; Divide by 10
BX
PUSH DX
INC
CX
OR
AX, AX
JNZ
B2A1
DX
; Retrieve remainder in DL
; Convert to ASCII
INT
21H
LOOP B2A2
MANOJKUMAR S.B
Microprocessor- 8086
BX
POP
CX
POP
DX
RET
B2A
ENDP
END
Notes:
DIV instruction requires the 32-bit dividend in the register pair DX and AX. The
number to be converted (and subsequently, the quotient) is in AX. So, DX is cleared
to 0 to set up the correct dividend.
As the digits are pushed on to the Stack, their count maintained in CX is incremented.
Thus the POPs occur in a loop controlled by CX. In the same loop, as we POP each
digit, 30H is added to produce the output in ASCII.
When the input number is less than 100, an alternative, simpler method exists.
AAM (ASCII Adjust AX After Multiplication) instruction converts value in AX in to 2Digit Unpacked BCD and leaves it in AX.
Separate the two bytes (unpack) to get the two ASCII characters representing the given
number (33H and 39H).
Works only when the number is less than 100 as the maximum unpacked BCD that we
can have in the AX register is 0909H only.
MANOJKUMAR S.B
Page 83
Microprocessor- 8086
AL, 2AH
; Test value
CALL
B2A
.EXIT
B2A
PROC
NEAR
PUSH DX
MOV AH, 0
; AX = Number
AAM
; AX = Unpacked BCD
; Convert to ASCII
PUSH AX
; Now, unpack and display
MOV DL, AH
; First Digit
; Display Function
INT
21H
POP
AX
; Retrieve value
MOV DL, AL
; Second Digit
; Display Function
INT
21H
DX
RET
B2A
ENDP
END
MANOJKUMAR S.B
Page 84
Microprocessor- 8086
Refinements:
Can we replace leading 0 with a blank so that the display looks better? Thus, instead of
07, the display should be 7.
Yes. We need to check if the first digit is 0. If so, display 20H (blank);
else,
We need to modify the previous program to incorporate this check for a leading 0.
MOV
MOV
INT
21H
B2A1:
; First Digit = 0?
JZ
B2A1
; Add 10H more to get the correct ASCII Code for the digit
MOV DL , AH
; First Digit
21H
AL, 2AH
; Test value
CALL
B2A
.EXIT
B2A
PROC
NEAR
PUSH DX
MOV AH, 0
MANOJKUMAR S.B
; AX = Number
Page 85
Microprocessor- 8086
AAM
; AX = Unpacked BCD
; Convert to ASCII
PUSH AX
; Now, unpack and display
ADD AH, 20H
CMP AH, 20H
; First Digit = 0?
JZ
B2A1
; Display Function
INT
21H
POP
AX
; Retrieve value
MOV DL, AL
; Second Digit
; Display Function
INT
21H
DX
RET
B2A
ENDP
END
MANOJKUMAR S.B
Page 86
Microprocessor- 8086
Conversion Procedure:
First ASCII digit 31H; Subtract 30H to get corresponding BCD digit 01H.
Next ASCII digit 35H; Subtract 30H to get corresponding BCD digit 05H.
Next ASCII digit 36H; Subtract 30H to get corresponding BCD digit 06H.
Based on the above ideas, the following program implements the ASCII to Binary
Conversion.
SMALL
.DATA
TEMP
DW
.CODE
.STARTUP
CALL RDNUM
MOV TEMP, AX
.EXIT
MANOJKUMAR S.B
Page 87
Microprocessor- 8086
RDN1:
MOV CX, 10
; Multiplier is 10
MOV BX, 0
; Result initialized to 0
MOV AH, 1
INT
21H
; Check the character. If less than 0 or greater than 9 Number entry is over
CMP AL, 0
JB
RDN2
CMP AL,9
JA
RDN2
AL, 30H
; BCD Digit
PUSH AX
MOV AX, BX
MUL
CX
MOV
BX, AX
POP
AX
MOV
AH, 0
; AX = Current Digit
ADD
BX, AX
; Update Result
JMP
RDN1
; Repeat
; Result = Result * 10
MOV
AX, BX
POP
CX
POP
BX
; Result in AX
RET
RDNUM
ENDP
END
Notes:
MANOJKUMAR S.B
Page 88
Microprocessor- 8086
In the procedure, RDNUM, the result is accumulated in the register BX and at the
end, it is moved in to register AX. The result in AX is moved, in the calling program,
in to the memory location TEMP.
The BCD digit is in AL. AH is cleared to 0 so that the 16-bit value in AX represents
the correct value and thus can be added directly to the accumulating result in BX.
This part of the code must be changed to implement 32-bit addition if larger results
are to be supported.
MANOJKUMAR S.B
Page 89
Microprocessor- 8086
a
g
d
Control Byte:
b
dp
c
Bit = 1 Segment is on
= 0 Segment is off
dp
As an example of determining the display code corresponding to a given BCD digit, the
following figure shows the display of digit 3 and the determination of the corresponding 7Segment code:
f
e
a
g
d
Control Byte: a = 1; b = 1; c = 1; d = 1;
e = 0; f = 0 ; g = 1; dp = 0.
b
0
dp
Based on the above logic, the following FAR Procedure returns the 7-Segment code in the
AL register, corresponding to the BCD digit provided as input parameter in the AL register
before calling the procedure.
; BCD to 7-Segment Code Program
; Input: AL = BCD Digit
; Output: AL = 7-Segment code.
MANOJKUMAR S.B
Page 90
BT7SEG
Microprocessor- 8086
PROC FAR
PUSH BX
BX
RET
TABLE
BT7SEG
DB
3FH ; 0
DB
06H ; 1
DB
5BH ; 2
DB
4FH ; 3
DB
66H ; 4
DB
6DH ; 5
DB
7DH ; 6
DB
07H ; 7
DB
7FH ; 8
DB
6FH ; 9
ENDP
Notes:
XLAT instruction does not normally contain an operand. Here we are using the operand
(TABLE). It is a dummy operand! It is being used here only to specify segment override.
XLAT uses DS by default. Here the table is in CS. So segment override is being
specified.
DOS provides a variety of services like Reading the Keyboard, Writing to Display, Disk
access facilities etc.
MANOJKUMAR S.B
Page 91
Microprocessor- 8086
Examples:
Before invoking a DOS function, user must Save & Restore registers if necessary.
Lower Level direct control of various I/O devices is possible via BIOS (Basic Input
Output System) function calls like INT 10H, INT 11H etc. (Not discussed in this session)
Reading Keyboard:
When a character typed on the key board is read, it may return the standard 7-Bit ASCII
code corresponding to the key typed. Additionally, Extended ASCII codes are used to
represent Function Keys, Key Combinations (Keys in combination with Shift, Control,
Alt keys). The full list of key codes used in a standard PC is available in the Text Book.
The code corresponding to the key typed is read. Further, the same character is displayed
on the screen also (echo). This routine responds to CNTL-C (That is, when CNTL-C is
typed, an immediate exit to DOS occurs)
If AL = 0, Extended ASCII code is indicated. In such a case, INT 21H must be executed
again to get the Extended ASCII code.
The Read Key Procedure may distinguish these cases suitably (for example, via Carry
flag). In the following routine, CARRY is cleared to indicate standard key code and it is
set to indicate extended key code.
MANOJKUMAR S.B
Page 92
Microprocessor- 8086
21H
OR
AL, AL
JNZ
KEY1
INT
21H
STC
KEY1: RET
KEY
ENDP
The code corresponding to the key typed is read. However, the character is not displayed
on the screen also (no echo). Also, this routine does not respond to CNTL-C (That is,
typing CNTL-C does not cause an exit to DOS)
On return, Z = 1 indicates that no char was typed. Else, AL ASCII code of character
typed. In this case, if AL = 0, Extended ASCII code is indicated. Now, INT 21H must be
executed again to get the Extended ASCII code.
The Read Key Procedure may distinguish these cases suitably (for example, via Carry
flag). In the following routine, Z = 1 indicates that no key was typed. If Z = 0, the key
code is returned in AL and the CARRY is cleared to indicate standard key code and it is
set to indicate extended key code.
KEYS
PROC FAR
MOV
AH, 06H
MOV
DL, 0FFH
INT
21H
JE
KEYS
OR
AL, AL
JNZ
KEYS1
INT
21H
STC
KEYS1: RET
MANOJKUMAR S.B
Page 93
KEYS
Microprocessor- 8086
ENDP
This service is used to read an entire line of characters. The characters read are
maintained in a buffer. Thus, this is Buffered Keyboard Input service.
Function Code: 0A H
Input Parameters:
DS:DX = Address of the Buffer
First byte of Buffer = Maximum Number of Characters to be read (up to 255)
Characters are read (displaying them as they are read) until the specified number of
characters are read OR until the Enter key (code is 0DH) is typed.
Upon return:
Hence, the Buffer Size must be 2 more than the number of characters to be read!
.MODEL
SMALL
.DATA
BUF1 DB
.CODE
.STARTUP
MOV BUF1, 100
21H
.EXIT
END
Display One ASCII Character:
Function Code: 02H or 06H As both of these are quite similar, we will illustrate the use
of Function Code 6 only.
MANOJKUMAR S.B
Page 94
.MODEL
Microprocessor- 8086
TINY
.CODE
.STARTUP
MOV AH, 06H
MOV DL, 41H ; Display character A, ASCII code is 41H
INT
21H
.EXIT
END
Display Character String:
It could include control characters like Carriage Return (0DH), Line Feed (0AH) etc.
However, the character $ is used as the terminator and thus can not be part of the string.
($ could be displayed as a single character using Function Code 06H as explained above.)
MODEL SMALL
.DATA
MSG1
DB
.CODE
.STARTUP
MOV AH, 09H
MOV DX, OFFSET MSG1
INT
21H
.EXIT
END
Terminate Program:
EXIT directive used in all the examples described so far effects program termination by
using Function Code 4CH (Terminate Program)
MANOJKUMAR S.B
Page 95
Microprocessor- 8086
21H
Instead of .EXIT, the above 2 lines could be directly written by the programmer.
These services allow the user to set / read the interrupt vector corresponding to a type
code. This is preferred to directly manipulating the Interrupt Vector Table.
MANOJKUMAR S.B
Page 96
Microprocessor- 8086
UNIT - 4
8086 INTERRUPTS
Introduction:
When the interrupt occurs and is recognized, the values of the Flag register, CS and IP
registers are saved on the stack and the control is transferred from the executing program
to an associated Interrupt Service Routine (ISR). After completing the ISR control
returns to the interrupted program.
Thus, this mechanism is similar to yet different from Far Call. CALL is always an
instruction in the program. The mechanism of calling & returning from ISR is some what
different from normal CALL mechanism.
The concept of an interrupt vis--vis a far call is illustrated in the following figure:
Interrupts
CALL
RETURN
Interrupt Service
Routine (ISR)
Interrupt
Return from
Interrupt
(ISR; Interrupt Service Procedure; Interrupt Handler All mean the same)
Interrupt can be because of:
An external interrupt signal at the pins NMI or INTR . These are called Hardware
interrupts (studied in a later session).
Internal causes resulting from execution of other instructions etc, like Interrupt
on Divide Error. (sometimes called exceptions)
MANOJKUMAR S.B
Page 97
Microprocessor- 8086
An interrupt instruction has an associated numeric operand called interrupt type code;
a number in the range of 0 to 255 (00H to FFH). (Thus we can have a total of 256 type codes)
The interrupt type code:
Is provided by external hardware like Interrupt Controller (in the case of external
interrupts).
Whatever be the source of interrupt & what ever be the type code:
One interrupt vector is required for each interrupt type code. We have 256
possible interrupt type codes and thus 256 possible interrupt vectors.
Consequently, to specify all these interrupt vectors, we need 256 x 4 = 1024 bytes
of memory.
Interrupt Vector Table: A table of 1024 bytes containing the 256 interrupt vectors.
Address range is 00000H (0:0) to 003FFH (0:03FFH). This is a memory block of 1KB
starting from 00000H
Example: Interrupt type code = 20H Corresponding interrupt vector starts at 20H x 4
= 80H. (In locations 80H, 81H, we have IP and in locations 82H, 83H, we have CS)
Another Example: Interrupt type code = 00H Corresponding interrupt vector starts at
00H x 4 = 00H. (00H, 01H: IP ; 02H, 03H: CS)
MANOJKUMAR S.B
Page 98
Microprocessor- 8086
0
1
2
3
4
5 1F
20H - FFH
Address
Function
00H 03H
Divide Error
04H 07H
Single Step
08H 0BH
NMI
0CH 0FH
Breakpoint
10H 13H
Interrupt on Overflow
14H 7FH
Reserved
80H 3FFH
User-Defined
Interrupt Processing:
When an interrupt is to be processed:
T and I flags are cleared (disabling Single Step and External Interrupts)
Control is transferred to the new CS : IP specified in the Interrupt Vector. (ISR begins
execution.)
To return from the ISR, the interrupt return (IRET) instruction is used.
I Flag:
I flag is cleared disabling recognition of external interrupts. To enable them within the
ISR, programmer can enable them using STI (Set Interrupt Flag) instruction.
CLI (Clear Interrupt Flag) instruction also clears the I flag disabling recognition of
interrupts from INTR pin.
MANOJKUMAR S.B
Page 99
Microprocessor- 8086
T Flag:
Setting the T flag enables Single Step. The, after the execution of every instruction, an
interrupt of type 1 is generated. This feature is quite useful for debugging. (The ISR can
display Register values & other useful information.)
Evidently, within the ISR, Single Step should not be in effect! So T is cleared. On return
from ISR, the value of T is restored.
T Flag - 1
Program to be traced
(single-stepped):
T flag = 1
INT 3 is 1 Byte long. (The only special case). Rest of INT n instructions are all 2
Byte long.
These instructions are commonly used to access system procedures. Example: INT 21H
to access DOS services.
A software interrupt instruction is more convenient than far call. It occupies less memory
as it needs only 1 or 2 bytes as against the 5 bytes required for a far Call. Further, there is
no need to remember the CS:IP values. These values are obtained from the Interrupt
Vector Table.
MANOJKUMAR S.B
Page 100
opcode
Microprocessor- 8086
type code
INT 3 Instruction:
Only INT n instruction that is 1 Byte long! Rest are 2 Byte long.
This is often used to effect a breakpoint in the program. The breakpoint service routine
can provide Register values and other information useful for debugging. Any INT n can
be used for implement a breakpoint. However, as INT 3 is only
1-byte long, it is
This instruction is placed in the program usually after arithmetic instructions that may
lead to overflow condition. (Recall that JO instruction also detects overflow condition.)
Interrupts in PC:
Several interrupt type codes are dedicated to interrupts from hardware devices like key
board, mouse etc. Examples: 9 for Keyboard; 17H for Parallel Port.
MANOJKUMAR S.B
Page 101
Microprocessor- 8086
Unit - 5
8086 INTERFACING
MANOJKUMAR S.B
Page 102
Microprocessor- 8086
The first 3 bits of # sent to control port selects one of 8 control words.
First three bits given below select one of 8 control registers (opcode).
000DDMMM
Mode set: Opcode 000.
DD sets displays mode.
MMM sets keyboard mode.
DD field selects either:
8- or 16-digit display
Whether new data are entered to the rightmost or leftmost display position.
MMM field:
Encoded: Sl outputs are active-high, follow binary bit pattern 0-7 or 0-15.
Decoded: SL outputs are active-low (only one low at any time). Pattern output: 1110, 1101,
1011, 0111.
Strobed: An active high pulse on the CN/ST input pin strobes data from the RL pins into an
internal FIFO for reading by micro later.
2-key lockout/N-key rollover: Prevents 2 keys from being recognized if pressed
simultaneously/Accepts all keys pressed from 1st to last.
Interface of 8279
001PPPPP
The clock command word programs the internal clock driver.
The code PPPPP divides the clock input pin (CLK) to achieve the desired operating frequency,
e.g. 100KHz requires 01010 for a 1 MHz CLK input.
010Z0AAA
The read FIFO control word selects the address (AAA) of a keystroke from the FIFO buffer
(000 to 111).
MANOJKUMAR S.B
Page 103
Microprocessor- 8086
MANOJKUMAR S.B
Page 104
Microprocessor- 8086
Row and column number are given the rightmost 6 bits (scan/return).
This can be converted to ASCII using the XLAT instruction with an ASCII code lookup table.
The CT and SH indicate whether the control or shift keys were pressed.
The Strobed Keyboard code is just the state of the RLx bits at the time a 1 was strobed on the
strobe input pin.
Basic I/O Instructions
We discussed IN, OUT, INS and OUTS as instructions for the transfer of data to and from an I/O
device. IN and OUT transfer data between an I/O device and the microprocessors
accumulator (AL, AX or EAX).
The I/O address is stored in:
Register DX as a 16-bit I/O address (variable addressing).
The byte, p8, immediately following the opcode (fixed address).
MANOJKUMAR S.B
Page 105
Microprocessor- 8086
8-bit port addresses used to access system board device, e.g. timer and keyboard.
16-bit port addresses used to access serial and parallel ports, harddrives, etc.
I/O Map
The basic input device (to the microprocessor) is a set of tri-state buffers.
The basic output device (from the microprocessor) is a set of latches.
MANOJKUMAR S.B
Page 106
Microprocessor- 8086
Handshaking
a. I/O devices are typically slower than the microprocessor.
b. Handshaking is used to synchronize I/O with the microprocessor.
c. A device indicates that it is ready for a command or data (through some I/O pin or port).
d. The processor issues a command to the device, and the device indicates it is busy (not
ready).
e. The I/O device fi nishes its task and indicates a ready condition, and the cycle continues.
f. There are two basic mechanisms for the processor to service a device.
g. Polling: Processor initiated. Device indicates it is ready by setting some status bit and the
processor periodically checks it.
h. Interrupts: Device initiated. The act of setting a status bit causes an interrupt, and the
processor calls an ISR to service the device.
Programmable Peripheral Interface (82C55)
a. The 82C55 is a popular interfacing component, that can interface any TTLcompatible I/O
device to the microprocessor.
b. It is used to interface to the keyboard and a parallel printer port in PCs (usually as part of
an integrated chipset).
c. Requires insertion of wait states if used with a microprocessor using higher that an 8
MHz clock.
d. PPI has 24 pins for I/O that are programmable in groups of 12 pins and has three distinct
modes of operation.
In the PC, an 82C55 or its equivalent is decoded at I/O ports 60H-63H.
MANOJKUMAR S.B
Page 107
Microprocessor- 8086
MANOJKUMAR S.B
Page 108
Microprocessor- 8086
MANOJKUMAR S.B
Page 109
Microprocessor- 8086
In previous example, both ports A and B are programmed as (mode 0) simple latched output
ports.Port A provides the segment data inputs to display and port B provides a means of selecting
one display position at a time. The values for the resistors and the type of transistors used are
determined using the current requirements (see text for details). Textbook has the assembly code
fragment demonstrating its use.
Examples of connecting LCD displays and stepper motors are also given.
MANOJKUMAR S.B
Page 110
Microprocessor- 8086
MANOJKUMAR S.B
Page 111
Microprocessor- 8086
Keyboard encoder debounces the key-switches, and provides a strobe whenever a key is
depressed.
DAV is activated on a key press strobing the ASCII-coded key code into Port A.
82C55: Mode 1 Strobed Output
Similar to Mode 0 output operation, except that handshaking signals are provided using port C.
Signal Definitions for Mode 1 Strobed Output
OBF Output buffer full is an output that goes low when data is latched in either port A or port
B. Goes low on ACK.
ACK The acknowledge signal causes the OBF pin to return to 0. This is a response from an
external device.
INTR Interrupt request is an output that requests an interrupt
INTE The interrupt enable signal is neither an input nor an output; it is an internal bit
programmed via the PC6(port A) or PC2(port B) bits.
PC5,PC4 The port C pins 5 and 4 are general-purpose I/O pins that are available for any
purpose.
Page 112
Microprocessor- 8086
ACK Acknowledge is an input that enables tri-state buffers which are otherwise in their highimpedance state
STB The strobe input loads data into the port A latch
IFB Input buffer full is an output indicating that the input latch contains information for the
external bi-directional bus
INTE Interrupt enable are internal bits that enable the INTR pin. Bit PC6(INTE1) and
PC4(INTE2)
PC2,PC1 and PC0 Theses port C pins are general-purpose I/O pins that are available for any
purpose.
82C55: Mode 2 Bi-directional Operation
Timing diagram is a combination of the Mode1 Strobed Input and Mode 1 Strobed Output
Timing diagrams.
MANOJKUMAR S.B
Page 113
Microprocessor- 8086
Unit - 6
Coprocessors
8087
80286
80287,80287XL
80386DX
80287,80387DX
80386SX
80387SX
80486DX
It is Inbuilt
80486SX
80487SX
MANOJKUMAR S.B
Page 114
Microprocessor- 8086
Architecture of 8087
MANOJKUMAR S.B
Page 115
Control Unit
Microprocessor- 8086
Execution Unit
8087 can be connected with any of the 8086/8088/80186/80188 CPUs only in their maximum mode of
operation. I.e. only when the MN/MX* pin of the CPU is grounded. In maximum mode, all the control
signals are derived using a separate chip known as bus controller. The 8288 is 8086/88 compatible bus
controller while 82188 is 80186/80188 compatible bus controller.
The BUSY pin of 8087 is connected with the TEST* pin of the used CPU. The QS0 and QS1 lines may be
directly connected to the corresponding pins in case of 8086/8088 based systems. However, in case of
80186/80188 systems these QS0 and QS1 lines are passed to the CPU through the bus controller. In case
of 8086/8088 based systems the RQ*/GT0* of 8087 may be connected to RQ*/GT1* of the 8086/8088.
The clock pin of 8087 may be connected with the CPU 8086/8088 clock input. The interrupt output of
8087 is routed to 8086/8088 via a programmable interrupt controller. The pins AD 0 - AD15, BHE*/S7,
RESET, A19 / S6 - A16 / S3 are connected to the corresponding pins of 8086/8088. In case of 80186/80188
systems the RQ/GT lines of 8087 are connected with the corresponding RQ*/GT* lines of 82188. The
interconnections of 8087 with 8086/8088 and 80186/80188 are shown in fig.
MANOJKUMAR S.B
Page 116
Microprocessor- 8086
The contents of the control register, generally referred to as the Control word, direct the working
of the 8087. A common way of loading the control register from a memory location is by
executing the instruction FLDCW src, where src is the address of a memory location.
FLDCW stands for Load Control Word. For example, FLDCW [BX] instruction loads the
control register of 8087 with the contents of the memory location whose 16 bit effective address
is provided in BX register.
The bit description of the control register is shown below.
Control Unit
1. Control unit: To synchronize the operation of the coprocessor and the processor.
2. This unit has a Control word and Status word and Data Buffer
3. If instruction is an ESCape (coprocessor) instruction, the coprocessor executes it, if
not the microprocessor executes.
4. Status register reflects the over all operation of the coprocessor.
Status Register
C3-C0
TOP
Top-of-stack (ST)
ES
Error summary
PE
Precision error
UE
OE
Overflow error
ZE
Zero error
DE
Denormalized error
IE
Invalid error
Busy bit
a. B-Busy bit indicates that coprocessor is busy executing a task. Busy can be tested by
examining the status or by using the FWAIT instruction. Newer coprocessor automatically
synchronize with the microprocessor, so busy flag need not be tested before performing
additional coprocessor tasks.
b. C3-C0 Condition code bits indicates conditions about the coprocessor.
MANOJKUMAR S.B
Page 117
Microprocessor- 8086
c. TOP- Top of the stack (ST) bit indicates the current register address as the top of the stack.
d. ES-Error summary bit is set if any unmasked error bit (PE, UE, OE, ZE, DE, or IE) is set. In
the 8087 the error summary is also caused a coprocessor interrupt.
e. PE- Precision error indicates that the result or operand executes selected precision.
f. UE-Under flow error indicates the result is too large to be represent with the current
precision selected by the control word.
g. OE-Over flow error indicates a result that is too large to be represented. If this error is
masked, the coprocessor generates infinity for an overflow error.
h. ZE-A Zero error indicates the divisor was zero while the dividend is a non-infinity or nonzero number.
i. DE-Denormalized error indicates at least one of the operand is denormalized.
j. IE-Invalid error indicates a stack overflow or underflow, indeterminate from (0/0,0,-0, etc) or
the use of a NAN as an operand. This flag indicates error such as those produced by taking
the square root of a negative number.
CONTROL REGISTER
1. Control register selects precision, rounding control, infinity control.
2. It also masks an unmasks the exception bits that correspond to the rightmost Six bits
of status register.
3. Instruction FLDCW is used to load the value into the control register.
Control Register
IC
Infinity control
RC
Rounding control
PC
Precision control
PM
Precision control
UM
Underflow mask
OM
Overflow mask
MANOJKUMAR S.B
Page 118
Microprocessor- 8086
ZM
DM
IM
a. IC Infinity control selects either affine or projective infinity. Affine allows positive
and negative infinity, while projective assumes infinity is unsigned.
INFINITY CONTROL
0 = Projective
1 = Affine
b. RC Rounding control determines the type of rounding.
ROUNDING CONTROL
00=Round to nearest or even
01=Round down towards minus infinity
10=Round up towards plus infinity
11=Chop or truncate towards zero
c. PC- Precision control sets the precision of he result as define in table
PRECISION CONTROL
00=Single precision (short)
01=Reserved
10=Double precision (long)
11=Extended precision (temporary)
d. Exception Masks It Determines whether the error indicated by the exception affects
the error bit in the status register. If a logic1 is placed in one of the exception control bits,
corresponding status register bit is masked off.
Numeric Execution Unit
1. This performs all operations that access and manipulate the numeric data in the coprocessors
registers.
2. Numeric registers in NUE are 80 bits wide.
3. NUE is able to perform arithmetic, logical and transcendental operations as well as supply a
small number of mathematical constants from its on-chip ROM.
4. Numeric data is routed into two parts ways a 64 bit mantissa bus and a 16 bit sign/exponent
bus.
MANOJKUMAR S.B
Page 119
Microprocessor- 8086
1. Multiplexed address-data bus lines are connected directly from the 8086 to 8087.
2. The status lines and the queue status lines connected directly from 8086 to 8087.
3. The Request / Grant signal RQ/GT0 of 8087 is connected to RQ /GT1 of 8086.
______
a.
b. Interrupt output INT of the 8087 to NMI input of 8086. This intimates an error condition.
c. The main purpose of the circuitry between the INT output of 8087 and the NMI input is
to make sure that an NMI signal is not present upon reset, to make it possible to mask
NMI input and to make it possible for other devices to cause an NMI interrupt.
d. BHE pin is connected to the system BHE line to enable the upper bank of memory.
e. The RQ/GT1 input is available so that another coprocessor such as 8089 I/O processor
can be connected and function in parallel with the 8087.
f. One type of Cooperation between the two processors that you need to know about it is
how the 8087 transfers data between memory and its internal registers.
g. When 8086 reads an 8087 instruction that needs data from memory or wants to send data
to memory, the 8086 sends out the memory address code in the instruction and sends out
the appropriate memory read or memory write signal to transfer a word of data.
h. In the case of memory read, the addressed word will be kept on the data bus by the
memory. The 8087 then simply reads the word of data bus. The 8086 ignores this word
MANOJKUMAR S.B
Page 120
Microprocessor- 8086
.If the 8087 only needs this one word of data, it can then go on and executes its
instruction.
i. Some 8087 instructions need to read in or write out up to 80-bit word. For these cases
8086 outputs the address of the first data word on the address bus and outputs the
appropriate control signal.
j. The 8087 reads the data word on the data bus by memory or writes a data word to
memory on the data bus. The 8087 grabs the 20-bit physical address that was output by
the 8086.To transfer additional words it needs to/from memory, the 8087 then takes over
the buses from 8086.
k. To take over the bus, the 8087 sends out a low-going pulse on RQ/GT0 pin. The 8086
responds to this by sending another low going pulse back to the RQ/GT0 pin of 8087 and
by floating its buses.
l. The 8087 then increments the address it grabbed during the first transfer and outputs the
incremented address on the address bus. When the 8087 output a memory read or
memory write signal, another data word will be transferred to or from the 8087.
m. The 8087 continues the process until it has transferred all the data words required by the
instruction to/from memory.
n. When the 8087 is using the buses for its data transfer, it sends another low-going pulse
out on its RQ/ GT0 pin to 8086 to know it can have the buses back again.
o. The next type of the synchronization between the host processor and the coprocessor is
that required to make sure the 8086 hast does not attempt to execute the next instruction
before the 8087 has completed an instruction.
p. Taking one situation, in the case where the 8086 needs the data produced by the
execution of an 8087 instruction to carry out its next instruction.
q. In the instruction sequence for example the 8087 must complete the FSTSW STATUS
instruction before the 8086 will have the data it needs to execute the MOV AX , STATUS
instruction.
r. Without some mechanism to make the 8086 wait until the 8087 completes the FSTSW
instruction, the 8086 will go on and execute the MOV AX , STATUS with erroneous data.
s. We solve this problem by connecting the 8087 BUSY output to the TEST pin of the 8086
and putting on the WAIT instruction in the program.
MANOJKUMAR S.B
Page 121
Microprocessor- 8086
t. While 8087 is executing an instruction it asserts its BUSY pin high. When it is finished
with an instruction, the 8087 will drop its BUSY pin low. Since the BUSY pin from 8087
is connected to the TEST pin 8086 the processor can check its pin of 8087 whether it
finished it instruction or not.
u. You place the 8086 WAIT instruction in your program after the 8087 FSTSW instruction.
When 8086 executes the WAIT instruction it enters an internal loop where it repeatedly
checks the logic level on the TEST input. The 8086 will stay in this loop until it finds the
TEST input asserted low, indicating the 8087 has completed its instruction.
The 8086 will then exit the internal loop, fetch and execute the next instruction.
Example
FSTSW STATUS
In this set of instructions we are not using WAIT instruction. Due to this the flow of
execution of command will takes place continuously even though the previous instruction
had not finished its completion of its work .so we may lost data .
FSTSW STATUS
FWAIT
MOV AX,STATUS
In this code we are adding up of FWAIT instruction so that it will stop the execution of the
command until the above instruction is finishes its work .so that you are not loosing data and
after that you will allow to continue the execution of instructions.
Another case where you need synchronization of the processor and the coprocessor is the
case where a program has several 8087 instructions in sequence.
The 8087 are executed only one instruction at a time so you have to make sure that 8087 has
completed one instruction before you allow the 8086 to fetch the next 8087 instruction from
memory.
________
Here again you use the BUSY-TEST connection and the FWAIT instruction to solve the
problem. If you are hand coding, you can just put the 8086 WAIT(FWAIT) instruction after
each instruction to make sure that instruction is completed before going on to next.
MANOJKUMAR S.B
Page 122
Microprocessor- 8086
If you are using the assembler which accepts 8087 mnemonics, the assembler will
automatically insert the 8-bit code for the WAIT instruction , 10011011 binary (9BH), as the
first byte of the code for 8087 instruction.
INTERFACING
1. Multiplexed address-data bus lines are connected directly from the 8086 to 8087.
2. The status lines and the queue status lines connected directly from 8086 to 8087.
3. The Request/Grant signal RQ/GT0 of 8087 is connected to RQ/GT1 of 8086.
4. BUSY signal 8087 is connected to TEST pin of 8086.
5. Interrupt output INT of the 8087 to NMI input of 8086. This intimates an error condition.
6. A WAIT instruction is passed to keep looking at its TEST pin, until it finds pin Low to
indicates that the 8087 has completed the computation.
7. SYNCHRONIZATION must be established between the processor and coprocessor in
two situations.
a. The execution of an ESC instruction that require the participation of the NUE must not be
initiated if the NUE has not completed the execution of the previous instruction.
b. When a processor instruction accesses a memory location that is an operand of a previous
coprocessor instruction .In this case CPU must synchronize with NPX to ensure that it has
completed its instruction.
Processor WAIT instruction is provided.
Exception Handling
The 8087 detects six different types of exception conditions that occur during instruction
execution. These will cause an interrupt if unmasked and interrupts are enabled.
1)INVALID OPERATION
2)OVERFLOW
3)ZERO DIVISOR
4)UNDERFLOW
5)DENORMALIZED OPERAND
6)INEXACT RESULT
MANOJKUMAR S.B
Page 123
Microprocessor- 8086
Data Types
Internally, all data operands are converted to the 80-bit temporary real format.
We have 3 types.
1. Integer data type
2. Packed BCD data type
3. Real data type
Example
Converting a decimal number into a Floating-point number.
1. Converting the decimal number into binary form.
2. Normalize the binary number
3. Calculate the biased exponent.
4. Store the number in the floating-point format.
Example
Step
Result
1.
100.25
2.
1100100.01 = 1.10010001 * 26
3.
110+01111111=10000101
4.
Sign = 0
Exponent =10000101
Significand = 10010001000000000000000
In step 3 the biased exponent is the exponent a 26 or 110,plus a bias of 01111111(7FH),
single precision no use 7F and double precision no use 3FFFH.
IN step 4 the information found in prior step is combined to form the floating point no.
MANOJKUMAR S.B
Page 124
4. Transcendental instructions
Microprocessor- 8086
Instruction
FLD source
FST Destination
FSTP destination
FST ST(3)
; Copy ST to ST(3)
FST [BX]
FXCH destination
FILD source
MANOJKUMAR S.B
Page 125
Microprocessor- 8086
FIST destination
INT_NUM
7
FISTP destination
Integer store and pop. Similar to FIST except that stack pointer is
incremented after copy.
FBLD source
location AMOUNT to ST
9
FBSTP destination
BCD store in memory and pop 8087 stack. Pops temporary real
from stack, converts to 10-byte BCD, and stores result to
memory.
Example:
FBSTP MONEY
2. Arithmetic Instructions
S. No.
Instruction
FADD destination,
source
MANOJKUMAR S.B
FADD ST(2), ST
FADD SUM
Page 126
Microprocessor- 8086
FADD
2
FADDP
destination, source
by one.
Example:
FADDP ST(2)
; Add ST(2) to ST
; Increment stack pointer so ST(2)
; becomes ST
FIADD source
FSUB destination,
Subtracts the real number at the specified source from the real
source
FSUB ST(3), ST
; ST(3) ST(2) ST
FSUB DIFFERENCE
FSUB
; ST(ST(1)-ST)
FSUBP destination,
source
FISUB source
FSUBR
destination, source
FSUBRP
destination, source
FISUBR source
10
FMUL destination,
source
Examples:
MANOJKUMAR S.B
Page 127
Microprocessor- 8086
FMUL ST(2), ST
FMULP
destination, source
FIMUL source
12
FDIV destination,
source
destination.
Example:
FDIV ST(2), ST
; Divides ST by ST(2)
; stores result in ST
13
FDIVP destination,
source
DIV
Example:
FDIV ST(2), ST
FIDIV source
15
FDIVR destination,
source
16
FDIVP destination,
source
17
FIDIVR source
MANOJKUMAR S.B
Page 128
Microprocessor- 8086
FDIVP and FIDIV instructions above except that they divide the
source operand by the destination operand and put the result in
the destination.
18
FSQRT
19
FSCALE
20
FPREM
21
FRNDINT
22
FXTRACT
23
FABS
24
FCHS
3. Compare Instructions
These instructions compare the contents of ST with contents of specified or default source. The source
may be another stack element or real number in memory. Such compare instructions set the condition
code bits C3, C2 and C0 of the status words use as shown in the table below.
C3
C2
C0
MANOJKUMAR S.B
Description
Page 129
Microprocessor- 8086
S. No.
Instruction
FCOM source
FCOM ST(4)
FCOM VALUE
memory
2
FCOMP source
FCOMPP
FICOM source
FICOMP source
FTST
FXAM
Instruction
FPTAN
Computes the values for a ratio of Y/X for an angle in ST. the
angle must be expressed in radians, and the angle must be in the
range of 0 < angle < /4.
(FPTAN does not work correctly for angles of exactly 0 and /4.)
FPATAN
F2XM1
MANOJKUMAR S.B
Page 130
Microprocessor- 8086
FYL2X
FYL2XP1
Instruction
Description
FLDZ
FLDI
FLDPI
FLD2T
FLDL2E
FLDLG2
Note: The load constant instruction will just push indicated constant into the stack.
Instruction
Description
FINIT/FNINT
FDISI/FNDISI
FENI/FNENI
FLDCW source
MANOJKUMAR S.B
Page 131
Microprocessor- 8086
5
6
FSTCW/FNSTCW
destination
FSTSW/FNSTW
destination
FCLEX/FNCLEX
FSAVE/FNSAVE
destination
FRSTOR source
10
FSTENV / FNSTENV
destination
11
FLDENV source
Loads the 8087 control register, status register, tag word and
exception pointers from a named area in memory.
12
FINCSTP
13
FDECSTP
14
FFREE destination
15
FNOP
16
FWAIT
Note: the processor control instructions actually do not perform computations but they are made used to
perform tasks like initializing 8087, enabling intempty, etc.
MANOJKUMAR S.B
Page 132
Microprocessor- 8086
8087 Programs
; Initialize 8087
RADIUS
; radius to ST
; square radius
FLDPI
; to ST
FMUL
FSTP AREA
; save area
FWAIT
RET
AREAS
ENDP
OR
Program to calculate the area of circle. This program takes test data from array RAD that contains five
sample radii. The five areas are stored in a second array called AREA. No attempt is made in this
program to use the data from the AREA array.
; A short program that finds the area of five circles whose radii are stored in array RAD
. MODEL SMALL
.386
; Select 80386
.387
; Select 80387
.DATA
.CODE
.STARTUP
MANOJKUMAR S.B
MOV SI, 0
; source element 0
MOV DI, 0
; destination element 0
MOV CX, 5
; count of 5
Page 133
Microprocessor- 8086
MAIN1:
FLD
RAD [SI]
; radius to ST
; square radius
FLDPI
; to ST
FMUL
; save area
INC
SI
INC
DI
LOOP MAIN1
.EXIT
END
RADIUS
ST (0)
ST (1)
ST (2)
ST (1)
ST (2)
ST (3)
ST (3)
FLDPI
ST (0)
ST (1)
ST (2)
RADIUS2
RADIUS2
ST
FMUL
ST
ST (3)
ST (0)
ST (1)
ST (2)
RADIUS2
ST
ST (3)
ST
ST (1)
ST (2)
ST (3)
MANOJKUMAR S.B
Page 134
Microprocessor- 8086
Fig: Operation of the stack for the above program. Note that the stack is shown after the
execution of the indicated instruction.
2. Program for determining the resonant frequency of an LC circuit. The equation solved by the
program is Fr = 1 / 2 LC. This example uses L1 for inductance L, C1 for capacitor C, and
RESO for the resultant resonant frequency.
; A sample program that finds the resonant frequency of an LC tank circuit.
. MODEL SMALL
.386
; Select 80386
.387
; Select 80387
.DATA
RESO DD
; resonant frequency
L1 DD
0.000001
; inductance
C1 DD
0.000001
; capacitance
2.0
; constant
TWO DD
.CODE
.STARTUP
FLD
L1
; get L
FMUL C1
; find LC
FSQRT
; find LC
FMUL TWO
; find 2LC
FLDPI
; get
FMUL
; get 2LC
FLD1
; get 1
FDIVR
; form 1/2LC
FSTP RESO
; save frequency
.EXIT
END
3. Program to find the roots of a polynomial expression (ax2+bx+cc=0) by using the quadratic
equation. The quadratic equation is b(b2 - 4ac)/2a
Note: In this program R1 and R2 are the roots for the quadratic equation. The constants are stored in
memory locations A1, B1, and C1. Note that no attempt is made to determine the roots if they are
imaginary. This example tests for imaginary roots and exits to DOS with a zero in the roots (R1 and R2),
MANOJKUMAR S.B
Page 135
Microprocessor- 8086
if it finds them. In practice, imaginary roots could be solved for and stored in a separate set of memory
locations.
; A program that finds the roots of a polynomial equation using the quadratic equation. Note
; imaginary roots are indicated if both root1 (R1) and root 2 (R2) are zero.
. MODEL SMALL
.386
; Select 80386
.387
; Select 80387
.DATA
TWO DD
2.0
FOUR DD
4.0
A1 DD
1.0
B1 DD
0.0
C1 DD
-9.0
R1 DD
R2 DD
.CODE
.STARTUP
FLDZ
FST
R1
;clear roots
FSTP R2
FLD
TWO
FMUL A1
; form 2a
FLD FOUR
FMUL A1
FMUL C1
FLD
; form 4ac
B1
FMUL B1
; form b2
FSUBR
; form b2 - 4ac
FTST
FSTSW AX
SAHF
; move to flags
JZ
ROOTS1
FSQRT
; if b2 - 4ac is zero
; find square root of b2 - 4ac
FSTSW AX
MANOJKUMAR S.B
Page 136
Microprocessor- 8086
TEST AX, 1
JZ
ROOTS1
FCOMPP
; clear stack
JMP
ROOTS2
; end
FLD
B1
ROOTS1:
FSUB ST, ST (1)
FDIV ST, ST (2)
FSTP R1
FLD
; save root1
B1
FADD
FDIVR
FSTP R2
; save root2
ROOTS2:
.EXIT
END
ALGORITHM
1. To calculate x to the power of y
a. Load base, power.
b. Compute (y )*( log2 x)
c. Separate integer(i) ,fraction(f) of a real number
d. Divide fraction (f) by 2
e. Compute (2 f/2) * ( 2f/2)
f. xy = (2x) * (2y )
Program:
Program to calculate x to the power of y
.MODEL SMALL
.DATA
x Dq 4.567
;Base
y Dq 2.759
;Power
temp DD
MANOJKUMAR S.B
Page 137
Microprocessor- 8086
temp1 DD
temp2 DD
tempint DD
tempint1 DD
two DW
diff DD
trunc_cw DW 0fffh
.STACK 100h
.CODE
start: mov ax, @DATA ;init data segment
mov ds, ax
load: fld y
fld x
comput: fyl2x
;compute (y * log2(x))
fst temp
fist tempint
fld temp
twopwrx: f2xm1
fst temp1
fld1
;load1
fadd
fst temp1
fild tempint
MANOJKUMAR S.B
Page 138
Microprocessor- 8086
fxch
scale: fscale
fst temp2
fist tempint1
MANOJKUMAR S.B
Page 139
Microprocessor- 8086
6. In 8087, _______ many register stack are there? And of _____ registers. These registers are
used as _________ stack.
a) 7, 40 bit, FIFO. b) 8, 60 bit, LILO. c) 8, 80 bit, LIFO d) 7, 80 bit, FILO.
7. If ______ and ________ connections are made so that an error condition in 8087 can
interrupt to the processor.
a) BHE, RQ/GT1 b) BUSY, TEST c) INT, NMI d) RQ/GT0, RQ/GT1
8. In 8087, which instruction is used for division real reversed______.
a) FDIV b) FIDIVR c) FDIVR d) FDIVRP
9. Which of the following is of compare instruction in 8087?
a) FTST b) FPREM c) FPATAN d) FLDI
10. In 8087 coprocessor one of the following instructions is not valid
a) FSIN
b)FPTAN
c) FIDIV
d) FSQRT
11.One of the following signals belongs to the 8087 coprocessor is
a) HOLD
b) BUSY
c) TEST
d) NMI
Key:
4.1 A 4.2 A 4.3 B 4.4 A 4.5 B 4.6 C
4.7 C 4.8 C 4.9 A 4.10 A 4.11 C
MANOJKUMAR S.B
Page 140
Microprocessor- 8086
Unit-7
Pin Details
AD14
AD13
AD12
AD11
AD10
AD9
AD8
AD7
AD6
10
AD5
11
AD4
AD2
12
1
13
1
14
AD1
15
AD0
INTR
16
1
17
1
18
CLK
19
GND
20
AD3
NMI
MANOJKUMAR S.B
8086
40
29
39
20
29
38
20
29
37
20
29
36
20
29
35
20
29
34
20
29
33
20
29
32
20
29
31
20
29
30
20
29
29
20
20
28
0
27
20
26
0
25
20
24
0
23
0
22
0
21
0
Minimum Mode
VCC
AD15
A16 /S 3
A17 /S 4
A18 /S 5
A19/S6
BHE/S
7
MN/ MX
RD
RQ/ GT0
RQ/ GT1
LOCK
S2
S1
S0
(HOLD)
(HLDA)
( WR )
( M/IO )
( DT/ R )
QS0
( DEN )
(ALE)
QS1
( INTA )
TEST
READY
RESET
Page 141
Microprocessor- 8086
8086 is a 40 pin DIP using MOS technology. It has 2 GNDs as circuit complexity demands a
large amount of current flowing through the circuits, and multiple grounds help in dissipating the
accumulated heat etc. 8086 works on two modes of operation namely, Maximum Mode and
Minimum Mode.
Pin Description:
GND Pin no. 1, 20
Ground
CLK Pin no. 19 Type I
Clock: provides the basic timing for the processor and bus controller. It is asymmetric with a
33% duty cycle to provide optimized internal timing.
VCC Pin no. 40
VCC: +5V power supply pin
Pin Description
AD15-AD0 Pin no. 2-16, 39 Type I/O
Address Data bus: These lines constitute the time multiplexed memory/ IO address (T1) and
data (T2, T3, TW, T4) bus. A0 is analogous to BHE for the lower byte of the data bus, pins D7-D0.
MANOJKUMAR S.B
Page 142
Microprocessor- 8086
It is low when a byte is to be transferred on the lower portion of the bus in memory or I/O
operations. Eight bit oriented devices tied to the lower half would normally use A0 to condition
chip select functions. These lines are active HIGH and float to 3-state OFF during interrupt
acknowledge and local bus hold acknowledge.
A16/S3
Characteristics
0 (LOW)
Alternate Data
Stack
1(HIGH)
Code or None
Data
S6 is 0 (LOW)
This information indicates which relocation register is presently being used for data accessing.
These lines float to 3-state OFF during local bus hold acknowledge.
MANOJKUMAR S.B
Page 143
Microprocessor- 8086
Pin Description
S 2 , S1 , S 0 - Pin no. 26, 27, 28 Type O
Status: active during T4, T1 and T2 and is returned to the passive state (1,1,1) during T3 or during
TW when READY is HIGH. This status is used by the 8288 Bus Controller to generate all
memory and I/O access control signals. Any change by S 2 , S1 or S 0 during T4 is used to indicate
the beginning of a bus cycle and the return to the passive state in T 3 or TW is used to indicate the
end of a bus cycle.
These signals float to 3-state OFF in hold acknowledge. These status lines are encoded as
shown.
Characteristics
S2
S1
S0
0(LOW)
Interrupt acknowledge
Halt
1(HIGH)
Code Access
Read Memory
Write Memory
Passive
MANOJKUMAR S.B
Page 144
Microprocessor- 8086
Status Details
S2
Indication
Interrupt Acknowledge
Halt
Code access
Read memory
Write memory
Passive
S4
S3
Alternate data
Stack
Code or none
Data
Indications
S5
S6
----- Always low (logical) indicating 8086 is on the bus. If it is tristated another
bus master has taken control of the system bus.
S7
----- Used by 8087 numeric coprocessor to determine whether the CPU is a 8086
or 8088
MANOJKUMAR S.B
Page 145
(v) Interrupts
Microprocessor- 8086
Pin Description:
NMI Pin no. 17 Type I
Non Maskable Interrupt: an edge triggered input which causes a type 2 interrupt. A subroutine
is vectored to via an interrupt vector lookup table located in system memory. NMI is not
maskable internally by software. A transition from a LOW to HIGH initiates the interrupt at the
end of the current instruction. This input is internally synchronized.
INTR Pin No. 18 Type I
Interrupt Request: is a level triggered input which is sampled during the last clock cycle of each
instruction to determine if the processor should enter into an interrupt acknowledge operation. A
subroutine is vectored to via an interrupt vector lookup table located in system memory. It can be
internally masked by software resetting the interrupt enable bit. INTR is internally synchronized.
This signal is active HIGH.
MANOJKUMAR S.B
Page 146
Pin Description:
Microprocessor- 8086
Write: indicates that the processor is performing a write memory or write I/O cycle, depending
on the state of the M/IO signal. WR is active for T2, T3 and TW of any write cycle. It is active
LOW, and floats to 3-state OFF in local bus hold acknowledge.
M/I O - Pin no. 28 type O
Status line: logically equivalent to S2 in the maximum mode. It is used to distinguish a memory
access from an I/O access. M/IO becomes valid in the T4 preceding a bus cycle and remains valid
until the final T4 of the cycle (M=HIGH), IO=LOW). M/IO floats to 3-state OFF in local bus
hold acknowledge.
DT/ R -Pin no. 27 Type O
Data Transmit / Receive: needed in minimum system that desires to use an 8286/8287 data bus
transceiver. It is used to control the direction of data flow through the transceiver. Logically
DT/ R is equivalent to S1 in the maximum mode, and its timing is the same as for M/I O .
(T=HIGH, R=LOW). This signal floats to 3-state OFF in local bus hold acknowledge.
DEN - Pin no. 26 Type O
Data Enable: provided as an output enable for the 8286/8287 in a minimum system which uses
the transceiver. DEN is active LOW during each memory and I/O access and for INTA cycles.
For a read or INTA cycle it is active from the middle of T2 until the middle of T4, while for a
MANOJKUMAR S.B
Page 147
Microprocessor- 8086
write cycle it is active from the beginning of T2 until the middle of T4. DEN floats to 3-state OFF
in local bus hold acknowledge.
ALE Pin no. 25 Type O
Address Latch Enable: provided by the processor to latch the address into the 8282/8283 address
latch. It is a HIGH pulse active during T1 of any bus cycle. Note that ALE is never floated.
INTA - Pin no. 24 Type O
INTA is used as a read strobe for interrupt acknowledge cycles. It is active LOW during T2, T3
Pin Description:
RQ/ GT0 , RQ/ GT1 - Pin no.
Request /Grant: pins are used by other local bus masters to force the processor to release the
local bus at the end of the processors current bus cycle. Each pin is bidirectional with RQ/ GT0
having higher priority than RQ/ GT1 . RQ/ GT has an internal pull up resistor so may be left
unconnected. The request/grant sequence is as follows:
1. A pulse of 1 CLK wide from another local bus master indicates a local bus request
(hold) to the 8086 (pulse 1)
MANOJKUMAR S.B
Page 148
Microprocessor- 8086
2. During a T4 or T1 clock cycle, a pulse 1 CLK wide from the 8086 to the requesting
master (pulse 2), indicates that the 8086 has allowed the local bus to float and that it will
enter the hold acknowledge state at the next CLK. The CPUs bus interface unit is
disconnected logically from the local bus during hold acknowledge.
3. A pulse 1 CLK wide from the requesting master indicates to the 8086 (pulse 3) that the
hold request is about to end and that the 8086 can reclaim the local bus at the next
CLK.
Each master-master exchange of the local bus is a sequence of 3 pulses. There must be one dead
CLK cycle after each bus exchange. Pulses are active LOW.
If the request is made while the CPU is performing a memory cycle, it will release the local bus
during T4 of the cycle when all the following conditions are met:
1. Request occurs on or before T2.
2. Current cycle is not the low byte of a word (on an odd address)
3. Current cycle is not the first acknowledge of an interrupt acknowledge sequence.
4. A locked instruction is not currently executing.
QS0
Characteristics
0(LOW)
No operation
1 (HIGH)
MANOJKUMAR S.B
Page 149
Microprocessor- 8086
Pin Description:
RD
Read: Read strobe indicates that the processor is performing a memory of I/O read cycle,
depending on the state of the S2 pin. This signal is used to read devices which reside on the 8086
local bus. RD is active LOW during T2, T3 and TW of any read cycle, and is guaranteed to
remain HIGH in T2 until the 8086 local bus has floated.
This signal floats to 3-state OFF in hold acknowledge.
READY Pin no. 22, Type I
READY: is the acknowledgement from the addressed memory or I/O device that it will complete
the data transfer. The READY signal from memory / IO is synchronized by the 8284A Clock
Generator to form READY. This signal is active HIGH. The 8086
synchronized. Correct operation is not guaranteed if the setup and hold times are not met.
TEST - Pin No 23 Type I
MANOJKUMAR S.B
Page 150
Microprocessor- 8086
TEST : input is examined by the Wait instruction. If the TEST input is LOW execution
continues, otherwise the processor waits in an idle state. This input is synchronized internally
during each clock cycle on the leading edge of CLK.
RESET Pin no. 21 Type I
Reset: causes the processor to immediately terminate its present activity. The signal must be
active HIGH for at least four clock cycles. It restarts execution, as described in the instruction set
description, when RESET returns LOW. RESET is internally synchronized.
BHE
A0
Characteristics
Whole word
None
MANOJKUMAR S.B
Page 151
Microprocessor- 8086
A14
A13
A12
A11
A10
A9
A8
AD7
AD6
10
AD5
11
AD4
AD2
12
1
13
1
14
AD1
15
AD0
INTR
16
1
17
1
18
CLK
19
GND
20
AD3
NMI
8088
40
29
39
20
29
38
20
29
37
20
29
36
20
29
35
20
29
34
20
29
33
20
29
32
20
29
31
20
29
30
20
29
29
20
20
28
0
27
20
26
0
25
20
24
0
23
0
22
0
21
0
VCC
AD15
A16 /S 3
A17 /S 4
A18 /S 5
A19 /S 6
HIGH (SSO )
MN/ MX
RD
RQ0/ GT0
RQ1/ GT1
(HOLD)
(HLDA)
LOCK
S2
S1
( WR )
( IO/ M )
S0
QS0
( DEN )
(ALE)
QS1
( INTA )
( DT/ R )
TEST
READY
RESET
Page 152
Microprocessor- 8086
3. The instruction queue length in the case of 8086 is 6 bytes. The BIU in 8088 needs more
time to fill up the queue a byte at a time. Thus to prevent overuse of the bus by the BIU,
the instruction queue in 8088 is shortened to 4 bytes.
4. To optimize the working of the queue, the 8086 BIU will fetch a word into the queue
whenever there is a space for a word in the queue. The 8088 BIU will fetch a byte into
the queue whenever there is space for a byte in the queue.
5. Pin number 34 of 8086 is BHE*/S7. BHE* is irrelevant for 8088, which can only access
8 bits at a time. Thus pin 34 o 8088 is assigned for the signal SSO*. This pin acts like
SO* status line in the minimum mode of operation. So, in the minimum mode, DT/R*,
IO/M*, and SSO* provide the complete bus status as shown below.
IO/M*
DT/R*
SSO*
Bus Cycle
Interrupt acknowledge
Halt
Code Access
Read Memory
Write Memory
Passive
6. In the maximum mode for 8088 the SSO* (pin 34) signal is always a 1. In the maximum
mode for 8086, the BHE*/S7 (pin 34) will provide BHE* information during the first
clock cycle, and will be 0 during subsequent clock cycles. In maximum mode, 8087 will
monitor this pin to identify the CPU as a 8088 or a 8086, and accordingly sets its own
queue length to 4 or 6 bytes.
MANOJKUMAR S.B
Page 153
Microprocessor- 8086
A minimum mode of 8086 configuration depicts a stand alone system of computer where no
other processor is connected. This is similar to 8085 block diagram with the following
difference.
The Data transceiver block which helps the signals traveling a longer distance to get boosted up.
Two control signals data transmit/ receive are connected to the direction input of transceiver
(Transmitter/Receiver) and DEN* signal works as enable for this block.
MANOJKUMAR S.B
Page 154
Microprocessor- 8086
In the bus timing diagram, data transmit / receive signal goes low (RECEIVE) for Read
operation. To validate the data, DEN* signal goes low. The Address/ Status bus carries A16 to
A19 address lines during BHE* (low) and for the remaining time carries Status information. The
Address/Data bus carries A0 to A15 address information during ALE going high and for the
remaining time it carries data. The RD* line going low indicates that this is a Read operation.
The curved arrows indicate the relationship between valid data and RD* signal.
The TW is Wait time needed to synchronize the fast processor with slow memory etc. The Ready
pin is checked to see whether any peripheral needs more time for data transmission.
Write Cycle Timing Diagram for Minimum Operation
This is the same as Read cycle Timing Diagram except that the DT/R* line goes high indicating
it is a Data Transmission operation for the processor to memory / peripheral. Again DEN* line
goes low to validate data and WR* line goes low, indicating a Write operation.
MANOJKUMAR S.B
Page 155
Microprocessor- 8086
The HOLD and HLDA timing diagram indicates in Time Space HOLD (input) occurs first and
then the processor outputs HLDA (Hold Acknowledge).
In the maximum mode of operation of 8086, wherein either a numeric coprocessor of the type
8087 or another processor is interfaced with 8086. The Memory, Address Bus, Data Buses are
shared resources between the two processors. The control signals for Maximum mode of
MANOJKUMAR S.B
Page 156
Microprocessor- 8086
operation are generated by the Bus Controller chip 8788. The three status outputs S0*, S1*, S2*
from the processor are input to 8788. The outputs of the bus controller are the Control Signals,
namely DEN, DT/R*, IORC*, IOWTC*, MWTC*, MRDC*, ALE etc. These control signals
perform the same task as the minimum mode operation. However the DEN is an active HIGH
signal which has to be converted to active LOW by means of an inverter.
Memory Read Timing in Maximum Mode
Here MRDC* signal is used instead of RD* as in case of Minimum Mode S0* to S2* are active
and are used to generate control signal.
Memory Write Timing in Maximum Mode
MANOJKUMAR S.B
Page 157
Microprocessor- 8086
Here the maximum mode write signals are shown. Please note that the T states correspond to the
time during which DEN* is LOW, WRITE Control goes LOW, DT/R* is HIGH and data output
in available from the processor on the data bus.
Request / Grant pin may appear that both signals are active low. But in reality, Request signal
goes low first (input to processor), and then the processor grants the request by outputting a low
on the same pin.
Read and Write Cycle Timing diagram of 8088
MANOJKUMAR S.B
Page 158
Microprocessor- 8086
In 8088, the timing diagram for both Read and Write are indicated along with Ready signal and
Wait states. In 8088, there are only 8 data lines as compared to 16 lines in the case of 8086. The
figure shown above is for a minimum mode operation of 8088.
8284 Clock Generator
The clock Generator 8284 performs the following tasks in addition to generating the system
clock for the 8086/8088.
1. Generating the Ready signal for h 8086/8088
2. Generating the Reset signal for h 8086/8088
8284 Block Diagram
Clock Logic
The clock logic generates the three output signals OSC, CLOCK, and PCLK.
MANOJKUMAR S.B
Page 159
Microprocessor- 8086
OSC is a TTL clock signal generated by the crystal oscillator in 8284. Its frequency is same as
the frequency of the crystal connected between X1 and X2 pins of 8284. In a PC, a crystal of
14.31 MHz is connected between X1 and X2. thus OSC output frequency will be 14.31MHz.
This signal is used by the Color Graphics Adapter (CGA). The Tank input is used by the crystal
oscillator only if the crystal is an overtone type crystal. An LC circuit is connected to the TANK
input to tune the oscillator to the overtone frequency of the crystal. Generally, in PCs, the TANK
input is connected to ground, as fundamental type crystal is used in a PC.
The Clock output of 8284 is used as the system clock for the 8086/8088, 8087, and the bus
controller 8288. It is having a duty cycle of 33%. It is derived from the OSC frequency generated
by the crystal oscillator, or from an External Frequency Input (EFI). These two signals are inputs
to a multiplexer. The F/C* (external frequency/crystal) input to the multiplexer decides this
aspect. If F/C*=0, OSC frequency is used for deriving Clock. If F/C*=1, EFI input is used for
deriving clock. The output of the multiplexer, which is OSC or EFI, is divided by 3 to provide
the Clock output. Thus, if F/C*=0, clock frequency will be 14.31MHz/3=4.77MHz.
Turbo PCs use 30MHz crystal oscillator circuit for generating EFI input. With F/C*=1, they
allow turbo clock speed of 10MHz. Such PCs provide a choice of switching between 4.77MHz
and 10MHz using a toggle switch or manual operation. The switching can also be controlled by
software using an output port.
The CSYNC input is a synchronization signal for synchronizing multiple 8284s in a system. In a
PC, CSYNC is tied to ground, as there is a single 8284.
MANOJKUMAR S.B
Page 160
Microprocessor- 8086
PCLK frequency output is obtained by dividing clock frequency by 2. PCLK is used by dividing
clock frequency by 2. PCLK is used by support chips like 8254 timer, which need a lower
frequency for their operation.
Pin functions of 8284A:
X1 and X2
EFI
F/C*
CSYNC
OSC
CLK
PCLK
MANOJKUMAR S.B
Page 161
Microprocessor- 8086
Clock Generator (8284A and the 8086/8088 microprocessor illustrating the connection for
the clock and reset signals (A 15 MHz crystal provides the 5 MHz clock for the
microprocessor)
Ready Logic
The Ready Logic generates the Ready signal for the 8086/8088. If the Ready signal is made low
by this circuit during T2 state of a machine cycle, the microprocessor introduces a wait state
between T3 and T4 states of the machine cycle.
The Ready logic is indicated in the figure. There are two pairs of signals in 8284 which can make
the Ready output of 8284 to go low. If (RDY1=0 or SEN1*=1) and (RDY2=0 or AEN2*=1), the
Ready output becomes low when the next clock transition takes place.
In PCs, RDY2 and AEN2* are not used, and as such RDY2 is tied to Ground and /or AEN2* is
tied to +5V. AEN1* is used for generating wait states in the 8086/8088 bus cycle, and RDY1 is
used for generating wait state in the DMA bus cycle.
MANOJKUMAR S.B
Page 162
Reset Logic
Microprocessor- 8086
The Reset logic generates the Reset input signal for the 8086/8088. When the REST* pin goes
low, the Reset output is generated by the 8284 when the next clock transition takes place.
In PCs, the RES* input is activated by one of the following.
1. From the manual Reset button on the front panel.
2. From the Power on Reset circuit, which uses RC components.
3. If the Power Good signal from the SMPS is not active.
Bus Interface
A bus is used to communicate between components in a computer system. They are typically
specialized, with (for instance) a memory bus having different characteristics from an IO bus.
Communications used in networks are different again.
An important distinction to be drawn early is that between a bus and a point-to-point network. A
bus has a single wire, or connection of wires with multiple sources and destinations. A point-topoint network has some sort of interconnection scheme between sources and destinations in
which the nodes are endpoints. In general a bus is cheaper and slower than other network
topologies; there is a shift in progress in some areas from busses to point-to-point networks; this
is most noticeable in high-speed ethernet and in AMD's multiprocessor system ``busses.''
Bus operations should be thought of in terms of transactions. A bus transaction is an operation
on the bus; for instance, a memory read, or an interrupt service, or an IO write. A bus transaction
is initiated by a bus master who requests that some operation take place; a slave will respond to
the transaction appropriately. Different busses support different sets of transactions
Bus Components
Busses typically have several components in common. In general, dedicated groups of wires are
used for the different components (but see multiplexing, below).
MANOJKUMAR S.B
Page 163
Microprocessor- 8086
1. Data: this is the whole purpose of the bus - to transmit data. Ordinarily, when you talk
about the ``width'' of a bus (8-bit bus, 32-bit bus, etc), you're talking about how many
wires are used for data.
2. Address: this is how the components on the bus recognize that data on the bus is
intended for them. Ordinarily, whenever a bus transaction takes place, an address is put
on the bus... recipient can tell who it's for. Memory and IO busses don't normally put a
source address on; that's either implicit in the transaction or irrelevant. Networks, on the
other hand, usually do.
3. Control: these wires contain a variety of information about the transaction, for instance
what type of transaction it is (read, write, interrupt request, etc). Most parallel busses may
also have a global clock, which would be a control line as well. A serial bus can't very
well do that, so the clock has to be carried with the data somehow.
4. Power and Ground: the bus has to have a ground wire, so the different components have
a common voltage reference (they'll normally have lots of ground wires, for electrical
immunity!). Also, all the cards in the bus need to get power from somewhere, so the bus
itself is a convenient place to distribute it.
PCI Bus
The Peripheral Component Interconnect (PCI) bus is the standard I/O bus on recent computers in
general, and PCs in particular. It was developed by Intel in 1993 to replace the various busses
which had been in use on both PCs and Macintoshes.
It is a 32-bit, 33MHz bus with multiplexed address and data, and very nice capabilities for
autoconfiguration ("Plug and Play"). It also supports both old, 5 volt devices and newer, 3.3 volt
devices.
Just as a brief note, it was developed by Intel in 1993 to replace the various busses which had
been in use on both PCs and Macintoshes. To Intel's credit, it is a remarkably architecture-neutral
bus. A very brief description would be that it is a 32-bit, 33MHz bus with multiplexed address
and data, and very nice capabilities for autoconfiguration ("Plug and Play"). It also supports both
old, 5 volt devices and newer, 3.3 volt devices.
There are many extensions to PCI. Best known is that it has simply been extended to 64 bits and
66 MHz. In addition, there is a variant called PC-104+, which is a 32-bit PCI bus in a highly
shock and vibration resistant packaging. PCI-X is a backward-compatible extension to PCI, with
PCI-X itself running at 266MHz and PCI-X 2.0 at 533 MHz. This latter also defines a 16 bit
interface for space-constrained applications, and a new bus mastering protocol (PCI SIG likes to
call this peer-to-peer) that looks a lot like messaging.
All transfers on the PCI bus are "burst" transfers. What this means is that once a device obtains
the bus to perform a transfer, it is able to hang on to the bus indefinitely, and keep sending more
data every bus cycle (there's actually a time in the bus controller which will take control back
after some configurable time period, to keep transfers from being too long. The longer the
tranfers are the better the throughput, but this can cause unacceptable delays for other devices).
Configuration Space
MANOJKUMAR S.B
Page 164
Microprocessor- 8086
One of the nicest features of PCI is its support for autoconfiguration. In addition to every device
having an address on the PCI bus, every card has its own address determined by which slot it is
plugged into. This is referred to as the card's configuration space, and can be queried (and parts
of it can be written) by the CPU. This normally occurs at boot time; it may be performed by the
BIOS prior to starting the boot loader, or it may be performed by the OS as it boots.
Here's a picture of the configuration space for a PCI device
Header(64 bytes)
00H
Identification
Status/Command
3FH
ss
Cla
BIST
Available
(192 bytes)
FFH
Special
Page 165
Microprocessor- 8086
Class Code
This is a 24 bit number, assigned by I-don't-know-who, which identifies what kind of
device is on the card. The difference between this and the vendor/device id fields is that
this will specify something like "serial port" You can run the device based on its class
code, but to take advantage of any extra features (like the fact it might be an 8-port card
instead of a single-port card) requires the vendor and device IDs.
Base Registers
Up to six base registers can be specified, for the devices located on the card. If you have
fewer than six logical devices you will actually use fewer than these; if you have more,
you will have to get into some ugly hacks (for instance, on an eight port serial card I
have, six of the ports' base addresses are specified in the base addresses, while two are at
fixed offsets from the first two of the six). Unlike the vendor and device ID fields, and
the class codes, the base register addresses are read/write.
PCI Commands
There are a total of 16 possible commands on a PCI cycle. They're in the following table:
Command
Command Type
0000
Interrupt Acknowledge
0001
Special Cycle
0010
I/O Read
0011
I/O Write
0100
Reserved
0101
Reserved
0110
Memory Read
0111
Memory Write
1000
Reserved
1001
Reserved
1010
Configuration Read
1011
Configuration Write
1100
1101
1110
Memory-Read Line
1111
MANOJKUMAR S.B
Page 166
Microprocessor- 8086
Processor Shutdown
0x0001
Processor Halt
0x0002
Reserved
I/O Read (0010) and I/O Write (0011)
Input/Output device read or write operation. The AD lines contain a byte address (AD0
and AD1 must be decoded). PCI I/O ports may be 8 or 16 bits. PCI allows 32 bits of
address space. On IBM compatible machines, the Intel CPU is limited to 16 bits of I/O
space, which is further limited by some ISA cards that may also be installed in the
machine (many ISA cards only decode the lower 10 bits of address space, and thus mirror
themselves throughout the 16 bit I/O space). This limit assumes that the machine supports
ISA or EISA slots in addition to PCI slots. The PCI configuration space may also be
accessed through I/O ports 0x0CF8 (Address) and 0x0CFC (Data). The address port must
be written first.
Memory Read (0110) and Memory Write (0111)
A read or write to the system memory space. The AD lines contain a doubleword address.
AD0 and AD1 do not need to be decoded. The Byte Enable lines (C/BE) indicate which
bytes are valid.
Configuration Read (1010) and Configuration Write (1011)
A read or write to the PCI device configuration space, which is 256 bytes in length. It is
accessed in doubleword units. AD0 and AD1 contain 0, AD2-7 contain the doubleword
address, AD8-10 are used for selecting the addressed unit a the malfunction unit, and the
remaining AD lines are not used.
Multiple Memory Read (1100)
This is an extension of the memory read bus cycle. It is used to read large blocks of
memory without caching, which is beneficial for long sequential memory accesses.
Dual Address Cycle (1101)
Two address cycles are necessary when a 64 bit address is used, but only a 32 bit physical
address exists. The least significant portion of the address is placed on the AD lines first,
followed by the most significant 32 bits. The second address cycle also contains the
command for the type of transfer (I/O, Memory, etc). The PCI bus supports a 64 bit I/O
address space, although this is not available on Intel based PCs due to limitations of the
CPU.
Memory-Read Line (1110)
This cycle is used to read in more than two 32 bit data blocks, typically up to the end of a
cache line. It is more effecient than normal memory read bursts for a long series of
sequential memory accesses.
Memory Write and Invalidate (1111)
0x0003 to 0xFFFF
MANOJKUMAR S.B
Page 167
Microprocessor- 8086
This indicates that a minimum of one cache line is to be transferred. This allows main
memory to be updated, saving a cache write-back cycle.
Interrupt Handling
PCI uses four pins, called INTA-INTD, for interrupt requests.
When an interrupt is required, the proper pin is asserted.
A card which only has a single interrupt will normally use INTA
In the modern systems BIOS exists which support PCI bus
-----------------------------------------------------------------------------------------------------------------Port details(LPT)
The parallel port LPT1 is normally at I/O port addresses 378H, 379H and 37AH
The secondary port(if present) is located at 278H, 279H and 27AH
The parallel printer interface is located on the rear of the PC
LPT stands for Line printer
The printer interface gives access to eight lines that can be programmed to receive or send data
The Centronics interface implemented by the parallel port uses two connecters
One is 25 pin D type on the back of the PC
The other one is 36 pin on the back of the Printer
The parallel port can work as both transmitter and as well as receiver
Other than printers CD ROMs can also be interfaced through parallel port.
______________________________________________________________________________
Universal Serial Bus (USB) allows the addition of a new device to a PC by plugging it into the
back of the machine or daisy-chaining it from another device on the bus. The device is
immediately available for use (no rebooting required) and often does not need a device driver to
be installed (depending on the operating system being used.
USB 1.1 allows communication speeds of 12 Megabits per second (or 1.5 Megabytes per
second). The enhanced USB 2.0 will use the same cables, connectors, and software interfaces
and will be backward compatible with older devices. USB 2.0 carries data at 360 to 480 Mbps
(60 MBps). All cables use four wires; the distance between two devices can be up to five meters.
A big advantage of USB devices, apart from their much greater speed, is that USB devices
configure themselves automatically: gone are the days when you had to fiddle with IRQ settings,
DMA channels, and I/O addresses to make a gadget work. Another benefit is that because USB
devices do not require IRQ settings, DMA channels, or I/O settings, COM and LPT ports
currently occupying an IRQ can be freed up, making more IRQ interrupts available for other
uses.
Another advantage of USB is that it is a standard port and can support just about any type of
device, including keyboards, mice, serial peripherals (e.g. modems), printers, audio input/output,
joysticks, digital cameras, scanners, external hard disks and CD burners. Soon, the collection of
space-consuming and costly dedicated ports for keyboards, printers, mice, modems etc will
disappear and replaced by USB. USB can be built into most chipsets for about $1. The other
advantage is that you can mix and match devices as much as you like. In the old days, if you had
MANOJKUMAR S.B
Page 168
Microprocessor- 8086
a parallel port scanner and a printer, they had to share the single printer port (and neither of them
would work properly for long.) If you have several USB devices, it is best to use a USB hub.
This is a little box that splits a single USB port into 4 or more ports
USB uses a four-wire cable interface. Two of the wires are used in a differential mode for both
transmitting and receiving data, and the remaining two wires are power and ground. The source
of the power to a USB device can come from the host, a hub, or the device can be "self
powered." There are two different connector types on each end of a USB cable. One of these
connectors is for upstream communications, and the other for downstream. Each cable length is
limited to about 5 meters.
USB has four types of communication transfer modes: control, interrupt, bulk, and isochronous.
Control mode is initiated by the host. In this mode, every data transfer must send data in both
directions, but only in one direction at a time. The control mode is used mainly for initialization
of devices, but it can also be used to transfer small amounts of data.
In interrupt mode, interrupts do not occur in the usual sense. As in control mode, the host has to
initiate the transfer of data. Interrupt mode works by the host querying devices to see if they need
to be serviced
Bulk mode and isochronous mode complement each other in a sense. Bulk mode is used when
data accuracy is of prime importance, but the rate of data transfer is not guaranteed. An example
of this would be disk drive storage. Isochronous mode sacrifices data accuracy in favor of
guaranteed timing of data delivery. An example of this would be USB audio speakers.
These four modes will be discussed in more detail below.
Above is an example of USB ports found on PCs and on some USB peripherals including
keyboards and monitors.
The PC host typically has connections for two external USB ports. Each of these two connectors
on the PC is actually a connection to a separate root hub inside the PC. If either of the two root
hubs needs to have more than one device connected to it, a downstream USB hub is required to
expand connections. Hubs are used to add to the number of devices that can be connected to one
USB port. They can be considered to be a repeater of sorts and also a controller. When a device
is connected downstream of a hub, the hub does the connect detection of the new device and
notifies the host.
Hubs can be inside the device itself -- for example, in a keyboard that may have an additional
two downstream USB connectors for additional devices. A hub can have a combination of high
and low speed devices connected to it, up to a maximum of four additional hubs downstream
MANOJKUMAR S.B
Page 169
Microprocessor- 8086
from itself. A hub's upstream port to the PC must be high speed. The hub acts as a traffic cop,
handling communication to downstream devices as either high or low speed. A hub can ignore a
downstream device that is not behaving properly. Hubs can be either self-powered or receive
power from the USB bus. USB 1.x hubs support both low and high-speed data transfers.
There are several hardware requirements for devices that are placed on the USB bus. Five volts
is the nominal supply voltage on the bus. A device that requires 100mA or less can be powered
from the host or any hub, provided that the total available power hasn't already been exhausted
by other devices. A device on the bus can draw up to 500mA from it. However, not all USB
hosts (especially a battery powered PC) or bus-powered hubs will allow a device to draw more
than 100mA from the bus. For this reason, a USB device that draws more than 100mA should, in
most cases, be self-powered .
A device tells the host how much current is required for its operation. Self-powered devices
usually get their power from a separate power supply or batteries. A battery-powered device
plugged into the bus can get its power from the bus if it meets the tests above, and it can then
switch back over to battery power when it is disconnected from the bus or when the host is shut
down. When a device is in suspend mode, it cannot draw any more than 500uA from the bus if it
is bus-powered. Also, if a device has not seen any activity on its bus in 3 mS, it needs to go into
suspend mode. A host can initiate a resume command to a device that is in suspend mode. A
device can also issue a remote wakeup to an inactive host to make it active.
All devices have endpoints, which are memory buffers. An endpoint can be as simple as an
addressable single register, or it can be a block of memory that is used to store incoming and/or
outgoing data. There may be multiple endpoints inside a device. Each device has at least one
endpoint -- "endpoint 0"-- which is used as a control endpoint. It must be able to both send and
receive data, but can only communicate in one direction at a time. Typically, when a device
receives data such as an Out or Setup command from the host, this data is stored in the endpoint
and the device's microprocessor is interrupted and works on this data. When a device receives an
In command that is addressed to it from the host, data for the host that is stored in the endpoint is
sent to the host.
The host is considered to be the master in most all cases. One exception is when a device issues a
remote wakeup to the host as discussed above. There are time limits for both the host and device
to respond to each other. For example, if the host requests data from a device using an In
command, the device must send the data back to the host within 500mS, in some cases.
Depending on the transaction type, the host and/or the device may respond to data received with
an acknowledgement. Data transfer involves quite a bit of error-checking and handshaking. The
different types of data packets sent and received use different ways to verify correct data transfer.
A logical connection link needs to be set up between the host and a device before a transaction
can occur. This connection is referred to as a Pipe. It is set up as soon as possible after a host has
recognized a device as being connected. When the host responds to a connect signal from the
device, one of the parameters that is sent to the host is the device's required data transfer type and
speed. The host can refuse to establish a Pipe if the host does not have enough bandwidth to
support the device's request or if its power requirements cannot be met. The device at its
discretion can lower its requested data rate and try again until the host accepts it and initiates a
Pipe.
MANOJKUMAR S.B
Page 170
Microprocessor- 8086
When a device is connected, it also sends to the host descriptor information on the types of
endpoints in the device, the type of data transfer it uses, size of data packets, endpoint addresses
within the device, and if used, the time required between data transfers.
The following describes a typical data flow for a device when it is initially plugged into a host's
bus while the host is active. Remember here that the host has an internal USB hub, and
additional hubs may be connected downstream from the host's hub.
1. The host recognizes that a device has been attached to one of its USB hubs. It realizes
this by a simple resistive divider that is connected to the differential data pair of wires in
the USB bus. These resistors are inside the USB hubs and devices.
2. The host sends a Get_Port_Status request to the hub to find out more about what has been
plugged in. It could be another hub, a device connected directly to the host hub, or a
device that has been plugged into one of the downstream hubs.
3. After receiving a response from the hub, the host issues a Set_Port_Feature command in
which the hub issues a reset over the data pair but only to the newly connected device on
the USB bus.
4. The host then checks to see if the device has come out of the reset state by issuing a
Get_Port_Status command to the hub. After reset, the device is in the Default state and
can only draw a maximum of 100mA. In Default state, the device can communicate with
the host through Endpoint 0.
5. The hub now detects the device's speed by using the resistive dividers that are attached to
the USB bus. The hub sends the speed of this device back to the host.
6. The host then sends a Get_Descriptor command to the hub in which the hub gets the
packet size needed from this particular device and sends the result back to the host.
7. The host now issues a Set_Address command to the hub which sends this information to
the device. The device in turn acknowledges the command back through the hub to the
host and sets up this address internally.
8. To learn more about this device, the host sends a Get_Descriptor command to the address
that the device has been given. The information that is returned to the host consists of
various details of the device that the host needs to know for its operation.
9. Based on the information received from the device, the host determines the best device
driver to use for communications with it.
10. The device driver in the host now takes over by requesting a Set_Configuration
command. There can be several configurations for one device, and the device driver
determines which to use based on information received from the device in response to the
Get_Descriptor command.
11. The device is now ready for use.
MANOJKUMAR S.B
Page 171
Microprocessor- 8086
UNIT - 8
PIN DESCRIPTIONS
Symbol
Type
Function
CLK2
In
D0 D31
I/O
Data Bus inputs data during memory, I/O, or interrupt read cycles, and
outputs data during memory and I/O cycles.
A2 A31
Out
Out
Byte Enable signals decode A0 and A1 to indicate specific banks for memory
data transfers.
W/R#
Out
D/C#
Out
M/IO#
Out
BE0#
BE3#
MANOJKUMAR S.B
Page 172
Microprocessor- 8086
LOCK#
Out
Bus Lock responds to a prefix byte on an instruction that indicates that other
bus masters may not intercede the current cycle until it is complete.
ADS#
Out
Address Status indicates that a valid set of addressing signals are being
driven onto the device pins.These include W/R#, D/C#, M/IO#, BE0#-BE3#,
and A2-A31.
NA#
In
READY#
In
BS16#
In
HOLD
In
HLDA
Out
BUSY#
In
ERROR#
In
PEREQ
In
INTR
In
NMI
In
RESET
In
Reset causes the processor to enter a known state and destroys any execution
in progress.
No Connect indicates pins that are not to have any electrical connections.
N/C
VCC
In
VSS
In
Ground.
DATA FLOW
Refer to the following diagram for illustration.
The Intel 80386 data flow consists of three primary areas. These are the bus interface unit (BIU),
the central processing unit (CPU), and a memory management unit (MMU). These are
interconnected within the device by several 32-bit-wide data busses and an internal control bus.
The Bus Interface Unit (BIU) provides the attachments of the device to the external bus system.
The circuits include a set of address bus drivers which generate or receive the A2 A31 address
lines; the BE0 BE3 byte selection lines; the control lines M/IO, D/C, W/R, Lock, ADS, NA,
BS16, and Ready; and interface with the D0 D31 data bus lines. The unit includes a pipeline
MANOJKUMAR S.B
Page 173
Microprocessor- 8086
control element which provides the memory access pipelining that permits fast data transfer from
contiguous memory locations. The unit also includes a set of multiplex transceivers to handle the
direction of incoming or outgoing data and address information. Also included is a control
element that handles requests for interrupts, DMA cycles, and coprocessor synchronization.
The Central Processing Unit (CPU) is connected to the BIU via two paths. One is the direct
ALU bus (across the bottom of the drawing) that allows exchange of addressing information and
data between the CPU and the BIU if needed. The second is the normal path for instruction parts
which go by way of an instruction prefetching element that is responsible for requesting
instruction bytes from the memory as needed; an instruction predecoder that accepts bytes from
the queue and ensures at least 3 instructions are available for execution; the instruction decoder
and execution unit that causes the instruction to be performed. This is accomplished by the use of
microprograms stored in the system control ROM which is stepped through to control the data
flow within and around the Arithmetic Logic Unit (ALU).
The ALU consists of a register stack which contains both programmer-accessible and nonaccessible 32-bit registers; a hardware multiply/divide element; and a 64-bit barrel shifter for
shifts, rotates, multiplies, and divides. The ALU provides not only the data processing for the
device but also is used to compute effective addresses (EAs) for protected mode addressing.
The Memory Management Unit (MMU) provides the support for both the segmentation of
main memory for both protected mode and real mode, and the paging elements for virtual
memory. In real mode, the segmentation of the main memory is limited to a maximum segment
size of 64K bytes, and a maximum memory space of 1.024 megabytes. This is in concert with the
Intel 8086 upon which this processor is based. In protected mode, several additional registers are
added to support variable length segments to a maximum theoretical size of 4 gigabytes, which
in turn supports multitasking and execution priority levels. Virtual mode using the devices
paging unit allows a program or task to consume more memory than is physically attached to the
device through the translation of supposed memory locations into either real memory or diskbased data.
MODES OF OPERATION
The Intel 80386 has three modes of operation available. These are Real Mode, Protected Mode,
and Virtual 8086 mode.
Real Mode operation causes the device to function as would an Intel 8086 processor. It is faster
by far that the 8086. While the 8086 was a 16-bit device, the 80386 can provide 32-bit
extensions to the 8086s instructions. There are additional instructions to support the shift to
protected mode as well as to service 32-bit data. In Real Mode, the address space is limited to
1.024 megabytes. The bottom 1,024 bytes contain the 256 4-byte interrupt vectors of the 8086.
The Reset vector is FFFF0h. While the system can function as a simple DOS computer in this
mode forever, the main purpose of the mode is to allow the initialization of several memory
tables and flags so that a jump to Protected Mode may be made.
Protected Mode provides the 80386 with extensive capabilities. These include the memory
management, virtual memory paging, multitasking, and the use of four privilege levels which
allows the creation of sophisticated operating systems such as Windows NT and OS/2. (These
will be further explained.)
Virtual 8086 Mode allows the system, once properly initialized in Protected Mode, to create one
or more virtual 8086 tasks. These are implemented essentially as would be a Real Mode task,
except that they can be located anywhere in memory, there can be many of them, and they are
MANOJKUMAR S.B
Page 174
Microprocessor- 8086
limited by Real Mode constructs. This feature allows a 386-based computer, for example, to
provide multiple DOS sessions or to run multiple operating systems, each one located in its own
8086 environment. OS/2 made use of this feature in providing multiple DOS sessions and to
support its Windows 3.1 emulator. Windows NT uses the feature for its DOS windows.
REGISTER ORGANIZATION
Programmer-visible Registers
The 386 provides a variety of General Purpose Registers (GPRs) that are visible to the
programmer. These support the original 16-bit registers of the 8086, and extend them to 32-bit
versions for protected mode programming.
Chart goes here.
The AX, BX, CX, and DX registers exist in the same form as in the 8086. The may be used as
16-bit registers when called with the "X" in their name. They may also be used as 8-bit registers
when defined with the "H" and "L" in their names. Hence, the AX register is used as a 16-bit
device while the AH and AL are used as 8-bit devices. Similarly, Source Index (SI), Destination
Index (DI), Base Pointer (BP) and Stack Pointer (SP) registers exist in their traditional 16-bit
form.
To use any of these registers as 32-bit entities, the letter "E", for extended, is added to their
names. Hence, the 16-bit AX register can become the 32-bit EAX register, the 16-bit DI register
becomes the 32-bit EDI register, etc.
The registers of the 386 includes the 8086s Code Segment (CS) register, Stack Segment (SS)
register, Data Segment (DS) register, and Extra Segment (ES) register which are used as
containers for values pointing to the base of these segments. Additionally, two more dataoriented segment registers, the FS and GS registers, are provided. In real mode, these registers
contain values that point to the base of a segment in the real modes 1.048 megabyte address
space. An offset is added to this displaced to the right which generates a real address. In
protected mode, the segment registers contain a "selector" value which points to a location in a
table where more information about the location of the segment is stored.
The 386 also provides an Instruction Pointer (IP) register and a Flags (FLAGS) register which
operate as they did in the 8086 in real mode. In protected mode, these become 32-bit devices
which provide extended features and addressing.
The 32-bit FLAGS register contains the original 16 bits of the 8086-80286 flags in bit positions 0
through 15 as follows. These are available to real mode.
MANOJKUMAR S.B
Bit
Flag
Description
CF
Carry Flag
Always a 1
PF
Parity Flag
Always a 0
AF
Always a 0
Page 175
Microprocessor- 8086
ZF
Zero Flag
SF
Sign Flag
TF
Trap Flag
IF
Interrupt Enable
10
DF
Direction Flag
11
OF
Overflow Flag
12-13
PL1,2
14
NT
15
Always a 0
Flag
Description
16
RF
Resume Flag
17
VM
Virtual Mode
Page 176
Microprocessor- 8086
I/O PRIVILEGE LEVEL - These two flags together indicate one of four privilege levels under
which the processor operates in protected mode. These are sometimes called "rings", with ring 0
being the most privileged and ring 3 the least.
RESUME FLAG This flag supports a debug register used to manage breakpoints in protected
mode.
VIRTUAL MODE This flag supports the third mode of operation of the processor, Virtual
8086 mode. Once in protected mode, if set, this flag causes the processor to switch to virtual
8086 mode.
Programmer-invisible Registers
To support protected mode, a variety of other registers are provided that are not accessible by the
programmer. In real mode, the programmer can see and reference the segment registers CS, SS,
DS, ES, FS, and GS as 16-bit entities. The contents of these registers are shifted four bit
positions to the left, then added to a 16-bit offset provided by the program. The resulting 20-bit
value is the real address of the data to be accessed at that moment. This allows a real address
space of 220 or 1.048 megabytes. In this space, all segments are limited to 64K maximum size.
In protected mode, segments may from 1 byte to 4.3 gigabytes in size. Further, there is more
information that is needed than in real mode. Therefore, the segment registers of real mode
become holders for "selectors", values which point to a reference in a table in memory that
contains more detail about the area in the desired segment. Also, a set of "Descriptor Registers"
is provided, one for each segment register. These contain the physical base address of the
segment, the segment limit (or the size of the segment relative to the base), and a group of other
data items that are loaded from the descriptor table. In protected mode, when a segment register
is loaded with a new selector, that selector references the table that has previously been set up,
and the descriptor register for that segment register is given the new information from the table
about that segment. During the course of program execution, addressing references to that
segment are made using the descriptor register for that segment.
Four Control Registers CR0 CR3 are provided to support specific hardware needs. CR0 is
called the Machine Control Register and contains several bits that were derived in the 80286.
These are:
PAGING ENABLED, bit 31 This bits when set enables the on-chip paging unit for virtual
memory.
TASK SWITCHED, bit 3 This bit is set when a task switch is performed.
EMULATE COPROCESSOR, bit 2 This bit causes all coprocessor OP codes to cause a
Coprocessor-Not-Found exception. This is turn will cause 80387 math coprocessor instructions
to have to be interpreted by software.
MONITOR COPROCESSOR, bit 1 Works with the TS bit above to synchronize the
coprocessor.
PROTECTION ENABLED, bit 0 This bit enables the shift to protected mode from real mode.
System Address Registers
Four special registers are provided to reference the tables needed to support protected mode.
These tables are the Global Descriptor Table (GDT), the Interrupt Descriptor Table (IDT), the
Local Descriptor Table (LDT) , and the Task State Segment (TSS). The addresses of these tables
are stored in four special registers called the Global Descriptor Table Register (GDTR), the
MANOJKUMAR S.B
Page 177
Microprocessor- 8086
Interrupt Descriptor Table Register (IDTR), the Local Descriptor Table Register (LDTR), and
the Task State Segment Register (TR).
These tables and registers will be discussed more fully below. Briefly, the GDTR and IDTR hold
the 32-bit linear base address and the 16-bit limit of the GDT and IDT, respectively. Both of
these tables are global in nature and so may be located anywhere, including in paged virtual
memory. These tables are not located using segment registers.
The LDTR and TR hold 16-bit selector values for the LDT descriptor and the TSS descriptor,
respectively. The LDT and TSS are task-specific segments, tied to individual tasks within the
system. Selectors to locate these segments are stored in system segment registers. These selectors
point to the table areas and work in conjunction with two descriptor registers that contain a 32-bit
linear base address for the table, a 32-bit segment limit, and some attribute bytes. This is in
concert with the approach for the programmer-visible segment registers.
MEMORY ORGANIZATION
In the 80386, data may take the physical form of an 8-bit byte, a 16-bit word, or a 32-bit double
word. The electrical organization of the memory connections to the processor follow this model.
For a word, the high-order byte is in the higher-addressed of the two bytes, while the low-order
byte is in the lower-addressed address. In the case of a 32-bit double word, the highest-most byte
is located in the highest address, and the lowest byte is located in the lowest address. The address
of the lowest byte is the address of the word or double word.
The 80386 also supports two other memory units. These are segments and pages. Segments can
be variable in length, and this supports a programmers approach to arranging code and data in
some sort of logical order for the work being done. Pages in this case are 4,096 bytes in length,
and these support the use of physical memory management, e.g., virtual memory.
Intel defines three distinct address spaces. These are physical, logical, and linear. The
programmer sees and works with the logical address space. It is constructed of the information
contained in a selectors table reference plus the addition of various offsets and indexes created
by the addressing modes of the programs instructions. The programmer does not care about the
amount of memory consumed he/she considers it to be "virtually" infinite in size. In fact, since
each task in an 80386 can have 16K selectors, and since offsets can be as much as 4 gigabytes in
size, the programmer can work with as much as 64 terabytes of memory.
The segmentation unit of the processor translates the logical address space of the programmer to
the 32-bit linear address space. If the paging unit is not enabled, the linear address space and the
physical address space are one and the same, and the linear address space is sent directly to the
device pins, thus becoming the physical address. If the paging unit is in use, then it will further
modify the linear address space to a different address space that may engage outside storage to
implement the memory that doesnt really exist in the system.
The primary difference between real and protected mode is the way in which the segmentation
unit performs the translation between logical and linear addresses. Just like the 8086, in real
mode the 80386 shifts the contents of the segment register left four bit positions, then adds the
offset to create a 20-bit linear address which is the same as the physical address (in the 8086,
there is no paging unit, so the linear and the physical addresses are always the same). In
protected mode, every selector has a linear base address connected with it. This linear base
address is stored in either the GDT or an LDT for the segment. From the table, the selectors
linear base address is added to the offset from the programs instructions to create the final linear
MANOJKUMAR S.B
Page 178
Microprocessor- 8086
address. If paging is off, this is also the physical address. If paging is on, further modification
occurs to create the final physical address from the logical address.
REAL MODE ARCHITECTURE
When the 80386 is reset, it assumes real mode. Real mode has the same base architecture as the
8086. The addressing mechanism, memory size, and interrupt handling are all identical to the
real mode of the 80286. However, the system allows access to the 32-bit register set. Almost all
of the 80386 instructions are accessible in real mode. The default operand size is 16 bits. To use
the 32-bit registers and addressing modes of the 80386 in real mode, override prefixes must be
used. The segment size in real mode is a maximum of 64K. Because of the paging feature
available for protected mode, it is not possible to use the lock prefix on repetitive string
instructions.
In real mode, the memory size is limited to one megabyte, using addressing lines A2 through
A19. The exception to this is that the A20 A31 lines are available from reset to the first
execution of an intersegment jump or call. This allows the ROM holding the power-on reset
vector and initialization code to be accessed. There is no paging in real mode, so the linear
address is the physical address.
The physical address is created by the offset addition of the contents of a particular segment
register (CS, DS, SS, or ES) and an offset provided by the analysis of an instruction, such as the
contents of an index register (SI or DI). The addition is offset such that the segment registers
contents is added four bit positions to the left of the offsets value, thus:
Illustration
Hence, although the segment registers and the offsets can contain no more than 16 bits each, the
sum generated is 20 bits long, sufficient to access any location with the 1.048 megabyte address
space of real mode. Since the segment register contents is always shifted four bits to the left,
segments must always begin on 16-byte boundaries. Segments may be overlapped, that is, there
is no restriction to segment registers containing values that are within another segments 64-K
address space. This allows programmers to use only what is needed for a small program.
There are two reserved areas of the memory map. The interrupt vectors occupy the first 1,024
bytes of them memory space, addresses 0 to 3FFh. This permits 256 4-byte jump vectors as in
the 8086. Also, locations FFFFFFF0h through FFFFFFFFh are reserved for the reset vector. The
system will initially attempt to execute an instruction at FFFFFFF0h following the clearing of
reset.
The Halt (HLT) instruction will cause the processor to stop execution and keeps it from using its
local bus. Halt can be cleared by three methods:
1. A non-maskable interrupt (NMI);
2. A regular interrupt (INTR) with interrupts enabled;
3. A system reset.
PROTECTED MODE ARCHTECTURE
The 80386 is most impressive when running in protected mode. The linear address space can be
as great as 232 (4294967295) bytes. With the paging unit enabled, the limit is 246 or about 64
terabytes. The device can run all 8086 and 80286 code. It provides a memory management and a
hardware-assisted protection mechanism that keeps one programs execution from interfering
MANOJKUMAR S.B
Page 179
Microprocessor- 8086
with another. Additional instructions are provided to support multitasking. The programmer sees
an expanded address space available to her/him, and different addressing scheme.
Memory Segmentation
Memory segmentation in protected mode uses a segment base value and an offset in the manner
of real mode. However, because of the increased size of the address space now available, a more
complex arrangement is used. The segment register now contains a value called a selector. This
is a 16-bit value which contains an offset into a table. This table, called a descriptor table,
contains descriptors which are 8-byte values that describe more about the segment in question.
Two tables provided are the Global Descriptor Table (GDT) and the Local Descriptor Table
(LDT). The GDT contains information about segments that are global in nature, that is, available
to all programs and normally used most heavily by the operating system. The LDT contains
descriptors that are application specific. Both of these tables have a limit of 64K, that is, 8,192 8byte entries. There is also an Interrupt Descriptor Table (IDT) that contains information about
segments containing code used in servicing interrupts. This table has a maximum of 256 entries.
The upper 13 bits of the selector are used as an offset into the descriptor table to be used. The
lower 3 bits are:
TI, a table selection bit 0 = use the GDT, 1 = use the LDT.
RPL, Requested Privilege Level bits = 00 is the highest privilege level, 11 is the lowest.
The selector identifies the table to be used and the offset into that table where a set of descriptor
bytes identifies the segment specifically. Each table can be 64K bytes in size, so if there are 8
bytes per table entry, a total of 8,192 entries can be held in one table at a given time. The
contents of a descriptor are:
Bytes 0 and 1 A 16-bit value that is connected to bits 0 3 of byte 6 to form the uppermost
offset, or limit, allowed for the segment. This 20 bit limit means that a segment can be between 1
byte and 1 megabyte in size. See the discussion of the granularity bit below.
Bytes 2 and 3 A 16-bit value connected to byte 4 and byte 7 to form a 32-bit base value for the
segment. This is the value added to the offset provided by the program execution to form the
linear address.
AV bit Segment available bit, where AV=0 indicates not available and AV=1 indicates
available.
D bit If D=0, this indicates that instructions use 16-bit offsets and 16-bit registers by default. If
D=1, the instructions are 32-bit by default.
Granularity (G) bit If G=0, the segments are in the range of 1 byte to 1 megabyte. If G=1, the
segment limit value is multiplied by 4K, meaning that the segments can have a minimum of 4K
bytes and a maximum limit of 4 gigabytes in steps of 4K.
Byte 5, Access Rights byte This byte contains several flags to further define the segment:
Bit 0, Access bit A=0 indicates that the segment has not been accessed; A=1 indicates
that the segment has been accessed (and is now "dirty").
Bits 1, R/W bit; bit 2, ED/C bit; and bit 3, E bit. If bit 3 = 0, then the descriptor
references a data segment and the other bits are interpreted as follows: bit 2, interpreted
as the ED bit, if 0, indicates that the segment expands upward, as in a data segment; if 1,
indicates that the segment expands in the downward direction, as in a stack segment; bit
MANOJKUMAR S.B
Page 180
Microprocessor- 8086
1, the R/W bit, if 0, indicates that the segment may not be written, while if 1 indicates
that the segment is writeable.
If bit 3 = 1, then the descriptor references a code segment and the other bits are
interpreted as follows: bit 2, interpreted as the C bit, if 0, indicates that we should ignore
the descriptor privilege for the segment, while if 1 indicates that privilege must be
observed; bit 1, the R/W bit, if 0, indicates that the code segment may not be read, while
if 1 indicates that the segment is readable.
Bit 4, System bit If 0, this is a system descriptor; if 1, this is a regular code or data
segment.
Bits 5 and 6, Descriptor Privilege Level (DPL) bits These two bits identify the privilege
level of the descriptor.
Bit 7, Segment Valid (P) bit If 0, the descriptor is undefined. If 1, the segment contains
a valid base and limit.
Use the illustration below to follow the flow of address translation. Numbers in circles on the
drawing match those below.
File goes here
1. The execution of an instruction causes a request to access memory. The segment portion
of the address to be used is represented by a selector value. This is loaded into the
segment register. Generally, this value is not changed too often, and is controlled by the
operating system.
2. The selector value in the segment register specifies a descriptor table and points to one of
8,192 descriptor areas. These contain 8 bytes that identify the base of the real segment, its
limit, and various access and privilege information.
3. The base value in the descriptor identifies the base address of the segment to be used in
linear address space.
4. The limit value in the descriptor identifies the offset of the top of the segment area from
the base.
5. The offset provided by the instruction is used to identify the specific location of the
desired byte(s) in linear address space, relative to the base value.
The byte(s) thus specified are read or written as dictated by the instruction.
Program Invisible Registers
Several additional registers are provided that are normally invisible to the programmer but are
required by the hardware of the processor to expedite its functions.
Each of the segment registers (CS, DS, SS, ES, FS, and GS) have an invisible portion that is
called a cache. The name is used because they store information for short intervals they are not
to be confused with the L1 or L2 cache of the external memory system. The program invisible
portions of the segment registers are loaded with the base value, the limit value, and the access
information of the segment each time the segment register is loaded with a new selector. This
allows just one reference to the descriptor table to be used for multiple accesses to the same
segment. It is not necessary to reference the descriptor table again until the contents of the
segment register is changed indicating a new segment of that type is being accessed. This system
MANOJKUMAR S.B
Page 181
Microprocessor- 8086
allows for faster access to the main memory as the processor can look in the cache for the
information rather than having to access the descriptor table for every memory reference to a
segment.
The Global Descriptor Table Register (GDTR) and the Interrupt Descriptor Table Register
(IDTR) contain the base address of the descriptor tables themselves and their limits, respectively.
The limit is a 16-bit value because the maximum size of the tables is 64K.
System Descriptors
The Local Descriptor Table Register contains a 16-bit wide selector only. This value references a
system descriptor, which is similar to that as described above, but which contains a type field
that identifies one of 16 types of descriptor (specifically type 0010) that can exist in the system.
This system descriptor in turn contains base and limit values that point to the LDT in use at the
moment. In this way, there is one global descriptor table for the operating system, but there can
be many local tables for individual applications or tasks if needed.
System descriptors contain information about operating system tables, tasks, and gates. The
system descriptor can identify one of 16 types as follows. You will notice that some of these are
to support backward compatibility with the 80286 processor.
MANOJKUMAR S.B
Type
Purpose
0000
Invalid
0001
0010
0011
0100
0101
Task Gate
0110
0111
1000
Invalid
1001
1010
Reserved
1011
1100
1101
Reserved
Page 182
1110
1111
Microprocessor- 8086
80386 Interrupt Gate
80386 Trap Gate
Data stored in a segment with privilege level P can be accessed only by code executing at
a privilege level that is at least as privileged as P.
A code segment or procedure with privilege level P can only by called by a task
executing at the same or a less privileged level than P.
At any point in time, a task can be operating at any of the four privilege levels. This is called the
tasks Current Privilege Level (CPL). A tasks privilege level may only be changed by a control
transfer through a gate descriptor to a code segment with a different privilege level.
The lower two bits of selectors contain the Requested Privilege Level (RPL). When a change of
selector is made, the CPL of the task and the RPL of the new selector are compared. If the RPL
is more privileged than the CPL, the CPL determines the level at which the task will continue. If
the CPL is more privileged than the RPL, the RPL value will determine the level for the task.
Therefore, the lowest privilege level is selected at the time of the change. The purpose of this
function is to ensure that pointers passed to an operating system procedure are not of a higher
privilege than the procedure that originated the pointer.
Gates
Gates are used to control access to entry points within the target code segment. There are four
types:
Call Gates those associated with Call, Jump, Return and similar operations codes. They
provide a secure method of privilege transfer within a task.
Task Gates those involved with task switching.
Interrupt Gates those involved with normal interrupt service needs.
Trap Gates those involved with error conditions that cause major faults in the
execution.
A gate is simply a small block of code in a segment that allows the system to check for privilege
level violations and to control entry to the operating system services. The gate code lives in a
segment pointed to by special descriptors. These descriptors contain base and offset values to
locate the code for the gate, a type field, a two-bit Default Privilege Level (DPL) and a five-bit
word count field. This last is used to indicate the number of words to be copied from the stack of
the calling routine to that of the called routine. This is used only in Call Gates when there is a
change in privilege level required. Interrupt and Trap gates work similarly except that there is no
pushing of parameters onto the stack. For interrupt gates, further interrupts are disabled. Gates
are part of the operating system and are mainly of interest to system programmers.
MANOJKUMAR S.B
Page 183
Microprocessor- 8086
Task Switching
An important part of any multitasking system is the ability to switch between tasks quickly.
Tasks may be anything from I/O routines in the operating system to parts of programs written by
you. With only a single processor available in the typical PC, it is essential that when the needs
of the system or operator are such that a switch in tasks is needed, this be done quickly.
The 80386 has a hardware task switch instruction. This causes the machine to save the entire
current state of the processor, including all the register contents, address space information, and
links to previous tasks. It then loads a new execution state, performs protection checks, and
begins the new task, all in about 17 microseconds. The task switch is invoked by executing an
intersegment jump or call which refers to a Task Switch Segment (TSS) or a task gate descriptor
in the LDT or GDT. An INT n instruction, exception, trap, or external interrupt may also invoke
a task switch via a task gate descriptor in the associated IDT.
Each task must have an associated Task Switch Segment. This segment contains an image of the
systems conditions as they exist for that task. The TSS for the current task, the one being
executed by the system at the moment, is identified by a special register called the Task Switch
Segment Register (TR). This register contains a selector referring to the task state segment
descriptor that defines the current TSS. A hidden base and limit register connected to the TR are
loaded whenever TR is updated. Returning from a task is accomplished with the IRET
instruction which returns control to the task that was interrupted with the switch. The current
tasks segment is stored and the previous tasks segment is used to bring it into the current task.
Control Registers
The 80386 has four "Control Registers" called CR0 through CR3. CR0 contains several bit flags
as follows:
PG When set to 1, causes the translation of linear addresses to physical addresses. Indicates
that paging is enabled and virtual memory is being used.
ET When set to 1, indicates that the 80387 math coprocessor is in use.
TS When set to 1, indicates that the processor has switched tasks.
EM When set to 1, causes a type 7 interrupt for the ESC (escape) instruction for the math
coprocessor.
MP When set to 1, indicates that the math coprocessor is present in the system.
PE Selects protected mode of operation.
CR 1 is not used by the 386. CR2 contains page fault linear addresses for the virtual memory
manager. CR3 contains a pointer to the base of the page directory for virtual memory
management.
Switching to Protected Mode
At reset, the 80386 begins operation in Real Mode. This is to allow setup of various conditions
before the switch to Protected Mode is made. The actual switch is accomplished by setting the
PE bit in CR0. The following steps are needed.
1. Initialize the interrupt descriptor table to contain valid interrupt gates for at least the first
32 interrupt types. The IDT can contain 256 8-byte gates.
2. Set up the GDT so that it contains a null descriptor at position 0, and valid descriptors for
at least one code, one data, and one stack segment.
3. Switch to protected mode by setting PE to 1.
4. Execute a near JMP to flush the internal instruction queue and to load the TR with the
base TSS descriptor.
MANOJKUMAR S.B
Page 184
Microprocessor- 8086
MANOJKUMAR S.B
Page 185
Microprocessor- 8086
D or DIRTY bit: This bit is not used in the page directory. In the page table entries, it indicates
that the 4K area defined by this entry has been written to, and so must be saved (as to disk) if the
area is to be reused for something else.
A or ACCESSED bit: This bit is set to a 1 when the processor accesses the 4K page.
R/W or Read/Write and U/S or User/Supervisor bits: These are used in conjunction with
privilege management.
P or PRESENT bit: This bit when set to 1 indicates that the referenced page is present in
memory. If 0, it can be used to indicate that the page is not in RAM, e.g., is on disk.
Performance of the paging system would be affected if the system needed to reference memory
tables each time a reference to RWM was made. To offset this, a Translation Lookaside Buffer
(TLB) is provided. This is a 4-way set-associative cache that contains entries for the last 32
pages needed by the processor. This provides immediate information about 98% of the time,
causing only 2% of memory accesses to make the page directory-page table translation.
HARDWARE HIGHLIGHTS
The instructor will provide you with illustrations of the timing sequences for the various read and
write cycles available on the 80386. There are two items of interest that we note here.
Address Pipelining
Under non-pipelined conditions, the bus signals of the 386 function very much like any other
processor. A machine cycle consists of two T-states, T1 and T2. These are defined by the
following edge of the system clock signal. At the beginning of T1, an address appears on the
BE0# through BE3# and A2 through A31 lines, along with various control lines. The address is
held valid until very near the end of T2. The ADS# line is pulled low (active) during T1 to
indicate that the address bus contains a valid address; the ADS# line is pulled high (negated)
during T2. The data is passed in or out at the transition between the end of T2 of the current
cycle and the start of T1 of the following machine cycle. During this time, the NA# line is
maintained high (negated).
In pipelining, the address bits are available machine cycle earlier than with no pipelining. The
ADS# line is pulled low during T2 of a cycle rather than T1, indicating that during T2, the
address of the data to be exchanged during the next machine cycle is available. Pipelining is
initiated by the incoming line NA#, that is controlled by the memory subsystem. If pulled low
during a T1, the memory expects that the address of the next bytes needed will be available
cycle early.
The purpose of pipelining is to minimize the need for wait states. The time needed to read or
write data remains the same. However, the time an address is available before the data is
expected is lengthened so that a wait state may not be needed. The memory subsystem has to be
designed to work within these parameters.
Dynamic Bus Sizing
Normally, the 80386 expects data to be transferred on a 32-bit wide data bus. However, it is
possible to force the system to transfer 32-bit data as two 16-bit quantities in two successive bus
cycles. This is initiated by the BS16# signal coming from the memory or I/O device subsystem.
This line is pulled low during the middle of T2. It indicates to the processor that 32-bit data will
be sent as two 16-bit words, with D0-D15 on the first transfer and D16-D31 on the second. The
data is transferred on the D0-D15 bus lines; the D16-D31 lines are ignored.
INSTRUCTION SET
The instruction set of the 80386 is compatible with that of the 8086 and the programming for that
processor can run on the 386 without modification. However, the 386 includes extension of the
MANOJKUMAR S.B
Page 186
Microprocessor- 8086
base instruction set to support 32-bit data processing and operation in protected mode. The
reader is referred to the Intel documentation for full particulars on each instruction and its
possible versions. Here we discuss the essential aspects of instruction organization.
Instructions vary in length, depending upon how much information must be given for the
instruction, the addressing modes used, and the location of data to be processed. The generic
instruction contains the following:
BYTE 1: This is the operation (OP) code for the instruction. Bit position 0 may be interpreted as
the "w" bit, where w=0 indicates byte mode and w=1 indicates word mode. Also, bit position 1
may be interpreted as the operation direction bit in double operand instructions as follows:
d
Direction of Operation
BYTE 2 (optional): This second byte of OP code may or may not be used depending on the
operation.
BYTE 3: This is the "mod r/m" byte. Bits 3, 4, and 5 contain more OP code information. Bits 0,
1, and 2 contain the "r/m", or "register/memory" of the instruction. These identify which registers
are in use or how the memory is addressed (the addressing mode). The r/m bits are interpreted
depending upon the two "mod" or mode bits according to this chart:
Mod r/m
00 000
DS: [BX+SI]
DS: [EAX]
00 001
DS: [BX+DI]
DS: [ECX]
00 010
DS: [BP+SI]
DS: [EDX]
00 011
DS: [BP+DI]
DS: [EBX]
00 100
DS: [SI]
00 101
DS: [DI]
DS: d32
00 110
DS: d16
DS: [ESI]
00 111
DS: [BX]
DS: [EDI]
01 000
DS: [BX+SI+d8]
DS: [EAX+d8]
MANOJKUMAR S.B
Page 187
Microprocessor- 8086
01 001
DS: [BX+DI+d8]
DS: [ECX+d8]
01 010
SS: [BP+SI+d8]
DS: [EDX+d8]
01 011
SS: [BP+DI+d8]
DS: [EBX+d8]
01 100
DS: [SI+d8]
sib is present
01 101
DS: [DI+d8]
SS: [EBP+d8]
01 110
SS: [BP+d8]
DS: [ESI+d8]
01 111
DS: [BX+d8]
DS: [EDI+d8]
10 000
DS: [BX+SI+d16]
DS: [EAX+d32]
10 001
DS: [BX+DI+d16]
DS: [ECX+d32]
10 010
SS: [BP+SI+d16]
DS: [EDX+d32]
10 011
SS: [BP+DI+d16]
DS: [EBX+d32]
10 100
DS: [SI+d16]
sib is present
10 101
DS: [DI+d16]
SS: [EBP+d32]
10 110
SS: [BP+d16]
DS: [ESI+d32]
10 111
DS: [BX+d16]
DS: [EDI+d32]
11 000
AL
AX
AL
EAX
11 001
CL
CX
CL
ECX
11 010
DL
DX
DL
EDX
11 011
BL
BX
BL
EBX
11 100
AH
SP
AH
ESP
11 101
CH
BP
CH
EBP
11 110
DH
SI
DH
ESI
11 111
BH
DI
BH
EDI
MANOJKUMAR S.B
Page 188
Microprocessor- 8086
BYTE 4 (optional): This is the "sib" byte and is not found in the 8086. It appears only in some
80386 instructions as needed. This byte supports the "scaled index" addressing mode. Bit
positions 0-2 identify a general register to be used as a base value. Bit positions 3-5 identify a
general register which contains an index register. Bit positions 6 and 7 identify a scaling factor to
be used to multiply the value in the index register as follows:
ss
Scale Factor
00
01
10
11
Index Register
000
EAX
001
ECX
010
EDX
011
EBX
100
101
EBP
110
ESI
111
EDI
The mod field of the mod r/m byte taken with the base value of the sib byte generates the
following scaled indexing modes:
MANOJKUMAR S.B
Mod base
Effective Address
00 000
00 001
00 010
00 011
Page 189
00 100
Microprocessor- 8086
00 101
00 110
00 111
01 000
01 001
01 010
01 011
01 100
01 101
01 110
01 111
10 000
10 001
10 010
10 011
10 100
10 101
10 110
10 111
Following a possible byte 4, there may be 1, 2, or 4 bytes of address displacement which provide
an absolute offset into the current segment for data location. Also following may be 1, 2, or 4
bytes to implement immediate data.
The byte and bit pattern of instructions vary. For instance, in conditional instructions a four-bit
field called "tttn" implements the conditions to be tested:
Mnemonic
MANOJKUMAR S.B
Condition
tttn
Page 190
Microprocessor- 8086
Overflow
0000
NO
No Overflow
0001
B/NAE
0010
NB/AE
0011
E/Z
Equal/Zero
0100
NE/NZ
0101
BE/NA
0110
NBE/A
0111
Sign
1000
NS
Not Sign
1001
P/PE
Parity/Parity Even
1010
NP/PO
No Parity/Parity Odd
1011
L/NGE
1100
NL/GE
1101
LE/NG
1110
NLE/G
1111
Pentium
About the Pentium Architecture
------------------------------- It is not a load/store architecture.
-- The instruction set is huge! We go over only a fraction of
the instruction set. The text only presents a fraction.
-- There are lots of restrictions on how instructions/operands are
put together, but there is also an amazing amount of flexibility.
MANOJKUMAR S.B
Page 191
Microprocessor- 8086
Registers
--------The Intel architectures as a set just do not have enough registers
to satisfy most assembly language programmers. Still, the processors
have been around for a LONG time, and they have a sufficient number
of registers to do whatever is necessary.
For our (mostly) general purpose use, we get
32-bit
16-bit
8-bit
EAX
EBX
ECX
EDX
AX
BX
CX
DX
AH
BH
CH
DH
8-bit
(high part of 16) (low part of 16)
AL
BL
CL
DL
and
EBP
ESI
EDI
ESP
BP
SI
DI
SP
There are a few more, but we won't use or discuss them. They
are only used for memory accessability in the segmented memory
model.
Using the registers:
As an operand, just use the name (upper case and lower case both
work interchangeably).
EBP is a frame pointer (see Chapter 11).
ESP is a stack pointer (see Chapter 11).
Oddities:
This is the only architecture that I know of where the programmer
can designate part of a register as an operand. On ALL other
machines, the whole register is designated and used.
ONE MORE REGISTER:
Many bits used for controlling the action of the processor and
setting state are in the register called EFLAGS. This register
contains the condition codes:
OF Overflow flag,
MANOJKUMAR S.B
SF Sign flag,
ZF Zero flag,
PF Parity flag,
CF Carry flag
Page 192
Microprocessor- 8086
The settings of these flags are checked in conditional control instructions. Many instructions
set one or more of the flags.There are many other bits in the EFLAGS register: TO BE
DISCUSSED LATER. The use of the EFLAGS register is implied (rather than explicit) in
instructions.
Accessing Memory
---------------There are 2 memory models supported in the Pentium architecture.
(Actually it is the 486 and more recent models that support 2 models.)
In both models, memory is accessed using an address. It is the way that addresses are formed
(within the processor) that differs in the 2 models.
FLAT MEMORY MODEL
-- The memory model that we use. AND, the memory model that every other manufactures'
processors also use.
SEGMENTED MEMORY MODEL
-- Different parts of a program are assumed to be in their own, set-aside portions of memory.
These portions are called segments.
-- An address is formed from 2 pieces: a segment location and an offset within a segment.
Note that each of these pieces can be shorter (contain fewer bits) than a whole address. This
is much of the reason that Intel chose this form of memory model for its earliest single-chip
processors.
-- There are segments for:
code
data
stack
other
-- Which segment something is in can be implied by the memory access involved. An
instruction fetch will always be looking in the code segment. A push instruction always accesses
the stack segment. Etc.
Addressing Modes
---------------Some would say that the Intel architectures only support 1 addressing mode.
(something like) this:
effective address = base reg + (index reg x scaling factor) + displacement
where
base reg is EAX, EBX, ECX, EDX or ESP or EBP
MANOJKUMAR S.B
It looks
Page 193
Microprocessor- 8086
The syntax of using this (very general) addressing mode will vary from system to system. It
depends on the preprocessor and the syntax accepted by the assembler.
For our implementation, an operand within an instruction that uses this addressing mode could
look like [EAX][EDI*2 + 80]
The effective address calculated with be the contents of register EDI multiplied times 2 added
to the constant 80, added to the contents of register EAX.
There are extremely few times where a high-level language compiler can utilize such a complex
addressing mode. It is much more likely that simplified versions of this mode will be used.
SOME ADDRESSING MODES
-- register mode -The operand is in a register. The effective address is the register
Example instruction:
mov eax, ecx
Both operands use register mode. The contents of register ecx is copied to register eax.
-- immediate mode -The operand is in the instruction. The effective address is within the instruction.
Example instruction:
mov eax, 26
The second operand uses immediate mode. Within the instruction is the operand. It is copied
to register eax.
-- register direct mode -The effective address is in a register.
Example instruction:
mov eax, [esp]
The second operand uses register direct mode. The contents of register esp is the effective
address. The contents of memory at the effective address are copied into register eax.
-- direct mode -The effective address is in the instruction.
Example instruction:
MANOJKUMAR S.B
Page 194
Microprocessor- 8086
The second operand uses direct mode. The instruction contains the effective address. The
contents of memory at the effective address are copied into register eax.
-- base displacement mode -The effective address is the sum of a constant and the contents of a register.
Example instruction:
mov eax, [esp + 4]
The second operand uses base displacement mode. The instruction contains a constant. That
constant is added to the contents of register esp to form an effective address. The contents of
memory at the effective address are copied into register eax.
-- base-indexed mode -- (Intel's name)
The effective address is the sum of the contents of two registers.
Example instruction:
mov eax, [esp][esi]
The contents of registers esp and esi are added to form an effective address. The contents of
memory at the effective address are copied into register eax.
Note that there are restrictions on the combinations of registers that can be used in this
addressing mode.
-- PC relative mode -The effective address is the sum of the contents of the PC and a constant contained within the
instruction.
Example instruction:
jmp a_label
The contents of the program counter is added to an offset that is within the machine code for
the instruction. The resulting sum is placed back into the program counter. Note that from the
assembly language it is not clear that a PC relative addressing mode is used. It is the assembler
that generates the offset to place in the instruction.
Instruction Set
---------------Generalities:
-- Many (most?) of the instructions have exactly 2 operands.
If there are 2 operands, then one of them will be required to use register mode, and the other
will have no restrictions on its addressing mode.
MANOJKUMAR S.B
Page 195
Microprocessor- 8086
-- There are most often ways of specifying the same instruction for 8-, 16-, or 32-bit oeprands. I
left out the 16-bit ones to reduce presentation of the instruction set. Note that on a 32-bit
machine, with newly written code, the 16-bit form will never be used.
Meanings of the operand specifications:
reg - register mode operand, 32-bit register
reg8 - register mode operand, 8-bit register
r/m - general addressing mode, 32-bit
r/m8 - general addressing mode, 8-bit
immed - 32-bit immediate is in the instruction
immed8 - 8-bit immediate is in the instruction
m - symbol (label) in the instruction is the effective address
Data Movement
------------mov reg, r/m
r/m, reg
reg, immed
r/m, immed
; copy data
lea reg, m
; get effective address
(A newer instruction, so its format is much restricted over the other ones.)
EXAMPLES:
it in ECX
mov EAX, 23 ; places 32-bit 2's complement immediate 23 into register EAX
movsx ECX, AL ; sign extends the 8-bit quantity in register AL to 32 bits, and places
mov [esp], -1 ; places value -1 into memory, address given by contents of esp
lea EBX, loop_top ; put the address assigned (by the assembler) to label loop_top into
register EBX
Integer Arithmetic
-----------------add reg, r/m
; two's complement addition
r/m, reg
reg, immed
r/m, immed
inc reg
; add 1 to operand
r/m
sub reg, r/m
; two's complement subtraction
r/m, reg
MANOJKUMAR S.B
Page 196
Microprocessor- 8086
reg, immed
r/m, immed
dec reg
; subtract 1 from operand
r/m
neg r/m
; get additive inverse of operand
mul eax, r/m
; unsigned multiplication edx||eax <- eax * r/m
imul r/m
; 2's comp. multiplication edx||eax <- eax * r/m
reg, r/m
; reg <- reg * r/m
reg, immed
; reg <- reg * immed
div r/m
; unsigned division
; does edx||eax / r/m
; eax <- quotient
; edx <- remainder
idiv r/m
; 2's complement division
; does edx||eax / r/m
; eax <- quotient
; edx <- remainder
cmp reg, r/m
; sets EFLAGS based on
r/m, immed
; second operand - first operand
r/m8, immed8
r/m, immed8
; sign extends immed8 before subtract
EXAMPLES:
neg [eax + 4] ; takes doubleword at address eax+4
; and finds its additive inverse, then places
; the additive inverse back at that address
; the instruction should probably be
;
neg dword ptr [eax + 4]
inc ecx
; adds one to contents of register ecx, and
; result goes back to ecx
Logical
------not r/m
; logical not
and reg, r/m
; logical and
reg8, r/m8
r/m, reg
r/m8, reg8
r/m, immed
r/m8, immed8
or reg, r/m
; logical or
reg8, r/m8
r/m, reg
r/m8, reg8
r/m, immed
MANOJKUMAR S.B
Page 197
Microprocessor- 8086
r/m8, immed8
xor reg, r/m
; logical exclusive or
reg8, r/m8
r/m, reg
r/m8, reg8
r/m, immed
r/m8, immed8
test r/m, reg
; logical and to set EFLAGS
r/m8, reg8
r/m, immed
r/m8, immed8
EXAMPLES:
and edx, 00330000h ; logical and of contents of register
; edx (bitwise) with 0x00330000,
; result goes back to edx
Floating Point Arithmetic
------------------------Since the newer architectures have room for floating point hardware on chip, Intel defined a
simple-to-implement extension to the architecture to do floating point arithmetic. In their usual
zeal, they have included MANY instructions to do floating point operations.
The mechanism is simple. A set of 8 registers are organized and maintained (by hardware) as a
stack of floating point values. ST refers to the stack top. ST(1) refers to the register within the
stack that is next to ST. ST and ST(0) are synonyms.
There are separate instructions to test and compare the values of floating point variables.
finit
; initialize the FPU
fld m32
; load floating point value
m64
ST(i)
fldz
; load floating point value 0.0
fst m32
; store floating point value
m64
ST(i)
fstp m32
; store floating point value
m64
; and pop ST
ST(i)
fadd m32
; floating point addition
m64
ST, ST(i)
ST(i), ST
faddp ST(i), ST
; floating point addition
; and pop ST
MANOJKUMAR S.B
Page 198
Microprocessor- 8086
I/O
--The only instructions which actually allow the reading and writing of I/O devices are
priviledged. The OS must handle these things. But, in writing programs that do something
useful, we need input and output. Therefore, there are some simple macros defined to help us do
I/O.
These are used just like instructions.
put_ch r/m
get_ch r/m
put_str m
Control Instructions
-------------------These are the same control instructions that all started with the character 'b' in SASM.
jmp m
; unconditional jump
jg m
; jump if greater than 0
jge m
; jump if greater than or equal to 0
jl m
; jump if less than 0
jle m
; jump if less than or equal to 0
-------------------------------------------------------------------------------------------------------Introduction to Pentium micro-processors
1. 80386: (1986)
a. 32-bit microprocessor.
b. 4GB main memory.
c. 12-33MHz.
d. Memory management unit added.
e. Variations: DX, EX, SL, SLC (cache) and SX.
f. 80386SX: 16MB through a 16-bit data bus and 24 bit address bus.
2. 80486: (1989)
a. 32-bit microprocessor, 32-bit data bus and 32-bit address bus.
b. 4GB main memory.
c. 20-50MHz. Later at 66 and 100MHz
d. Incorporated an 80386-like microprocessor, 80387-like floating point coprocessor
and an 8K byte cache on one package.
e. About half of the instructions executed in 1 clock instead of 2 on the 386.
f. Variations: SX, DX2, DX4.
g. DX2: Double clocked version: 66MHz clock cycle time with memory transfers at
33MHz.
3. Pentium: (1993)
MANOJKUMAR S.B
Page 199
Microprocessor- 8086
a.
b.
c.
d.
e.
f.
g.
h.
i.
MANOJKUMAR S.B
Page 200
Microprocessor- 8086
Next Generation just around the corner 90nm transistors, 125 million possible.
MANOJKUMAR S.B
Page 201