Escolar Documentos
Profissional Documentos
Cultura Documentos
Immediate Addressing
Before After
Ex1: MOV DX, 1234H DX ABCDH 1234H
Before After
Ex2: MOV CH, 23H CH 4DH 23H
Register Addressing
Before After
Ex1: MOV CX, SI CX 1234H 5678H
SI 5678H 5678H
Before After
Ex2: MOV DL, AH Dl 89H BCH
AH BCH BCH
Memory Addressing
1
Indirect with Addressing with Indexed addressing with
displacement displacement addressing displacement
Memory Direct Addressing
Before After
Ex1: MOV BX, DS:5634H BX ABCDH 8645H
Before After
Ex2: MOV CL, DS:5634H CL F2H 45H
DS:5634H 45H
DS:5635H 86H
Before After
Ex1: MOV CL, [SI] CL 20H 78H
SI 3456H
DS:3456H 78H
Before After
Ex2: MOV DX, [BX] DX F232H 3567H
BX A2B2H
Before After
Ex3: MOV AH, [DI] AH 30H 86H
DI 3400H
2
DS:3400H 86H
Only SI, DI and BX can be used inside [ ] from memory addressing point of view. From
user point of view [BP] is also possible. This scheme provides 3 ways of addressing an
operand in memory.
Before After
Ex1: MOV DH, 2345H[BX] DH 45H 67H
Before After
Ex2: MOV AX, 45H[BP] AX 1000H CDABH
Base register can only be BX or BP. This scheme provides 4 ways of addressing an
operand in memory.
Before After
Ex1: MOV CL, 2345H[SI] CL 60H 85H
Before After
Ex2: MOV DX, 37H[DI] DX 7000H B2A2H
Index register can only be SI or DI. This scheme provides 4 ways of addressing an
operand in memory.
3
Based Indexed Addressing
Before After
Ex1: MOV CL, [SI][BX] CL 40H 67H
SI 2000H
BX 0300H
Before After
Ex2: MOV CX, [BP][DI] CX 6000H 6385H
BP 3000H
DI 0020H
This scheme provides 4 ways of addressing an operand in memory. One register must be
a Base register and the other must be an Index register.
For ex. MOV CX, [BX][BP] is an invalid instruction.
Before After
Ex1: MOV DL, 37H[BX+DI] DL 40H 12H
Before After
Ex2: MOV BX, 1234H[SI+BP] BX 3000H 3665H
SI 4000H
BP 0020H
4
Memory modes as derivatives of Based Indexed Addressing with Displacement
Before After
Ex. 1: IN AL, 83H AL 34H 78H
Before After
Ex. 2: IN AX, 83H AX 5634H F278H
Before After
Ex. 3: OUT 83H, AL AL 50H
Before After
Ex. 4: OUT 83H, AX AX 6050H
5
Variable Port Addressing
I/O port address is provided in DX register. Port address ranges from 0000 to FFFFH.
Data transfer with AL or AX only.
Before After
Ex. 1: IN AL, DX AL 30H 60H
DX 1234H
Before After
Ex. 2: IN AX, DX AX 3040H 7060H
DX 4000H
Before After
Ex. 3: OUT DX, AL AL 65H
DX 5000H
Before After
Ex. 4: OUT DX, AX AX 4567H
DX 5000H
Concept of Template
In 8085, MOV r1, r2 (ex. MOV A, B) has the following template.
6
0 1 3-bit r1 code 3-bit r2 code
Using the template for MOV r1, r2 we can generate opcodes of 26 = 64 opcodes.
REG = A register of 8086 (8-bit or 16-bits) (except Segment registers, IP, and Flags
registers)
Thus REG = AL/ BL/ CL/ DL/ AH/ BH/ CH/ DH/ AX/ BX/ CX/ DX/ SI/ DI/ BP/ SP
Register name
7
3-bit When W = 1 When W = 0
Register
code
000 AX AL
001 CX CL
010 DX DL
011 BX BL
100 SP AH
101 BP CH
110 SI DH
111 DI BH
Aid to remember: ALl Children Drink Bournvita (AL, CL, DL, BL)
SPecial Beverages SIamese DrInk (SP, BP, SI, DI)
Case of MOD = 11
Example: Code for MOV AX, BX treated as ‘Move from BX to destination register AX’
Example: Alternative code for MOV AX, BX treating it as ‘Move from source register
BX to register AX’
There are 2 possible opcodes for MOV AX, BX as we can choose either AX or BX as
REG.
Example: Code for MOV AL, BH treated as ‘Move from BL to destination register AL’
8
Example: Alternative code for MOV AL, BH treating it as ‘Move from source register
BH to register AL’
There are 2 possible opcodes for MOV AL, BH as we can choose either AL or BH as
REG.
The table shows 24 memory addressing modes i.e. 24 different ways of accessing data
stored in memory.
Aid to remember:
SubInspector DIxit is a BoXer ( [SI+BX] and [DI]+[BX] )
He says’ SImple DIet DIRECTs a BoXer' ( [SI], [DI], Direct addressing, [BX] )
Note that there is a unique opcode for MOV CL, [SI] as CL only can be REG.
9
Ex: Code for MOV 46H[BP], DX
D W MO REG R/M d8
D
1 0 0 0 1 0 0 1 01 01 0 110 46H = 89 56 46H
Word 8-bit DX is [BP+d8]
operation Disp. source
8 9 5 6
Note that there is a unique opcode for MOV 46H[BP], DX as DX only can be REG.
Note that there is a unique opcode for MOV 0F246H[BP], DX as DX only can be REG.
D W MO REG R/M d8
D
1 0 0 0 1 0 0 1 01 01 0 110 00H = 89 56 00H
Word 8-bit DX is [BP+d8]
operation Disp. source
8 9 5 6
10
Note that when MOD = 00 and R/M = 110, it represents Direct Addressing.
11
Prerequisite information
8086 is a 16-bit processor
It has 20 address pins (16 of them multiplexed with data pins)
It can address a maximum of 220 = 1 Million locations.
Address ranges from 00000H to FFFFFH.
Memory is byte addressable. Every byte has a separate address.
Registers of 8086
Segment Registers: CS, DS, ES, SS each of 16 bits
8 bit data Registers: AH, AL, BH, BL, CH, CL, DH, DL
16 bit data Registers: AX, BX, CX, DX, SI, DI, BP, SP
16 bit Address registers: SI, DI, BP, BX
Default combination of Segment and Address registers
CS:IP
SS:SP SS:BP
DS:BX DS:DI DS:DI (For other than string operations)
ES:DI (For string operations)
Special purpose Registers
IP and FLAGS
Flag is a bit of special information.
9 Flags in the 16 bit FLAGS register
6 Status Flags and 3 Control Flags
Status flags are: Cy, Ac, S, Z, P, Overflow
Control flags are: IE, T, D
Special roles of some registers
BX can be used as Address register
CX is used as default down counter register
DX used to hold 16-bit I/O port address in variable port addressing
DX used to hold MS 16 bits of 32 bit result after multiplication
DX used to hold MS 16 bits of 32 bit numerator before division
Accumulator: 8-bit is AL 16-bit is AX 32-bit is DX AX
12
8086 Instruction set
Abbreviations used
Conventions used:
R
MOV for MOV R, M and
MOV M, R
M
ROR R/M, 1/CL for ROR R,1 ROR M,1 ROR R,CL ROR M, CL
Data Transfer group, Arithmetic group, Logical group, Stack group, and I/O group of
instructions explained first. They occupy several chapters in books.
13
1-operand instructions Ex. PUSH SI
0-operand instructions: Ex. DAA
Branch group, String instructions, and Interrupt instructions are explained later.
2-Operand instructions
2-Operand instructions involving R and R/M
Exchange Instruction
Before After
XCHG DX, [BX] DX 1234H ABCDH
BX 1000H
DS:1000H ABCDH 1234H
DS:1002H
Add instruction
Unlike in 8085, result of add/subtract can be in any register or memory location
Before After
ADD [BX], DX DX 1234H
BX 1000H
In 3234H, 34H has DS:1000H 2000H 3234H
three 1’s. So P flag =0 DS:1002H
Before After
ADC DH ,[SI] DH 30H 81H
Add with Carry Carry flag 1 0
SI 2000H
81H DS:2000H 50H
1000 0001B(Two 1’s) DS:2001H 60H
Before After
SUB DH, CL DH 30H 0BH
14
Subtract (without borrow)
CL 25H
0BH =
0000 1011B(Three 1’s)
New flag values: Ac=1, S=0, Z=0, V=0, P=0, Cy=0
Before After
SBB DH, CL DH 20H FAH
Subtract (with borrow) Cy flag 1 1
CL 25H
FAH =1111 1010(Six 1’s)
2’s complement of FAH=0000 0110 = +06 So, FAH = -06
New flag values: Ac=1, S=1, Z=0, V=0, P=1, Cy=1
15
AND instruction
Before After
AND BH, CL BH 56H 06H
Subtract (with borrow) AND 1 1
0FH=0000 1111B CL 0FH
06H=0000 0110B
Use: Selectively reset to 0 some bits of the destination
Bits that are ANDed with 0’s are reset to 0
Bits that are ANDed with 1’s are not changed
OR instruction
Before After
OR BH, CL BH 56H 5FH
56H=0101 0110B OR
0FH=0000 1111B CL 0FH
5FH=0101 1111B
Use: Selectively set to 1 some bits of the destination
Bits that are ORed with 1’s are set to 1
Bits that are ORed with 0’s are not changed
Ex-OR instruction
Before After
XOR BH, CL BH 56H 59H
56H=0101 0110B XOR
0FH=0000 1111B CL
0FH
59H=0101 1001B
Use: Selectively complement some bits of the destination.
Bits that are XORed with 1’s are complemented
Bits that are XORed with 0’s are not changed
TEST instruction
Before After
TEST BH, CL BH 56H 56H
56H=0101 0110B AND
0FH=0000 1111B CL 0FH 0FH
06H=0000 0110B
Only flages are affected Temp 45H 06H
TEST basically performs AND operation. Result of AND is not stored
in destination. It is stored in Temp register. Temp is not accessible to
programmer. There is no instruction like MOV Temp, 67H
16
Compare Instruction
Before After
CMP BH, CL BH 56H 56H
56H=0101 0110B
0FH=0000 1111B CL 0FH
Only flags are affected
Temp 45H 47H
CMP basically performs Subtract operation. Result of CMP is not
stored in destination. It is stored in Temp register. Temp is not
accessible to programmer.
MOV
ADD/ADC/SUB/SBB R/M, d8/d16
AND/OR/XOR/TEST/CMP
8 byte registers + 8 word registers+ 24 byte
memory + 24 word memory = 64 opcodes
10 instructions x 64 = 640 opcodes
Before After
MOV DX, ABCDH DX 1234H ABCDH
Before After
MOV BH, 12H BH 56H 12H
Before After
ADD [BX], 12H BX 1000H
Before After
ADD [BX], 1234H BX 1000H
17
DS:1000H 2000H 3234H
DS:1002H
Before After
ADC DH, 32H DH 30H 63H
Add with Carry Carry flag 1 0
63H= 0110 0011 It has four 1’s
New flag values: Ac=0, S=0, Z=0, V=0, P=1
Before After
SUB DH, 40H DH 30H F0H
Subtract (without borrow)
F0H=1111 0000 B(Four 1’s)
New flag values: Ac=0, S=1, Z=0, V=0, P=1, Cy=1
Before After
SBB DH, 25H DH 20H 06H
Subtract (with borrow) Cy flag 1 1
06H= 0000 0110B(Two 1’s)
New flag values: Ac=1, S=0, Z=0, V=0, P=1, Cy=1
Before After
AND BH, 0FH BH 56H 06H
56H = 0101 0110B AND
Before After
18
OR BH, 0FH BH 56H 5FH
56H = 0101 0110B OR
Before After
XOR BH, 0FH BH 56H 59H
56H = 0101 0110B XOR
0FH = 0000 1111B CL 0FH
59H = 0101 1001B
Use: Selectively complement some bits of the destn.
Bits that are XORed with 1’s are complemented
Bits that are XORed with 0’s are not changed
Before After
TEST BH, 0FH BH 56H 56H
56H=0101 0110B AND
Before After
CMP BH, 0FH BH 56H 56H
56H=0101 0110B AND
Temp 45H 47H
19
2-Operand Instructions involving SR and R16/M16
SR
MOV
R16/M16
Before After
MOV DS, CX DS 1122H 2233H
CX 2233H
Note that there is no instruction to load an immediate data to a Segment
register.
No. of opcodes = 2 x 4 x (8+24) = 256
Before After
MOV DS, [BX] DS 1122H 2233H
BX 2000H
DS:2000H 2233H
Before After
IN AL, DX AL 50H 45H
DX 2111H
Input port no. 2111H 45H
Before After
IN AL, 30H AL 45H
50H
Input port no. 30H 45H
Before After
IN AX, DX AX 3050H 4045H
DX 1177H
Input port no. 60H 45H
Input port no. 61H 40H
Before After
OUT 30H, AL AL
50H
20
Out port no. 30H 40H 50H
Before After
OUT DX, AX AX 3050H
DX 2177H
Out port no. 2177H 45H 50H
Out port no. 2178H 40H 30H
Before After
OUT 60H, AX AX 3050H
21
BH 0100 0010 1010 0001
Cy 1 0
22
SAR R/M, 1/CL Used for division by 2n of signed nos
SAR BH, CL R/M Cy
Before After
LEA BX, [SI] BX 1000H 2000H
Before After
LDS SI, 3000H DS 2000H 7000H
Loads DS and SI using single instruction
SI 1000H 6000H
DS:3000H 6000H
DS:3002H
7000H
Before After
LES DI, 3000H ES 2000H 7000H
23
1-Operand instruction types
Increment R16
Increment R8
Increment M8
NOTE:- In this instruction there is a single operand, [BX]. It is not clear whether it is
byte or word operand. Byteptr assembler directive announces to the assembler that it is a
byte operation.
Increment M16
NOTE:- In this instruction there is a single operand, [BX]. It is not clear whether it is
byte or word operand. wordptr assembler directive announces to the assembler that it is a
word operation.
Decrement R16
DEC BX 8 opcodes Before After
Ex. 1 BX 1234H 1233H
24
Ex. 2 BX 0000H FFFFH
Decrement R8
DEC DH 8 opcodes Before After
Ex. 1 DH 12H 11H
Ex. 2 DH 00H FFH
Decrement M8
DEC byteptr [BX] Before After
24 opcodes BX 2000H
DS:2000H 00H FFH
DS:2001H 12H 12H
NOTE:-In this instruction there is a single operand, [BX]. It is not clear whether it is byte
or word operand. Byteptr assembler directive announces to the assembler that it is a byte
operation.
Decrement M16
DEC wordptr [BX] Before After
24 opcodes BX 2000H
DS:2000H 00H FFH
DS:2001H 12H 11H
NOTE:- In this instruction there is a single operand, [BX]. It is not clear whether it is
byte or word operand. wordptr assembler directive announces to the assembler that it is a
word operation.
Perform 1’s complement of R16
NOTE:- In this instruction there is a single operand, [BX]. It is not clear whether it is
byte or word operand. Byteptr assembler directive announces to the assembler that it is a
byte operation.
25
Perform 1’s complement of M16
NOTE:- In this instruction there is a single operand, [BX]. It is not clear whether it is
byte or word operand. wordptr assembler directive announces to the assembler that it is a
word operation.
NOTE:- In this instruction there is a single operand, [BX]. It is not clear whether it is
byte or word operand. Byteptr assembler directive announces to the assembler that it is a
byte operation.
Perform 2’s complement of M16
NOTE:- In this instruction there is a single operand, [BX]. It is not clear whether it is
byte or word operand. wordptr assembler directive announces to the assembler that it is a
word operation.
PUSH R16
26
Ex. PUSH CX Before After
CX 1234H
SP 5678H 5676H
Empty
Empty SS: 5676H 1122H Full 1234H
Full SS:5678H 3344H 3344H
Suppose SP content is 5678H. It means locations 5678, 567A, 567C … in stack segment
are full. Locations 5676, 5674, … are empty. Information pushed to location 5676 and SP
value changes to 5676H. Push operation is always on 16 bit data.
PUSH M16
PUSH SR
PUSH Flags
POP R16
27
Full SS: 5678H 1122H Empty 1122H
SS: 567AH 3344H Full 3344H
NOTE:- Suppose SP content is 5678H. It means locations 5678, 567A, 567C … in stack
segment are full. Locations 5676, 5674, … are empty. Information poped from location
5678 and SP value changes to 567AH. Pop operation is always on 16 bit data.
POP M16
POP SR
POP Flags
28
Unsigned Multiply R16 with AX and store product in DX AX
CX 00FEH 00FEH
AX 0002H 01FC 01FCH = 508
H
DX 1234H 0000H
29
NOTE:-IDIV CH instruction divides AX by CH treating them as signed numbers. The 8-
bit quotient is stored in AL and the 8-bit remainder stored in AH.
30
Branch group of instructions
Conditional Jump instructions in 8086 are just 2 bytes long. 1-byte opcode followed by
1-byte signed displacement (range of –128 to +127).
Jumps based on a single flag Jumps based on more than one flag
JZ r8 ;Jump if zero flag set (if result is 0). JE also means same.
JNZ r8 ;Jump if Not Zero. JNE also means same.
JS r8 ;Jump if Sign flag set to 1 (if result is negative)
JNS r8 ;Jump if Not Sign (if result is positive)
JC r8 ;Jump if Carry flag set to 1. JB and JNAE also mean same.
JNC r8 ;Jump if No Carry. JAE and JNB also mean same.
JP r8 ;Jump if Parity flag set to 1. JPE (Jump if Parity Even) also means same.
JNP r8 ;Jump if No Parity. JPO (Jump if Parity Odd) also means same.
JO r8 ;Jump if Overflow flag set to 1 (if result is wrong)
JNO r8 ;Jump if No Overflow (if result is correct)
JE is abbreviation for Jump if Equal. JNE is abbreviation for Jump if Not Equal.
JB is abbreviation for Jump if Below. JNAE is for Jump if Not Above or Equal.
JAE for Jump if Above or Equal. JNB for Jump if Not Above.
JE, JNE, JB, JNAE, JAE and JNB are used after a compare operation.
Examples for JE or JZ instruction
31
CMP SI, DI
JE SAME
ADD CX, DX ;Executed if Z = 0
Should be<=127 bytes : (if SI not equal to DI)
:
SAME: SUB BX, AX ;Executed if Z = 1
(if SI = DI)
Range for JMP (unconditional jump) can be +215 = + 32K. JMP instruction discussed in
detail later
32
65H is greater than 95H Signed comparison - True
JNBE / JA instruction
‘
Jump if Not (Below or Equal)’ or ‘Jump if Above’
Jump if No Jump if Ex.
Cy = 0 AND Z= 0 Cy = 1 OR Z = 1 CMP BX, CX
Surely Above Below OR Equal JBE BX_BE
[(S=1 AND V=0) OR (S=0 AND V=0)] [(S=0 AND V=0) OR (S=1 AND V=1)]
OR Z=1 AND Z=0
[(surely negative) or (wrong answer [(surely positive) or (wrong answer
positive!)] or Equal negative!)] and not equal
i.e. [S XOR V=1] OR Z=1 i.e.[S XOR V=0] AND Z=0
JNLE / JG instruction
33
JL / JNGE instruction
‘Jump if Less than’ or ‘Jump if NOT (Greater than or Equal)’
Jump if No Jump if
[S=1 AND V=0] OR [S=0 AND V=1] [S=0 AND V=0] OR [S=1 AND V=1]
(surely negative)or (wrong answer (surely positive) or (wrong answer
positive!) negative!)
Jump if No Jump if
[S=0 AND V=0] OR (S=1 AND V=1) [S=1 AND V=0] OR (S=1 AND V=1)
(surely positive) or (wrong answer (surely negative) or (wrong answer
negative!) positive!)
Near Jump or Intra segment Jump Far Jump or Inter segment Jump
(Jump within the segment) (Jump to a different segment)
Near Jump
Direct Jump (common) Indirect Jump (uncommon)
2-bytes Short Jump (EB r8) 3-bytes Long Jump (E9 r16) 2 or more bytes
Range: + 27 Range: +215 Starting with FFH
Range: complete segment
Three Near Jump and two Far Jump instructions have the same mnemonic JMP, but they
have different opcodes
34
For Backward jump: Assembler knows the quantum of jump. Generates Short Jump code
if <=128 bytes is the required jump. Generates code for Long Jump if >128 bytes is the
required jump.
For Forward jump: Assembler doesn’t know jump quantum in pass 1. Assembler
reserves 3 bytes for the forward jump instruction. If jump distance turns out to be >128
bytes, the instruction is coded as E9 r16 (E9H = Long jump code). If jump distance
becomes <=128 bytes, the instruction is coded as EB r8 followed by code for NOP (E8H
= Short jump code).
FRWD = CS:FFF0H :
:
CS:FFFFH :
35
Can be treated as a CS:0000H :
small (20H) : Jump distance =FFE0H.
Forward Branch! BKWD = CS:0010H : Too very long
backward jump
:
CS:FFF0H JMP BKWD
:
CS:FFFFH :
Also called Far Direct Jump. It is the common inter segment jump scheme
It is a 5 byte instruction. 1 byte opcode (EAH), 2 byte offset value, 2 byte segment value
Also called Far Indirect Jump. It is not commonly used. Instruction length depends on the
way jump location is specified. It can be a minimum of 2 bytes.
Ex. JMP DWORD PTR 2000H[BX]
If BX contents is
1234H branch takes place to location ABCDH:5678H. It is a 4-byte instruction.
36
DS:3234H 5678H
DS:3236H ABCDH
Iteration Instructions
Iteration instructions
LOOP Instruction
Let us say, we want to repeat a set of instructions 5 times.
For 8085 processor For 8086processor
MVI C, 05H MOV CX, 0005H
AGAIN: MOV B, D AGAIN: MOV BX, DX
: :
DCR C LOOP AGAIN
JNZ AGAIN
JCXZ Instruction
Jump if CX is Zero is useful for terminating the loop immediately if CX value is 0000H
It is a 2 byte instruction. It is used for forward jump only. Maximum distance for forward
jump is only 127 bytes.
Ex. MOV CX, SI
JCXZ SKIP
AGAIN: MOV BX, DX
:
:
LOOP AGAIN
SKIP: ADD SI, DI ; Executed after JCXZ if CX = 0
LOOPZ instruction
37
LOOP while Zero is a 2-byte instruction. It is used for backward jump only. Backward
jump takes place if after decrement of CX it is still not zero AND Z flag = 1. LOOPE is
same as LOOPZ. LOOPE is abbreviation for LOOP while Equal. LOOPE is normally
used after a compare instruction.
CALL Instructions
Near Direct CALL Near Indirect CALL Far Direct CALL Far Indirect CALL
It is a 3-byte instruction. It has the format CALL r16 and has the range + 32K bytes.
Covers the entire Code segment. It is the m
ost common CALL instruction.
It is functionally same as the combination of the instructions PUSH IP and ADD IP, r16.
Ex. CALL Compute
DS:3234H 5678H
DS:3236H ABCDH
38
Far direct CALL is functionally same as:
PUSH CS
PUSH IP
IP = 2-byte offset value provided in CALL
CS = 2-byte segment value provided in CALL
Not commonly used. Instruction length depends on the way the called location is
specified.
DS:3234H 5678H
DS:3236H ABCDH
Conditional CALL?
RETURN instructions
RET is abbreviation for Return from subroutine
RET instructions
Near RET or Intra segment RET Far RET or Inter segment RET
RET RET d16 RET RET d16
39
Near RET d16 instruction
It is a 3-byte instruction. 1-byte opcode (C2H) and 2-byte data. It is functionally same as:
POP IP
SP = SP + d16
Ex. RET 0004H
RET d16 is useful for flushing out the parameters that were passed to the subroutine
using the stack
Main Program
:
: SP after CALL Compute IP
PUSH Var1 Var2
PUSH Var2 Var1
CALL Compute SP before PUSH Var1
:
:
Subroutine
COMPUTE PROC Near IP
: SP if RET is executed Var2
: Var1
RET 0004H SP if RET 0004H is executed
COMPUTE ENDP
40
It is functionally same as: POP IP + POP CS + ADD SP, d16
41
Interrupts & related instructions
Interrupt types
Every interrupt type in 8086 has an 8-bit Interrupt type number (ITN) as shown below.
ITN Interrupt type ITN Interrupt type
42
Processor makes a branch to the subroutine!
In INT n, which is a 2-byte instruction, n is the ITN. INT n has the opcode CDH
Ex. DIV BL
Before After
AH 40H 00H This is an example for divide by 0 error. It only
AL 60H 2030H means quotient is too large for the register!
BL 02H
43
Action for divide by 0 error
Processor makes a branch to the subroutine at location 5678:1234H if the contents of IVT
is as shown in the table above.
Processor makes a branch to the subroutine at location 5566:3344H as per the IVT
44
1. Complete the instruction in progress
2. Activate INTA o/p twice. In response 8086 receives ITN n instruction from an external
device like 8259 PIC
3. Push Flags on the stack. Reset IE and T flags
4. PUSH CS
5. PUSH IP
6. IP loaded from word location n x 4 = say, W
7. CS loaded from next word location W+2
Processor makes a branch to the subroutine!
If several interrupts occur during the execution of an instruction, in which order interrupts
will be serviced? There will be priorities as indicated below.
Divide by 0 error, INT n Highest priority
NMI
INTR
Single step interrupt Lowest priority
In reality NMI has highest priority! If NMI occurs during the servicing of INT n,
processor branches to NMI routine as IE flag has no effect on NMI.
45
Intel 8255 PPI
There are 3 ports in 8255 from user’s point of view - Port A, Port B and Port C.
Port C is composed of two independent 4-bit ports : PC7-4 (PC Upper) and PC3-0 (PC
Lower)
Selection of Ports
A1 A0 Selected port
0 0 Port A
0 1 Port B
1 0 Port C
1 1 Control port
There is also a Control port from the Processor point of view. Its contents decides the
working of 8255.
When CS (Chip select) is 0, 8255 is selected for communication by the processor. The
chip select circuit connected to the CS pin assigns addresses to the ports of 8255.
For the chip select circuit shown, the chip is selected when A7=0, A6=1, A5=1, A4=1,
A3=1, A2=1, and M/IO*= 0.
Port A, Port B, Port C and Control port will have the addresses as 7CH, 7DH, 7EH, and
7FH respectively.
46
There are 3 modes of operation for the ports of 8255. Mode 0, Mode 1, and Mode 2.
Mode 0 Operation
It is Basic or Simple I/O. It does not use any handshake signals. It is used for interfacing
an i/p device or an o/p device. It is used when timing characteristics of I/O devices is well
known.
Mode 1 Operation
It uses handshake I/O. 3 lines are used for handshaking. It is
used for interfacing an i/p device or an o/p device. Mode 1 operation is
used when timing characteristics of I/O devices is not well known, or used when I/O
devices supply or receive data at irregular intervals.
Handshake signals of the port inform the processor that the data is available, data transfer
complete etc. More details about mode 1 operation is provided later.
Mode 2 Operation
It is bi-directional handshake I/O. Mode 2 operation uses 5 lines for handshaking. It is
used with an I/O device that receives data some times and sends data sometimes. Ex.
Hard disk drive. Mode 2 operation is useful
when timing characteristics of I/O devices is not well known, or when I/O devices supply
or receive data at irregular intervals.
Port A can work in Mode 0, Mode 1, or Mode 2
Port B can work in Mode 0, or Mode 1
Port C can work in Mode 0 only, if at all
47
8255 Mode Definition Control word
Mode definition control word is used to configure the ports of 8255 as input or output in
Mode 0, Mode 1, or Mode 2.
Control port having Mode Definition (MD) control word
1 M2A M1A I/P A I/P CU M1B I/P B I/P CL
Means Mode
Definition control 1 - PCU as input
word 0 - PCU as output 1 -PCL as input
1 - PA as input 0 -PCL as output
M2A 0 - PA as output 1 - PB as input
M1A
0 0 Port A in Mode 0 0 - PB as output
0 1 Port A in Mode 1 1 - PB in Mode 1
1 0/1 Port A in Mode 2 0 - PB in Mode 0
Ex. 1: Configure Port A as input in Mode 0, Port B as output in mode 0, Port C (Lower)
as output and Port C (Upper) as input ports.
Required MD control word:
1 0 0 1 1 0 0 0 = 98H
PC Upper as input
Ex. 2: Configure Port A as input in Mode 1, Port B as output in mode 1, Port C7-6 as
input ports. (PC5-0 are handshake lines, some are input lines and others are output. So
they are shown as X)
Required MD control word:
1 0 1 1 1 1 0 X = BCH or BDH
48
Required program segment for the configuration:
MOV AL, BCH
OUT 7FH, AL
Ex. 3:Configure Port A in Mode 2, Port B as output in mode 1. (PC7-3 are handshake
lines for Port A and PC2-0 are handshake signals for port B)
Required MD control word:
1 1 X X X 1 0 X = C4H / C5H..
Port C Bit Set / Reset (PCBSR) control word is used for setting to 1 or resetting to 0 any
one selected bit of Port C. It is useful for enabling or disabling Port A or Port B interrupts
when they are in mode 1 or mode 2.
Control port having Port C Bit Set / Reset control word
0 X X X SB2 SB1 SB0 S/R*
49
OUT 7FH, AL
PCBSR command word is used for enabling / disabling interrupts from Ports A and B
when they are configured for other than Mode 0.
PB7-0
Port B STB*B
(PC2)
50
IBFB INT B
(PC1) (PC0)
Waveforms for Handshake Interrupt input port
STB*
IBF
INT
RD*
Data from
I/O dev.
When input device has data to send it checks if IBF (input buffer full) signal is 0. If 0, it
sends data on PB7-0 and activates STB* (Strobe) signal. STB* is active low. When STB*
goes high, the data enters the port and IBF gets activated. If the Port interrupt is enabled,
INT is activated. This interrupts the processor. Processor reads the port during the ISS.
Then IBF and INT get deactivated.
8255
Port B PB7-0
OBF*B INT B
(PC1) (PC0)
51
Waveforms for Handshake interrupt output port
WR*
OBF*
INT
ACK*
When output device wants to receive data it checks if OBF* (output buffer full) signal is
0.Data
If 0, sent out data on PB7-0 and activates ACK* (Acknowledge) signal. ACK* is
it receives
active low. When ACK* goes high, the data goes out of the port and OBF* is set to 1. If
theon port
Port pins is enabled, INT is activated. This interrupts the processor. Processor
interrupt
sends another byte to the port during the ISS. Then OBF* and INT are reset to 0.
Similarly,
even if I/O device has emptied the output buffer, INT output is not going to be activated
for output operation. Then, how the processor knows that the output buffer is empty?
52
PA status in Mode 2
Applications of 8255
There are many applications for 8255. We discuss below only Analog to Digital
conversion and Digital to Analog conversion.
PB2 C
Program for A/D Conversion
DAC 0800 is an 8-bit Digital to Analog Converter chip. It comes in a 16 pin DIP. It has 8
digital inputs. DAC 0808 generates analog current output.Current to voltage converter is
used externally. Current output is generated in about 100 milli seconds.
54
Program for Triangular waveform generation using DAC
UP: OUT 7CH, AL; Next 4 instructions generate rising portion of triangular waveform
INC AL;
JB UP
DOWN: OUT 7CH, AL; Next 4 instructions generate falling portion of triangular
waveform
DEC AL;
JA DOWN
55
String instructions
8086 has instructions that work on a character string also. The instruction mnemonics end
with S.
String Instructions
Used for copying a string byte or word at a time. MOVSB for Move String Byte at a time.
MOVSW for Move String Word at a time. MOVSB and MOVSW are more common than
MOVS. Flags are not affected by the execution of MOVS/MOVSB/MOVSW instruction.
Instruction Source Destination If D = 0 If D = 1
pointed by pointed by
MOVSB DS:SI ES:DI incr. by 1 SI and DI decr. by 1 SI and DI
MOVSW DS:SI ES:DI incr. by 2 SI and DI decr. by 2 SI and DI
It is usual to have REP (for REPeat) prefix for MOVSB / MOVSW instruction. The
following code moves a 6-character string, as CX has 06. Without REP prefix, six
MOVSB instructions are needed.
MOV CX, 06
REP MOVSB
EXIT: MOV DL, BL
56
MOV CX, 06 MOV CX, 06
REP MOVSB JCXZ EXIT
EXIT: Next instrn. AGAIN: MOV AL, [SI]
MOV ES:[DI], AL
The above 3 instructions are PUSHF
equivalent to the 10 INC SI
instructions on the right INC DI
(assuming D= 0) POPF
LOOP AGAIN
EXIT: Next instrn.
CMPS Compares two strings (of equal size), say String1 and String2, a byte or word at a
time. String values remain unaffected. Only flags affected. Basically it performs the
subtraction DS:[SI] - ES:[DI]
CMPSB for comparing Strings Byte at a time. CMPSW for comparing Strings Word at a
time. CMPSB and CMPSW are more common than CMPS.
Normally CMPSB or CMPSW instructions are used to check if two given strings are
same or not.
To check for equality, REPE (Repeat while Equal) prefix is used.
REPE can also be written as REPZ (Repeat while Z flag is set) or REP (Repeat, ‘while
equal’ is implied)
57
MOV CX, 08 MOV CX, 08
REPE CMPSB JCXZ EXIT
EXIT: JE EQUAL AGAIN: MOV AL, [SI]
CMP AL, ES:[DI]
The above 3 instructions are equivalent to the PUSHF
10 instructions on the right (assuming D= 0). INC SI
REPE CMPSB instruction causes CMPSB to INC DI
be repeated as long as the compared bytes are POPF
equal and CX contents is not yet 0000. LOOPE AGAIN
EXIT: JE EQUAL
Suppose we want to initialize 6 consecutive memory locations with the same value 25H.
The REP prefix for STOSB proves useful.
58
Use of REP prefix with STOSB
SCAS is used for scanning a string in memory for a particular byte or word. It
compares contents of byte in AL or word in AX with byte or word at memory pointed by
ES:[DI].
59
SCAS
performs AL/AX contents minus byte or word pointed by ES:[DI]. Operand values are
not changed.
Flags are affected based on result of subtraction.
SCASB is used for scanning string byte at a time. SCASW is used for scanning string
word at a time.
SCASB and SCASW are more common than SCAS.
Normally SCASB or SCASW instructions are used to check if a particular byte or word
is present in the given string.
In such a case, REPNE (Repeat while Not Equal) prefix is used.
REPNZ (Repeat while Zero flag Not set) is same as REPNE
NOTE: In case it is desired to check if all the bytes or words in a string are equal to a
particular value, the REPE (Repeat while Equal) prefix is used.
REPE can also be written as REPZ (Repeat while Zero flag is set) or REP.
60
0-Operand instructions
CLD
Clear Direction flag to 0
STD
Set direction flag to 1
CLI
Clear Interrupt enable flag to 0
STI
Set Interrupt enable flag to 1
There is no instruction to set or clear Trap flag !
DAA Instruction
DAA instruction is very useful for performing decimal addition. Just add the decimal
numbers as if they are hexadecimal numbers, and then execute DAA to get correct
decimal answer !
61
Ex.1: Add decimal numbers 22 and 18
MOV AL, 22H ; (AL)= 22H
ADD AL, 18H ; (AL) = 3AH Illegal, incorrect answer!
DAA ; (AL) = 40H Just treat it as decimal with Cy = 0
62
Ex.6: Add decimal numbers 36 and 42
MOV AL, 36H ; (AL)= 36H
ADD AL, 42H ; (AL) = 78H
DAA ; (AL) = 78H Just treat it as decimal with Cy = 0
78H
+00H In this case, DAA same as ADD AL, 00H
=78H
DAS instruction
DAS instruction is very useful for performing decimal subtraction. Just subtract the
decimal numbers as if they are hexadecimal numbers, and then execute DAS to get
correct decimal answer!
63
-60H When Cy = 1, it means result is negative
-00H
=11H
Suppose AH is 00H and AL has sum of 2 unpacked BCD numbers. AAA coverts it to
unpacked BCD in AH and AL.
64
ADD AL, 07H ; (AL) = 0FH
MOV AH, 00H ; It is important
AAA ; AH = 01, AL = 05
AAA has added 6 to LS hex digit of AL, as it was >9.
Ac becomes 1. MS digit remains 0. AH incremented by 1, as Ac was generated.
AAS instruction
65
AAS ; AH = FF, AL = 07 (10’s comp.of 3)
6 subtracted from AL. MS digit made 0
AH decremented by 1, as Ac = 1. When AH = FFH, it means answer is negative.
AAM instruction
CBW instruction
CBW treats the byte in AL as a signed number. It converts the number to 16 bits by sign
extension and stores in AX. It is useful before a signed number division.
It is equivalent to MOV AH, 00/FFH. For a positive number in AL, AH will become
00H. For a negative number in AL, AH will become FFH
Before After
Ex.1 AL 34H 34H
AH 12H 00
34H in AL is sign extended as 0034H in AX
66
Before After
Ex.2 AL 89H 89H
AH 12H FFH
89H in AL is sign extended as FF89H in AX
CWD instruction
CWD
is equivalent to MOV DX, 0000/FFFFH. For a positive number in AX, DX will become
0000H. For a negative number in AX, DX will become FFFFH.
Before After
Ex.1 AX 5678H 5678H
1234H 0000H
DX
5678H in AX is sign extended as 0000 5678H in DX AX
Before After
Ex.2 AX 9ABCH 9ABCH
DX 1234H FFFFH
9ABCH in AX is sign extended as FFFF 9ABCH in DX AX
SAHF instruction
LAHF instruction
LS byte of Flags register contains S, Z, Ac, P, and Cy flags (all status flags
except overflow flag). LAHF is used for reading these status flag bits.
Before After
AH 12H 34H
LS byte of Flags 34H
XLAT instruction
Look up Table
67
XLAT is abbreviation for Translate 2000H 30H
It is used for code conversion. 2001H 31H
XLAT is equivalent to MOV AL, [AL+BX] :
Ex. Convert 0 to 9, A to F to Ascii code 2005H 35H
:
MOV BX, 2000H 2009H 39H
MOV AL, Value ; Value 0 to F, say 5 200AH 41H
XLAT ; AL now has 35H 200FH 46H
68