Professional Documents
Culture Documents
FEE 402 Section2
FEE 402 Section2
FEE 402 Section2
Fig. 2.1
There are two types of microcomputers: the general purpose digital microcomputer and the
embedded microcomputer. The general purpose digital microcomputer is what most people
understand by the word computer. It is what is commonly referred to as a personal computer
(PC) and it has all the necessary memory and input/output devices required by the user to
execute a wide range of application programs.
An embedded computer on the other hand is one dedicated to a specific application. A good
example of an embedded microcomputer lies at the heart of an Automated Teller Machine
(ATM). When a customer inserts his/her card in a slot, the microcomputer reads the relevant
details from the magnetic strip on the card. The customer keys in an identity code (PIN), and the
microcomputer validates the code and then invites the customer to perform a transaction. Once
the transaction is completed, the microcomputer updates the data on the magnetic strip and
operates a mechanical subsystem that returns the card to the user.
16
FEE 402: Electronics B
17
FEE 402: Electronics B
2.1.1 Microprocessor
A microprocessor is the central processing unit (CPU) of a microcomputer constructed within a
single chip, and it is entirely useless on its own. To make it into a viable microcomputer, it needs
to be connected to the memory, I/O and the bus system.
The above functions are performed by CPU‘s three components; an arithmetic and logic unit
(ALU) which can perform arithmetical and logical calculations like add, subtract, AND, OR, etc.,
registers which are temporary binary data storage locations and the control unit which generates
and control bus signals. A special register in the CPU is the address counter or program counter
(or instruction pointer) which is used to hold the address of the next instruction to be fetched
from the memory.
2.1.2 Memory
The memory section usually consists of RAM and ROM. External memory may consist of flash
memory, magnetic floppy disks, magnetic hard disks, DVDs or compact disks. Memory has
three purposes:
to store instructions that the computer is going to perform
to store the data to be operated on by the computer
to store intermediate and final results of operations performed during program execution.
The operation of the memory is controlled by information carried in the control bus.
2.1.3 Input/Output
The input/output or I/O section allows the computer to take in data from the outside world or
send data to the outside world. Examples of input devices include: keyboards, mouse, joystick
and ADCs. Output devices include: video display terminals or CRT, printers, LED readouts and
DACs. Modems are both input and output devices. The I/O devices allow the user and the
computer to communicate with each other. The actual physical devices used to interface the
computer buses to external systems are called ports.
18
FEE 402: Electronics B
For some devices, data transfer with the microcomputer is in serial form (one bit at a time) e.g.
the CRT, while for others, data transfer is in parallel form e.g. the microcomputer transfers
information to a printer one byte at a time.
Address Bus
This is a unidirectional bus, because information flows over it only in one direction - from the
CPU to the memory or I/O elements. It is used to specify the address of a memory location or an
I/O device involved in a data transfer.
The number of memory locations that the CPU can address is determined by the number of
address lines. If the CPU has N address lines then it can directly address 2N memory locations.
For example, a CPU with 16 address lines can address 216 or 65, 536 memory locations.
The address bus consists of 16, 20, 24, 32, 64 or more parallel signal lines.
Data Bus
This is a bidirectional bus, since data can flow to or from the CPU. It is used to carry data
associated with a memory or I/O transfer. During a READ operation, the CPU's data pins act as
inputs and receive data that has been placed on the data bus by the memory or I/O element
selected by the address on the address bus. During a WRITE operation, the CPU's data pins act
as outputs and place data on the data bus, which is then sent to the selected memory or I/O
device.
The data bus consists of 8, 16, 32, 64 or more parallel signal lines.
Control Bus
This is a set of signals that are used to synchronize the activities of the separate microcomputer
elements. Some control signals are from the CPU to other elements to tell them what kind of
operation is in progress, e.g. READ/WRITE signal. 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 write. 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. The memory read signal enables
the addressed memory device to output the byte of data on the data bus where it is read by the
CPU.
19
FEE 402: Electronics B
The I/O elements can also send control signals to the CPU e.g. RESET signal which causes the
CPU to reset to a particular starting state, INTERRUPT REQUEST signal used by I/O devices to
get attention of the CPU when it is performing other tasks.
The first microprocessor was the Intel 4004 produced in 1971. This 4004 was a 4-bit device
intended to be used with some other devices in making a calculator. Some logic designers,
however, saw that this device could be used to replace PC boards full of combinational and
sequential logic devices. Also, the ability to change the function of a system by just
reprogramming, rather than redesigning the hardware, is very appealing. It was these factors that
pushed the evolution of microprocessors.
In 1972 Intel came out with the 8008 which was capable of working with 8-bit words. In 1974
Intel announced the 8080 which had a much larger instruction set than 8008. The 8080 is
referred to as a second-generation microprocessor.
Soon after Intel produced 8080, Motorola came out with MC 6800, another 8-bit general purpose
CPU. Some of the other competitors were the MOS technology 6502 and the Zilog Z80. The 16-
bit microprocessors entered the marketplace in the late 1970s and early 1980s. Then came the
32-bit processors.
Modern microprocessors are 32-bit or 64-bit. Generally, a microprocessor with a large number of
bits can process instructions more quickly than one with a small word size when both
microprocessors are running at the same clock speed. Microprocessors are also designed such
that the ones with a larger word size can process more instructions than their predecessors [the
computer made with the Intel 4004 was a calculator-like device, while modern 32 and 64-bit
microprocessors are used to make full multimedia computers (those that can process data, video
and sounds)].
20
FEE 402: Electronics B
Some people classify microprocessors into two groups based on their origin. These are the 6‘s
group and the 8‘s group. Family trees for these groups are presented Figure 2.2.
Fig. 2.2
We observe that as we progress upward on the family tree the trend is towards greater
complexity. Complexity is noted in the figure, in terms of the bit size of the internal registers.
The 6‘s group traces its origin back to the original 6800 Microprocessor designed by Motorola.
The 8‘s group traces its origin back to Intel‘s 8080 Microprocessor. Each branch in Fig.2.2 is
labeled near the top with the manufacturer responsible for its development.
For many years, manufacturers identified the microprocessors with number codes such as 4004,
8086, 68000, Z8000, etc., but of late, the microprocessors have brand names e.g. PowerPC,
Pentium, Itanium, Xeon, etc.
21
FEE 402: Electronics B
Microprocessors are made for different applications. The most powerful microprocessors are
used in servers and workstations. Then there are microprocessors for PCs (microcomputers). At
the lower end are the microprocessors used in embedded systems - these are low-cost
microprocessors, and they make over well over 50% of all the microprocessors that are sold.
Microprocessors have provided a cost-effective solution for control and many modern appliances
have a number of microprocessors in their control circuitry. A modern luxury car, such as a
Mercedes Benz S-class or a BMW 7-series has over 100 microprocessors for functions such as
engine management, dashboard control, braking system, traction control, climate control, cruise
control, navigational system, etc. A Boeing 777 jet has over 1200 microprocessors.
The most recent microprocessors are 64-bit and these include Intel's Xeon and Itanium 2, AMD's
Opteron and Athlon 64, and IBM's Power PC G5. These processors were initially mostly used in
servers, but they are now in the PC market.
The Pentium microprocessors have millions of transistors packed within a small area e.g. the
Pentium IV processor has over 40 million transistors. The millions of transistors switching in
such close proximity collectively generate a lot of heat energy which could potentially damage
the IC. For this reason, Pentium microprocessors have fans mounted on the IC for cooling
purposes.
Table 2.1 shows the evolution of Intel microprocessors starting with 8086. From the year 2001 to
the present the design of microprocessors has emphasized on multi-threaded and multi-core
microprocessors.
22
FEE 402: Electronics B
Table 2.1
The 8086 was the first 16-bit Microprocessor to be introduced by Intel Corporation, as illustrated
in Table 2.1. It is designed to be upwardly compatible with the older 8080/8085 series of 8-bit
microprocessors. The upward compatibility allows programs written for the 8080/8085 to be
easily converted to run on the 8086.
The word 16-bit means that its arithmetic logical unit, internal registers, and most of its
instructions are designed to work with 16-bit binary words. The 8086 has a 16-bit data bus, so it
can read data form or write data to memory and ports either 16-bits or 8-bits at a time. The 8086
has a 20-bit address bus, so it can address any one of 220 or 1,048,576 memory locations (1
Mbyte of memory). Each of the 1,048,576 memory addresses of the 8086 represents a byte-wide
location. Words will be stored in two consecutive memory locations. If the first byte of a word is
at an even address, the 8086 can read the entire word in one operation. If the first byte of the
word is at an odd address, the 8086 will read the first byte of the word in one operation, and the
second byte in another operation.
23
FEE 402: Electronics B
3. Microprocessor Architecture
The term architecture, as used in microprocessor circuits, describes the functional components
that make up the microprocessor and the interaction between them as seen by the programmer.
As illustrated by the block diagram in Fig.3.1, the 8086 microprocessor architecture can be
broadly divided into two independent functional parts known as the Execution Unit (EU) and the
Bus Interface Unit (BIU).
Fig. 3.1
24
FEE 402: Electronics B
The EU is where the actual processing of data takes place inside the 8086 µP. It is here that
instructions are received, decoded, and executed from the instruction queue portion of BIU. The
instructions are taken from the top of the instruction queue on the first-in, first-out, or FIFO,
basis. Data processed by the EU can be transmitted to the memory or peripheral devices through
the BIU. EU has no direct connection with the outside world and relies solely on the BIU to feed
it with instructions and data.
The BIU on the other hand sends out addresses, fetches instructions from memory, reads data
from ports and memory, and writes data to ports and memory. In other words the BIU handles all
transfers of data and addresses on the buses for the execution unit. The BIU has the task of
making sure that the bus is used to its fullest capacity in order to speed up operations. This
function is carried out in two ways:
by fetching the instructions before they are needed by the execution unit and storing them
in the instruction queue, the 8086 µP is able to increase computing speed.
by taking care of all bus control functions, the EU is free to concentrate on processing
data and carrying out the instructions.
The ALU can also perform logical operations such as NOT, AND, OR, and EXCLUSIVE OR.
25
FEE 402: Electronics B
Fig. 3.2
Six flags are status flags: AF, CF, OF, SF, PF and ZF. The remaining three flags are control
flags: DF, IF, and TF.
AF (auxiliary flag). If this flag is set, there has been a carry out or borrow out of bit 3
into bit 4. This flag is used during decimal arithmetic instructions.
CF(carry flag). If this flag is set, there has been a carry out or borrow of the most
significant bit. It is used by instructions that add and subtract multi byte numbers.
26
FEE 402: Electronics B
OF (overflow flag). If this flag is set, an arithmetic overflow has occurred; that is, a
significant digit has been lost because the size of the result exceeded the capacity of its
destination location.
SF (sign flag). Since negative binary numbers are represented in the 8086/8088 in
standard 2s complement notation, SF indicates the sign of the result ( 0 = positive, 1 =
negative).
PF (parity flag). If this flag is set, the result has even parity, an even number of 1s. This
flag can be used to check for transmission errors.
ZF (zero flag). If this flag is set, the result of the operation is 0.
TF (trap flag) . Setting TF puts the processor into single-step mode for debugging. In
this mode the µP automatically generates an internal interrupt after each instruction,
allowing a program to be inspected as it executes instruction by instruction.
Each of these registers may be used separately as 8-bit storage areas or combined to form one
16-bit (one word) storage area. The acceptable register pairs are AH and AL, BH and BL, CH
and CL and DH and DL. The AH-AL pair is referred to as the AX register, the BH-BL pair is
referred to as the BX register, the CH-CL pair is referred to as the CX register, and the DH-DL
pair is referred to as the DX register.
27
FEE 402: Electronics B
The AL register is also called the Accumulator. For 16-bit operations, AX is called the
accumulator.
The 8086 register set is very similar to those of earlier generation 8080 and 8085
microprocessors. Many programs written for the 8080 and 8085 could easily be translated to run
on the 8086.
Stack Pointer
A Stack is a section of memory set aside to store addresses and data while a subprogram is being
executed. An entire 64 K bytes segment is set aside as Stack in 8086 µP. The upper 16 bits of the
starting address for this segment is kept in the stack segment register. The Stack Pointer (SP)
register contain the 16-bit offset from the start of the segment to the memory location where a
word was most recently stored on the Stack. The memory location where a word was most
recently stored is called the top of Stack. Fig.3.2 illustrates the details.
Fig. 3.3
The physical address for a stack read or for a stack write is produced by adding the contents of
the stack pointer register to the segment base address in SS. To do this the contents of the Stack
segment register are shifted four bit positions left and the contents of SP are added to the shifted
result. In the figure 5000 H in SS is shifted left four bit positions to give 50000H. When FFEOH
in the SP is added to this, the resultant physical address for the top of the stack will be 5FFEOH.
The physical address can be represented either as a single number 5FFEOH, or it can be
represented in SS:SP form as 5000:FFEOH.
28
FEE 402: Electronics B
29
FEE 402: Electronics B
Instruction Pointer
The Instruction Pointer (IP) is a 16- bit register that is used to point to, or tell the µP, the
instruction to execute next. Therefore, the instruction pointer is used to control the sequence in
which the program is executed. Each time the execution unit accepts an instruction, the
instruction pointer, is incremented to point to the next instruction in the program.
Segment Registers
There are four segment registers. They are the code segment (CS), the data segment (DS), the
stack segment (SS), and the extra segment (ES). These registers are used to define a logical
memory space or memory segment that is set aside for a particular function:
The CS register points to the current code segment. Instructions are fetched from this
segment.
The DS register points to the current data segment. Program variables and data are held in
this area.
The SS register points to the current stack segment. Stack operations are performed on
locations in the SS segment.
The ES register points to the current extra segment, which is also used for data storage.
30
FEE 402: Electronics B
Each of the segment registers can be up to 64 kilobytes long. A particular segment is made up of
an uninterrupted section of memory locations. A segment can be addressed separately using the
base address that is contained in its segment register. The base address is the starting address for
that segment.
Address Generator
The address-generator unit is used with the segment registers to generate the 20-bit physical
address required to identify all the possible memory addresses. The 20 address lines give a
maximum physical memory size of 220 address locations, or 1,048,576 bytes of memory. But all
the registers in the µP are only 16 bits wide. The physical address is obtained by shifting the
segment base value four bit positions (one hexadecimal position) and adding the offset or logical
address of the segment.
Fig. 3.4
31
FEE 402: Electronics B
The 8086 operates in time periods called bus cycles. Each bus cycle requires 4 clock cycles to
complete. Therefore, the bus cycle for 5 MHz µp is completed after a minimum time of 800 ns.
A typical bus cycle is shown in fig. 3.5.
(a)
(b)
Fig. 3.5
One cycle of a clock is referred to as a state. A state is measured from the 50 percent point on the
falling edge of one clock pulse to 50 percent point on the falling edge of the next clock pulse- T1
in the figure is a state. Each basic bus operation such as reading a byte from memory or writing a
word to a port requires some number of states. The group of states required for a basic bus
operation is called a machine cycle. The total time it takes the 8086 to fetch and execute an
instruction is called an instruction cycle. An instruction cycle consists of one or more machine
cycles.
Two major bus cycles are the read bus cycle and the write bus cycle. The read bus cycle is
activated when the microprocessor is reading information from the memory or an I/O device.
During the read bus cycle, there are normally four clock cycles T1 ,T2, T3 and T4. However, if the
32
FEE 402: Electronics B
device outputting data to the µP needs more time to send the data, a wait state (Tw) is initiated
by placing extra clock cycles (Tw's) between cycles T3 and T4.
Once a microprocessor has fetched the necessary program instructions and data through the BIU,
its next step is to execute the instructions. The EU receives the instruction from the instruction
queue and executes it. Some instructions may take 2 clock cycles to execute, where as others
may take as many as 100 clock cycles to execute. In older microprocessors this left the bus idle
while the µP was executing a long instruction, as shown in the fig. 3.6. However, since the 8086
µP is broken up into two functional units, the BIU and EU, it avoids much of the idle time
required by older microprocessors. It does this by having the BIU pre fetch instructions and place
them into the instruction queue and data registers while the EU is executing the program
instructions. Therefore, while the bus is busy during a read cycle, the EU can be executing the
previous instructions. When the bus is busy during a write cycle, the EU can be executing
another instruction. This greatly increases the effective speed of the entire system.
Fig. 3.6
33
FEE 402: Electronics B
Fig 3.7
For the minimum mode, MN/ MX (pin 33) is asserted HIGH. This is the mode for typically
smaller systems with a single µp. Functions shown on the parenthesis (pins 24-31) apply.
The µp operates in maximum mode when MN/ MX is asserted LOW. This is the mode for larger
systems with more than one µp.
34
FEE 402: Electronics B
T4 T1 T2 T3 T4
Fig. .2
S5: The status of the interrupt enable flag bit is updated at the beginning of each cycle.
The status of the flag is indicated through this bus.
S6: When Low, it indicates that 8086 is in control of the bus. During a "Hold
acknowledge" clock period, the 8086 tri-states the S6 pin and thus allows another bus master to
take control of the status bus.
Table 3.1
35
FEE 402: Electronics B
After the first clock cycle of an instruction execution, the A17/S4 and A16/S3 pins specify which
segment register generates the segment portion of the 8086 address. Thus by decoding these lines
and using the decoder outputs as chip selects for memory chips, up to 4 Megabytes (one Mega
per segment) of memory can be accessed. This feature also provides a degree of protection by
preventing write operations to one segment from erroneously overlapping into another segment
and destroying information in that segment.
During T1 state the BHE should be used to enable data onto the most significant half of the data
bus, pins D15 - D8. Eight-bit oriented devices tied to the upper half of the bus would normally
use BHE to control chip select functions. BHE is Low during T1 state of read, write and
interrupt acknowledge cycles when a byte is to be transferred on the high portion of the bus.
The S7 status information is available during T2, T3 and T4 states. The signal is active
Low and floats to 3-state during "hold" state. This pin is Low during T1 state for the first
interrupt acknowledge cycle.
RD (O): READ
The Read strobe indicates that the processor is performing a memory or I/O read cycle. This
signal is active low during T2 and T3 states and the Tw states of any read cycle.
This signal floats to tri-state in "hold acknowledge cycle".
TEST (I)
TEST pin is examined by the "WAIT" instruction. If the TEST pin is Low, execution continues.
Otherwise the processor waits in an "idle" state. This input is synchronized internally
during each clock cycle on the leading edge of CLK.
It is a level triggered input which is sampled during the last clock cycle of each instruction to
determine if the processor should enter into an interrupt acknowledge operation. A subroutine is
vectored via an interrupt vector look up table located in system memory. It can be internally
masked by software resetting the interrupt enable bit.
INTR is internally synchronized. This signal is active HIGH.
36
FEE 402: Electronics B
Reset (I)
Reset causes the processor to immediately terminate its present activity. To be recognised, the
signal must be active high for at least four clock cycles, except after power-on which requires a
50 Micro Sec. pulse. It causes the 8086 to initialize registers DS, SS, ES, IP and flags to all
zeros. It also initializes CS to FFFF H. Upon removal of the RESET signal from the RESET
pin, the 8086 will fetch its next instruction from the 20 bit physical address FFFF0H. The reset
signal to 8086 can be generated by the 8284. (Clock generation chip). To guarantee reset from
power-up, the reset input must remain below 1.5 volts for 50 Micro sec. after Vcc has reached
the minimum supply voltage of 4.5V. The RES input of the 8284 can be driven by a simple RC
circuit.
37
FEE 402: Electronics B
8086 RESET line provide an orderly way to start an executing system. When the processor
detects the positive-going edge of a pulse on RESET, it terminates all activities until the signal
goes low, at which time it initializes the system as shown in table 3.2.
Flags Cleared
CS register FFFFH
DS register 0000H
SS register 0000H
ES register 0000H
Queue Empty
Ready (I)
Ready is the acknowledgement from the addressed memory or I/O device that it will
complete the data transfer. The READY signal from memory or I/O is synchronized by the
8284 clock generator to form READY. This signal is active HIGH. The 8086 READY input is
not synchronized. Correct operation is not guaranteed if the setup and hold times are not met.
Since the 8086 does not have on-chip clock generation circuitry, an 8284 clock generator chip
must be connected to the 8086 clock pin. The crystal connected to 8284 must have a frequency 3
times the 8086 internal frequency. The 8284 clock generation chip is used to generate READY,
RESET and CLK.
38
FEE 402: Electronics B
WR (O): Write
Indicates that the processor is performing a write memory or write IO cycle, depending on the
state of the M / IO signal. WR is active for T2, T3 and Tw of any write cycle. It is active
LOW, and floats to 3-state OFF during local bus "hold
acknowledge ".
39
FEE 402: Electronics B
with T2 until the middle of T4, while for a write cycle, it is active from the beginning of T2 until
the middle of T4. It floats to tri-state off during local bus "hold acknowledge".
The following pins function descriptions are for the 8086 systems in maximum mode (i.e.. MN/
MX = 0). Only the pins which are unique to maximum mode are described below.
40
FEE 402: Electronics B
S2 S1 S0 Characteristics
0 0 0 Interrupt acknowledge
0 1 1 Halt
1 0 0 Code access
1 0 1 Read memory
1 1 0 Write memory
1 1 1 Passive State
Table 3.3
Queue status allows external devices like In-circuit Emulators or special instruction set extension
co-processors to track the CPU instruction execution. Since instructions are executed from the
8086 internal queue, the queue status is presented each
CPU clock cycle and is not related to the bus cycle activity. This mechanism allows
(1) A processor to detect execution of a ESCAPE instruction which directs the co-
processor to perform a specific task and
(2) An in-circuit Emulator to trap execution of a specific memory location.
QS1 QS1 Characteristics
0 0 No operation
Table 3.4
41
FEE 402: Electronics B
LOCK (O)
It indicates to another system bus master, not to gain control of the system bus while LOCK is
active Low. The LOCK signal is activated by the "LOCK" prefix instruction and remains active
until the completion of the instruction. This signal is active Low and floats to tri-state OFF
during 'hold acknowledge".
42
FEE 402: Electronics B
4 Interrupts in µP 8086
When the 8086 is executing a program, it can get interrupted because of one of the following:
a) Due to an interrupt getting activated. This is called hardware interrupt.
b) Due to an exceptional happening during an instruction execution, such as division of a
number by zero. This is generally termed as exceptions or Traps.
c) Due to the execution of an Interrupt instruction like "INT 21H". This is called a Software
interrupt.
The action taken by the 8086 is similar for all the three cases, except for minor differences.
There are two basic types of interrupts, maskable and non-maskable. A nonmaskable interrupt
requires an immediate response by the µP. It is usually used for serious circumstances like power
failure. A maskable interrupt is an interrupt that the µP can ignore depending upon some
predetermined condition defined by the status register. Interrupts are also prioritized to allow for
the case when more than one interrupt needs to be serviced at the same time.
In 8086 there are two interrupt pins. They are NMI and INTR. NMI stands for non maskable
interrupt. Whenever an external device activates this pin, the microprocessor will be interrupted.
This signal cannot be masked. NMI is a vectored interrupt. This means, the 8086 knows where to
branch to service the NMI request. If both NMI and INTR are activated at the same time, NMI
will be serviced first.
In an 8086 system the first 1 K bytes memory from 00000H to 003FFH is set aside as a table for
storing the starting addresses of interrupt service procedures. Since 4 bytes are required to store
the CS and IP values for each interrupt service procedure, the table can store starting addresses
for up to 256 interrupt procedures. The starting address of an Interrupt Service procedure stored
in this table is often called as Interrupt Vector Table or the Interrupt Pointer Table.
43
FEE 402: Electronics B
Fig. 4.1 shows how the 256 interrupt pointers are arranged in the memory table. The lowest five
types are dedicated to specific interrupts such as the divide by zero interrupt and the non
maskable interrupt. The next 27 interrupt types, from 5 to 31 are reserved by Intel for use in
future microprocessors. The upper 224 interrupt types, from 32 to 255, are available to use for
hardware and software interrupts.
Fig. 4.1
44
FEE 402: Electronics B
INTR is a non vectored interrupt, which means, the 8086 does not know where to branch to
service the interrupt. The 8086 has to be told by an external device like a Programmable
Interrupt controller regarding the branch.
Whenever the INTR pin is activated by an I/O port, if Interrupts are enabled and NMI is not
active at that time, the microprocessor finishes the current instruction that is being executed and
gives out a ‗0‘ on INTA pin twice. When INTA pin goes low for the first time, it asks the
external device to get ready. In response to the second INTA the microprocessor receives the 8
bit, say N, from a Programmable Interrupt Controller. The action taken is as follows:
i. Complete the current instruction.
ii. Activates INTA output, and receives type Number, say N.
iii. Flag register value, CS value of the return address & IP value of the return address are
pushed on to the stack.
iv. IP value is loaded from contents of word location N x 4.
v. CS is loaded from contents of the next word location.
vi. Interrupt Flag and trap Flag are reset to 0.
45
FEE 402: Electronics B
At the end of the ISS, there will be an IRET instruction. This performs popping off from the
stack top to IP, CS and Flag registers. Finally, the register values which are also saved on the
stack at the start of ISS, are restored from the stack and a return to the interrupted program takes
place using the IRET instruction.
Action taken by the 8086 when divide by zero error occurs is as follows:
i. Flag register value is pushed on to the stack.
ii. CS value of the Return address and IP value of the Return address are pushed on to the
stack.
iii. IP is loaded from contents of word location 0x4 = 00000H.
iv. CS loaded from contents of next word location, 00002H.
v. Interrupt Flag and trap Flag are reset to 0.
The action taken by the ISS could be to display a suitable error message on the CRT and then
halt the proceedings. Or, it could be to set a bit in a memory location to indicate an error, and
then return to the interrupted program using the IRET instruction.
If the 8086 trap flag is set, the 8086 will automatically do a type 1 interrupt after executing each
instruction. It is an internal interrupt caused by the completion of an instruction execution. It is
useful for debugging a program.
The action taken by the 8086 when T flag is set to 1, and an instruction execution is completed is
as follows:
46
FEE 402: Electronics B
The action taken by the ISS could be to display the contents of the various registers on the CRT
and then return to the interrupted program using the IRET program.
When one of these instructions is executed a branch to an ISS takes place. Because their
execution results in a branch to an ISS, they are called interrupts. Software Interrupt instructions
can be used to test the working of the various Interrupt handlers. For example, we can execute
INTO instruction to execute type 0 ISS, without really having to divide a number by 0. Similarly,
we can execute INT 2 instruction to test NMI ISS.
Thus a branch to the ISS take place. During the ISS, interrupts are disabled because the Interrupt
flag is reset to 0. At the end of the ISS, there will be an IRET instruction. Thus a return back to
the interrupted program takes place with Flag registers unchanged.
47
FEE 402: Electronics B
The mnemonic for the instruction is INT3. It is a 1 byte instruction. Op-code for this is CCH.
The execution of INT3 instruction results in the following:
i. Flag register value is pushed on to the Stack.
ii. CS value of the return address and IP value of the return address are pushed on to the
Stack.
iii. IP is loaded from the contents of the word location 3x4 = 0000CH.
iv. CS is loaded from the contents of the next word location.
v. Interrupt Flag and Trap Flag are reset to 0.
Thus a branch to the ISS takes place. During the ISS, interrupts are disabled because Interrupt
flag is reset to 0. At the end of the ISS, there will be an IRET instruction to return back to the
interrupted program.
A break point interrupt service procedure usually saves all the register contents on the Stack.
Depending upon the system, it may then send the register contents to the CRT display and wait
for the next command from the user.
There are two ways to detect and respond to an overflow error in a program. One way is to put
the jump if overflow instruction, JO, immediately after the arithmetic instruction. If the overflow
flag is Set, execution will jump to the address specified in the JO instruction. At this address an
error routine may be put which respond to the overflow.
The second way is to put the INTO instruction immediately after the arithmetic instruction in the
program. The mnemonic for the instruction is INTO. It is a 1 byte instruction. The op-code for
this is CEH. It is a conditional interrupt instruction. Only if the overflow flag is Set, a branch
takes place to an interrupt handler whose interrupt type number is 4. If the overflow flag is reset,
the execution continues with the next instruction.
48
FEE 402: Electronics B
Thus a branch to ISS takes place. During the ISS, interrupts are disabled. At the end of ISS, there
will be an IRET instruction, returning back to the interrupted program. Instructions in the ISS
procedure perform the desired response to the error condition.
49
FEE 402: Electronics B
It is thus possible to write instructions to load an 8-bit immediate number into an 8-bit memory
location or to load a 16-bit number into two consecutive memory locations.
The contents of any 16-bit register can be moved into any 16-bit register, or the contents of any
8-bit register can be moved into any 8-bit register. However, an instruction of the type MOV
CX,AL cannot be used because this is an attempt to copy a byte- type operand (AL) into a word
type destination (CX). A byte in AL would fit in CX, but the 8086 would not know which half of
CX to put it in. But if the byte from AL is to be copied into the high byte of CX, the instruction
MOV CH,AL could execute it. The instruction MOV CL,AL will copy the byte from AL to CL,
the low byte of CX.
50
FEE 402: Electronics B
Fig. 5
51
FEE 402: Electronics B
There are two modes of memory addressing: Direct and indirect modes.
Another example of this addressing mode is the instruction MOV BX,[437AH]. When executed,
this instruction copies a word from memory into BX register. Since each memory address of the
8086 represents a byte of storage, the word must come from two memory locations. The byte at a
displacement of 437AH from the data segment base will be copied into BL. The contents of the
next higher address, displacement 437BH will be copied into BH register. The 8086 will
automatically access the required number of bytes in memory for a given instruction.
The above two examples show how direct addressing mode can be used to specify the source of
an operand. It can also be used to specify the destination of an operand. For example the
instruction MOV [437AH],BX will copy the contents of the BX register to two memory
locations in the data segment. The contents of BL will be copied to the memory location as a
displacement of 437AH and the contents of BH will be copied to the memory location at a
displacement of 437BH.
In the indirect addressing mode, the memory address is not directly given. A register is used to
indicate the address where the data can be found. Therefore, the register acts as an indirect
address to locate the data. For example, in the instruction MOV (BX),CX the source of data is
the CX register. The destination where the data are to be placed or copied to, is the address
pointed to by the BX register. The brackets ( ) around BX indicate that the BX register contains
the effective address and not a numeric value.
52
FEE 402: Electronics B
6. Input/Output Concepts
Fig. 6.1
The port is the path way for data as it is transferred between the microprocessor and its
peripherals.
When the CPU performs an output instruction (I/O write cycle) the data on the bus must be
stored by the port. When an input instruction (I/O read cycle) is executed, the I/O port must gate
its data on to the data bus lines. Just as each memory location has its own (memory) address,
each I/O port has its own (port) address.
53
FEE 402: Electronics B
Peripheral devices (I/O port) may be connected to the µP using Port mapped or memory mapped
techniques.
The 8086 has two I/O instructions: IN AL (or AX),port and OUT port, AL (or AX). There are
two forms of each instruction. In the direct form, IN AL (or AX), port or OUT port, AL (or AX),
the I/O port address is supplied within the instruction and restricts the access to ports with
addresses between 0 and 255. The indirect Form, IN AL ( or AX), DX and OUT DX, AL ( or
AX) uses register DX to hold the port address. This allows access to the full range of I/O ports
from 0 to 65,535.
The advantage of the indirect form is that an I/O procedure can be setup and shared between
several peripherals by passing the port address (in register DX) to the procedure.
Since I/O port is mapped to a memory address, it is accessible in software using any of the
memory read or write instructions. For example, the instruction MOV BH, MEM1 becomes an
input instruction (input the data at "port" MEM1 to register BH). Indirect I/O is also possible.
The instruction sequence can be as follows:
LEA SI,MEM1 ; Point SI at the port
MOV (SI),CX ; output CX to port
The above instructions allow CX to be output to the 16-bit port at address DS:MEM1.
54
FEE 402: Electronics B
There are two major advantages of using memory-mapped I/O. One of them is that, by
discarding the extra complexity that port I/O brings, a CPU requires less internal logic and is
thus cheaper, faster, easier to build, consumes less power and can be physically smaller. The
other advantage is that, because regular memory instructions are used to address devices, all of
the CPU's instructions and addressing modes are available for the I/O. This is compared to the
single input and output commands available with an I/O mapped port.
In a serial I/O port, the data bits are lined up and transmitted in single file fashion one bit at a
time. This technique is slower than parallel port design.
55
FEE 402: Electronics B
Microprocessor controlled data transfer can further be divided into two categories:
o Programmed I/O
o Interrupt Driven I/O
Programmed data transfer has the advantage that it allows simple hardware interfaces, because
most of the management of the I/O operations is performed by software. The disadvantage of this
technique is that valuable CPU time is wasted while the CPU waits for the peripheral device to
get ready.
56
FEE 402: Electronics B
If we assume that 100µs is required to respond to the interrupt and supply the peripheral with
data, then in the case of a 10,000 µs per character printer, 9900 µs will be available to the
processor for its main task.
The 8086 has two interrupt pins labeled INTR and NM1. NM1 is a nonmaskable interrupt, which
means that it requires an immediate response from the processor and it cannot be blocked. INTR
is maskable via the IF flag. Only when this flag is set will interrupts on this input be accepted.
Interrupts can be generated by both hardware and software. Interrupts are also prioritized to
allow for the case when more than one are applied.
Since NMI input is nonmaskable, care must be taken when using this interrupt. This is because
there may be some programs which we do not want to interrupt- reading or writing data to a disk
drive, for example. For this reason, NMI input is normally reserved for catastrophic events like
memory error or impending power failure.
Since the main purpose of DMA operation is to transfer data between external devices and
memory without involving the µP, another device is required. This device is called a DMA
controller. The DMA controller must be capable of performing read and write operations in the
same manner as the µP. Therefore, the DMA controller is actually a special- purpose
microprocessor whose only task is to perform high-speed data transfer between memory and an
external device. The major difference between an I/O program controlled transfer and DMA is
that data transfer does not employ the registers of the CPU.
The primary advantage of the DMA data transfer technique is that it provides an efficient
transfer of large amount of data between storage devices and the main memory without involving
the CPU.
DMA request takes precedence over all other bus activities, including interrupts. In fact, no
interrupt, maskable or non-maskable, will be recognized during a DMA operation.
57
FEE 402: Electronics B
There is a 16-bit program flags register. Three of the bits indicate whether the result of the most
recent arithmetic/logical instruction was zero (ZF), has a negative sign (SF), or generated a carry
or borrow (CF) from the most-significant bit. The overflow bit (OF) indicates overflow for
signed operations (a carry/borrow from the second most-significant bit). A fourth bit, the
interrupt enable bit (IF) controls whether maskable interrupt requests (on the IRQ pin) are
recognized.
The address of the next instruction to be executed is held in a 16-bit instruction pointer (IP)
register (the ―program counter‖). A 16-bit stack pointer (SP) implements a stack to support
subroutine calls and interrupts/exceptions.
There are also four segment registers (CS, DS, ES, SS) which have the base address of the code,
data and stack to be placed in any four 64 kByte ―segments‖ within the CPU‘s 1 megabyte (20-
bit) address space.
58
FEE 402: Electronics B
16-bit registers can be pushed (the SP is first decremented by two and then the value stored at
SP) or popped (the value is restored from the memory at SP and then SP is incremented by 2).
For example:
the order of the operands is destination, source — the reverse of that used on the 68000!
semicolons begin a comment
the suffix ‘H‘ is used to indicate a hexadecimal constant, if the constant begins with a
letter it must be prefixed with a zero to distinguish it from a label
the suffix ‘B‘ indicates a binary constant
square brackets indicate accesses to memory
the size of the transfer (byte or word) is determined by the size of the destination
59
FEE 402: Electronics B
The 8086 has separate I/O and memory address spaces. Values in the I/O space are accessed with
IN and OUT instructions. The port address is loaded into DX and the data is read/written to/from
AL or AX, for example:
MOV DX,372H ; load DX with port address
OUT DX,AL ; output byte in AL to port
; 372 (hex)
IN AX,DX ; input word to AX
Load effective address instruction used to load an address pointer offset from memory into a 16-
bit register. General format:
LEA Reg16,EA
The full address in 8086 is 20-bits and thus can‘t be stored in one register (16-bits). However it
can be fetched and stored in two registers. The instruction Load register and DS (LDS) {also
referred to as Load full pointer} is used to load a full address pointer from memory into a 16-bit
register (mostly SI) and DS register. General format and operation for LDS:
LDS Reg16,EA
The instruction Load register and ES (LES) operates the same, except it initializes ES.
Exchange instruction XCHG is Used to exchange the data between two data registers or a data
register and memory. General format:
XCHG D,S
The operation swaps the content of the source and destination. Direct memory addressing is also
applicable, e.g
XCHG [SUM],BX
60
FEE 402: Electronics B
Arithmetic and logic instructions can be performed on byte and 16-bit values. Format examples
are:
For arithmetic operations, Flags updated based on result : CF, OF, SF, ZF, AF, PF. In case of
Logic operations, Flags updated based on result are CF, OF, SF, ZF, PF while AF is undefined
Integer multiplication instructions are MUL (Multiply two unsigned byte or word operands) and
and IMUL (Multiply two signed byte or word operands). Formats:
Note that:
61
FEE 402: Electronics B
The CALL and RET instructions call and return from subroutines. The processor pushes IP on
the stack during a CALL instruction and the contents of IP are popped by the RET instructions.
For example:
CALL readchar
...
readchar:
...
RET
Other Examples
1. Using JMP, Unconditional Jump. Transfers control to another part of the program. 4-byte
address may be entered in this form: 1234h:5678h, first value is a segment second value
is an offset.
Algorithm:
always jump
Example:
ORG 100h ;The code begins at offset 100h within the code segment
MOV AL, 5h
JMP label1 ; jump over 2 lines!
PRINT 'Not Jumped!'
MOV AL, 0
62
FEE 402: Electronics B
label1:
PRINT 'Got Here!'
RET
2. Using JA, Short Jump if first operand is Above second operand (as set by CMP
instruction). Unsigned.
Algorithm:
if (CF = 0) and (ZF = 0) then jump
Example:
ORG 100h ;The code begins at offset 100h within the code segment
MOV AL, 250h
CMP AL, 5h
JA label1
PRINT 'AL is not above 5'
JMP exit
label1:
PRINT 'AL is above 5'
exit:
RET
Example:
ORG 100h ;The code begins at offset 100h within the code segment
MOV AL, 255h
ADD AL, 1h
JC label1
PRINT 'no carry.'
JMP exit
label1:
PRINT 'has carry.'
exit:
RET
4. In the following program, 32 8-bit numbers are stored in consecutive address with the
first number at offest address 8100H. The program counts how many of these 32 numbers
are equal to zero, and stores this count in the offset address 8200H.
Two indices are set: SI to count through the number of loops, and DI to count
through the array:
63
FEE 402: Electronics B
ORG 100H
MOV CH,0
MOV SI,0
MOV DI,8100H
Label1: MOV AH,[DI]
CMP AH,0
JNE Label2
INC CH
Label2: INC SI
INC DI
CMP SI,20H
JNE Label1
MOV [8200H],CH
INT 20H
The 8086 reserves the lower 1024 bytes of memory for an interrupt vector table. There is one
4-byte vector for each of the 256 possible interrupt/ exception numbers. When an interrupt or
exception occurs, the processor: (1) clears the interrupt flag in the flags register, (2) pushes the
flags register, CS, and IP (in that order), (3) loads IP and CS (in that order) from the appropriate
interrupt vector location, and (4) transfers control to that location.
For external interrupts (IRQ or NMI) the interrupt number is read from the data bus during an
interrupt acknowledge bus cycle. For internal interrupts (e.g. INT instruction) the interrupt
number is determined from the instruction.
The INT instruction allows a program to generate any of the 256 interrupts. This ―software
interrupt‖ is typically used to access operating system services.
The CLI and STI instructions clear/set the interrupt-enable bit in the flags register to disable/
enable external interrupts.
The IRET instruction pops the IP, CS and flags register values from the stack and thus returns
control to the instruction following the one where interrupt or exception occurred.
64
FEE 402: Electronics B
bit number stored in address 1002H, with the final 8-bit result being stored in the
address 1100H.
(b) Eight 16-bit numbers which are stored in successive offset addresses starting at
1200H are added together. Then an AND operation with the number 8888H is
performed on the 16-bit result and with the resulting number being stored in the offset
addresses 1400H and 1401H.
(c) A 16-bit number stored in the addresses 1200H and 1201H is subtracted from the 16-
bit number in the addresses 1300H and 1301H. Also, another 16-bit number stored in
the addresses 1400H and 1401H is subtracted from the 16-bit number in the addresses
1500H and 1501H. The results of both subtractions are added together and the final
result stored in the addresses 1600H and 1601H.
(d) The characters of the keyboard are read as they are typed and their ASCII values are
stored into the offset addresses beginning with 2000H, until a carriage return (ASCII
value ODH) is hit.
65
FEE 402: Electronics B
66
FEE 402: Electronics B
------------------------------------------------------
|Mnemonic |Description |
|------------------+----------------------------------
|LEA r,s |Load EA to register |
|LES r,s |Load pointer to ES |
|LOCK |Bus Lock prefix |
|LODS |Load memory at SI into AX |
|LOOP a |Loop CX times |
|LOOPNZ/LOOPNE a |Loop while Not Zero/Not Equal |
|LOOPZ/LOOPE a |Loop while Zero/Equal |
|MOV d,s |Move |
|MOVS |Move memory at SI to DI |
|MUL s |Multiply (unsigned) in AX(,DX) |
|NEG d |Negate |
|NOP |No Operation (= XCHG AX,AX) |
|NOT d |Logical NOT |
|OR d,s |Logical inclusive OR |
|OUT p,s |Output |
|POP d |Pop |
|POPF |Pop Flags |
|PUSH s |Push |
|PUSHF |Push Flags |
|RCL d,c |Rotate through Carry Left |
|RCR d,c |Rotate through Carry Right |
|REP/REPNE/REPNZ |Repeat/Repeat Not Equal/Not Zero |
|REPE/REPZ |Repeat Equal/Zero |
|RET (s) |Return from call |
|ROL d,c |Rotate Left |
|ROR d,c |Rotate Right |
|SAHF |Store AH into 8080 Flags |
|SAL/SAR d,c |Shift Arithmetic Left/Right |
|SBB d,s |Subtract with Borrow |
|SCAS |Scan memory at DI compared to AX |
|SEG r |Segment register |
|SHL d,c |Logical Shift Left |
|SHR d,c |Shift logical Right |
|STC |Set Carry |
|STD |Set Direction |
|STI |Set Interrupt |
|STOS |Store AX into memory at DI |
|SUB d,s |Subtract |
|TEST d,s |AND function to flags |
|WAIT |Wait |
|XCHG r(,d) |Exchange |
|XLAT |Translate byte to AL |
|XOR d,s |Logical Exclusive OR |
|------------------+---------+------------------------
67
FEE 402: Electronics B
Appendix
T2 – The or control signals are issued, is asserted and in the case of a Write, data
is put onto the data bus. The turns on the data bus buffers to connect the CPU to the
external data bus. The READY input to the CPU is sampled at the end of T2 and if
READY is LOW, a wait state TW (one or more) is inserted before T3 begins.
T3 – This clock period is provided to allow memory/IO devices to access data for a Write cycle.
If the bus cycle is a Read cycle, the data bus is sampled at the end of T3.
T4 – All bus signals are deactivated in preparation for the next cycle. For the Write cycle, the
trailing edge of the signal transfers data to the memory or IO device which activates
the system and cause a Write when returns to logic 1 level.
68