الحاسبات 1920 2 المعالجات المايكروية PDF
الحاسبات 1920 2 المعالجات المايكروية PDF
Microprocessor 8086
Second Stage
1
Introduction to Computer
Types of Computers
Microsoft Office is available for both the PC and Apple machines. The
software works exactly the same on either machine. The majority of
companies use PCs to handle their work requirements. This may primarily
be due to the wide variety of software programs available to the PC
machine and the relative lower cost of a PC as compared to an Apple
machine.
2
2. Supercomputer
3. Mainframe computers
Mainframe computers are large enough to fill an entire room and require a
large capital investment. They can simultaneously handle hundreds of
different programs and users without sacrificing performance. They
process large volumes of data at an incredible speed. Mainframes are
commonly found in government agencies or large organizations, e.g.,
telephone companies, credit card companies, airlines, or universities. For
instance, you access a mainframe computer whenever you use your
bankcard at an Automated Teller Machine (ATM). Mainframe computers
can sometimes be called centralized systems as they control the flow of
data to and from computers or terminals.
4. Minicomputers
Minicomputers can fill part of a room, and often cost tens of thousands of
dollars. They process data at a slower rate and in smaller volumes than the
mainframe computers. Several people can use a minicomputer
3
simultaneously; but as the number of users increases, each user would
notice a reduction in speed. Minicomputers are commonly found in
medium-sized manufacturing companies, legal or accounting firms, and
department store where the scanners in a grocery store would link to a
minicomputer.
Computer Components
Any kind of computers consists of Software and Hardware:
Software
Software is a generic term for organized collections of computer data and
instructions, often broken into two major categories: system software that
provides the basic non-task-specific functions of the computer, and
application software which is used by users to accomplish specific tasks.
Hardware:
Computer hardware is the collection of physical elements that constitutes
a computer system. Computer hardware refers to the physical parts or
components of a computer such as the monitor, mouse, keyboard, etc. all
of which are physical objects that can be touched.
4
Fig. 1: Basic Block of a Microcomputer
5
5. If the instruction requires an ALU or specialized hardware to complete,
instruct the hardware to perform the requested operation.
RAM:
Random Access Memory (RAM): is a memory scheme within the
computer system responsible for storing data on a temporary basis, so that
it can be promptly accessed by the processor as and when needed. It is
volatile in nature, which means that data will be erased once supply to the
storage device is turned off. RAM stores data randomly and the processor
accesses these data randomly from the RAM storage. RAM is considered
"random access" because you can access any memory cell directly if you
know the row and column that intersect at that cell.
ROM:
Input Devices:
Input device is any peripheral (piece of computer hardware equipment to
provide data and control signals to an information processing system such
as a computer or other information appliance.
Input device Translate data from form that humans understand to one that
the computer can work with. Most common are keyboard and mouse.
6
Output devices
An output device is any piece of computer hardware equipment used to
communicate the results of data processing carried out by an information
processing system (such as a computer) which converts the electronically
generated information into human-readable form.
Bus System
Address Bus
Data Bus
Control Bus
Address Bus: The address bus consists of 16, 20, 24, or more parallel
signal lines. On these lines the CPU sends out the address of the memory
location that is to be written to or read from. The number of address lines
determines the number of memory locations that the CPU can address. If
the CPU has N address lines then it can directly address 2N memory
locations.
Data Bus: The data bus consists of 8, 16, 32 or more parallel signal lines.
As indicated by the double-ended arrows on the data bus line, the data bus
lines are bi-directional. This means that the CPU can read data in on these
7
lines from memory or from a port as well as send data out on these lines to
memory location or to a port. Many devices in a system will have their
outputs connected to the data bus, but the outputs of only one device at a
time will be enabled.
Control Bus: The control bus consists of 4-10 parallel signal lines. The
CPU sends out signals on the control bus to enable the outputs of addressed
memory devices or port devices. Typical control bus signals are memory
read, memory write, I/O read, and I/O writer. To read a byte of data from
a memory location, for example, the CPU sends out the address of the
desired byte on the address bus and then sends out a memory read signal
on the control bus.
Microprocessor 8086
8086 is the first 16-bit microprocessor from INTEL, released in the year
1978. The term 16 bit means that its ALU, its internal registers and most
of the instructions are designed to work with 16 bit binary words. 8086
microprocessor has a 16-bit data bus and 20-bit address bus. So, it can
address any one of 220 =1048576=1 megabyte memory locations. INTEL
8088 has the same ALU, same registers and same instruction set as the
8086.But the only difference is 8088 has only 8-bit data bus and 20-bit
address bus. Hence the 8088 can only read/write/ports of only 8-bit data at
a time. The 8086 microprocessor can work in two modes of operations
.They are Minimum mode and Maximum mode. In the minimum mode of
operation the microprocessor do not associate with any co-processors and
cannot be used for multiprocessor systems. But in the maximum mode the
8086 can work in multi-processor or co-processor configuration. This
minimum or maximum operations are decided by the pin MN/ MX (Active
8
low). When this pin is high 8086 operates in minimum mode otherwise it
operates in Maximum mode.
9
Fig.2: Architecture of 8086 Microprocessor
11
To implement above functions, the BIU contains the segment
registers, the instruction pointer, address generation adder, bus
control logic, and an instruction queue.
The BIU uses a mechanism known as an instruction stream queue to
implement pipeline architecture.
The Execution unit is responsible for decoding and exe uting all
instructions.
The EU consists of arithmetic logic unit (ALU), status and control
flags, general‐ purpose registers, and temporary‐ operand registers.
The EU extracts instructions from the top of the queue in t e BIU,
decodes the ,generates operands if necessary, passes them to the IU
and requests it to perform the read or write by cycles to memory or
I/O and perform the operation specified by the instruction on the
operands.
During the execution of the instruction, the EU tests the status and
control flags and updates them based on the results of executing the
instruction.
11
memory and waiting for memory to send back the next instruction
byte or bytes.
Except in the case of JMP and CALL instructions, where the queue
must be dumped and then reloaded starting from a new address, this
pre-fetch and queue scheme greatly speeds up processing.
Fetching the next instruction while the current instruction executes
is called pipelining.
Register Organization
The 14 registers of 8086 microprocessor are categorized into four groups.
They are general purpose data registers, Pointer & Index registers, Segment
registers and Flag register as shown in the table below.
12
1. General Purpose Registers:
Accumulator register: consists of two 8-bit registers AL and AH,
which can be combined together and used as a 16-bit register AX.
AL in this case contains the low order byte of the word, and AH
contains the high order byte. Accumulator can be used for I/O
operations and string manipulation.
Base register: consists of two 8-bit registers BL and BH, which can
be combined together and used as a 16-bit register BX. BL in this
case contains the low order byte of the word, and BH contains the
high order byte. BX register usually contains a data pointer used for
based, based indexed or register indirect addressing.
13
2. Index and Pointer Register
14
3. Segment Registers
15
Extra segment (ES) used to hold the starting address of Extra
segment. Extra segment is provided for programs that need to access
a second data segment. Segment registers cannot be used in
arithmetic operations.
16
Auxiliary Flag (AF): If an operation performed in ALU generates
Parity Flag (PF): This flag is used to indicate the parity of result.
If lower order 8‐ bits of the result contains even number of 1‟s, the
Parity Flag is set and for odd number of 1‟s, the Parity Flag is reset.
17
2. Control Flags
Control flags are set or reset deliberately to control the operations of the
execution unit. Control flags are as follows:
18
Memory Segmentation:
The memory in an 8086 based system is organized as segmented
memory.
The CPU 8086 is able to access 1MB of physical memory. The
complete 1MB of memory can be divided into 16 segments, each of
64KB size and is addressed by one of the segment register.
The 16-bit contents of the segment register actually point to the
starting location of a particular segment. The address of the
segments may be assigned as 0000H to F000h respectively.
To address a specific memory location within a segment, we need
an offset address. The offset address values are from 0000H to
FFFFH so that the physical addresses range from 00000H to
FFFFFH.
A program can have more than four segments, but can only access
four segments at a time.
19
Physical address is calculated as below:
Ex:
Segment address =1005H
Offset address =5555H
Segment address =1005H = 0001 0000 0000 0101
Shifted left by 4 Positions=0001 0000 0000 0101 0000 + Offset address =
5555H= 0101 0101 0101 0101
Physical address=155A5H =0001 0101 0101 1010 0101
Physical address = Segment address * 10H + Offset address.
21
3. Permits a program and/or its data to be put into different areas of
memory each time the program is executed, i.e., provision for
relocation is done.
Addressing modes:
The different ways in which a source operand is denoted in an instruction
are known as the addressing modes. There are 8 different addressing modes
in 8086 programming. They are
1. Immediate addressing mode
2. Register addressing mode
3. Direct addressing mode
4. Register indirect addressing mode
5. Based addressing mode
6. Indexed addressing mode.
7. Based indexed addressing mode
8. Based, Indexed with displacement.
21
Example:
MOV BX, AX
22
indirect displacement to the contents of either base register BX or
base pointer register BP.
Example:
MOV [BX] + 1234H, AL;
EA=BX+1234H
PH=DS*10+EA
Example:
MOV BX, [SI+06];
EA=SI+06
PH=DS*10+EA
23
contents of any one of the base register (BX or BP) and any one of
the index register, in a default segment.
Example:
MOV AX, 50H [BX] [SI]
Here, 50H is an immediate displacement, BX is base register and SI is an
index register the effective address of data is computed as
10H * DS + [BX] + [SI] + 50H
24
Instruction Set of 8086
The 8086 microprocessor supports 6 types of Instructions. They are
MOV instruction
It is a general purpose instruction to transfer byte or word from register to
register, memory to register, register to memory or with immediate
addressing.
25
General Form:
MOV destination, source
XCHG instruction
General Format
26
LEA Instruction – Load Effective Address
General Form
Example:
PUSH instruction
The PUSH instruction decrements the stack pointer by two and copies the
word from source to the location where stack pointer now points. Here the
source must of word size data. Source can be a general purpose register,
segment register or a memory location.
The PUSH instruction first pushes the most significant byte to sp-1, then
the least significant to the sp-2. Push instruction does not affect any flags.
27
Example:
POP instruction
The POP instruction copies a word from the stack location pointed by the
stack pointer to the destination. The destination can be a General purpose
register, a segment register or a memory location. Here after the content is
copied the stack pointer is automatically incremented by two.
Example:
POP CX ; Copy a word from the top of the stack to CX and increment
SP by 2.
The IN instruction will copy data from a port to the accumulator. If 8 bit is
read the data will go to AL and if 16 bit then to AX. Similarly OUT
instruction is used to copy data from accumulator to an output port.
Both IN and OUT instructions can be done using direct and indirect
addressing modes.
28
Example
2. Arithmetic Instructions
29
ADD instruction
Add instruction is used to add the current contents of destination with that
of source and store the result in destination. Here we can use register and/or
memory locations. AF, CF, OF, PF, SF, and ZF flags are affected
General Format:
Example"
This instruction performs the same operation as ADD instruction, but adds
the carry flag bit (which may be set as a result of the previous calculation)
to the result. All the condition code flags are affected by this instruction.
The examples of this instruction along with the modes are as follows:
31
Example:
SUB instruction
General Format:
Example:
SUB AL, 0FH ; subtract the immediate content, 0FH from the
content of AL and store the result in AL
SUB AX, BX ; AX = AX-BX
SUB AX,0100H ; Immediate Destination AX
SUB AX,BX; Register
SUB AX,[5000H]; Direct
SUB [5000H], 0100H; Immediate
31
SBB: SUBTRACT with Borrow
The subtract with borrow instruction subtracts the source operand and the
borrow flag (CF) which may reflect the result of the previous calculations,
from the destination operand. Subtraction with borrow, here means
subtracting 1 from the subtraction obtained by SUB, if carry (borrow) flag
is set.
The result is stored in the destination operand. All the flags are affected
(condition code) by this instruction. The examples of this instruction are as
follows:
Example:
CMP: Compare
32
Example:
INC and DEC instructions are used to increment and decrement the content
of the specified destination by one. AF, CF, OF, PF, SF, and ZF flags are
affected.
Example:
General Format
MUL OP
AX = AL * operand.
33
When operand is a word:
Example:
General Format
IMUL op
AX = AL * operand.
Example:
34
DIV Instruction - Unsigned divide
General Format
AL=AX/operand
AH=remainder (modulus)
DX=remainder (modulus)
If you want to divide a byte by a byte, you must first put the dividend byte
in AL and fill AH with all 0's. The SUB AH, AH instruction is a quick
way to do. If you want to divide a word by a word, put the dividend word
in AX and fill DX with all O's, The SUB DX, DX instruction does this
quickly.
Example:
DIV BH ; AX/ BH
35
IDIV Instruction - Divide by signed byte or word
General Format
AL=AX/operand
AH=remainder (modulus)
DX=remainder (modulus)
Example:
MOV BL, 4;
RET
36
3. BIT Manipulation Instructions:
AND instruction
This instruction logically ANDs each bit of the source byte/word with the
corresponding bit in the destination and stores the result in destination. The
source can be an immediate number, register or memory location, register
can be a register or memory location.
The CF and OF flags are both made zero, PF, ZF, SF are affected by the
operation and AF is undefined.
General Format:
Example
AND CX, [SI] ; AND word at offset [SI] in data segment with word
in CX register. Result in CX register
37
OR Instruction
This instruction logically ORs each bit of the source byte/word with the
corresponding bit in the destination and stores the result in destination. The
source can be an immediate number, register or memory location, register
can be a register or memory location.
The CF and OF flags are both made zero, PF, ZF, SF are affected by the
operation and AF is undefined.
General Format:
OR Destination, Source
CX=00111110 10100101
XOR Instruction
The XOR operation is again carried out in a similar way to the AND and
OR operation. The constraints on the operands are also similar. The XOR
operation gives a high output, when the 2 input bits are dissimilar.
Otherwise, the output is zero. The example instructions are as follows:
XOR AX,0098H;
XOR AX,BX;
XOR AX,[5000H];
38
Shift / Rotate Instructions
Shift instructions move the binary data to the left or right by shifting them
within the register or memory location. They also can perform
multiplication of powers of
There are two type of shifts logical shifting and arithmetic shifting, later is
used with signed numbers while former with unsigned.
39
Fig. 6: Rotate operations.
SHL/SAL instruction.
Both the instruction shifts each bit to left, and places the MSB in CF and
LSB is made 0. The destination can be of byte size or of word size, also it
can be a register or a memory location. Number of shifts is indicated by
the count.
General Format:
41
Example:
execution,
CY B7 B6 B5 B4 B3 B2 B1 B0
0 1 0 1 1 0 1 1 1
B
CY B7 B6 B5 B4 B3 B2 B1 0
1 0 1 1 0 1 1 1 0
SHR instruction
This instruction shifts each bit in the specified destination to the right and
0 is stored in the MSB position. The LSB is shifted into the carry flag. The
destination can be of byte size or of word size, also it can be a register or a
memory location. Number of shifts is indicated by the count.
General Format:
41
Example:
ROL instruction
This instruction rotates all the bits in a specified byte or word to the left
some number of bit positions. MSB is placed as a new LSB and a new CF.
The destination can be of byte size or of word size, also it can be a register
or a memory location. Number of shifts is indicated by the count.
General Format:
BL is made
MOV BL, B7H ; B7H
Before execution,
CY B7 B6 B5 B4 B3 B2 B1 B0
0 1 0 1 1 0 1 1 1
42
After the execution,
CY
B7 B6 B5 B4 B3 B2 B1 B0
1 0 1 1 0 1 1 1 1
ROR instruction
This instruction rotates all the bits in a specified byte or word to the right
some number of bit positions. LSB is placed as a new MSB and a new CF.
The destination can be of byte size or of word size, also it can be a register
or a memory location. Number of shifts is indicated by the count.
General Format:
Example:
ROR BL, 1 ; shift the content of BL register one place to the right.
RCR instruction
This instruction rotates all the bits in a specified byte or word to the right
some number of bit positions along with the carry flag. LSB is placed in a
new CF and previous carry is placed in the new MSB. The destination can
be of byte size or of word size, also it can be a register or a memory
location. Number of shifts is indicated by the count.
General Format:
43
RCR destination, count
Example:
RCR BL, 1 ; shift the content of BL register one place to the right.
4. String Instructions
The string instructions function easily on blocks of memory. They are user
friendly instructions, which help for easy program writing and execution.
They can speed up the manipulating code. They are useful in array
handling, tables and records. By using these string instructions, the size of
the program is considerably reduced.
44
Table 4: string instructions
MOVS/MOVSB/MOVSW
These instructions copy a word or byte from a location in the data segment
to a location in the extra segment. The offset of the source is in SI and that
of destination is in DI. For multiple word/byte transfers the count is stored
in the CX register.
MOVS affect no flags. MOVSB is used for byte sized movements while
MOVSW is for word sized.
45
MOVSB
Algorithm:
ES :[ DI] = DS : [ SI]
if DF = 0 then
SI = SI + 1
DI = DI + 1
Else
SI = SI - 1
DI = DI-1
Example:
ORG 100h
CLD
LEA SI , a1
LEA DI , a2
MOV CX , 5
REP MOVSB
RET
a1 DB 1 , 2 , 3 , 4 , 5
a2 DB 5 DUP) 0 (
MOVSW
Algorithm:
ES :[ DI] = DS : [ SI]
if DF = 0 then O SI = SI + 2 O DI = DI + 2
Else
SI = SI – 2
DI = DI – 2
46
Example:
REP/REPE/REP2/REPNE/REPNZ
Example:
REP REPE/REPZ
CX=0
CX=0 OR ZF=0
REPNE/REPNZ CX=0 OR ZF=1
LODS/LODSB/LODSW
47
LODSB
Algorithm:
AL = DS : [SI]
if DF = 0 then
SI = SI + 1
Else
SI = SI – 1
Example:
ORG 100h
LEA SI , a1
MOV CX , 5
MOV AH , 0Eh
m : LODSB
INT 10h
LOOP m
RET
a1 DB ' H ' , ' e ' , ' l ' , ' l ' , ' o '
LODSW
AX = DS : [SI]
if DF = 0 then
SI = SI + 2
else
SI = SI - 2
48
Example:
ORG 100h
LEA SI , a1
MOV CX , 5
REP LODSW ; finally there will be 555h in AX .
RET
a1 dw 111h , 222h , 333h , 444h , 555h
STOS/STOSB/STOSW
STOSB
Example:
ORG 100h
LEA DI , a1
MOV AL , 12h
49
MOV CX , 5
REP STOSB
RET
a1 DB 5 dup( 0 )
STOSW
Algorithm:
ES :[ DI] = AX
if DF = 0 then
DI = DI + 2
Else
DI = DI - 2
Example:
ORG 100h
LEA DI , a1
MOV AX , 1234h
MOV CX , 5
REP STOSW
RET
a1 DW 5 dup( 0 )
51
CMPS/CMPSB/CMPSW
CMPS is used to compare the strings, byte wise or word wise. The
comparison is affected by subtraction of content pointed by DI from that
pointed by SI. The AF, CF, OF, PF, SF and ZF flags are affected by this
instruction, but neither operand is affected.
CMPSB
Algorithm:
DS :[ SI] - ES : [ DI]
set flags according to result : OF , SF , ZF , AF , PF , CF
if DF = 0 then
SI = SI + 1
DI = DI + 1
Else
SI = SI - 1
DI = DI - 1
CMPSW
DS :[ SI] - ES : [ DI]
set flags according to result : OF , SF , ZF , AF , PF , CF
if DF = 0 then
SI = SI + 2
DI = DI + 2
Else
SI = SI - 2
DI = DI – 2
51
• Scan string SCASB/SCASW
SCASB
Compare bytes: AL from ES:[DI].
Algorithm:
AL - ES : [ DI]
set flags according to result : OF , SF , ZF , AF , PF , CF
if DF = 0 then
DI = DI + 1
Else
DI = DI – 1
SCASW
Compare bytes: AX from ES:[DI].
Algorithm:
AL - ES : [ DI]
set flags according to result : OF , SF , ZF , AF , PF , CF
if DF = 0 then
DI = DI + 2
Else
DI = DI – 2
52
5. Control Transfer Instructions
These instructions transfer the program control from one address to other
address. (Not in a sequence). They are again classified into four groups.
They are:
JUMP Instruction
a) Unconditional jump.
b) Conditional jump.
53
In an unconditional jump, no status requirements are imposed for the jump
to occur. That is, as the instruction is executed, the jump always takes place
to change the execution sequence.
On the other hand, for a conditional jump instruction, status conditions that
exist at the moment the jump instruction is executed decide whether or not
the jump will occur. If this condition or conditions are met, the jump takes
place, otherwise execution continues with the next sequential instruction
of the program. The conditions that can be referenced by a conditional
jump instruction are status flags such as carry (CF), parity (PF), and
overflow (OF).
54
6. Process Control Instructions
These instructions directly affected the state of flags. Figure below shows
these instructions.
55
2. Flag manipulation instructions
1. STC instruction
This instruction sets the carry flag. It does not affect any other flag.
2. CLC instruction
This instruction resets the carry flag to zero. CLC does not affect any other
flag.
3. CMC instruction
This instruction complements the carry flag. CMC does not affect any other
flag.
4. STD instruction
This instruction is used to set the direction flag to one so that SI and/or DI
can be decremented automatically after execution of string instruction.
STD does not affect any other flag.
5. CLD instruction
This instruction is used to reset the direction flag to zero so that SI and/or
DI can be incremented automatically after execution of string instruction.
CLD does not affect any other flag.
6. STI instruction
This instruction sets the interrupt flag to 1. This enables INTR interrupt of
the 8086. STI does not affect any other flag.
7. CLI instruction
This instruction resets the interrupt flag to 0. Due to this the 8086 will not
respond to an interrupt signal on its INTR input. CLI does not affect any
other flag.
56
2. External Hardware Synchronization Instructions:
57
Interrupts:
Broadly the interrupts are divided into two types. They are external
(hardware) Interrupts and internal (Software) Interrupts. The hardware
interrupts are classified as non-maskable and maskable interrupts. The
hardware interrupt is caused by any peripheral device by sending a signal
through a specified pin to the microprocessor. Whereas internal interrupts
are initiated by the state of the CPU (e.g. divide by zero error) or by an
instruction. So, the software interrupt is one which interrupts the normal
58
execution of a program of the microprocessor. The 8086 has two hardware
interrupt pins namely NMI and INTR. In the two, the NMI is a non-
maskable interrupt and the INTR interrupt request is a maskable interrupt
which has lower priority .The third pin associated with the hardware
interrupts are the INTA called interrupt acknowledge.
Hardware interrupts
59
Maskable and Non-Maskable Interrupts
Software interrupts
Coming to the software interrupts, 8086 can generate 256 interrupt types
through the instruction INT n .Any of the 256 interrupt types can be
generated by specifying the interrupt type after INT instruction. For
example the first five types are as follows:
The interrupts from Type 5 to Type 31 are reserved for other advanced
microprocessors, and interrupts from 32 to Type 255 are available for
hardware and software interrupts.
61
Interrupt vector table
Interrupt vector table on 8086 is a vector that consists of 256 total interrupts
placed at first 1 kb of memory from 0000h to 03ffh, where each vector
consists of segment and offset as a lookup or jump table to memory address
of bios interrupt service routine (f000h to ffffh) or dos interrupt service
routine address, the call to interrupt service routine is similar to far
procedure call.
The size for each interrupt vector is 4 bytes (2 word in 16 bit), where 2
bytes (1 word) for segment and 2 bytes for offset of interrupt service
routine address. So it takes 1024 bytes (1 kb) memory for interrupt vector
table.
62
In 8086 microprocessor following tasks are performed when
microprocessor encounters an interrupt:
1. The value of flag register is pushed into the stack. It means that first
the value of SP (Stack Pointer) is decremented by 2 then the value
of flag register is pushed to the memory address of stack segment .
2. The value of starting memory address of CS (Code Segment) is
pushed into the stack .
3. The value of IP (Instruction Pointer) is pushed into the stack .
4. IP is loaded from word location (Interrupt type) * 04 .
5. CS is loaded from the next word location .
6. Interrupt and Trap flag are reset to 0.
63