Atal Chaudhuri
Dept. of Computer Sc. & Engg.
Jadavpur University
1
Data Registers
AH
AL
BX
BH
BL
CX
CH
CL
DX
DH
DL
ALU
( Address / Data )
Instruction Queue
Control
Logic
20 pins
( Control )
20 pins
PSW
Segment Registers
CS
BP
SS
SI
DS
DI
ES
IP
+ 5 volts
Ground
Clock
5MHz / ( 8 , 10 )
Data Registers.
ii.
iii.
Segment Registers.
Data Registers :
AX , BX , CX , DX
AX
AH , AL
BX
BH , BL
CX
CH , CL
DX
DH , DL
as
Accumulator.
BX
as
CX
as
Counter.
DX
as
I / O address.
Maximum 64 KB long.
Allow the memory capacity to be 1 MB, even though the address operands are 16-bits wide.
2.
Allow the instruction, data or stack portion of a program to be more than 64 KB long by using
more than one code, data or stack segment.
3.
Facilitate the use of separate memory areas for a program, its data and the stack.
4.
Permit a program and / or its data to be put into different areas of memory each time the program
is executed ( highly useful in time sharing multiprogramming environments ).
Segment over_ride table showing default and alternate segment base for memory referenced instructions.
Type of memory ref.
Default Segment
Alternate Segment
Offset
Instruction Fetch
CS
None
IP
Stack Operation
SS
None
SP
DS
CS,ES,SS
Effective address
String source
DS
CS,ES,SS
SI
String Destination
ES
None
DI
BP as base register
SS
CS,ES,DS
Effective address
overridden by ES )
To provide flexible base addressing and indexing, a data address may be formed by adding together
a combination of the BX or BP register contents, SI or DI register contents and a displacement. The
result of such an address computation is called an Effective Address ( EA ) or Offset . The final
data address, however, is determined by the EA and the appropriate Data segment ( DS ), Extra
segment ( ES ) or stack segment ( SS ) register.
or
Offset
16 bits
Segment Address
16 bits
Physical Address
20 bits
Example :
431B
+ 123A0
Effective Address
Beginning Segment Address
157BB
In accessing the stack segment the 8086 gets the base address from SS and the offset from one of the
pointer register SP or BP.
In accessing the data segment the 8086 gets the base address from DS and the offset BX or one of the
index registers SI or DI and also BP.
( Normally, 8086 assumes these registers combinations but one can override these assignments. For
instance, one can access a location in the extra segment by directing the 8086 to apply ES rather than
SS to an offset in BP ).
Instruction Pointer :
General operation cycle for a computer is as follows :
1.
Fetching the next instruction from the address indicated by the PC.
2.
Putting it in the instruction register and decoding it while the PC is incremented to point to the
next instruction.
3.
Executing the instruction and if a branch is to be taken resetting the PC to the branch address.
4.
8086 follows the same basic pattern but here some of the operations may be overlapped.
The address of the next instruction is determined by the sum of the ( IP ) and ( CS ) X16 10 and the
instruction register is a 6 byte FIFO queue that is continually being filled whenever the system
bus is not needed for some other operation. This look ahead feature significantly increases the
CPU throughput except when branching takes place.
8086 CPU has two units EU ( Execution Unit ) and BIU ( Bus Interface Unit ).
BIU fetches the instruction and EU executes with the help of ALU under control of Control Unit.
( 8088 instruction queue is 4-byte wide )
OF DF IF TF SF ZF X AF X PF X CF
10
If DF = 1 a string is processed from high address to low address or from right to left and vice-versa.
11
OF is 1 if the addition of two like signed numbers, or the subtraction of two opposite signed
numbers, has produced a result that exceeds the 2s complement capacity of the operand, otherwise it
is 0.
OF is also 1 if the most significant bit ( sign bit ) of the operand is changed at any time during an
arithmetic shift operation, otherwise it is 0.
OF flag in combination with CF flag, also indicates the length of a multiplication result. If the upper
half of the product is non-zero, CF and OF are 1, otherwise both bits are 0.
Finally OF is 1 if the quotient produced by a divide operation overflow the result register.
12
Addressing Modes :
In assembly language each instruction may have maximum four fields :
[ Label ] Mnemonic / Opcode [ Operand ] [ Comment ]
Addressing modes used in data handling :
Immediate :
The datum is 8-bit or 16-bit long and is part of the instruction.
Instruction
Datum
Immediate
Direct :
The 16-bit effective address of the datum is part of the instruction.
Instruction
Memory
EA.
Datum
Direct
13
Register :
The datum is in the register that is specified by the instruction.
Instruction
Register
Register
Datum
Register
Register may be
AX , BX , CX , DX , SI , DI , SP , BP ,
for 16-bit
AH , AL , BH , BL , CH , CL , DH , DL ,
for 8-bit
14
Register Indirect :
The effective address of the datum is in any base register, index register or BX, i.e.
EA =
( BX )
( BP )
( SI )
( DI )
Instruction
Register
Memory
Register
EA.
Datum
Register Indirect
15
Register Relative :
The effective address is the sum of our 8-bit or 16-bit
displacement and the contents of a base register or an index
register.
EA =
( BX )
( BP )
( DI )
( SI )
Instruction
Register
Displacement
Memory
EA.
Datum
Register
Address
Register relative
16
Based Index :
The effective address is the sum of the base register and an index register, both are specified by the
instruction i.e.
( BX )
( SI )
EA =
+
( BP )
( DI )
Instruction
Base reg.
Index reg.
Register
Index
Memory
EA.
Datum
Register
Base address
Based indexed
17
( BX )
EA =
( SI )
+
( BP )
Base reg.
Index reg.
Displacement
Register
Index
Memory
EA
Datum
Register
Base address
18
Example :
( BX )
= 0158
( DI) = 10A5
Displacement =1B57
( DS) = 2100
And DS is used as the segment register, then the effective and physical address produced by these
quantities and the various addressing mode would be :
Direct : EA = 1B57
Physical address = 1B57 + 21000 = 22B57
Register : No effective address datum is in specified register.
Register indirect assuming register BX : EA = 0158
Physical address = 0158 + 21000 = 21158
Register relative assuming register BX : EA = 0158 + 1B57 = 1CAF
Physical address = 1CAF + 21000 = 22CAF
Based indexed assuming registers BX and DI : EA = 0158 + 10A5 = 11FD
Physical address = 11FD + 21000 = 221FD
Relative based indexed assuming BX and DI : EA = 0158 + 1B57 = 2D54
Physical address = 2D54 + 21000 = 23D54
19
Intrasegment Direct :The effective branch address is the sum of an 8-bit or 16-bit displacement and
the current contents of IP. When the displacement is 8 bits long, it is referred to as a short jump. It
may be used with either conditional or unconditional branching, but a conditional branch instruction
have only an 8-bit displacement.
Instruction
Displacement
IP
Intrasegment Direct
Intrasegment Indirect : The effective branch address is the contents of register or memory location
that is accessed using any of the above data addressing modes except the immediate mode. The
contents of IP are replaced by the effective branch address. This addressing mode may be used for
unconditional branch instructions.
Register
Effective branch address
Instruction
Addressing Mode
Or
Memory
Effective branch address
Intrasegment Indirect
.
20
Intersegment Direct : Replaces the contents of IP with a part of the instruction and the contents of
CS with another part of the instruction. The purpose of this addressing mode is to provide a means
of branching from one code segment to another.
Instruction
Offset
CS
Segment
IP
Intersegment Direct
Intersegment Indirect : Replaces the contents of IP and CS with the contents of two consecutive
words in memory that are referenced using any above data- related addressing modes except the
immediate and the register.
Two consecutive
words in memory
Instruction
Branch address Offset
.
EA computed according
Addressing Mode
Segment address
to addressing mode
Intersegment Indirect
.
21
Note that the physical branch address is the new content of the IP plus the content of CS multiplied
by 1610. An intersegment branch must be conditional.
To demonstrate how indirect branching works with some of the data addressing modes, suppose that
BX = 1256
SI = 528F
Displacement = 20A1.
Then :
With direct addressing, the effective branch addresses are the contents :
20A1 + ( DS ) x 1610.
With register relative addressing assuming register BX, the effective branch addresses
are the contents of :
1256 + 20A1 + ( DS ) x 1610.
With based indexed addressing assuming registers BX and SI, the branch are the
contents of :
1256 + 528F + ( DS ) x 1610.
22
I/O-3
GND
Reset
22
Ready
Ground
Terminates current
activity, clears PSW, IP, DS,
ES, SS, and instruction
queue and sets CS to FFFF.
Thus processing begins from
FFFF0 when signal is
withdrawn. (Signal must be
high for at least 4 clock
cycles)
Acknowledgement from
memory or I/O interface
that CPU can complete the
current bus cycles. CPU
samples the ready input at
the rising edge of 3rd clock
of bus cycles, if finds high
continues otherwise wait
24
state is introduced.
Test
32
RD
O/3
33
Min/Max
24-31
BHE/S7
O-3
35-38
A19/S6 O-3
- A16/S3
AD15
Vcc
Minimum Mode:
24
INTA
25
ALE
26
DEN
I/O-3
Same as AD14-AD0.
+5volts + 10%.
O-3
Indicates recognition of an
interrupt request.
O
Address latch enable,
generated during T1 state of
bus cycles and address is
latched during the high to
low transition of ALE.
O-3
Data enable, active during
T2 to T4 of the bus cycles to
inform the transreceiver that
CPU is ready to send or
receive data.
27
DT/R
O-3
28
M/IO
O-3
29
WR
O-3
30
31
HLDA
HOLD
O
I
28
26,27,28 S0,S1,S2
O-3
S1
0
0
1
1
0
S0
0
1
0
1
0
29
S1 S0
0
1
1
0
1
1
Memory Read
Memory Write
Inactive
LOCK
O-3
processors from getting access to the system bus. The LOCK pin
remains low till the prefixed instruction is carried out. It is
connected with 8289 bus arbiter.
30
RQ/GT1
I/O
31
RQ/GT0
I/O
BHE A0
Word transfer at
an even address
Byte transfer at
an even address
Byte transfer at
an odd address
Word transfer at
an odd address
AD15-AD0
AD7-AD0
AD15-AD8
0
1
1
0
INSTRUCTION SET
There are 92 basic instruction types. The instruction set can be divided into seven
functional groups.
1.
2.
3.
4.
5.
6.
7.
Data transfer instructions move information between registers and memory locations or
I / O ports.
Arithmetic instructions perform arithmetic operations on Binary and Binary Coded
Decimal ( BCD ) numbers.
Bit manipulation instructions perform shift, rotate, and logical operations on memory
locations and registers.
Control transfer instructions perform jumps, calls to procedures and returns from
procedures, to control the sequence in which a program executes.
String instructions move, compare and scan strings of information.
Interrupt instructions cause the microprocessor to be interrupted, so that it can service
some specific condition.
Processor control instructions set and clear status flags and change the microprocessors
execution state.
32
Assembler format
OF DF IF TF SF ZF AF PF CF
GENERAL -PURPOSE
MOV
MOV
destination,source
PUSH
POP
XCHG
XLAT
PUSH
source
POP
XCHG
destination
destination,source
XLAT
INPUT / OUTPUT
source-table
-
IN
OUT
IN
OUT
accumulator,port
port,accumulator
ADDRESS TRANSFER
LEA
LDS
LES
-
LEA
reg16,mem16
LDS
LES
reg16,mem32
reg16,mem32
33
Flags
Mnemonic
Assembler format
OF DF IF TF SF ZF AF PF CF
FLAG TRANSFER
LAHF
SAHF
PUSHF
POPF
NOTE:
LAHF
SAHF
PUSHF
POPF
- * *
34
Arithmetic instructions
Flags
Mnemonic
Assembler format
OF DF IF TF SF ZF AF PF CF
ADDITION
ADD
ADC
AAA
DAA
INC
SUB
SBB
AAS
DAS
*
DEC
NEG
CMP
ADD
ADC
AAA
DAA
INC
destination,source
destination,source
destination
*
*
?
?
*
- - - - - -
* *
* *
? ?
* *
* *
*
*
*
*
*
* *
* *
? *
* *
*
*
*
?
*
*
*
*
*
*
*
?
*
*
*
*
* * *
* * *
* * *
*
*
*
*
*
SUBTRACTION
SUB
SBB
AAS
DAS
destination,source
destination,source
*
*
?
?
DEC
NEG
CMP
destination
destination
destination,source
*
*
*
- - - -
*
*
?
*
35
Flags
Mnemonic
Assembler format
OF DF IF TF SF ZF AF PF CF
MULTIPLICATION
MUL
IMUL
AAM
MUL
IMUL
AAM
source
source
*
*
?
?
?
*
?
?
*
?
?
?
? *
? *
* ?
?
?
?
?
?
?
?
?
?
?
SIGN-EXTENSION
- - -
- - - - - - - - - - -
DIVISION
DIV
IDIV
DIV
IDIV
AAD
AAD
CBW
CWD
CBW
CWD
source
source
?
?
?
36
Assembler format
Flags
OF DF IF TF SF ZF AF PF CF
LOGICAL
AND
AND
destination,source
OR
0
XOR
0
NOT
SAL/SHL
TEST
SAR
*
SHR
*
ROL
ROR
RCL
ROR
OR
destination,source
XOR
destination,source
* 0
NOT
SAL
TEST
SAR
SHIFT
destination
destination,count
destination,source
destination,count
*
0
*
--
- -- -- -
- ** **
* *
??
?
- ** 0*
*
SHR
destination,countROTATE
ROL
ROR
RCL
ROR
destination,count
destination,count
destination,count
destination,count
*
*
*
*
- - - - -
- - - - -
- *
- *
- *
- *
37
Assembler format
Flags
OF DF IF TF SF ZF AF PF CF
ITERATION CONTROLS
LOOP
LOOPE /
LOOPZ
LOOPNE /
LOOPNZ
CALL
RET
JMP
LOOP
short-label
LOOPE
short-label
LOOPNE short-label
UNCONDITONAL TRANSFERS
CALL
RET
JMP
target
[pop-value]
target
- - - -
- - - -
- - - -
38
Mnemonic
Assembler format
Flags
OF DF IF TF SF ZF AF PF CF
CONDITIONAL TRANSFERS
JA / JNBE
JAE/ JNB
JB/JNAE/JC
JBE / JNA
JCXZ
JE / JZ
JG/JNLE
JGE /JNL
JL / JNGE
JLE /JNG
JNC
JNE /JNZ
JNO
JNP/ JPO
JNS
JO
JP/JPE
JS
JA
JAE
JB
JBE
JCXZ
JE
JG
JGE
JL
JLE
JNC
JNE
JNO
JNP
JNS
JO
JA
JA
short-label
short-label
short-label
short-label
short-label
short-label
short-label
short-label
short-label
short-label
short-label
short-label
short-label
short-label
short-label
short-label
short-label
short-label
- - - - - - - - - - - - - - - - - - 39-
Description
Jump if ...
JA
JAE
JB
JBE
JC
Jump If Above
Jump If Above or Equal
Jump If Below
Jump If Below or Equal
Jump If Carry
CF = 0 and ZF = 0
CF = 0
CF = 1
CF =1 OR AF = 1
CF =1
JCXZ
JE
*JG
*JGE
*JL
Jump If CX is zero
Jump If Equal
Jump If Greater
Jump If Greater or Equal
Jump If Less
(CX) = 0
ZF=1
ZF=0 and SF = OF
SF = OF
SF OF
*JLE
JNA
JNAE
JNB
JNBE
ZF =1 or SF OF
CF =1 or AF = 1
CF =1
CF =0
CF =0 or ZF = 0
40
Instruction
Description
Jump if ...
JNC
JNE
*JNG
*JNGE
*JNL
Jump If No Carry
Jump If Not Equal
Jump If Not Greater
Jump If Not Greater nor Equal
Jump If Not Less
CF =0
ZF = 0
ZF = 1 or SF OF
SF OF
SF = OF
*JNLE
OF
*JNO
JNP
*JNS
JNZ
ZF = 0 and SF=
Jump If No Overflow
Jump If No Parity (odd)
Jump If No Sign
Jump If Not Zero
OF = 0
PF = 0
SF = 0
ZF = 0
*JO
Jump On Overflow
JP
Jump On Parity (Even)
JPE
Jump If Parity Even
JPO
Jump If Parity Odd
*JS
Jump On Sign
JZ
Jump
Zero
* Pertinent for signed
(2sIfcomplement)
arithmetic.
OF = 1
PF = 1
PF = 1
PF = 0
SF = 1
ZF = 1
41
String instructions
Mnemonic
Assembler format
Flags
OF DF IF TF SF ZF AF PF CF
REPEAT PREFIXES
REP
REP
REPE /
REPE
REPZ
REPNE /
REPNE
*
*
*
*
*
*
*
*
*
*
*
42*
MOVS
REPNZ
MOVS
MOVSB
MOVSW
MOVSB
MOVSW
MOVE
dest-string,sourcestring
COMPARE
CMPS
CMPS
CMPSB
CMPSW
CMPSB
CMPSW
dest-string,sourcestring
Flags
Mnemonic
Assembler format
OF DF IF TF SF ZF AF PF CF
SCAN
SCAS
SCASB
*
SCASW
*
LODS
LODSB
LODSW
STOS
STOSB
STOSW
SCAS dest-string
SCASB
*
*
*
*
*
*
*
*
*
*
SCASW
LODS
source-string
LODSB
LODSW
STOS
dest-string
STOSB
STOSW
43
Interrupt instructions
Mnemonic
INT
Assembler format
INT
interrupt-type
Flags
OF DF IF TF SF ZF AF PF CF
-
INTO
INTO
- Note:
-means unchanged
IRET
IRET and * means changed.
* *
When INT is executed, the 8088 does the following:
44
Flags
Mnemonic
Assembler format
OF DF IF TF SF ZF AF PF CF
FLAG OPERATIONS
STC
STC
CLC
CLC
CMC
*
STD
CMC
STD
CLD
CLD
STI
HLT
WAIT
CLI
ESC
LOCK
STI
HLT
WAIT
CLI
ESC
LOCK
- - -- -- - -
--
--
--
EXTERNAL SYNCHRONIZATION
ext-opcode,source
--
--
1 - -0 -- - -
45
Flags
Mnemonic
Assembler format
OF DF IF TF SF ZF AF PF CF
NO OPERATION
NOP
NOP
NOP is also convenient when you are testing sequences of instructions in your
computer. That is, you can make NOP the last instruction in a test program- a
convenient spot at which to stop a trace. You will probably find other uses for
this innocuous, but handy instruction.
46
47
CS =0000
ES =3000
CX =0010
DS =1000 SS =2000
AX =2000 BX =3000
DX =4000 SS =0100
BP =0200
SI = 0300
IP = 0100
DI = 0400
MOV [BX+SI+2040],AL
(15340H) B= (AL)
MOV reg,data
MOV mem/reg, data
PUSH reg
PUSH mem/reg
PUSH Seg reg
PUSHF (Push flag reg.)
LAHF
SAHF
POP reg
POP mem/reg
POP Seg reg
POPF (Pop to flag reg.)
48
Exchange (XCHG)
Exchange the contents of two registers or exchange the contents of a register
and memory location ( Segment registers cannot be used as operands)
XCHG
AX,BX
XCHG
AL,BH
XCHG
WORDLOC,DX
XCHG
DL,BYTELOC
Translate (XLAT)
Looks up a value in a byte table and load it into AL. The table may be 256 byte
long. Before executing XLAT, BX must contain the starting address of SOURCE
TABLE and AL must contain the index into the table.
To get the tenth byte of STABLE
MOV
AL,10
MOV
BX, OFFSET STABLE
XLAT
STABLE
The 8 bit content of AL is added with the 16 bit content of BX. This forms the
effective address for DS register. After execution the content of the
corresponding memory location gets loaded to AL register.
49
50
ARITHMETIC INSTRUCTIONS
Add (ADD) , Add With Carry (ADC)
for ADD instruction:
destination = destination + source
for ADC instruction:
destination = destination + source + carry
either bytes or words can be added. The following forms are legal:
Register
= Register
Register = Register
+ Register
+ [Memory]
[Memory]
= [Memory]
+ Register
[Memory]
= [Memory]
+ data
[Register]
= [Register]
+ data
52
AL
ADD
AL,100
54
DAS converts the content of AL to two packed decimal digits. The adjustment
procedure is:
a) If the low four bits of AL hold a value greater than 9 or if AF is 1 subtract 6
from AL register and set AF to 1.
b) If the high four bits of AL hold a value greater than 9 or if CF is 1 subtract 60H
from AL register and set CF to 1.
Compare Destination to Source (CMP)
CMP subtracts a source operand from a destination operand and sets or clears
certain flags based on the result but unlike SUB the result is not saved into the
destination.
Unsigned operands
SF
ZF
CF
Source < destination
D
0
0
Source = destination
D
1
0
Source > destination
D
0
1
Signed operands
Source < destination
0
0
D
Source = destination
0
1
D
Source > destination
1
0
D
56
58
60
1)
2)
3)
Conditional Jump
Here the assembler recognizes some of these
instructions by two or three different mnemonics,
basically they are provided for convenience and
better readability.
Most important thing to be noted is that
conditional jump takes only short-label i.e. the
jump must be no more than -128 bytes or +127
bytes away from the conditional transfer
instruction.
66
67
68
71
72
73
Interrupt (INT)
INT interrupt type
( INT xx )
where interrupt type is anything between 0 to 255
(i.e. ,altogether 256 interrupt types)
77
Wait (WAIT)
Also puts 8088 into an idle state, but instead of just sitting it checks an
input line called TEST at five clock intervals. If TEST is found low
8088 resumes execution at the instruction following WAIT
otherwise continues to be in idle state. 8088 services interrupts
while idle but upon return from the interrupt it again enters the
WAIT state..
ESCAPE (ESC)
Through ESC code other processors in the system receive their
instructions and operand. Basically 8088 reads the instruction and
operand for the co-processor and then both executes parallely till
synchronization is required.
78
LOCK
This one byte prefix instruction forces (maximum mode) LOCK pin to
go low and this signal in conjunction with the bus arbiter will not allow
the bus transfer until the prefixed instruction is carried out. (It should be
noted that in the case of two consecutive LOCK prefixed instructions,
there will be an unlocked period between these instructions).
If LOCK prefix is used in minimum mode, delays the generation of
HLDA response to a HOLD request until the execution of the locked
instruction is completed.
In multiprocessing environment, for accessing common resources
semaphore is used, which can be implemented by LOCK prefixed
XCHG instruction (SWAP structure) as follows (see next slide).
79
LOCK
Enter
MOV AL, 1
Test semaphore and set busy
BUSY (1)
Semaphore
AVAILABLE (0)
Use Resource
MOV SEMAPHORE, 0
Exit
80
ESC
It performs no operation as far as 8086/8088 is concerned, but provides
another processor (co-processor 8087) an instruction and/or a memory
operand from its program. When used in conjunction with WAIT and
TEST, ESC can initiate a subroutine that executes concurrently in
another processor.
Processor B
Begin
End
ESC - Test = 1
Processor
A
Escape
Continue till
result of B
is required
Test = 0
Wait
Continue
81
82
STRING INSTRUCTIONS
Repeat prefixes causes a string instruction to be executed the number of times
specified in the CX register.
MOV CX , 500
REP MOVS Dest , Source
Source is the string in the segment pointed by SI
Destination is the string in the extra segment pointed by DI.
If DF=0 SI & DI gets incremented after each
execution of string instruction.
DF=1 SI & DI gets decremented after each
execution.
REPE
REPEAT if equal
Causes the instruction to be repeated as long as ZF = 1 and CX != 0.
MOV CX , 100
REPE CMPS Dest , Source
Continues comparison for 100 elements or till a mismatch occurs.
REPE
REPEAT if not equal
Causes the instruction to be repeated as long as ZF = 0 and CX != 0.
83
Checking Results
Since repeated compare operations can terminate on either two
conditions.
Contents of CX are zero
ZF changed to 0 (REPE)/to 1 (REPNE)
One may want to determine which of these caused the termination.
Easiest way to do this is using JZ on JNZ instruction following CMPS.
CLD
MOV CX,100
REPNE:CMPS dest, source :Search for a match
JNZ Not found
:Matching elements found?
:Yes, continue here
Not found:No,continue here
Compare Byte String CMPSB | dont need any operand, DI
Compare Word String CMPSW |&SI with ES &DS are used.
85
86
87
88
THANKS
89