Micro Processer 8086-1
Micro Processer 8086-1
College of Engineering
By
Abdulmuttalib A. Hussein Aldouri
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
1
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
In general, the CPU has three main units: (See the figure below)
Arithmetic and Logic Unit (ALU): Performs arithmetic and logical
operations. For example, it can add together two binary numbers either
from memory or from some of the CPU registers.
Control Unit: controls the action of the other computer components so
that instructions are executed in the correct sequence.
Registers - Temporary storage inside CPU. Registers can be read and
written at high speed as they are inside the CPU.
2
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Memory
The memory in a computer system is of two fundamental types:
Main Memory: used to store information for immediate access by the
CPU. Main Memory is also referred to as Primary Storage or Main Store.
Closely connected to the processor.
The contents are quickly and easily changed.
Stores the programs that the processor is actively working with.
Main memory includes:
Random Access memory (RAM): for temporary storage.
Read-only memory (ROM): for permanent storage.
Input/Output Devices
Input/output devices provide an interface between the computer and the
user. There is at least one input device (e.g. keyboard, mouse, measuring device
such as a temperature sensor) and at least one output device (e.g. printer, screen,
control device such as an actuator). Input and output devices like keyboards and
printers, together with the external storage devices, are referred to as peripherals.
System Bus
There are three types of busses:
1. Address Buss: A unidirectional lines determine the size of memory
addressable by the processor.
2. Data Bus : A bi-directional lines indicate the size of the data transferred
between the processor and memory or I/O device.
3. Control Bus: consists of a set of control signals indicates the type of
action taking place on the system bus.
3
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Width (bit)
Register
Size of
Bus
4
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
5
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
6
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
7
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
15 87 0
8
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
9
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Memory Organization
8086/8088µP, via its 20-bit address bus, can address 220 = 1,048,576 bytes
or (1 Mbyte) of memory locations. Thus the memory space of 8086µP is
1,048,576 bytes or 524,288 words. The memory map of 8086µP as shown,
where the whole memory space starting from 00000H to FFFFFH.
The 8086µP operate in the Real mode memory addressing. Real mode
operation allows the microprocessor to address only the first 1 Mbyte of
memory. Even though the 8086 has a 1 Mbyte address space, not all this
memory is active at one time. Actually, the 1 Mbytes of memory are partitioned
into 64 Kbyte (65,536) segments. The 8086µP allows four memory segments.
The figure shows these memory segments.
18
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
11
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
For example if (DS)=2000H and the Logical address is 1234H then the physical
address is: PA = (2000)*10 + 1234H = 20000H + 1234H = 21234H
12
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Destination(D) Source(S)
Operation ,
reg. / mem. reg. / mem.
An instruction consists of an op-code and operands. The operand may reside in
the accumulator, or in a general purpose register or in a memory location.
Examples:
MOV AX , BX ; copy the content of BX to AX.
ADD AL , 12H ; add 12H to AL and store the result in AL.
An addressing mode is a method of specifying an operand. The following are
the different addressing modes of 8086µP:
1. Register Addressing Mode: The operands both are registers (8-bit or 16-
bit). Some examples are:
MOV AX , BX
ADD AL , BH
2. Immediate Addressing Mode: The source can be either 8-bit or 16-bit
number and the destination is a register . Some examples are:
MOV AL , 83H
ADD BX , 1284H
3. Memory Addressing Mode: One of the operands is a memory location. The
different memory addressing modes are:
a) Direct Addressing Mode: The effective address of the specified memory
location is given directly between [ ] brackets, for example: (DS)=1000H
MOV AL , [3000H]
Note: The default
{ } { } segment register is DS
13
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
SI
{ } { } { }
14
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
{ } { } { }
For example if (BX) = 2000H , (DI) = 0030H and (DS) = 1000H then the
instruction MOV CX , [BX + DI] load the register CX with 1040H form
memory locations 12030H and 12031H.
PA = 10000 + [2000H + 0030H] = 12030H
0030H
{ } { } { } { }
15
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
BP
SS
(SS) x 10H
The table below shows the operand that stores the effective address and the
default segment register used to form the physical address
Note:
1. CS : IP together determine the address of the first instruction to execute.
In this way the first instruction in CS being execution, if the first
instruction is two byte long, the processor increment IP by 2 to indicate
the next instruction.
2. Segment override prefix means that we can use any segment register to
form the physical address as follows:
MOV AX , ES : [2000H]
ADD SS : [BX+SI] , DX
AND CS : [BP] , AL
16
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
17
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
18
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
The allowed operands for the source and destination are listed below:
Examples :
MOV AX , BX
MOV AL , FFH
MOV [BX] , CX
MOV [SI] , FF87H
MOV BH , [PB+DI]
MOV DS , AX
MOV AL , 'A'
MOV [BX] , [SI] is not allowed, why?
MOV DS , FA34H is not allowed, why?
MOV DS , CS is not allowed, why?
MOV CS, AX is not allowed, why?
19
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
The allowed operands for the source and destination are listed below:
Example
(AX)=1000H and (BX)=2000H
XCHG AX , BX
After execution :
(AX)=2000H and (BX)=1000H
AX
BX
28
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
BX
SI
Note : All data transfer instructions do not affect the status flags.
21
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
22
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
23
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Ex: Write an ALP that divide a signed byte stored in 5600AH by the
content of BL.
Ans.
MOV AX , 5000H
MOV DS , AX
MOV SI , 6000H
MOV AL , [SI + 0AH]
CBW
IDIV BL
MOV [SI + 0AH] , AX
HLT
24
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
MOV AX , 5000H
MOV DS , AX
MOV AL , 20H
MOV CL , 30H
ADD AL , CL
MOV CL , 10H
MUL CL
MOV SI , 4000H
MOV [SI] , AX
HLT
Ex: Write an ALP to multiply the word 1234H by the double word
12345678H. Store the result in locations starting in 54000H.
MOV AX , 1234H
MOV BX , 5678H
MOV CX , 1234H
MUL BX
MOV SI , AX
MOV DI , DX
MOV AX , 1234H
MUL CX
ADC AX , DI
ADC DX , 0000H
MOV BX , 5000H
MOV DS , BX
MOV [4000H], SI
MOV [4002H],AX
MOV [4004H],DX
HLT
25
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Logical Instructions:
This group includes AND, OR, XOR and NOT instructions. These
instructions perform logic operations. They are used to clear , set, and
complement certain bits in a register or a memory location.
Note: After executing AND , OR, or XOR instructions CF and OF are both 0.
PF, SF, and ZF are updated . AF is undefined.
26
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Shift Instructions:
Shift instructions can perform two basic types of shift operations: the
logical shift and the arithmetic shift. Also, each of these operations can be
performed to the right or to the left. They have the ability to shift the contents of
either an internal register or a storage location in memory.
27
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Ex: If (CL)=2 and (AX)= 1234AH. Then, executing: SAR AX , CL leads to:
(AX)=048EH & (CF)=0
28
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Ex : If (CL) =0416 and AX=1234A16. Determine the new contents of AX and the
carry flag after executing the instructions:
a) ROL AX, 1 b) ROR AX, CL
Ans.
a) (AX) = 2468H and (CF) = 0
29
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
LAHF instruction : Load (copy to) AH with the low byte the flag register.
(AH) ← (Low byte of flag register)
SAHF instruction : Store (copy) AH register to low byte of flag register.
(Low byte of flag register) ← (AH)
Ex: Write an ALP that complements the first byte of flags register.
LAHF
XOR AH , 0FFH
SAHF
HLT
38
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Examples
CMP AX , BX
CMP DL , CL
CMP AL , [BX]
CMP CX , 1234h
CMP [SI+100H] , 1234H
31
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
32
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Conditional Jump
33
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Ex: Write a piece of code that transfers a block of 256 bytes stored at
locations starting at 34000H to locations starting at 36000H.
Ans.
MOV AX , 3000H 360FF ← 340FF
MOV DS , AX ←
MOV BX , 0000H ←
.
MOV CX , 100H
.
NEXT: MOV AL , [BX + 4000H] .
MOV [BX + 6000H] , AL .
INC BX .
DEC CX 36001 ← 34001
JNZ NEXT 36000 ← 34000
HLT
Ex : Write an ALP that adds an array of integers of size 256 byte stored at
locations starting at 54000H with another array starting at address 56000H,
store the result at locations starting at 58000H.
Ans.
MOV AX , 5000H 540FF 560FF 580FF
MOV DS , AX . .
MOV BX , 0000H . .
MOV CX , 100H . .
NEXT: MOV AL , [BX + 4000H] . + . =
. .
ADD AL , [BX + 6000H]
. .
MOV [BX + 8000H] , AL . .
INC BX 54001 56001 58001
DEC CX 54000 56000 58000
JNZ NEXT
HLT
Ex: Write an ALP to find the absolute subtraction of two arrays of size
1024 bytes starting at 82000H and 84000H respectively. Store the results at
locations starting at 86000H.
Ans.
MOV AX , 8000H
MOV DS , AX
XOR SI , SI
MOV CX , 400H
NEXT: MOV AL , [SI + 2000H]
CMP AL , [SI + 4000H]
JB N1
SUB AL , [SI + 4000H]
JMP N2
N1: MOV BL , [SI + 4000H]
34
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
XCHG AL , BL
SUB AL , BL
N2: MOV [SI + 6000H] , AL
INC SI
DEC CX
JNZ NEXT
HLT
Ex: Write an ALP that counts the number of 1’s in a byte stored in 54000H
and stores the result in 54001H
Ans.
XOR BL , BL ; clear BL to keep the number of 1s
MOV CL , 8 ; rotate total of 8 times
MOV AX , 5000H
MOV DS , AX
MOV SI , 4000H
MOV AL , [SI]
AGAIN: ROL AL,1 ; rotate it once
JNC NEXT ; check for 1
INC BL ; if CF=1 then add one to count
NEXT: DEC CL ; go through this 8 times
JNZ AGAIN ; if not finished go back
INC SI
MOV [SI] , BL
HLT
Ex : Write a piece of code to find the number of negative integers in an
array of size 1024 byte contain signed numbers stored at addresses starting
at 21000H, store the result in a location 51000H.
Ans.
MOV AX , 2000H
MOV DS , AX
MOV CX , 400H
MOV DL , 00H
MOV SI , 0000H
AGAIN: MOV AL , [SI+1000H]
ROL AL , 1
JNC NEXT
INC DX
NEXT: INC SI
DEC CX
JNZ AGAIN
MOV AX , 5000H
MOV [1000H] , DX
HLT
35
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Ex: Write an ALP to find the maximum byte of a block of 256 bytes
starting at 53000H. Store the result at 56000H.
MOV AX , 5000H
MOV DS , AX
MOV SI , 3000 H
MOV CX , 0100 H
MOV AH , 00 H
NEXT: CMP AH , [SI]
JAE PASS
MOV AH, [SI]
PASS: INC SI
DEC CX
JNZ NEXT
MOV DI , 6000
MOV [DI] , AH
HLT
H.W :
Write a piece of code to find the number of odd integers in an array
of size 1024 byte stored at addresses starting at 21000H, store the
result in location 51000H.
Write a piece of code that exchanges a block of 256 bytes stored at
locations starting at 34000H with another block starting at 36000H.
By using XCHG instruction.
By using MOV instruction.
Write an ALP to find the 2’s complement of a block of 100 bytes
starting at 53000H and store the result in
Write an ALP to find the minimum value of a byte from a block of
256 bytes starting at 53000H. Store the result at 56000H.
Write an ALP to find the sum of the following series:
Sum=1 + 2 + 3 + 4 + ………………………..+ 100
36
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
count= final;
while (count !=0)
{
Loop program statements;
--count;
}
….. // next statement
37
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Loop Instructions
There are three loop instructions. They can be used in place of certain
conditional jump instructions. These instructions give the programmer a
flexibility in writing programs in a simpler manner. The different loop
instructions and the operations they perform are listed below:
CX = CX - 1
NEXT
CX ≠ 0 ? Yes
No
Ex: Write a piece of code that transfers a block of 256 bytes stored at
locations starting at 34000H to locations starting at 36000H.
Ans.
MOV AX , 3000H
MOV DS , AX
MOV BX , 0000H
MOV CX , 100H
NEXT: MOV AL , [BX + 4000H]
MOV [BX + 6000H] , AL
INC BX
LOOP NEXT
HLT
38
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Ex: Write an ALP to find the maximum byte from a block of 256 bytes
starting at 53000H.
Ans.
MOV AX , 5000H
MOV DS , AX
LEA SI , [3000H]
MOV CX , 0100H
MOV AH , 00H
AGAIN: CMP AH , [SI]
JAE NEXT
MOV AH , [SI]
NEXT: INC SI
LOOPNE AGAIN
MOV [SI] , AH
HLT
Ex : Write an ALP that counts the number of letters M in a string of size
256 bytes starts at 5F600H.
MOV AX , 5000H
MOV DS , AX
LEA SI , [F600H]
MOV CX , 100H
MOV DL , 00H
MOV AL , 4DH ; 4D is the ASCII code of the letter M
AGAIN: CMP AL , [SI]
JNE NEXT
INC DL
NEXT: INC SI
LOOP AGAIN
HLT
Ex: Write an ALP to find the average of an array of size 256 bytes of
unsigned integers starting at 6A000H.
MOV AX , 6000H
MOV DS , AX
XOR BX , BX
XOR AX , AX
MOV CX , 100H
AGAIN: ADD AL , [BX+2000H] H.W: Repeat the previous example
ADC AH , 00H if the array has signed integers
NEXT: INC BX
LOOP AGAIN
XOR DX , DX
DIV BX
HLT
39
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Stack Instructions
The stack in the 8086/8088 µPs, like that in many microprocessors, is a region
of memory that can store information temporarily ( )بصىرة مؤقتتduring the
execution of the program. It is called a stack, because you "stack" things on it.
The stack is a Last-In-First-Out, (LIFO) structure so the last thing stored in
the stack is the first thing retrieved. The philosophy is that you retrieve ()يستزجع
(pop) data in the opposite order of storing (push) it .
In the 8086/8088µP, the stack pointer is SS:SP (physical address) , which is a 16
bit pointer into a 20 bit address space.
In a POP operation, the data is retrieved (POPed) from that address
(SS : SP). The SP register is incremented by 2.
(Low byte of the operand) ← SS : SP
(High byte of the operand) ← SS : (SP) + 1
(SP)← (SP) + 2
In a PUSH operation, the data of the source is stored (pushed) at address
(SS : SP). The SP register is decremented 2.
SS: (SP) – 1 ← (High byte of the operand)
SS: (SP) – 2 ← (Low byte of the operand)
(SP) ← (SP) – 2
POPF retrieves a word from the stack and places it into the flags register
and increments the stack pointer SP by 2.
(Low Byte of Flags Reg.) ← SS : SP
(High Byte of Flags Reg.) ← SS : (SP) + 1
(SP) ← (SP) + 2
PUSHF pushes the contents of the flags register onto the stack at address
(SS : SP) and decrements the stack pointer SP by 2.
SS : (SP) – 1 ← (High Byte of Flags Reg.)
SS : (SP) – 2 ← (Low Byte of Flags Reg.)
(SP) ← (SP) – 2
48
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
The figure below shows the Execution of PUSH CX if (SS) = 3000H , (SP) =
0034H and (CX)=2030H. This instruction copies the contents of CX in locations
30033H and 30032H in stack segment. The SP is decremented by 2.
41
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
String Instructions
1. MOVS ( MOVSB / MOVSW ) MOVES STRING BYTE OR STRING
WORD: This instruction copies a byte or a word from location in the data
segment to a location in the extra segment. The offset of the source in the data
segment must be in the SI register. The offset of the destination in the extra
segment must be in the DI register. For multiple-byte or multiple-word
moves, the number of elements to be moved is put in the CX
register(counter). If DF is 0, then SI and DI will incremented by 1 after a byte
move and by 2 after a word move. If DF is 1, then SI and DI will be
decremented by 1 after a byte move and by 2 after a word move. MOVS does
not affect any flag.
MOVSB MOVSW
DF=0 DF=1 DF=0 DF=1
(ES:DI) ← (DS:SI) (ES:DI) ← (DS:SI) (ES:DI) ← (DS:SI) (ES:DI) ← (DS:SI)
SI ← SI + 1 SI ← SI - 1 SI ← SI + 2 SI ← SI - 2
DI ← DI + 1 DI ← DI - 1 DI ← DI + 2 DI ← DI - 2
2. LODS ( LODSB / LODSW ) LOAD STRING BYTE INTO AL OR
STRING WORD INTO AX :This instruction copies a byte or a word from a
string location pointed to by SI to AL or to AX. If DF is 0, SI will be
automatically incremented (by 1 for a byte string, and 2 for a word string) to
point to the next element of the string. If DF is 1, SI will be automatically
decremented (by 1 for a byte string, and 2 for a word string) to point to the
previous element of the string. LODS does not affect any flag.
LODSB LODSW
DF=0 DF=1 DF=0 DF=1
(AL) ← (DS:SI) (AL) ← (DS:SI) (AX) ← (DS:SI) (AX) ← (DS:SI)
SI ← SI + 1 SI ← SI - 1 SI ← SI + 2 SI ← SI - 2
STOSB STOSW
DF=0 DF=1 DF=0 DF=1
(ES:DI) ← (AL) (ES:DI) ← (AL) (ES:DI) ← (AX) (ES:DI) ← (AX)
DI ← DI + 1 DI ← DI - 1 DI ← DI + 2 DI ← DI - 2
42
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
43
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Ex: Write a piece of code that transfers a block of 256 bytes stored at
locations starting at 34000H to locations starting at 36000H.
Ans.
MOV AX , 3000H
MOV DS , AX
MOV ES , AX
MOV SI , 4000H
MOV DI , 6000H H.W : Repeat by using MOVSW
MOV CX , 100H
CLD
NEXT: MOVSB
LOOP NEXT
HLT
Ex: Write an ALP to initialize a block of memory starting at 8A000H with
English alphabet (A to Z) using string instructions.
Ans.
MOV AX , 8000H
MOV ES , AX
MOV AL , 'A'
MOV DI , A000H
MOV CX , 18H
CLD
AGAIN: STOSB
INC AL
LOOP AGAIN
HLT
Ex: Write an ALP that scans the byte 05H in a block of memory of size 16
starting at 2A000H, if is found loads BL with 01H otherwise load BL with
00H.
MOV AX, 2000H
MOV ES, AX
MOV AL, 05
MOV DI, 0A000H
MOV CL, 10H
MOV BL , 00H
CLD
AGAIN: SCASB
LOOPNE AGAIN
JNE END
MOV BL , 01H
END: HLT
44
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Ex: Write an ALP that loads a block of memory of size 256 starting at
7A000H with a value 55H by repeating the STOSB instruction.
Ans.
MOV AX, 7000H
MOV ES, AX
MOV DI, 0A000H H.W Repeat by using MOV instruction
MOV AL, 55H
MOV CX, 100H
CLD
REP STOSB
HLT
45
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
46
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Subroutines
A subroutine is a special program that performs a certain task()مهمت او وظيفت معينت
and is called for execution from any point in the main program. A subroutine is
also known as a procedure. A return instruction must be included at the end of
the subroutine to return execution to the main program.
A RET instruction at the end of the procedure will return execution to the next
instruction after the CALL instruction by restoring the saved values IP (in the
case of near call) or CS and IP (in the case of far call) from the stack.
47
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Ans.
MOV CL , n ; n is positive integer less than or equal to 08H
MOV CH , m ; m is positive integer less than or equal to 08H
CMP CL , CH
JC STOP
MOV BL , CL
CALL FACT
MOV SI , AX
MOV BL , CH
CALL FACT
MOV DI , AX
SUB CL , CH
MOV BL , CL
CALL FACT
MUL DI
MOV CX , AX
MOV AX , SI
XOR DX , DX
DIV CX
STOP: HLT
48
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
MOV BX ,0000H
MOV SI , 0001H
XOR DI , DI
NEXT: CALL SUBR
ADD BX , AX
ADC DI , 0000H
INC SI
CMP SI , 0007H
JNZ NEXT
HLT
49
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Note : The variable-port IN / OUT instructions has advantage that the port
address can be computed or dynamically determined in the program. Suppose,
for example, that an 8086-based computer needs to input data from 10 terminals,
each having its own port address. Instead of having a separate procedure to input
data from each port, you can write one generalized input procedure and simply
pass the address of the desired port to the procedure in DX.
58
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Ex: Write a piece of code to send 55H to output port device which its
address is F300H.
MOV AL , 55H
MOV DX , F300H
OUT DX , AL
Ex: Write a piece of code to input the contents of the byte-wide input port
at A000H of the I/O address space into BL.
MOV DX, 0A000h
IN AL, DX
MOV BL, AL
Ex: Write a piece of code to read data from two byte-wide input ports at
addresses AAH and A9H and output the data as a word to the word-wide
output port at address B000H.
IN AL, 0AAh
MOV AH, AL
IN AL, 0A9h
MOV DX, 0B000h
OUT DX, AX
51
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Miscellaneous Instructions
HLT (HALT PROCESSING)
The HLT instruction causes the 8086 to stop fetching and executing
instructions. The 8086 will enter a halt state. The different ways to get the
processor out of the halt state are with an interrupt signal on the INTR
pin, an interrupt signal on the NMI pin, or a reset signal on the RESET
input.
ESC (ESCAPE)
This instruction is used to pass instructions to a coprocessor, such as the
8087 Math coprocessor, which shares the address and data bus with 8086.
Instructions for the coprocessor are represented by a 6-bit code embedded
in the ESC instruction. As the 8086 fetches instruction bytes, the
coprocessor also fetches these bytes from the data bus and puts them in its
queue. However, the coprocessor treats all the normal 8086 instructions as
NOPs. When 8086 fetches an ESC instruction, the coprocessor decodes
the instruction and carries out the action specified by the 6-bit code
specified in the instruction. In most cases, the 8086 treats the ESC
instruction as a NOP.
52
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
4. Get a new value for IP from an absolute memory address of 4 times the
type specified in the instruction. For an INT 8 instruction, for example,
the new IP will be read from address 00020H.
5. Get a new for value for CS from an absolute memory address of 4 times
the type specified in the instruction plus 2, for an INT 8 instruction, for
example, the new value of CS will be read from address 00022H.
6. Reset both IF and TF. Other flags are not affected.
53
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
BYTE 1 Specification
OPCODE field (6-bits) : Specifies the operation to be performed such as
MOV , ADD , SUB …..etc.
Register direction bit (D-bit):
D = 1 : the register operand specified by REG in byte 2 is a destination
operand.
D = 0 : the register operand specified by REG in byte 2 is a source
operand
Data size bit (W-bit): Specifies whether the operation will be performed
on 8-bit or 16-bit data.
W = 1 : 16-bit data size
W = 0 : 8-bit data size
MOD Explanation
00 Memory Mode no displacement
01 Memory Mode 8-bit displacement
10 Memory Mode 16-bit displacement
11 Register Mode (no displacement)
Register (REG) field (3-bit) : Identifies the register for the first operand
Register/Memory (R/M) field (3-bit): Together with MOD field to
specify the second operand.
54
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Ex: Encode the following instruction in machine code. Assume that the
OPCODE for XOR instruction is 0011002.
XOR CL , [1234H]
Ans.
OPCODE = 001100 (for XOR), D = 1 (destination), W = 0 (8-bit) this leads to:
BYTE 1 = 001100102 = 3216
In byte 2 the destination operand, specified by REG is CL , then:
REG = 001 , MOD = 00, R/M = 110
Therefore:
BYTE 2 = 000011102 = 0E16
BYTE 3 = 3416 and BYTE 4 = 1216
The machine code for the instruction is:
XOR CL , [1234H] = 320E341216
Ex: Encode the following instruction in machine code. Assume that the
OPCODE for ADD instruction is 0000002.
ADD [BX+DI+1234H] , AX
Ans.
OPCODE = 000000 (for ADD) , D = 0 (source), W = 1 (16-bit) This leads to :
BYTE 1 = 000000012 = 0116
In byte 2 the destination operand, specified by REG is AX, then :
REG = 000 , MOD = 10 , R/M = 001
Therefore :
BYTE 2 = 100000012 = 8116
BYTE 3 = 3416 and BYTE 4 = 1216
The machine code for the instruction is:
ADD [BX+DI+1234H] , AX = 0181341216
56
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Segment Register SR
ES 00
CS 01
SS 10
DS 11
57
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Ex: Encode the “block move” program and show how it would be stored in
memory starting at address 20016.
Ans.
MOV AX , 2000H
MOV DS , AX
MOV SI , 100H
MOV DI , 120H
MOV CX , 10H
NXTPT: MOV AH , [SI]
MOV [DI] , AH
INC SI
INC DI
DEC CX
JNZ NXTPT
HLT
58
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
59
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
68
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Address/Data Bus:
The address bus is 20 bits long and consists of signal lines A0 (LSB)
through A19 (MSB). However, only address lines A0 through A15 are used
when accessing I/O.
The data bus lines are multiplexed with address lines. For this reason,
they are denoted as AD0 through AD15. Data line D0 is the LSB.
Status Signals:
The four most significant address lines A16 through A19 of the 8086µP are
multiplexed with status signals S3 through S6. These status bits are output
on the bus at the same time that data are transferred over the other bus
lines.
Bit S4 and S3 together from a 2 bit binary code that identifies which of
the 8086 internal segment registers are used to generate the physical
address as shown in the table below.
Status line S5 reflects the status of the IF. The last status bit S6 is always
at the logic 0 level.
Control Signals:
When Address latch enable (ALE) is logic 1 it signals that a valid
address is on the bus. This address can be latched in external circuitry on
the 1-to-0 edge of the pulse at ALE.
̅̅̅ (memory/IO) tells external circuitry whether a memory or I/O
transfer is taking place over the bus. Logic 1 signals a memory operation
and logic 0 signals an I/O operation.
̅ (data transmit/receive) signals the direction of data transfer over
the bus. Logic 1 indicates that the bus is in the transmit mode (i.e., data
are either written into memory or to an I/O device). Logic 0 signals that
61
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
the bus is in the receive mode (i.e., reading data from memory or from an
input port).
The bank high enable (̅̅̅̅̅̅) signal is used as a memory enable signal for
the most significant byte half of the data bus, D8 through D15.
̅̅̅̅̅ (write) is switched to logic 0 to signal external devices that valid
output data are on the bus.
̅̅̅̅ (read) indicates that the MPU is performing a read of data off the bus.
During read operations, one other control signal, ̅̅̅̅̅̅ (data enable), is
also supplied. It enables external devices to supply data to the
microprocessor.
The READY signal can be used to insert wait states into the bus cycle so
that it is extended by a number of clock periods. This signal is supplied
by a slow memory or I/O subsystem to signal the MPU when it is ready
to permit the data transfer to be completed.
Interrupt Signals:
Interrupt request (INTR) is an input to the 8086µP that can be used by an
external device to signal that it needs to be serviced. Logic 1 at INTR
represents an active interrupt request.
When the MPU recognizes an interrupt request, it indicates this fact to
external circuits with logic 0 at the interrupt acknowledge (INTA) output.
On the 0-to-1 transition of non-maskable interrupt (NMI), control is
passed to a non-maskable interrupt service routine at completion of
execution of the current instruction. NMI is the interrupt request with
highest priority and cannot be masked by software.
The RESET input is used to provide a hardware reset for the MPU.
Switching RESET to logic 0 initializes the internal registers of the MPU
and initiates a reset service routine.
DMA Interface Signals:
The Direct Memory Access (DMA) interface of the 8086 minimum mode
consist of the HOLD and HLDA signals.
When an external device wants to take control of the system bus, it
signals the MPU by switching HOLD to the logic level 1.
When in the hold state, lines AD0 through AD15, A16/S3 through
A19/S6, ̅̅̅̅̅̅ ̅̅̅ ̅ ̅̅̅̅̅ ̅̅̅̅ ̅̅̅̅̅̅ are all put in the
high-Z state. The MPU signals external devices that it is in this state by
switching HLDA to 1.
62
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Maximum-Mode Interface
The maximum-mode configuration is mainly used for implementing a
multiprocessor/coprocessor system environment which means that
multiple processor exist in the system and each processor execute its own
program.
8086µP does not directly provide all the signals that are required to
control the memory, I/O and interrupt interfaces.
̅̅̅ ̅̅̅ ̅̅̅ are input to the external bus controller device (8288 Bus
Controller) , the bus controller generates the timed command and control
signals.
63
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
System Clock
To synchronize the internal and external operations of the microprocessor
a clock (CLK) input signal is used. The CLK can be generated by the
8284 clock generator IC.
The 8086µP is manufactured in three speeds: 5 MHz, 8 MHz and 10 MHz
For 8086µP, we connect either a 15-, 24- or 30-MHz crystal between
inputs X1 and X2 inputs of the clock chip (see Fig. 8-11). The
fundamental crystal frequency is divided by 3 within the 8284 to give
either a 5-, 8- or 10-MHz
clock signal, which is directly connected to the CLK input of the 8086µP.
64
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
65
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
66
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
The byte transfer operation and word transfer operation of the 8088µP is
as illustrated in figure below. The byte transfer operation needs one bus
cycle, while word transfer operation needs two bus cycles.
67
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
68
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
78
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Isolated Input/Output:
The I/O devices are treated separate from memory. I/O ports are organized as
bytes of data; the memory address space contains 1M consecutive byte
addresses in the range 00000H, through FFFFFH; and the I/O address space
contains 64K consecutive byte addresses in the range 0000H through FFFFH as
shown in figure below.
The way in which the microprocessor deals with input/output devices is similar
to the way in which it deals with memory. The only difference is that just the 16
least significant lines of the bus, AD0 through AD15, are in use (because I/O
addresses are 16 bit long), and throughout the bus cycles, the M/IO control
signal is set to 0.
71
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
72
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Maximum-Mode Interfaces
73
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
74
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Memory Unit:
It is a collection of storage cells together with associated circuits needed to
transfer information in and out of storage device. The memory stores binary
information in groups of bits called bytes or words. The memory unit is mainly
divided into two main parts:
1. Main Memory (Primary Memory)
2. Secondary storage devices (Magnetic Memory)
Bit Select
DRAM Cell
75
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
SRAM (Static RAM) - one-bit memory cells use bistable latches (flip-
flop) for data storage. It is faster with a typical assess time of 10
nanoseconds. It is more expensive and can only store a quarter of the data
that DRAM is able to in the same given area, however data will remain
stored as long as power is on. Fast SRAM can be found in most CPU's
called cache memory.
Bit Select
Write En
RAM Cell
76
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Magnetic Memory
The magnetic memory is cheaper than static memory. It is in the form of
magnetic disks {hard disk (HD) , compact disk (CD) and floppy disk} . It is
used as secondary memory or Auxiliary memory. The size is in G-Bytes but the
speed is very low compared with main memory.
Memory Unit
Main Secondary
Memory Memory
RAM ROM HD CD
EEPROM
77
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
A memory device must have three types of lines or connections: Address, Data,
and Enable & Control.
Address Lines: The input lines that select a memory location within the
memory device. Decoders are used, inside the memory chip, to select a
specific location. The number of address pins on a memory chip specifies
the number of memory locations.
If a memory chip has 13 address pins (A0 - A12), then then the size is 213 =
23 x 210 = 8 Kbyte.
If a memory chip has 4Kbytes, then it has N pins:
2N = 4 Kbyte = 212 → N = 12 address pins (A0 - A11)
Data Connections: All memory devices have a set of data output pins
(for ROM devices), or input/output pins (for RAM devices).
78
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Enable and Control Connections: All memory devices have at least one
Chip Select CS or Chip Enable (CE) input, used to select or enable the
memory device. If a device is not selected or enabled then no data can be
read from, or written into it.
The CS or CE input is usually controlled by the microprocessor through
the higher address lines via an address decoding circuit. RAM chips
have two control input signals that specify the type of memory operation:
the Read (RD) and the Write (WR) signals. ROM chips can perform
only memory read operations, thus there is only read (RD) signal. In most
real ROM devices the Read signal is called the Output Enable (OE)
signal.
RAM ROM
Address Decoding
The physical address space, or memory map, of a microprocessor refers to
the range of addresses of memory locations that can be accessed by the
microprocessor. The size of the address space depends on the number of address
lines of the microprocessor.
79
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
2. Connect the data lines of each memory chip in parallel on the data lines of
the processor.
3. Connect the address lines of each memory chip in parallel with the low
address lines of the processor.
4. Connect the CS lines of each memory device with the high address lines
of the processor through an address decoding circuit..
5. Connect together all WR and RD lines of each memory device.
The following example is assumed that the processor has only 7 address lines
(A0 – A6) , thus it can address 27=128 memory locations . The data lines are 4
(D0 – D3) The size of the RAM used in the system is 32 locations (4 chips of 8
locations), then 3 address lines (A0 – A2) are used to address the 8 locations of
each RAM chip. There are 4 address lines (A3 – A6) used as chip select.
The memory block occupied by the memory module depends on the connection
of the address selection circuit (AND gate) that enables the decoder.
Two address lines are used to control the address selection circuit, thus the
circuit can be configured to occupy four different areas in the address space.
88
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
81
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
A single NAND gate is used to decode each memory device. The inputs of
the NAND gate can be connected on the address lines either directly, or
through inverters, according to the required memory map.
This method has the advantage that it adds a short time delay in the memory
path.
The disadvantage of this circuit is that too many gates (NAND and NOT) are
needed for memory systems that have a few memory chips. This increases
the cost of the system, adds to the complexity of the PCB board (too many
chips and lines).
The figure below shows a memory map of two memory chips. Lines A0 – A14
are connected to each chip and lines A15 – A19 are connected to NAND gates to
generate the CS signal.
82
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
83
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
H.W.
Show how a 32Kbyte ROM module can be connected on an 8088 system using
2764 EPROM chips (8K x 8), occupying the address range starting from the
address E0000H. Use the following address decoding circuits:
1. NAND gates
2. A line decoder and a NAND gate
84
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
Example:
Design an 8K x 8 RAM module using 2K x 8 RAM chips. The module should
be connected on an 8-bit processor with a 16-bit address bus, and occupy the
address range starting from the address A000. Show the circuit and the memory
map.
Solution:
Number of memory devices needed = 8K / 2K = 4
Decoder needed = 2 x 4
Number of address lines on each 2K x 8 memory chip = 11
2m = 2K = 21 x 210 = 211 → (A0 - A10)
Two address lines are needed for the decoder → (A11 , A12)
Number of address lines needed for the address selection circuit
= 16 - 11 - 2 = 3 → (A13, A14 , A15)
85
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
86
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
87
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
the appropriate bank is enabled, thus data is copied only in the appropriate
memory chip.
By providing separate write signals, we require
one decoder to select a 16-bit wide memory.
The signal ̅̅̅̅̅̅̅̅ is activated only when ̅̅̅̅̅̅̅
and ̅̅̅̅̅ both are activated, thus it is used with
memory chips from odd memory bank. The
signal ̅̅̅̅̅̅̅ is activated only when A0 and ̅̅̅̅̅ both are activated, thus it is
used with memory chip from even memory bank.
Note: It is not necessary to generate separate read signals for two memory
banks. Read memory signal always put 16-bit data on the data bus, but the CPU
reads data according to 8-bit read operation or 16-bit read operation.
88
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
89
8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI
𝑊𝑅 D7 – D0 A14 – A0
RAM2
̅̅̅
CS
E ̅̅̅̅̅
̅̅̅̅ 𝑊𝑅 D7 – D0 A14 – A0
RAM1
̅̅̅
CS
E ̅̅̅̅̅
D7 – D0 A14 – A0 ̅̅̅̅
EPROM2
̅̅̅
CS
̅̅̅̅
E
D7 – D0 A14 – A0
EPROM1
̅̅̅
CS
̅̅̅̅
E
Y0 Y1 Y2 Y3
Decoder
A B E
A19 – A0
D15 – D0
̅̅̅̅
𝐑𝐃
̅̅̅̅̅̅̅
𝑩𝑯𝑬
̅̅̅̅̅
𝐖𝐑
8086µP
𝐈𝐎
𝐌 ̅̅̅
H.W: Design an 8086µP based system 256Kbyte memory (128 Kbyte EPROM
and 128 Kbyte RAM), the size of the RAM and EPROM chips is 32 Kbyte.
Draw the complete schematic of the design indicating the address map. Assume
the starting Address of the EPROM is F0000H and for the RAM is 30000H.
98