Microprocessor and Microcontroller
Microprocessor and Microcontroller
LECTURE NOTES
UGMIT RAYAGADA
Unit -1
Microprocessor and Microcomputer:
Microprocessor:
The processor on a single chip is called a Microprocessor which can process micro-instructions.
Instructions in the form of 0sand 1s are called micro-instructions. The microprocessor is the
CPU part of a microcomputer, and it is also available as a single integrated circuit. Thus as main
components, the microprocessor will have theControl Unit (CU) and the Arithmetic Logic Unit
(ALU) of a microcomputer. An example is Intel 8085 microprocessor. In addition to the
microprocessor features, a microcomputer will have the following additional features:
ROM/PROM/EPROM/EEPROM for storing program;
Microcomputer:
A microcomputer can be defined as a small sized, inexpensive, and limited capability computer.
It has the same architectural block structure that is present on a computer. Present-day
microcomputers are having smaller sizes. Nowadays, they are of the size of a notebook. But in
the coming days also their sizes will get more reduced as well. Due to their lower costs,
individuals can possess them as their personal computers. Because of mass production, they
are becoming still cheaper. Initially, in the earlier days, they were not very much powerful.
Their internal operations and instructions were very much limited and restricted. But at present
days, microcomputers have not only multiplied and divide instructions on unsigned and signed
numbers but are also capable of performing floating point arithmetic operations
Evolution of Microprocessor:
1. 4-bit Microprocessor: The first microprocessor (Intel 4004) was invented in 1971. It was
a 4-bit calculation device with a speed of 108 kHz. Since then, microprocessor power has
grown exponentially. It has 3200 PMOS transistors. It is a 4-bit device used in calculator.
2. 8-Bit microprocessor:
In 1972, Intel came out with the 8008 which is 8-bit. In 1974, Intel announced the 8080
followed by 8085 i.e a 8-bit processor 8085 processor has 8 bit ALU (Arithmetic Logic unit).
Similarly 8086 processor has 16 bit ALU. This had a larger instruction set then 8080. used NMOS
transistors, so it operated much faster than the 8008. The 8080 is referred to as a “Second
generation Microprocessor”.
Bus structure:
The simplest and most common way of interconnecting various parts of the computer. To
achieve a reasonable speed of operation, a computer must be organized so that all its units can
handle one full word of data at a given time.A group of lines that serve as a connecting port for
several devices is called a bus. In addition to the lines that carry the data, the bus must have
lines for address and control purpose. Simplest way to interconnect is to use the single bus as
shown
Since the bus can be used for only one transfer at a time, only two units can
actively use the bus at any given time. Bus control lines are used to arbitrate multiple
requests for use of one bus.
Address Bus:
The address bus is a group of 16 lines generally identified as A0 to A15.
The address bus is unidirectional: bits flow in one direction-from the MPU to peripheral
devices.
The MPU uses the address bus to perform the first function: identifying a peripheral or a
memory location.
Data bus:
The data bus is a group of eight lines used for data flow.
These lines are bi-directional - data flow in both directions between the MPU and
memory and peripheral devices.
The MPU uses the data bus to perform the second function: transferring binary
information.
The eight data lines enable the MPU to manipulate 8-bit data ranging from 00H to FFH
(28 = 256 numbers).
The largest number that can appear on the data bus is 11111111 i.e 0FFH.
Control Bus:
The control bus carries synchronization signals and providing timing signals.
The MPU generates specific control signals for every operation it performs. These
signals are used to identify a device type with which the MPU wants to communicate.
The 8085
microprocessor is an 8-bit processor available as a 40-pin IC package and uses +5 V for power. It
can run at a maximum frequency of 3 MHz. Its data bus width is 8-bit and address bus width is
16-bit, thus it can address 216 = 64 KB of memory. The internal architecture of 8085 is shown in
above figure.
Arithmetic and Logic Unit:
The ALU performs the actual numerical and logical operations such as Addition (ADD),
Subtraction (SUB), AND, OR etc. It uses data from memory and from Accumulator to perform
operations. The results of the arithmetic and logical operations are stored in the accumulator.
Registers :
The 8085 includes six registers, one accumulator and one flag register, as shown in Fig. 3. In
addition, it has two 16-bit registers: stack pointer and program counter. They are briefly
described as follows. The 8085 has six general-purpose registers to store 8-bit data; these are
identified as B, C, D, E, H and L. they can be combined as register pairs - BC, DE and HL to
perform some 16-bit operations. The programmer can use these registers to store or copy data
into the register by using data copy instructions.
Accumulator:
The accumulator is an 8-bit register that is a part of ALU. This register is used to store 8-bit
data and to perform arithmetic and logical operations. The result of an operation is stored in
the accumulator. The accumulator is also identified as register A.
Flag register:
The ALU includes five flip-flops, which are set or reset after an operation according to data
condition of the result in the accumulator and other registers. They are called Zero (Z),
Carry (CY), Sign (S), Parity (P) and Auxiliary Carry (AC) flags. Their bit positions in the
flag register are shown in Fig. 4. The microprocessor uses these flags to test data conditions.
Sign (S) flag: – If the MSB of the result of an operation is 1, this flag is set, otherwise
it is reset.
Zero (Z) flag:– If the result of an instruction is zero, this flag is set, otherwise reset.
Auxiliary Carry (AC ) flag:– If there is a carry out of bit 3 and into bit 4 resulting from
the execution of an arithmetic operation, it is set otherwise reset. This flag is used for BCD
operation and is not available to the programmer to change
the sequence of an instruction.
Carry (CY) flag:– If an instruction results in a carry (for addition operation) or borrow
(for subtraction or comparison) out of bit D7, then this flag is set, otherwise reset.
Parity (P) flag:– This flag is set when the result of an operation contains an even
number of 1’s and is reset otherwise.
Instruction Register/Decoder:
It is an 8-bit register that temporarily stores the current instruction of a program. Latest
instruction sent here from memory prior to execution. Decoder then takes instruction and
decodes or interprets the instruction. Decoded instruction then passed to next stage.
Control Unit :
Control Generates signals on data bus, address bus and control bus within microprocessor to
carry out the instruction, which has been decoded.
Data Bus: Data bus carries data in binary form between microprocessor and other external
units such as memory. It is used to transmit data i.e. information, results of arithmetic etc
between memory and the microprocessor. Data bus is bidirectional in nature. The data bus
width of 8085 microprocessor is 8-bit i.e. 28 combination of binary digits and are typically
identified as D0 – D7. Thus size of the data bus determines what arithmetic can be done. If only
8-bit wide then largest number is 11111111 (255 in decimal). Therefore, larger numbers have to
be broken down into chunks of 255. This slows microprocessor.
Address Bus: The address bus carries addresses and is one way bus from microprocessor to the
memory or other devices. 8085 microprocessor contain 16-bit address bus and are generally
identified as A0 - A15. The higher order address lines (A8 – A15) are unidirectional and the
lower order lines (A0 – A7) are multiplexed (time-shared) with the eight data bits (D0 – D7) and
hence, they are bidirectional.
Control Bus: Control bus are various lines which have specific functions for coordinating and
controlling microprocessor operations. The control bus carries control signals partly
unidirectional and partly bidirectional. The following control and status signals are used by 8085
processor:
ALE (output): Address Latch Enable is a pulse that is provided when an address appears on the
AD0 – AD7 lines, after which it becomes 0.
´ (active low output): The Read signal indicates that data are being read from the selected I/
RD
O or memory device and that they are available on the data bus.
WR´ (active low output): The Write signal indicates that data on the data bus are to be written
into a selected memory or I/O location.
IO/ Ḿ (output): It is a signal that distinguished between a memory operation and an I/O
operation. When IO/ Ḿ = 0 it is a memory operation and IO/ Ḿ = 1 it is an I/O operation.
S1 and S0 (output): These are status signals used to specify the type of operation being
performed; they are listed in Table ,
Properties:
It is a 8-bit microprocessor
Manufactured with N-MOS technology
40 pin IC package
It has 16-bit address bus and thus has 216 = 64 KB addressing capability.
Operate with 3 MHz single-phase clock
+5 V single power supply
The logic pin layout and signal groups of the 8085nmicroprocessor are shown in Fig. 6. All the
signals are classified into six groups:
Address bus
Data bus
Control & status signals
Power supply and frequency signals
Externally initiated signals
Serial I/O signals
RESET IN: When the signal on this pin is low, the PC is set to 0, the buses are tristated
and the processor is reset.
RESET OUT: This signal indicates that the processor is being reset. The signal can
be used to reset other devices.
READY: When this signal is low, the processor waits for an integral number of
clock cycles until it goes high.
HOLD: This signal indicates that a peripheral like DMA (direct memory access)
controller is requesting the use of address and data bus.
HLDA: This signal acknowledges the HOLD request.
INTR: Interrupt request is a general-purpose interrupt.
INTA : This is used to acknowledge an interrupt.
RST 7.5, RST 6.5, RST 5,5 – restart interrupt: These are vectored interrupts and
have highest priority than INTR interrupt.
TRAP: This is a non-maskable interrupt and has the highest priority.
General purpose registers can be used either All specif purpose registers has some special
data and address register. functions
´ , WR
The control and status signals are ALE, RD ´ , IO/ Ḿ , S0, S1 and READY.
´ is an interrupt
The interrupt signals are TRAP, RST 7.5, RST 6.5, RST 5.5, INTR. INTA
acknowledgement signal indicating that the processor has acknowledged an INTR
interrupt.
Serial I/O signals are SID and SOD
DMA signals are HOLD and HLDA
Reset signals are RESET ´ ∈¿ ¿ and RESET OUT.
ALE:
Pin 30 of 8085 is the ALE pin which stands for ‘Address Latch Enable’. ALE signal is used
to demultiplex the lower order address bus (AD0 – AD7). Pins 12 to 19 of 8085 are AD0 – AD7
which is the multiplexed address-data bus. Multiplexing is done to reduce the number of pins of
8085. Lower byte of address (A0 – A7) are available from AD0 – AD7 (pins 12 to 19) during T1 of
machine cycle. But the lower byte of address (A0 – A7), along with the upper byte A8 – A15
(pins 21 to 28) must be available during T2 and rest of the machine cycle to access memory
location or I/O ports. Now ALE signal goes high at the beginning of T1 of each machine cycle
and goes low at the end of T1 and remains low during the rest of the machine cycle. This high to
low transition of ALE signal at the end of T1 is used to latch the lower order address byte (A0 –
A7) by the latch IC 74LS373, so that the lower byte A0 – A7 is continued to be available till the
end of the machine cycle. The situation is explained in the following figure:
DMA:
DMA mode of data transfer is fastest and pins 39 and 38 (HOLD and HLDA) become active
only in this mode.
When DMA is required, the DMA controller IC (8257) sends a 1 to pin 39 of 8085. At
the end of the current instruction cycle of the microprocessor it issues a1 to pin 38 of
the controller. After this the bus control is totally taken over by the controller.
When 8085 is active and 8257 is idle, then the former is MASTER and the latter is
SLAVE, while the roles of 8085 and 8257 are reversed when 8085 is idle and 8257 becomes
active. mode of data transfer is fastest and pins 39 and 38 (HOLD and HLDA) become active
only in this mode.
When DMA is required, the DMA controller IC (8257) sends a 1 to pin 39 of 8085. At
the end of the current instruction cycle of the microprocessor it issues a1 to pin 38 of
the controller. After this the bus control is totally taken over by the controller.
When 8085 is active and 8257 is idle, then the former is MASTER and the latter is
SLAVE, while the roles of 8085 and 8257 are reversed when 8085 is idle and 8257 becomes
active.
IO/ Ḿ signal indicates whether I/O or memory operation is being carried out. A high on
this signal indicates I/O operation while a low indicates memory operation. S0 and S1
indicate the type of machine cycle in progress.
RESET ´ ∈¿ ¿ It is an input signal which is active when its status is low. When this pin is
low, the following occurs:
The program counter is set to zero (0000H).
Interrupt enable and HLDA F/Fs are resetted.
All the buses are tri-stated.
Internal registers of 8085 are affected in a random manner.
RESET OUT:
When this signal is high, the processor is being reset. This signal is synchronised to the
processor clock and is used to reset other devices which need resetting
STACK:
Stack is a reserved of memory used to keep track of a program's internal operations, including
functions, return addresses, passed parameters ed parameters, etc. A stack is
usually maintained as a "last in, first out" (LIFO) data s ) data structure, so that the last item
added to the structure is the first item used. Sometimes it is useful to have region of memory
for temporary storage, which does not have to be allocated named variables. When we use
subroutines and interrupts it will be essential to have such a storage region. Such region is
called a Stack
The Stack Pointer register will hold the address of the top location of the stack. And the
program counter is a register always it will hold the address of the memory location from where
the next instruction for execution will have to be fetched. The complete programmer's view of
8085 is shown in the following figure
SP is a special purpose 16-bit register. It contains a memory address. Suppose SP contents are
FC78H, then the 8085 interprets it as follows
Memory locations FC78H, FC79H, ..., FFFFH are having useful information. In other words, these
locations are treated as filled locations. Memory locations FC77H, FC76H, ..., 0000H are not
having any useful information. In other words, these locations are treated as empty locations.
On a stack, we can perform two operations. PUSH and POP. In case of PUSH operation, the SP
register gets decreased by 1 and new data item used to insert on to the top of the stack. On the
other hand, in case of POP operation, the data item will have to be deleted from the top of the
stack and the SP register will get increased by the value of 1.
Thus, the contents of SP specify the top most useful location in the stack. This is known as
stacktop .In other words, it indicates the memory location with the smallest address having
useful information. This is pictorially represented in the following figure –
8085 Interrupts:
Six signals are associated with interrupt logic. There are five interrupt request pins through
which the 8085 may be interrupted. These are TRAP, RST 7.5, RST 6.5, RST 5.5, and INTR. The
first four interrupts (TRAP, RST 7.5, RST 6.5, RST 5.5) are supported by hardware implemented
vectoring, i.e. the locations of Interrupt Service Routines(ISR) in memory for these interrupts
are fixed and automatic vectoring to the location takes place whenever any of these four
interrupts is recognized. The interrupt service routine locations for these interrupts are as
follows:
The last interrupt INTR is a general-purpose interrupt The interrupt request on this line is
acknowledged by the 8085 on the output line. On receiving the acknowledgement, the
interrupting device places on the data bus the address of Interrupt Service Routine (ISR) in the
memory. The microprocessor executes the ISR to service the interrupt. There are two ways by
which the interrupting device can send the ISR address. It can input one byte code of one of the
“RST n” instructions (RST 0 to RST 7). These are other wise known as software interrrupts . The
microprocessor will execute this instruction to branch to ISR at 8 bit address location. The RST
n is a single byte subroutine call instruction. Another way is to input a 3-byte code for “CALL
addr16” instruction. On receiving this 3-byte code, the microprocessor will execute this
instruction to branch to ISR at addr16.
TRAP interrupt is the non-maskable interrupt for 8085. It means that if an interrupt
comes via TRAP, 8085 will have to recognise the interrupt.
Interrupts INTR, RST 5.5 and RST 6.5 are level-triggered, i.e. a high signal should be present on
these lines when the interrupt is recognized. In other words, if a device sends an interrupt pulse
of short duration on any of these lines, it is possible that this interrupt request is ignored totally
owing to the reason that the interrupt pulse may not be present when these lines are scanned
by the microprocessor for the interrupt request. RST 7.5 is edge-triggered and TRAP is level- as
well as edge-triggered. Both are associated with internal latches which store the interrupt
requests as and when the interrupt signal on these lines goes from low to high. TRAP is
normally reserved for power failure or some other highest priority interrupt. Thus, it will be
desirable to protect this pin from spurious signals. It is because of this reason that TRAP is level
as well as edge-triggered.
The interrupts of 8085 have their priorities fixed—TRAP interrupt has the highest priority,
followed by RST 7.5, RST 6.5, RST 5.5 and lastly INTR.
Interrupt masking:
An interrupt which can be disabled by software means, is called a maskable interrupt. Thus an
interrupt which cannot be masked is an unmaskable interrupt. Various interrupts can be
enabled or disabled selectively by masking. There is an interrupt mask register in the interrupt
control system Various bits of this register are used for enabling or disabling of interrupts. The
first 3 bits—bit 0, bit 1, and bit 2—are devoted to interrupt masks for RST 5.5, RST 6.5 and RST
7.5 interrupts respectively. If any of these bits is set to 1, the particular interrupt is disabled,
otherwise it is enabled. Bit 3 is the overriding bit for bit 0, bit 1, and bit 2. This bit is called the
Mask Set Enable (MSE). Only when this bit is set to 1, the mask bits for interrupts RST 5.5, RST
6.5, and RST 7.5 are taken into consideration. Bit 4 is used to reset the RST 7.5 latch. When this
bit is set to 1, the latch associated. with the RST 7.5 interrupt is reset regardless of whether RST
7.5 is masked or not. Bit 5 is not used. Bit 6 and bit 7 are devoted to serial output; these bits are
discussed under Section 3.2.7 on Serial Input– Output. All the interrupts can also be
reset/disabled through the signal at RESET ´ ∈¿ ¿ the pin.
To mask any interrupt, the user will set the bit pattern in accumulator (ACC) according to his
need and execute the SIM (Set Interrupt Mask) instruction. As an example, if we want to enable
RST 7.5 and RST 5.5 and disable RST 6.5, the bit pattern will be
0 0 0 1 1 0 1 0 = 1AH
The user will transfer 1AH to ACC and execute the SIM instruction. The user will often wish to
know about the status of the various interrupts, i.e. whether they are enabled and whether any
interrupt is pending on these lines. This is facilitated by the RIM (Read Interrupt Mask)
instruction. This instruction when executed, loads the accumulatorwith a byte—the structure of
which is shown inbelow Figure. Bit 0, bit 1, and bit 2 of this byte indicate interrupt masks set
for interrupts RST 5.5, RST 6.5, and RST 7.5 respectively. Bit 3 shows whether the interrupt
system is enabled or disabled. The next three bits indicate whether any interrupt is pending on
RST 5.5, RST 6.5, and RST 7.5 respectively. Bit7 is reserved for serial input data. The user will
examine the status of various interrupts and then take appropriate decision to mask or unmask
any interrupt.
Microprocessor and Microcontroller
LECTURE NOTES
UGMIT RAYAGADA
UNIT-2
Instruction Word Size in Microprocessor
The 8085 instruction set is classified into 3 categories by considering the length of the
instructions. In 8085, the length is measured in terms of “byte” rather then “word” because
8085 microprocessor has 8-bit data bus. Three types of instruction are: 1-byte instruction, 2-
byte instruction, and 3-byte instruction.
1. One-byte instructions:
In 1-byte instruction, the opcode and the operand of an instruction are represented in
one byte.
Example-1
2. Two-byte instructions
Two-byte instruction is the type of instruction in which the first 8 bits indicates the
opcode and the next 8 bits indicates the operand.
3. Three-byte instructions
Three-byte instruction is the type of instruction in which the first 8 bits indicates the
opcode and the next two bytes specify the 16-bit address. The low-order address is
represented in second byte and the high-order address is represented in the third byte.
8085 Addressing Modes
The process of specifying the data to be operated on by the instruction is called addressing. The
various formats for specifying operands are called addressing modes. The 8085 has the
following five types of addressing:
I. Immediate addressing
II. Memory direct addressing
III. Register direct addressing
IV. Indirect addressing
V. Implicit addressing
Immediate Addressing:
In this mode, the operand given in the instruction - a byte or word – transfers to the destination
register or memory location.
Ex: MVI A, 9AH
The operand is a part of the instruction.
The operand is stored in the register mentioned in the instruction.
Implicit Addressing
In this addressing mode the data itself specifies the data to be operated upon.
Ex: CMA
The instruction complements the content of the accumulator. No specific data or operand is
mentioned in the instruction
8085 Instruction Set
DATA TRANSFER INSTRUCTIONS
General purpose registers: The 8086 microprocessor has a total of fourteen registers that are
accessible to the programmer. It is divided into four groups. They are:
Four General purpose registers
Four Index/Pointer registers
Four Segment registers
Two Other register
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.
Count register consists of two 8-bit registers CL and CH, which can be combined together and
used as a 16-bit register CX. When combined, CL register contains the low order byte of the
word, and CH contains the high-order byte. Count register can be used in Loop, shift/rotate
instructions and as a counter in string manipulation.
Data register consists of two 8-bit registers DL and DH, which can be combined together and
used as a 16-bit register DX. When combined, DL register contains the low order byte of the
word, and DH contains the high-order byte. Data register can be used as a port number in I/O
operations. In integer 32-bit multiply and divide instruction the DX register contains high-order
word of the initial or resulting number.
Index or Pointer Registers
These registers can also be called as Special Purpose registers.
Stack Pointer (SP) is a 16-bit register pointing to program stack, i.e. it is used to hold the
address of the top of stack. The stack is maintained as a LIFO with its bottom at the start of the
stack segment (specified by the SS segment register).Unlike the SP register, the BP can be used
to specify the offset of other program segments. Base Pointer (BP) is a 16-bit register pointing
to data in stack segment. It is usually used by subroutines to locate variables that were passed
on the stack by a calling program. BP register is usually used for based, based indexed or
register indirect addressing. Source Index (SI) is a 16-bit register. SI is used for indexed, based
indexed and register indirect addressing, as well as a source data address in string manipulation
instructions. Used in conjunction with the DS register to point to data locations in the data
segment. Destination Index (DI) is a 16-bit register. Used in conjunction with the ES register in
string operations. DI is used for indexed, based indexed and register indirect addressing, as well
as a destination data address in string manipulation instructions. In short, Destination Index and
SI Source Index registers are used to hold address.
Segment Registers
Most of the registers contain data/instruction offsets within 64 KB memory segment. There are
four different 64 KB segments for instructions, stack, data and extra data. To specify where in 1
MB of processor memory these 4 segments are located the processor uses four segment
registers.
Code segment (CS) is a 16-bit register containing address of 64 KB segment with processor
instructions. The processor uses CS segment for all accesses to instructions referenced by
instruction pointer (IP) register. CS register cannot be changed directly. The CS register is
automatically updated during far jump, far call and far return instructions.
Stack segment (SS) is a 16-bit register containing address of 64KB segment with program stack.
By default, the processor assumes that all data referenced by the stack pointer (SP) and base
pointer (BP) registers is located in the stack segment. SS register can be changed directly using
POP instruction.
Data segment (DS) is a 16-bit register containing address of 64KB segment with program data.
By default, the processor assumes that all data referenced by general registers (AX, BX, CX, DX)
and index register (SI, DI) is located in the data segment. DS register can be changed directly
using POP and LDS instructions.
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.
Instruction Pointer (IP) is a 16-bit register. This is a crucially important register which is used to
control which instruction the CPU executes. The IP, or program counter, is used to store the
memory location of the next instruction to be executed. The CPU checks the program counter
to ascertain which instruction to carry out next. It then updates the program counter to point to
the next instruction. Thus the program counter will always point to the next instruction to be
executed.
Flag Register contains a group of status bits called flags that indicate the status of the CPU or
the result of arithmetic operations. There are two types of flags:
1. The status flags which reflect the result of executing an instruction. The programmer cannot
set/reset these flags directly.
2. The control flags enable or disable certain CPU operations. The programmer can set/reset
these bits to control the CPU's operation.
Nine individual bits of the status register are used as control flags (3 of them) and status flags (6
of them).The remaining 7 are not used.
A flag can only take on the values 0 and 1. We say a flag is set if it has the value 1.The status
flags are used to record specific characteristics of arithmetic and of logical instructions.
1. The Direction Flag (D): Affects the direction of moving data blocks by such instructions as
MOVS, CMPS and SCAS. The flag values are 0 = up and 1 = down and can be set/reset by the
STD (set D) and CLD (clear D) instructions.
2. The Interrupt Flag (I): Dictates whether or not system interrupts can occur. Interrupts are
actions initiated by hardware block such as input devices that will interrupt the normal
execution of programs. The flag values are 0 = disable interrupts or 1 = enable interrupts and
can be manipulated by the CLI (clear I) and STI (set I) instructions.
3. The Trap Flag (T): Determines whether or not the CPU is halted after the execution of each
instruction. When this flag is set (i.e. = 1), the programmer can single step through his program
to debug any errors. When this flag = 0 this feature is off. This flag can be set by the INT 3
instruction.
3. The Sign Flag (S): This flag is set when the result of an arithmetic or logic operation is
negative. This flag is a copy of the MSB of the result (i.e. the sign bit). A value of 1 means
negative and 0 = positive.
4. The Zero Flag (Z): This flag is set when the result of an arithmetic or logic operation is equal
to zero. A value of 1 means the result is zero and a value of 0 means the result is not zero.
5. The Auxiliary Carry Flag (A): This flag is set when an operation causes a carry from bit 3 to bit
4 (or a borrow from bit 4 to bit 3) of an operand. A value of 1 = carry and 0 = no carry.
6. The Parity Flag (P): This flags reflects the number of 1s in the result of an operation. If the
number of 1s is even its value = 1 and if the number of 1s is odd then its value = 0.
Architecture of 8086:
8086 has two blocks Bus Interface Unit (BIU) and Execution Unit (EU).
The BIU performs all bus operations such as instruction fetching, reading and writing operands
for memory and calculating the addresses of the memory operands. The instruction bytes are
transferred to the instruction queue.
EU executes instructions from the instruction system byte queue.
Both units operate asynchronously to give the 8086 an overlapping instruction fetch and
execution mechanism which is called as Pipelining. This results in efficient use of the system bus
and system performance.
BIU contains Instruction queue, Segment registers, Instruction pointer, Address adder.
EU contains Control circuitry, Instruction decoder, ALU, Pointer and Index register, Flag register.
Execution Unit:
1. The Execution unit is responsible for decoding and executing all instructions.
2. The EU extracts instructions from the top of the queue in the BIU, decodes them,
generates operands if necessary, passes them to the BIU and requests it to perform the
read or write bus cycles to memory or I/O and perform the operation specified by the
instruction on the operands.
3. 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.
4. If the queue is empty, the EU waits for the next instruction byte to be fetched and
shifted to top of the queue.
5. When the EU executes a branch or jump instruction, it transfers control to a location
corresponding to another set of sequential instructions.
6. Whenever this happens, the BIU automatically resets the queue and then begins to
fetch instructions from this new location to refill the queue.
General Bus Operation
1. The 8086 has a combined address and data bus commonly referred as a time
multiplexed address and data bus.
2. The main reason behind multiplexing address and data over the same pins is the
maximum utilization of processor pins and it facilitates the use of 40 pin standard
DIP package.
3. The bus can be demultiplexed using a few latches and transceivers, when ever
required.
4. Basically, all the processor bus cycles consist of at least four clock cycles. These are
referred to as T1, T2, T3, and T4. The address is transmitted by the processor during
T1. It is present on the bus only for one cycle.
5. The negative edge of this ALE pulse is used to separate the address and the data or
status information. In maximum mode, the status lines S0, S1 and S2 are used to
indicate the type of operation.
6. Status bits S3 to S7 are multiplexed with higher order address bits and the BHE
signal. Address is valid during T1 while status bits S3 to S7 are valid during T2
through T4.
Maximum mode:
1. In the maximum mode, the 8086 is operated by strapping the MN/MX pin to ground.
2. In this mode, the processor derives the status signal S2, S1, S0. Another chip called bus
controller derives the control signal using this status information.
3. In the maximum mode, there may be more than one microprocessor in the system
configuration.
Minimum mode:
1. In a minimum mode 8086 system, the microprocessor 8086 is operated in minimum
mode by strapping its MN/MX pin to logic 1.
2. In this mode, all the control signals are given out by the microprocessor chip itself.
3. There is a single microprocessor in the minimum mode system.
AD15-AD0: These are the time multiplexed memory I/O address and data lines.
Address remains on the lines during T1 state, while the data is available on the
data bus during T2, T3, Tw and T4. These lines are active high and float to a
tristate during interrupt acknowledge and local bus hold acknowledge cycles.
5. A19/S6, A18/S5, A17/S4, and A16/S3: These are the time multiplexed address and
status lines.
During T1 these are the most significant address lines for memory operations.
During I/O operations, these lines are low.
During memory or I/O operations, status information is available on those lines
for T2, T3, Tw and T4.
The status of the interrupt enable flag bit is updated at the beginning of each
clock cycle.
The S4 and S3 combine indicate which segment registers is presently being used
for memory accesses as in below figure
These lines float to tri-state off during the local bus hold acknowledge.
The status line S6 is always low. The address bit is separated from the status bit
using latches controlled by the ALE signal.
6. BHE/S7: The bus high enable is used to indicate the transfer of data over the higher
order (D15-D8) data bus as shown in table. It goes low for the data transfer over D15-D8
and is used to derive chip selects of odd address memory bank or peripherals. BHE is
low during T1 for read, write and interrupt acknowledge cycles, whenever a byte is to be
transferred on higher byte of data bus. The status information is available during T2, T3
and T4. The signal is active low and tristated during hold. It is low during T1 for the first
pulse of the interrupt acknowledge cycle.
7. RD – Read: This signal on low indicates the peripheral that the processor is performing
memory or I/O read operation. RD is active low and shows the state for T2, T3, Tw of
any read cycle. The signal remains tristated during the hold acknowledge.
8. READY: This is the acknowledgement from the slow device or memory that they have
completed the data transfer. The signal made available by the devices is synchronized
by the 8284A clock generator to provide ready input to the 8086. the signal is active
high.
9. INTR-Interrupt Request: This is a triggered input. This is sampled during the last clock
cycles of each instruction to determine the availability of the request. If any interrupt
request is pending, the processor enters the interrupt acknowledge cycle. This can be
internally masked by resulting the interrupt enable flag. This signal is active high and
internally synchronized.
10. TEST: This input is examined by a ‘WAIT’ instruction. If the TEST pin goes low, execution
will continue, else the processor remains in an idle state. The input is synchronized
internally during each clock cycle on leading edge of clock.
11. CLK- Clock Input: The clock input provides the basic timing for processor operation and
bus control activity. It’s an asymmetric square wave with 33% duty cycle.
The following pin functions are for the minimum mode operation of 8086.
1. M/IO – Memory/IO: This is a status line logically equivalent to S2 in maximum mode.
When it is low, it indicates the CPU is having an I/O operation, and when it is high, it
indicates that the CPU is having a memory operation. This line becomes active high in
the previous T4 and remains active till final T4 of the current cycle. It is tristated during
local bus “hold acknowledge “.
2. INTA – Interrupt Acknowledge: This signal is used as a read strobe for interrupt
acknowledge cycles. i.e. when it goes low, the processor has accepted the interrupt.
3. ALE – Address Latch Enable: This output signal indicates the availability of the valid
address on the address/data lines, and is connected to latch enable input of latches.
This signal is active high and is never tristated.
4. DT/R – Data Transmit/Receive: This output is used to decide the direction of data flow
through the transceivers (bidirectional buffers). When the processor sends out data,
this signal is high and when the processor is receiving data, this signal is low.
5.
6. DEN – Data Enable: This signal indicates the availability of valid data over the
address/data lines. It is used to enable the transceivers (bidirectional buffers) to
separate the data from the multiplexed address/data signal. It is active from the middle
of T2 until the middle of T4. This is tristated during ‘hold acknowledge’ cycle.
7. HOLD, HLDA- Acknowledge: When the HOLD line goes high; it indicates to the processor
that another master is requesting the bus access. The processor, after receiving the
HOLD request, issues the hold acknowledge signal on HLDA pin, in the middle of the
next clock cycle after completing the current bus cycle.
8. At the same time, the processor floats the local bus and control lines. When the
processor detects the HOLD line low, it lowers the HLDA signal. HOLD is an
asynchronous input, and is should be externally synchronized. If the DMA request is
made while the CPU is performing a memory or I/O cycle, it will release the local bus
during T4 provided
The request occurs on or before T2 state of the current cycle.
The current cycle is not operating over the lower byte of a word.
The current cycle is not the first acknowledge of an interrupt acknowledge
sequence.
A Lock instruction is not being executed.
The following pin functions are applicable for maximum mode operation of 8086:
1. S2, S1, and S0 – Status Lines: These are the status lines which reflect the type of
operation, being carried out by the processor. These become activity during T4 of the
previous cycle and active during T1 and T2 of the current bus cycles.
2. LOCK: This output pin indicates that other system bus master will be prevented from
gaining the system bus, while the LOCK signal is low. The LOCK signal is activated by the
‘LOCK’ prefix instruction and remains active until the completion of the next instruction.
When the CPU is executing a critical instruction which requires the system bus, the LOCK
prefix instruction ensures that other processors connected in the system will not gain
the control of the bus.
3. The 8086, while executing the prefixed instruction, asserts the bus lock signal output,
which may be connected to an external bus controller. By prefetching the instruction,
there is a considerable speeding up in instruction execution in 8086. This is known as
instruction pipelining.
At the starting the CS: IP is loaded with the required address from which the execution is to be
started. Initially, the queue will be empty and the microprocessor starts a fetch operation to
bring one byte (the first byte) of instruction code, if the CS: IP address is odd or two bytes at a
time, if the CS: IP address is even. The first byte is a complete opcode in case of some
instruction (one byte opcode instruction.
The first byte is a complete opcode in case of some instruction (one byte opcode instruction)
and is a part of opcode, in case of some instructions (two byte opcode instructions), the
remaining part of code lie in second byte.
The second byte is then decoded in continuation with the first byte to decide the instruction
length and the number of subsequent bytes to be treated as instruction data. The queue is
updated after every byte is read from the queue but the fetch cycle is initiated by BIU only if at
least two bytes of the queue are empty and the EU may be concurrently executing the fetched
instructions.
The next byte after the instruction is completed is again the first opcode byte of the next
instruction. A similar procedure is repeated till the complete execution of the program. The
fetch operation of the next instruction is overlapped with the execution of the current
instruction. As in the architecture, there are two separate units, namely Execution unit and Bus
interface unit.
While the execution unit is busy in executing an instruction, after it is completely decoded, the
bus interface unit may be fetching the bytes of the next instruction from memory, depending
upon the queue status.
RQ/GT0, RQ/GT1 – Request/Grant: These pins are used by the other local bus master
in maximum mode, to force the processor to release the local bus at the end of the processor
current bus cycle.
Each of the pin is bidirectional with RQ/GT0 having higher priority than RQ/GT1. RQ/GT pins
have internal pull-up resistors and may be left unconnected. Request/Grant sequence is as
follows.
1. A pulse of one clock wide from another bus master requests the bus access to 8086.
2. During T4(current) or T1(next) clock cycle, a pulse one clock wide from 8086 to the
requesting master, indicates that the 8086 has allowed the local bus to float and that it
will enter the ‘hold acknowledge’ state at next cycle. The CPU bus interface unit is likely
to be disconnected from the local bus of the system.
3. A one clock wide pulse from another master indicates to the 8086 that the hold request
is about to end and the 8086 may regain control of the local bus at the next clock cycle.
Thus each master to master exchange of the local bus is a sequence of 3 pulses. There
must be at least one dead clock cycle after each bus exchange. The request and grant
pulses are active low. For the bus request those are received while 8086 is performing
memory or I/O cycle, the granting of the bus is governed by the rules as in case of HOLD
and HLDA in minimum mode.
Write Cycle Timing Diagram for Minimum Mode:
The working of the minimum mode configuration system can be better described in terms of
the timing diagrams rather than qualitatively describing the operations.
The opcode fetch and read cycles are similar. Hence the timing diagram can be
categorized in two parts, the first is the timing diagram for read cycle and the second is
the timing diagram for write cycle.
The read cycle begins in T1 with the assertion of address latch enable (ALE) signal and
also M / IO signal. During the negative going edge of this signal, the valid address is
latched on the local bus.
The BHE and A0 signals address low, high or both bytes. From T1 to T4 , the M/IO signal
indicates a memory or I/O operation.
At T2, the address is removed from the local bus and is sent to the output. The bus is
then tristated. The read (RD) control signal is also activated in T2.
The read (RD) signal causes the address device to enable its data bus drivers. After RD
goes low, the valid data is available on the data bus. The addressed device will drive the
READY line high. When the processor returns the read signal to high level, the addressed
device will again tristate its bus drivers.
A write cycle also begins with the assertion of ALE and the emission of the address. The
M/IO signal is again asserted to indicate a memory or I/O operation. In T2, after sending
the address in T1, the processor sends the data to be written to the addressed location.
The data remains on the bus until middle of T4 state. The WR becomes active at the
beginning of T2 (unlike RD is somewhat delayed in T2 to provide time for floating).
The BHE and A0 signals are used to select the proper byte or bytes of memory or I/O
word to be read or write.
Hold Response sequence: The HOLD pin is checked at leading edge of each clock pulse. If
it is received active by the processor before T4 of the previous cycle or during T1 state of
the current cycle, the CPU activates HLDA in the next clock cycle and for succeeding bus
cycles, the bus will be given to another requesting master.
The control of the bus is not regained by the processor until the requesting master does
not drop the HOLD pin low. When the request is dropped by the requesting master, the
HLDA is dropped by the processor at the trailing edge of the next clock.
Maximum Mode 8086 System
1. In the maximum mode, the 8086 is operated by strapping the MN/MX pin to ground.
2. In this mode, the processor derives the status signal S2, S1, S0. Another chip called bus
controller derives the control signal using this status information.
3. In the maximum mode, there may be more than one microprocessor in the system
configuration.
4. The components in the system are same as in the minimum mode system.
5. The basic function of the bus controller chip IC8288 is to derive control signals like RD
and WR (for memory and I/O devices), DEN, DT/R, ALE etc. using the information by the
processor on the status lines.
6. The bus controller chip has input lines S2, S1, S0 and CLK. These inputs to 8288 are
driven by CPU.
7. It derives the outputs ALE, DEN, DT/R, MRDC, MWTC, AMWC, IORC, IOWC and AIOWC.
The AEN, IOB and CEN pins are especially useful for multiprocessor systems.
8. AEN and IOB are generally grounded. CEN pin is usually tied to +5V. The significance of
the MCE/PDEN output depends upon the status of the IOB pin.
9. If IOB is grounded, it acts as master cascade enable to control cascade 8259A, else it
acts as peripheral data enable used in the multiple bus configurations.
10. INTA pin used to issue two interrupt acknowledge pulses to the interrupt controller or
to an interrupting device.
11. IORC, IOWC are I/O read command and I/O write command signals respectively.
12. These signals enable an IO interface to read or write the data from or to the address
port.
13. The MRDC, MWTC are memory read command and memory write command signals
respectively and may be used as memory read or write signals.
14. All these command signals instructs the memory to accept or send data from or to the
bus.
15. For both of these write command signals, the advanced signals namely AIOWC and
AMWTC are available.
16. Here the only difference between in timing diagram between minimum mode and
maximum mode is the status signals used and the available control and advanced
command signals.
17. R0, S1, S2 are set at the beginning of bus cycle.8288 bus controller will output a pulse as
on the ALE and apply a required signal to its DT / R pin during T1.
18. In T2, 8288 will set DEN=1 thus enabling transceivers, and for an input it will activate
MRDC or IORC. These signals are activated until T4. For an output, the AMWC or AIOWC
is activated from T2 to T4 and MWTC or IOWC is activated from T3 to T4.
19. The status bit S0 to S2 remains active until T3 and become passive during T3 and T4.
20. If reader input is not activated before T3, wait state will be inserted between T3 and T4.
Interrupts:
The meaning of ‘interrupts’ is to break the sequence of operation. While the CPU is executing a
program, on ‘interrupt’ breaks the normal sequence of execution of instructions, diverts its
execution to some other program called Interrupt Service Routine (ISR).After executing ISR , the
control is transferred back again to the main program. Interrupt processing is an alternative to
polling.
Need for Interrupt: Interrupts are particularly useful when interfacing I/O devices that provide
or require data at relatively low data transfer rate.
Types of Interrupts: There are two types of Interrupts in 8086. They are:
(i)Hardware Interrupts
(ii)Software Interrupts
One pin that acknowledges, INTA, the interrupt requested on INTR. INTR and NMI
INTR is a maskable hardware interrupt. The interrupt can be enabled/disabled using STI/CLI
instructions or using more complicated method of updating the FLAGS register with the help
of the POP instruction.
When an interrupt occurs, the processor stores FLAGS register into stack, disables further
interrupts, fetches from the bus one byte representing interrupt type, and jumps to interrupt
processing routine address of which is stored in location 4 * <interrupt type>. Interrupt
processing routine should return with the IRET instruction.
NMI is a non-maskable interrupt. Interrupt is processed in the same way as the INTR
interrupt. Interrupt type of the NMI is 2, i.e. the address of the NMI processing routine is
stored in location 0008h. This interrupt has higher priority than the maskable interrupt.
INT <interrupt number> instruction - any one interrupt from available 256 interrupts.
INTO instruction - interrupt on overflow
Single-step interrupt - generated if the TF flag is set. This is a type 1 interrupt. When the
CPU processes this interrupt it clears TF flag before calling the interrupt processing
routine.
Processor exceptions: Divide Error (Type 0), Unused Opcode (type 6) and Escape opcode
(type 7).
Software interrupt processing is the same as for the hardware interrupts.
Ex: INT n (Software Instructions)
Control is provided through:
1. IF and TF flag bits
2. IRET and IRETD
The addressing modes for Sequential and control flow instructions are explained as follows.
2. Intrasegment
Intrasegment direct
Intrasegment indirect
1. Intersegment direct:
In this mode, the address to which the control is to be transferred is in a different segment. This
addressing mode provides a means of branching from one code segment to another code
segment. Here, the CS and IP of the destination address are specified directly in the instruction.
Example: JMP 5000H, 2000H;
Jump to effective address 2000H in segment 5000H.
2. Intersegment indirect:
In this mode, the address to which the control is to be transferred lies in a different segment
and it is passed to the instruction indirectly, i.e. contents of a memory block containing four
bytes, i.e. IP(LSB), IP(MSB), CS(LSB) and CS(MSB) sequentially. The starting address of the
memory block may be referred using any of the addressing modes, except immediate mode.
Example: JMP [2000H].
Jump to an address in the other segment specified at effective address 2000H in DS.
MOV CL, [2000H] ; Copy the 8 bit content of the memory location, at a displacement of 2000H
from data segment base to the CL register
MOV [589H], BX ; Copy the 16 bit content of BX register on to the memory location, which at a
displacement of 589H from the data segment base.
2. 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
Example:-
PUSH CX ; Decrements SP by 2, copy content of CX to the stack (figure shows execution of this
instruction)
PUSH DS ; Decrement SP by 2 and copy DS to stack
3. 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.
The execution pattern is similar to that of the PUSH instruction.
Example:
POP CX ; Copy a word from the top of the stack to CX and increment SP by 2.
4. IN & OUT instructions
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.
Example:
IN AL, 0F8H; Copy a byte from the port 0F8H to AL
MOV DX, 30F8H; Copy port address in DX
IN AL, DX; Move 8 bit data from 30F8H port
IN AX, DX; Move 16 bit data from 30F8H port
OUT 047H, AL; Copy contents of AL to 8 bit port 047H
MOV DX, 30F8H; Copy port address in DX
OUT DX, AL; Move 8 bit data to the 30F8H port
OUT DX, AX; Move 16 bit data to the 30F8H port
5. XCHG instruction
The XCHG instruction exchanges contents of the destination and source. Here destination and
source can be register and register or register and memory location, but XCHG cannot
interchange the value of 2 memory locations.
General Format
Example:
XCHG BX, CX; exchange word in CX with the word in BX
XCHG AL, CL; exchange byte in CL with the byte in AL
XCHG AX, SUM[BX];here physical address, which is DS+SUM+[BX]. The content at physical
address and the content of AX are interchanged.
1. 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:
ADD Destination, Source
Example:
ADD AL, 0FH ; Add the immediate content, 0FH to the content of AL and store the result in AL
ADD AX, BX ; AX <= AX+BX
ADD AX,0100H – IMMEDIATE
ADD AX,BX – REGISTER
ADD AX,[SI] – REGISTER INDIRECT OR INDEXED
ADD AX, [5000H] – DIRECT
ADD [5000H], 0100H – IMMEDIATE
ADD 0100H – DESTINATION AX (IMPLICT)
3. SUB instruction
SUB instruction is used to subtract 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:
SUB Destination, Source
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
Example:
SBB AX,0100H – IMMEDIATE (DESTINATION AX)
SBB AX,BX – REGISTER
SBB AX,[5000H] – DIRECT
SBB [5000H], 0100H – IMMEDIATE
5. CMP: COMPARE
The instruction compares the source operand, which may be a register or an immediate data or
a memory location, with a destination operand that may be a register or a memory location. For
comparison, it subtracts the source operand from the destination operand but does not store
the result anywhere. The flags are affected depending upon the result of the subtraction. If
both of the operands are equal, zero flag is set. If the source operand is greater than the
destination operand,carry flag is set or else, carry flag is reset. The examples of this instruction
are as follows:
Example:
CMP BX,0100H – IMMEDIATE
CMP AX,0100H – IMMEDIATE
7. 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:
AND Destination, Source
Example:
AND BL, AL ; suppose BL=1000 0110 and AL = 1100 1010 then after the operation BL would be
BL= 1000 0010.
AND CX, AX ; CX <= CX AND AX AND CL, 08 ; CL<= CL AND (0000 1000)
8. 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
Example:
OR BL, AL ; suppose BL=1000 0110 and AL = 1100 1010 then after the operation BL would be
BL= 1100 1110.
9. NOT instruction
The NOT instruction complements (inverts) the contents of an operand register or a memory
location, bit by bit. The examples are as follows:
Example:
NOT AX (BEFORE AX= (1011)2= (B)16 ; AFTER EXECUTION AX= (0100)2= (4)16)
XOR AX,BX
Rotate on the other hand rotates the information in a register or memory either from one end
to another or through the carry flag.
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.
All flags are affected.
General Format:
SAL/SHL destination, count
Example:
MOV BL, B7H;BL is made B7H
SAL BL, 1; shift the content of BL register one place to left.
Before execution
cy B7 B6 B5 B4 B3 B2 B1 B0
0 1 0 1 1 0 1 1 1
After execution
cy B7 B6 B5 B4 B3 B2 B1 B0
1 0 1 1 0 1 1 1 0
1. 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.
BEFORE:
B7 B6 B5 B4 B3 B2 B1 B0 CY
1 0 1 1 0 1 1 1 0
AFTER:
B7 B6 B5 B4 B3 B2 B1 B0 CY
0 1 0 1 1 0 1 1 1
2. 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.
All flags are affected
3. 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.
All flags are affected
General Format: ROR destination, count
Example:
MOV BL, B7H ; BL is made B7H
ROR BL, 1 ; shift the content of BL register one place to the right.
4. 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.
1. CALL instruction
The CALL instruction is used to transfer execution to a subprogram or procedure. There are two
types of CALL instructions, near and far.
A near CALL is a call to a procedure which is in the same code segment as the CALL instruction.
8086 when encountered a near call, it decrements the SP by 2 and copies the offset of the next
instruction after the CALL on the stack. It loads the IP with the offset of the procedure then to
start the execution of the procedure. A far CALL is the call to a procedure residing in a different
segment. Here value of CS and offset of the next instruction both are backed up in the stack.
And then branches to the procedure by changing the content of CS with the segment base
containing procedure and IP with the offset of the first instruction of the procedure
Example:
Near call
CALL PRO ; PRO is the name of the procedure
CALL CX ; Here CX contains the offset of the first instruction of
the procedure, that is replaces the content of IP with the content of CX
Far call
CALL DWORD PTR[8X] ; New values for CS and IP are fetched from four
memory locations in the DS. The new value for CS is fetched from [8X] and [8X+1], the new IP is
fetched from [8X+2] and [8X+3].
2. RET instruction
RET instruction will return execution from a procedure to the next instruction after the CALL
instruction in the calling program. If it was a near call, then IP is replaced with the value at the
top of the stack, if it had been a far call, then another POP of the stack is required. This second
popped data from the stack is put in the CS, thus resuming the execution of the calling
program. RET instruction can be followed by a number, to specify the parameters passed.
3. JMP instruction
This is also called as unconditional jump instruction, because the processor jumps to the
specified location rather than the instruction after the JMP instruction. Jumps can be short
jumps when the target address is in the same segment as the JMP instruction or far jumps
when it is in a different segment.
4. Conditional Jump :
Conditional jumps are always short jumps in 8086. Here jump is done only if the condition
specified is true/false. If the condition is not satisfied, then the execution proceeds in the
normal way.
1. HLT instruction
The HLT instruction will cause the 8086 microprocessor to fetching and executing instructions.
The 8086 will enter a halt state. The processor gets out of this Halt signal upon an interrupt
signal in INTR pin/NMI pin or a reset signal on RESET input.
2. WAIT instruction
When this instruction is executed, the 8086 enters into an idle state. This idle state is continued
till a high is received on the TEST input pin or a valid interrupt signal is received. Wait affects no
flags. It generally is used to synchronize the 8086 with a peripheral device(s).
3. ESC instruction
This instruction is used to pass instruction to a coprocessor like 8087. There is a 6 bit instruction
for the coprocessor embedded in the ESC instruction. In most cases the 8086 treats ESC and a
NOP, but in some cases the 8086 will access data items in memory for the coprocessor
4. LOCK instruction
In multiprocessor environments, the different microprocessors share a system bus, which is
needed to access external devices like disks. LOCK Instruction is given as prefix in the case when
a processor needs exclusive access of the system bus for a particular instruction. It affects no
flags
Example:
LOCK XCHG SEMAPHORE, AL :The XCHG instruction requires two
bus accesses. The lock prefix prevents another processor from taking control of thesystem bus
between the 2 accesses
5. NOP instruction
At the end of NOP instruction, no operation is done other than the fetching and decoding of the
instruction. It takes 3 clock cycles. NOP is used to fill in time delays or to provide space for
instructions while trouble shooting. NOP affects no flags
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.
String Instructions:
1. 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.
When direction flag is 0, SI and DI are incremented and when it is 1, SI and DI are decremented.
MOVS affect no flags. MOVSB is used for byte sized movements while MOVSW is for word sized.
Example:
CLD ; clear the direction flag to auto increment SI and DI MOV AX, 0000H ;
MOV DS, AX ; initialize data segment register to 0 MOV ES, AX ; initialize extra segment register
to 0 MOV SI, 2000H ; Load the offset of the string1 in SI MOV DI, 2400H ; Load the offset of the
string2 in DI MOV CX, 04H ; load length of the string in CX
REP MOVSB ; decrement CX and MOVSB until CX will be 0
2. REP/REPE/REP2/REPNE/REPNZ
REP is used with string instruction; it repeats an instruction until the specified condition
becomes false.
3. LODS/LODSB/LODSW
This instruction copies a byte from a string location pointed to by SI to AL or a word from a
string location pointed to by SI to AX.LODS does not affect any flags. LODSB copies byte and
LODSW copies word.
STOS/STOSB/STOSW
The STOS instruction is used to store a byte/word contained in AL/AX to
the offset contained in the DI register. STOS does not affect any flags. After copying the content
DI is automatically incremented or decremented, based on the value of direction flag.
5. 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.
ASSEMBLER DIRECTIVES:
There are some instructions in the assembly language program which are not a part of
processor instruction set. These instructions are instructions to the assembler, linker and
loader. These are referred to as pseudo-operations or as assembler directives. The assembler
directives enable us to control the way in which a program assembles and lists. They act during
the assembly of a program and do not generate any executable machine code.
There are many specialized assembler directives. Let us see the commonly used assembler
directive in 8086 assembly language programming.
1. ASSUME:
It is used to tell the name of the logical segment the assembler to use for a specified segment.
E.g.: ASSUME CS: CODE tells that the instructions for a program are in a logical segment named
CODE.
2. DB -Define Byte:
The DB directive is used to reserve byte or bytes of memory locations in the available memory.
While preparing the EXE file, this directive directs the assembler to allocate the specified
number of memory bytes to the said data type that may be a constant, variable, string, etc.
Another option of this directive also initializes the reserved memory bytes with the ASCII codes
of the characters specified as a string. The following examples show how the DB directive is
used for different purposes.
3. DD -Define Double word - used to declare a double word type variable or to reserve memory
locations that can be accessed as double word.
4. DQ -Define Quad word
This directive is used to direct the assembler to reserve 4 words (8 bytes) of memory for the
specified variable and may initialize it with the specified values.
5. DT -Define Ten Bytes:
The DT directive directs the assembler to define the specified variable requiring 10-bytes for its
storage and initialize the 10-bytes with the specified values. The directive may be used in case
of variables facing heavy numerical calculations, generally processed by numerical processors.
6. DW -Define Word:
The DW directives serves the same purposes as the DB directive, but it now makes the
assembler reserve the number of memory words (16-bit) instead of bytes. Some examples are
given to explain this directive
. 7. END-End of Program:
The END directive marks the end of an assembly language program. When the assembler comes
across this END directive, it ignores the source lines available later on. Hence, it should be
ensured that the END statement should be the last statement in the file and should not appear
in between. Also, no useful program statement should lie in the file, after the END statement
8. ENDP-End Procedure - Used along with the name of the procedure to indicate the end of a
procedure.
E.g.: SQUARE_ROOT PROC: start of procedure SQUARE_ROOT ENDP: End of procedure
9. EQU-Equate - Used to give a name to some value or symbol. Each time the assembler finds
the given name in the program, it will replace the name with the vale.
E.g.: CORRECTION_FACTOR EQU 03H MOV AL, CORRECTION_FACTOR
10. LABEL- Used to give a name to the current value in the location counter.
This directive is followed by a term that specifies the type you want associated with that name.
E.g: ENTRY_POINT LABEL FAR
NEXT: MOV AL, BL
11. OFFSET- Used to determine the offset or displacement of a named data item or procedure
from the start of the segment which contains it.
E.g.: MOV BX, OFFSET PRICES
12. ORG- The location counter is set to 0000 when the assembler starts reading a segment. The
ORG directive allows setting a desired value at any point in the program.
E.g.: ORG 2000H
ARRAY SUM
.MODEL SMALL
.DATA
ARRAY DB 12H, 24H, 26H, 63H, 25H, 86H, 2FH, 33H, 10H, 35H
SUM DW 0
.CODE
START:MOV AX, @DATA
MOV DS, AX
MOV CL, 10
XOR DI, DI
LEA BX, ARRAY
BACK: MOV AL, [BX+DI]
MOV AH, 00H
ADD SUM, AX
INC DI
DEC CL
JNZ BACK
END START
ASCIITOHEX
DATA SEGMENT
A DB 41H
R DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV AL,A
SUB AL,30H
CMP AL,39H
JBE L1
SUB AL,7H
L1: MOV R,AL
INT 3H
CODE ENDS
END START
8BMUL
DATA SEGMENT
A1 DB 25H
A2 DB 25H
A3 DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS:DATA START:MOV AX,DATA
MOV DS,AX
MOV AL,A1
MOV BL,A2
MUL BL
MOV A3,AL
MOV AX,4C00H
INT 21H
CODE ENDS
END START
16BIT MUL
DATA SEGMENT
A1 DW 1000H
A2 DW 1000H
A3 DW ?
A4 DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS:DATA
START:MOV AX,DATA
MOV DS,AX
MOV AX,A1
MOV BX,A2
MUL BX
MOV A3,DX
MOV A4,AX
MOV AX,4C00H
INT 21H
CODE ENDS
END START
HEX TO ASCII
DATA SEGMENT
A DB 08H
C DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START: MOV AX,DATA
MOV DS,AX
MOV AL,A
ADD AL,30H
CMP AL,39H
JBE L1
ADD AL,7H
L1: MOV C,AL
INT 3H
CODE ENDS
END START
The 16-bit microcontroller performs greater precision and performance as compared to 8-bit.
For example 8 bit microcontrollers can only use 8 bits, resulting in a final range of 0×00 – 0xFF
(0-255) for every cycle. In contrast, 16 bit microcontrollers with its 16 bit data width has a range
of 0×0000 – 0xFFFF (0-65535) for every cycle. A longer timer most extreme worth can likely
prove to be useful in certain applications and circuits. It can automatically operate on two 16 bit
numbers. Some examples of 16-bit microcontroller are 16-bit MCUs are extended 8051XA,
PIC2x, Intel 8096 and Motorola MC68HC12 families.
CISC & RISC processor
Each instruction takes different clock cycles to execute, so in CISC, the pipeline is not
possible. (to execute multiple instructions in parallel).
RISC Advantages
The instructions are simple/primitive, and they execute fast.
RISC uses registers to access operands, which are much faster to read.
Instruction length is fixed, especially the opcode so opcode decoding and register
operand accessing can occur simultaneously.
The source code is long, but it is flexible enough to be optimized.
Because each instruction takes one clock cycle, instruction pipelining is possible. This
improves processor operating speed.
RISC processors are more responsive to interrupts because interrupts are checked
between each small instruction/every machine cycle.
Architecture of 8051Microcontroller
Some of the features that have made the 8051 popular are:
64 KB on chip program memory.
128 bytes on chip data memory (RAM).
4 register banks.
Basic Pins:
Pin 9: PIN 9 is the reset pin which is used reset the microcontroller‘s internal registers and ports
upon starting up. (Pin should be held high for 2 machine cycles.)
Pins 18 & 19: The 8051 has a built-in oscillator amplifier hence we need to only connect a
crystal at these pins to provide clock pulses to the circuit.
Pin 40 and 20: Pins 40 and 20 are VCC and ground respectively. The 8051 chip needs +5V
500mA to function properly, although there are lower powered versions like the Atmel 2051
which is a scaled down version of the 8051 which runs on +3V.
Pins 29, 30 & 31: As described in the features of the 8051, this chip contains a built-in flash
memory. In order to program this we need to supply a voltage of +12V at pin 31. If external
memory is connected then PIN 31, also called EA/VPP, should be connected to ground to
indicate the presence of external memory. PIN 30 is called ALE (address latch enable), which is
used when multiple memory chips are connected to the controller and only one of them needs
to be selected. We will deal with this in depth in the later chapters. PIN 29 is called PSEN. This is
"program store enable". In order to use the external memory it is required to provide the low
voltage (0) on both PSEN and EA pins.
Ports:
There are 4 8-bit ports: P0, P1, P2 and P3.
PORT P1 (Pins 1 to 8): The port P1 is a general purpose input/output port which can be used for
a variety of interfacing tasks. The other ports P0, P2 and P3 have dual roles or additional
functions associated with them based upon the context of their usage.
PORT P3 (Pins 10 to 17): PORT P3 acts as a normal IO port, but Port P3 has additional functions
such as, serial transmit and receive pins, 2 external interrupt pins, 2 external counter inputs,
read and write pins for memory access.
PORT P2 (pins 21 to 28): PORT P2 can also be used as a general purpose 8 bit port when no
external memory is present, but if external memory access is required then PORT P2 will act as
an address bus in conjunction with PORT P0 to access external memory. PORT P2 acts as A8-
A15, as can be seen from ABOVE fig
PORT P0 (pins 32 to 39) PORT P0 can be used as a general purpose 8 bit port when no external
memory is present, but if external memory access is required then PORT P0 acts as a
multiplexed address and data bus that can be used to access external memory in conjunction
with PORT P2. P0 acts as AD0-AD7, as can be seen from above fig
Oscillator Circuits
The 8051 requires the existence of an external oscillator circuit. The oscillator circuit usually
runs around 12MHz, although the 8051 (depending on which specific model) is capable of
running at a maximum of 40MHz. Each machine cycle in the 8051 is 12 clock cycles, giving an
effective cycle rate at 1MHz (for a 12MHz clock) to 3.33MHz (for the maximum 40MHz clock).
The 8051 has an internal clock circuit, hence acrystal of proper frequency can be connected
directly to these two pins. The frequency range is 3.5–12 MHz. An external oscillator can also be
connected instead of a crystal. In this case, the XTAL1 pin is grounded and the oscillator signal is
given to the XTAL2 pin
´ : This pin gives out active low output pulses. This signal is used for fetching the data from
PSEN
the external program memory. A pulse is generated after every six clock cycles. This is used as a
read signal for reading from the external program memory. If the data to be fetched I inside the
´
chip itself, then is PSENnot generated. This signal is also not generated during the external
data memory operation.
´ /VPP : This pin, if connected to 5 V, will indicate to the processor that the 4 KB of the
EA
program memory within the chip should be used. Hence addresses 0000H to 0FFFH will be
within the chip. Once the address exceeds this, i.e. 1000H to FFFFH, the external program
memory is accessed. This pin, if pulled low, will inform the processor that the on chip
ROM is not being used. Hence the entire 64 KB will be external to the chip. So, depending on
whether a designer is using the internal program memory or not, he/she will have to connect
the pin to either 5 V or ground. This pin is also used while programming the 8751 as
programming supply (VPP) voltage (21 V).
RST/VPD: A high on this pin, for more than 24 oscillator cycles, will reset the chip. VPD may be
used to supply power to the internal RAM during power failure or power down modes.
Memory Organisation
The 8051 can access up to 64 KB of program memory and 64 KB of external data memory and
also the internal data RAM locations. Figure below shows the memory mapping in this chip
SPECIAL FUNCTION REGISTERS:
All the resources in the 8051 can be accessed through special function registers maintained in
the internal data memory. The resources like timers would require setting the modes and
controlling them. Similarly, serial data input would require a serial data buffer as well as the
control. The 8051 offers 4 ports which can be used for various purposes like input/output,
memory interface, and so on. The functions of these resources can also be programmed
through special function registersWe shall now describe all the special function registers
available in the 8051. As the name itself suggests, these registers have some special functions
like controlling the timer/counter, enabling interrupts, controlling the serial port operations,
etc. There are 21 special function registers in the 8051. Some special function registers are bit
addressable. The various special function registers are shown below:
ACC......................–.............Accumulator*
B...........................–.............B Register*
PSW......................–.............Program Status Word*
SP..........................–.............Stack Pointer
DPTR (Low)........–.............Data Pointer Low
DPTR (High).......–.............Data Pointer High
P0.........................–.............Port 0*
P1.........................–.............Port 1*
P2.........................–.............Port 2*
P3.........................–.............Port 3*
IP..........................–.............Interrupt Priority*
IE.........................–.............Interrupt Enable*
TMOD.................–.............Timer/Counter Mode
TCON..................–.............Timer/Counter Control*
TH0......................–.............(Timer/Counter) 0 High
TL0......................–.............(Timer/Counter) 0 Low
TH1......................–.............(Timer/Counter) 1 High
TL1......................–.............(Timer/Counter) 1 Low
SCON..................–.............Serial Control*
SBUF...................–.............Serial Data Buffer
PCON..................–.............Power Control
Accumulator: ACC (also called A register) is the accumulator. Thereare many instructions that
use the accumulator as the destination. In nearly all arithmetic operations, ACC is used as one
of the operands and after the operation, the result is also stored in the ACC.
B Register: This register is mainly used for multiply and divide operations in which this register
acts as one of the operands and after the operations a part of the result is stored in this
register. This register otherwise is just like a scratch pad, i.e. a temporary register.
Stack Pointer: The stack pointer in the 8051 is an 8-bit wide register. This pointer can point to
any location in the internal data RAM, i.e. locations from 0–127. When the chip is reset, this
register is initialized to 07H. During PUSH and CALL instructions the stack pointer is first
incremented and then the data is stored in the stack. That is, if initially (SP) = 20H, then the first
bytes of the data will be stored at the 21H address.
Data Pointer: The data pointer (DPTR) is a 16-bit register, consisting of a high byte (DPH) and a
low byte (DPL). This register normally contains a 16-bit address
Serial Data Buffer (SBUF): This register holds the data that has to be transmitted through the
serial port and also holds the data that is received. This register is interconnected to two 8-bit
shift registers. When the data is written into the SBUF, it is loaded in the transmit shift register
and hence the process of moving a data byte into the SBUF starts the transmission process.
During reception, the data coming to the 8051 is clocked into the receive shift register and once
all the 8 bits of data or a frame is received, it is transferred to the SBUF.
Control and status registers: All the special function registers (like IP, IE, TMOD, TCON, PCON,
SCON, etc.) that are used for controlling the internal resources or to see the status of these
resources come under this category. These registers contain the control and status bits of the
interrupt systems, timers, and serial port operations.
THE LIST OF ALL SFRS
8051 TIMER AND COUNTER
8051 Microcontroller Interrupts
There are five interrupt sources for the 8051, which means that they can recognize 5 different
events that can interrupt regular program execution. Each interrupt can be enabled or disabled
by setting bits in the IE register. Also, as seen from the picture below the whole interrupt
system can be disabled by clearing bit EA from the same register. Namely, if the bits IT0 and IT1
stored in the TCON register areset, program interrupt will occur on changing logic state from 1
to 0, (only at the moment). If these bits are cleared, the same signal will generate interrupt
request and it will be continuously executed as far as the pins are held low.
Interrupt Priorities
It is not possible to predict when an interrupt will be required. For that reason, if several
interrupts
are enabled. It can easily occur that while one of them is in progress, another one is requested.
In such situation, there is a priority list making the microcontroller know whether to continue
operating or meet a new interrupt request.
The priority list consists of 3 levels:
1. Reset! The absolute master of the situation. If an request for Reset omits, everything is
stopped and the microcontroller starts operating from the beginning.
Handling Interrupt
Once some of interrupt requests arrives, everything occurs according to the following order:
1. Instruction in progress is ended
2. The address of the next instruction to execute is pushed on the stack
3. Depending on which interrupt is requested, one of 5 vectors (addresses) is written to the
program counter in accordance to the following table:
4. The appropriate subroutines processing interrupts should be located at these addresses.
Instead of them, there are usually jump instructions indicating the location where the
subroutines reside.
5. When interrupt routine is executed, the address of the next instruction to execute is poped
from the stack to the program counter and interrupted program continues operating
from where it left off.
ADDRESSING MODES
The instructions of 8051 may be classified based on the source or destination type
1. Register addressing
2. Direct addressing
3. Register-indirect addressing
4. Immediate addressing
5. Base register plus index register indirect addressing
Register Addressing
Register addressing permits access to eight registers (R0–R7) of the register bank. There are
four banks of eight registers. One of the four banks is selected by a 2-bit field in PSW (Program
Status Word). Other registers used are A, B, AB and DPTR.
For example,
Hence, after the execution of this instruction, the registers Rn and A have the original contents
of register Rn.
Direct Addressing
In direct addressing, the address of the operand is specified in the instruction. Direct addressing
has operands as byte or bit. Direct addressing of byte provides operation on one of the
following.
2. 128 bits subset of special function register address space (80H to FFH)
Indirect Addressing
In this addressing, the address of the operand is not specified directly. Instead, the address of
the operand is specified as the contents of the register mentioned in the instruction. For
example, in the instruction MOV A, @Ri, the contents of register Ri give the address of the
location from where the operand is picked up. The above instruction causes the operand to be
picked up from the mentioned location for transfer to register A. The operation sequence has
been explained in Figure for the instruction MOV A, @R0.
This type of instruction is useful when the same set of operations is performed on different
data sets stored in memory. In the present case either R0 or R1 may be used for accessing the
locations within the 256 byte locations. However, register indirect addressing is also used for
accessing external data memory. The 16-bit pointer (DTPR) can be used for accessing any
location within the full 64 KB memory space.
Immediate Addressing
In this case the operand on which the operation has to be performed according to the
instruction, is specified in the instruction itself. For example, the instruction ADD A, #data, adds
the 8-bit data specified in the instruction to the contents of register A and the result is placed in
register A itself .
Unsigned Addition
Unsigned numbers make use of the carry flag to detect when the reset an ADD operation is a
number larger than FFh. If the carry is set to 1 after an ADD, then the carry can be added to a
higher order byte. So that the sum is not lost,
Signed Addition
Signed numbers may be added two ways additions of like signed number and addition of unlike
signed numbers. If unlike signed numbers are added then it is not possible for the result to be
larger than – 128d or +127d, and the sign of the result will always be corrected.
ADD A, <src-bytes>
Function: ADD
Description: ADD adds the byte variables indicated to the accumulator, leaving the result in
the accumulator. The carry and auxiliary carry flags are set, respectively. If there is carry out
from bit-7 or bit-3, and cleared otherwise. When adding unsigned integers, the carry flag
indicates an overflow occurred.
When adding signed integers, OV indicates a negative number produced as the sum of two
positive operands or a positive sum from two negative operands.
Four source operand addressing modes are allowed:
Register, direct, register-direct, immediate.
byte: 1 OR 2
cycle: 1
Example: The accumulator holds 89H (10001001B) and register 0 holds 95H (10010101B) .The
instruction,
ADD A, R0
Will leave 1E (00011110B) in the accumulator with the AC flag cleared and both the carry flag
and OV set to 1.
ADD A, direct
Operation: (A) ← (A) + (direct)
Example: ADD A, 20H: Adds contents of A and memory whose address is 20H and store
result in A.
2 bytes instruction
1 machine cycle
ADD A, @Ri
Operation: (A) ← (A) + ((Ri))
Example: ADD A, @R2; Adds contents of A and memory whose address is given by register
R2, and stores result in A.
1 byte instruction
1 machine cycle
ADD A, #data
Operation: (A) ← (A) + #data
Example: ADD A, # 20H; Adds the contents of A and 20H.
2 bytes instruction
1 machine cycle
BYTE: 1 OR 2
1 machine cycle
ADDC A, R0
Will leave 6EH (01101110B) in the accumulator with AC cleared and both the carry flag and
or set to 1.
ADDC A, Rn
Operation: (A) ← (A) + (C) + (Rn)
1 byte instruction
1 machine cycle
ADDC A, direct
Operation: (A) ← (A) + (C) + (direct)
Example: ADDC A, 20H; adds the contents of A, memory location whose address is 20H and
the carry flag and stores result in A.
2 bytes instruction
1 machine cycle
ADDC A, @Ri
Operation: (A) ← (A) + (C) + ((Ri))
Example: ADDC A, @R2; Adds the contents of A, memory location whose address is given
by register R2 and the carry flag and stores result in the A.
1 byte instruction
1 machine cycle
ADDC A, #data
Operation: (A) ← (A) + (C) + #data
Example: ADDC A, #20H; Adds the contents of A and carry flag and 20H and stores result
in A.
2 bytes instruction
1 machine cycle
SUBB A, <src-byte>
Function: Subtract with borrow.
Description: SUBB subtracts the indicated variable and the carry flag together from the
accumulator, leaving the result in the accumulator, SUBB sets the carry (borrow) flag if a
borrow is needed for bit-7 and clears otherwise. If C was set before executing a SUBB
instruction, this indicates that a borrow was needed for the previous step in a multiple
precision subtraction. So, the carry is subtracted from the accumulator along with the source
operand1. AC is set if a borrow is needed for bit 3 and cleared otherwise. OV is set if a borrow is
needed into bit-6, but Not into bit-7 or into bit but not bit-6.
When subtracting signed integers OV indicates a negative number produced when a negative
value is subtracted from a positive value or a positive result when a positive number is
subtracted from a negative number. The source operand allows four addressing modes:
register, direct, register-indirect, or immediate.
SUBB A, Rn
Operation: (A) ← (A) – (C) – (Rn)
Example: SUBB A, R3; Subtracts contents of R3 and carry together from A and stores results
in A.
1 byte instruction
1 machine cycle
SUBB A, direct
Operation: (A) ← (A) – (C) – (direct)
Example: SUBB A, 20H; Subtracts the contents of memory location 20H and carry together
from A and stores result in A.
2 bytes instruction
1 machine cycle
SUBB A, @Ri
Operation: (A) ← (A) – (C) – ((Ri))
Example: SUBB A, @R2; subtracts the contents of memory location whose address is given
by R2 and carry together from A and stores result in A.
1 byte instruction
1 machine cycle
SUBB A, #data
Operation: (A) ← (A) – (C) - #data
Example: SUBB A, #20H; Subtracts 20H from A and stores result in A.
2 bytes instruction
1 machine cycle
INC Rn
Operation: (A) ← (A) + 1
Rn → 5F
INC direct
Operation: INC
(direct) ← (direct) + 1
Add a 1 to content of the direct memory address. Assume initially the internal memory
location 25H contains data 60H. After executing INC 25H, the location will have the updated
value 26H.
2 bytes instruction.
1 machine cycle.
INC @Ri
Operation: ((Ri)) ← ((Ri)) +1
Example: INC @ R1; increment contents of memory location whose address is given by
register R1 by 1.
Example: Registers DPH and DPL contain 12H and FEH respectively. The instruction
sequence,
INC DPTR
INC DPTR
INC DPTR
Will change DPH and DPL to 13H and 01H
Operation: (DPTR) ← (DPTR) + 1
1 byte instruction.
2 machine cycles.
DEC byte
Function: Decrement
Description: The variable indicate is decremented by 1. An original value of 00H will
underflow to 0FFH. No flags are affected. Four operand addressing modes are allowed:
accumulator, register-direct, register-indirect.
Note: Register0 contains 7FH (01111111B). Internal RAM locations 7EH and 7FH contain
00H and 40H respectively. The instruction sequence,
DEC @R0
DEC R0
DEC @R0
Will leave register 0 set to 7EH and internal RAM locations 7EH & 7FH and 7FH set to 0FFH
and 3FH.
DEC A
Operation: (A) ← (A)–1
Subtract a 1from accumulator
Let us assume A = 23H, after executing the instruction DEC A, there will contain 22H.
1 byte instruction.
1 machine cycle.
DEC Rn
Operation: (Rn) ← (Rn)–1
Example: DEC R3; decrements the contents of R3 by 1
1 byte instruction.
1 machine cycle
DEC direct
Operation: (direct) ← (direct) –1
Example: DEC 20H; decrements the contents of memory location whose address is 20H
by 1.
2 bytes instruction.
1 machine cycle
DEC @Rn
Operation: ((R1)) ← ((Ri)) – 1
Example: DEC @R2; decrements the contents of memory location whose address is given by
register R2 by 1.
1 byte instruction.
1 machine cycle.
Note:
1. No math flags are affected.
2. All 8-bit address contents overflow FFh to 00h.
3. DPTR is 16-bit; DPTR overflows from FFFFh to 0000h.
4. The 8-bit address contents underflow from 00h to FFh.
5. There is no DEC DPTR to match the INC DPTR.
MUL AB
Function: Multiply
Description: MUL AB the unsigned eight bit integers in the accumulators and register B. The
low-order byte of the sixteen-bit product is left in the accumulator and the high-order byte in B.
If the product is greater than 255(FFH) the overflow flag is set; otherwise it is cleared. The carry
flag is always cleared.
Example: Originally, the accumulator holds the value 80(50H). Register B holds the value
160(0A0H).
The instruction,
MUL AB
Will give the product 12,800(3200H), 80B is changed to 32H (00110010B) and the accumulator
is cleared. The overflow flag is set, carry is cleared.
MOV A, #12
MOV B, #05
MUL AB
;A = 5A; B = 00
1 byte instruction.
4 machine cycles.
DIV AB
Function: Divide
Description: DIV AB divides the unsigned eight bit integer in the accumulator by the unsigned
eight-bit integer in register B. The accumulator receives the integer part of the quotient;
register B receives the integer remainder. The carry and or flags will be cleared.
Exception if B had originally contained 00H, the values returned in the accumulator and
B register will be undefined and the overflow flag will be set. The carry flag is cleared in any
case.
Logical OR
ORL destination, source; ORL does a bitwise "OR" operation between source and destination,
leaving the resulting value in destination. The value in source is not affected. " OR " instruction
logically OR the bits of source and destination.
ORL A,#DATA ORL A, Rn
ORL A,DIRECT ORL A,@Ri
ORL DIRECT,A ORL DIRECT, #DATA
Logical Ex-OR
XRL destination, source: XRL does a bitwise "EX-OR" operation between source and
destination, leaving the resulting value in destination. The value in source is not affected.
" XRL" instruction logically EX-OR the bits of source and destination.
XRL A,#DATA XRL A,Rn
XRL A,DIRECT XRL A,@Ri
XRL DIRECT,A XRL DIRECT, #DATA
Logical NOT
CPL complements operand, leaving the result in operand. If operand is a single bit then the
state of the bit will be reversed. If operand is the Accumulator then all the bits in the
Accumulator will be reversed.
CPL A, CPL C, CPL bit address
SWAP A – Swap the upper nibble and lower nibble of A
Rotate Instructions
RR A
This instruction is rotate right the accumulator. Its operation is illustrated below. Each bit is
shifted one location to the right, with bit 0 going to bit 7.
RL A
Rotate left the accumulator. Each bit is shifted one location to the left, with bit 7 going to bit 0
RRC A
Rotate right through the carry. Each bit is shifted one location to the right, with bit 0 going into
the carry bit in the PSW, while the carry was at goes into bit 7
RLC A
Rotate left through the carry. Each bit is shifted one location to the left, with bit 7 going into the
carry bit in the PSW, while the carry goes into bit 0.
Relative Jump
Jump that replaces the PC (program counter) content with a new address that is greater than
(the address following the jump instruction by 127 or less) or less than (the address following
the jump by 128 or less) is called a relative jump. Schematically, the relative jump can be shown
as follows: -
The advantages of the relative jump are as follows:-
1. Only 1 byte of jump address needs to be specified in the 2's complement form, ie. For
jumping ahead, the range is 0 to 127 and for jumping back, the range is -1 to -128.
2. Specifying only one byte reduces the size of the instruction and speeds up program
execution.
3. The program with relative jumps can be relocated without reassembling to generate
absolute jump addresses.
In 8051, 64 kbyte of program memory space is divided into 32 pages of 2 kbyte each. The
hexadecimal addresses of the pages are given as follows:-
It can be seen that the upper 5bits of the program counter (PC) hold the page number and the
lower
11bits of the PC hold the address within that page. Thus, an absolute address is formed by
takingpage numbers of the instruction (from the program counter) following the jump and
attaching the specified 11bits to it to form the 16-bit address.
Example: -
LCALL <address 16>
LJMP <address 16>
JMP @A+DPTR
Another classification of jump instructions is
1. Unconditional Jump
2. Conditional Jump
1. The unconditional jump is a jump in which control is transferred unconditionally to the target
location.
a. LJMP (long jump). This is a 3-byte instruction. First byte is the op-code and second and third
bytes represent the 16-bit target address which is any memory location from 0000 to FFFFH
eg: LJMP 3000H
b. AJMP: this causes unconditional branch to the indicated address, by loading the 11 bit
address to
0 -10 bits of the program counter. The destination must be therefore within the same 2K
blocks.
c. SJMP (short jump). This is a 2-byte instruction. First byte is the op-code and second byte is
the relative target address, 00 to FFH (forward +127 and backward -128 bytes from the current
PC value). To calculate the target address of a short jump, the second byte is added to the PC
value which is address of the instruction immediately below the jump
JB bit, rel ; jump if the direct bit is set to the relative address specified.
JNB bit, rel ; jump if the direct bit is clear to the relative address specified.
JBC bit, rel ; jump if the direct bit is set to the relative address specified and then clear the bit.
a. During execution of SCALL, [PC] = [PC]+2; (if address where LCALL resides is say, 0x8549;
during execution of this instruction [PC] = 8549h + 2h = 854Bh
b. [SP]=[SP]+1; (if SP contains default value 07, then SP increments and [SP]=08
c. [[SP]] = [PC7-0]; (lower byte of PC content ie., 4B will be stored in memory location 08.
d. [SP]=[SP]+1; (SP increments again and [SP]=09)
e. [[SP]] = [PC15-8]; (higher byte of PC content ie., 85 will be stored in memory location 09.
With these the address (0x854B) which was in PC is stored in stack
f. [PC10-0]= address (11 bit); the new address of subroutine is loaded to PC. No flags are
affected.
RET instruction
RET instruction pops top two contents from the stack and load it to PC.
g. [PC15-8] = [[SP]] ;content of current top of the stack will be moved to higher byte of PC.
h. [SP]=[SP]-1; (SP decrements)
i. [PC7-0] = [[SP]] ;content of bottom of the stack will be moved to lower byte of PC.
j. [SP]=[SP]-1; (SP decrements again)
PORT 0:
Port 0 has 8 pins P0.0, P0.1, P0.2, P0.3, P0.4, P0.5, P0.6, and P0.7, pins 39, 38, 37, 36, 35, 34, 33,
32. This port can be used for input or output. Each pin of the port 0 must be connected
externally to a 10K Ohm pull-up resistor, to use Port 0 as both input or output ports. This is
because the Port 0 [P0] is an open drain. This is not the case in other ports P1, P2 and P3. 'Open
drain' is a term used for MOS chips, as 'open collector' is used for TTL chips. To use Port 0 for
both input and output, have to connect Port 0 to pull-up resistors. When external pull-up
resistors connected upon reset, Port 0 is configures as output port.
Port 0 can be used to configures for both data and address. The Port 0 is also designated as AD0
- AD7. When connecting an 8051 to an external memory, port 0 provides both address and
data. The 8051 multiplexes address and data through Port 0 to save pins. Address latch enable
[ALE] indicates if Port 0 has address or data. When ALE=0, it provides data D0 - D7, but when
ALE = 1, it has address A0 - A7. With the help of 74LS373 latch, ALE is used for demultiplexing
address and data.
PORT 1:
Port 1 has a total of 8 pins. P1.0, P1.1, P1.2, P.13, P1.4, P1.5, P1.6, and P1.7. Port 1 can be use
as an input or output. As compares to Port 0, this port does not need any pull-up resistors. Port
1 already has internally connected pull-up resistors. Port 1 is configured as a output port when
reset.
Example # 1:
Lets examine the code, which will continuously send Output to Port 1 the alternating values
55H and AAH
In Example # 2 above, the Port 1 is configures as input port by writing 1's to it, then data is
received from that port and saved in R3, R4 and R5
PORT 2:
Port 2 also has total of 8 pins. P2.0, P2.1, P2.2, P2.3, P2.4, P2.5, P2.6, and P2.7. Port 2 can be
used for input or output port. To make Port 2 as an input, the Port 2 must be programmed by
writing 1's to all bits.
In Example # 3 above, the code will send out continuously to Port 2 an alternating values 55H
and AAH to toggle the bits or Port 2 continuously.
The Role of Port 2 as an Input Port:
The Port 2 must be programmed by writing 1's to all the port 2 bits to make Port 2 as an input
port.
Example #4:
In Example # 4 above, the Port 2 is configures as input port by writing all 1's, then data is
received from that port and is sent to Port 1 continuously.
The Dual Role or Port 2:
Port 2 has dual role. Port 2 is also designated as A8 - A15. This indicates that Port 2 has a dual
function. An 8031 microcontroller is capable of accessing 64K bytes of external memory, it
needs a path for the 16 bits of the address. P0 provides the lower 8 bits via A0 - A7 while Port 2
provides bits A8 - A15 of the address. When the 8031 is connected to external memory, Port 2
is used for the upper 8 bits of the 16-bit address, and it cannot be used for input / output
operations.
Port 3:
Port 3 has total 8 Pins. P3.0, P3.1, P3.2, P3.3, P3.4, P3.5, P3.6, and P3.7. Pins 10, 11, 12, 13, 14,
15, 16, and 17. Port 3 can be sued as an input port or output port. Port 3 does need any pull-up
resisters, just like the Port 1 and Port 2 does not require pull-up resistors. Only Port 0 require
Pull-up resistors. Port 3 configured as an output port upon system reset. Port 3 has additional
function of providing signals of interrupts.
Alternate Functions of Port 3:
Serial Communication
BASICS OF SERIAL COMMUNICATION:
Computers transfer data in two ways:
Parallel:
Often 8 or more lines (wire conductors) are used to transfer data to a device that is only
a few feet away.
Serial:
To transfer to a device located many meters away, the serial method is used
The data is sent one bit at a time.
At the transmitting end, the byte of data must be converted to serial bits using parallel-
in-serial-out shift register.
At the receiving end, there is a serialin- parallel-out shift register to receive the serial
data and pack them into byte.
When the distance is short, the digital signal can be transferred as it is on a
simple wire and requires no modulation.
If data is to be transferred on thetelephone line, it must be convertedfrom 0s and 1s to
audio tones.
1. This conversion is performed by a device called a modem,
“Modulator/demodulator”.
It is possible to write software to use either of these methods, but the programs can be tedious
and long There are special IC chips made by many manufacturers for serial communications.
UART (universal asynchronous Receivertransmitter) USART (universal synchronous
asynchronous Receiver-transmitter).
A line driver such as the MAX232 chip is required to convert RS232 voltage levels to TTL levels,
and vice versa. 8051 has two pins that are used specifically for transferring and receiving data
serially
These two pins are called TxD and RxD andare part of the port 3 group (P3.0 and P3.1)
These pins are TTL compatible; therefore, they require a line driver to make them RS232
compatible
We need a line driver (voltage converter) to convert the R232’s signals to TTL voltage levels that
will be acceptable to 8051’s TxD and RxD pins
To allow data transfer between the PC and an 8051 system without any error, we must make
sure that the baud rate of 8051 system matches the baud rate of the PC’s COM port
Hyperterminal function supports baud rates much higher than listed below
SBUF :
It is an 8-bit register used solely for serial communicationFor a byte data to be transferred via
the TxD line, it must be placed in the SBUF Register The moment a byte is written into SBUF, it
is framed with the start and stop bits and transferred serially via the TxD line SBUF holds the
byte of data when it is received by 8051 RxD line .When the bits are received serially via RxD,
the 8051 deframes it by eliminating the stop and start bits, making a byte out of the data
received, and then placing it in SBUF
SCON
It is an 8-bit register used to program the start bit, stop bit, and data bits of data framing,
among other things.
SM0, SM1
They determine the framing of data by specifying the number of bits per character,
and the start and stop bits
SM2
This enables the multiprocessing capability of the 8051
REN (receive enable)
It is a bit-adressable register. When it is high, it allows 8051 to receive data on RxD pin. If low,
the receiver is disable
TI (transmit interrupt)
When 8051 finishes the transfer of 8-bit character. It raises TI flag to indicate that it is ready to
transfer another byte. TI bit is raised at the beginning of the stop bit
RI (receive interrupt)
When 8051 receives data serially via RxD, it gets rid of the start and stop bits and places the
byte in SBUF register. It raises the RI flag bit to indicate that a byte has been received and
should be picked up before it is lost. RI is raised halfway through the stop bit
DATA BUS (PIN27-PIN34):
There bi-directional, tri state data bus lines are connected to the system data bus. They are
used to transfer data and control word from microprocessor (8085) or Microcontroller (8051)
to
8255 or to receive data or status word from 8255 to the 8051 and 8085.
PORT A (PA0 – PA) [Pin 1-4 & Pin 37-40]:
There 8 bit bi-directional Input/Output pins are used to send data to output device and to
receive data from input device. It functions as an 8bit data output latch / buffer, when used in
output mode and an 8bit data input buffer, when used in input mode.
PORT B (PB0-PB7) (PIN18-PIN25):
There 8bit bi-directional input pins are used to send data to output device and to receive data
from input device. It functions as 8bit data output latch/buffer when used in output mode and
on 8bit data in input buffer – when used in input mode.
PORT C (PC0-PC7) (PIN10-PIN17):
There 8bit bi-directional input pins are divided into two groups PCL(PC3-PC0)and PC4(PC7-PC4)
these groups individually can transfer data in or out when transformed for simple inputs and
used as handshake signals when transformed for handshake for bi-directional modes.
RD (read) (pin5):
When this pin is low, the CPU can read the data in the ports or the states word through the
data buffer.
WR (write) (pin 36):
When this pin is low the CPU can write the data on the ports or in the control register
through the data bus buffer.
CS (chip select) (pin 6):
This is an active low input which can be enabled for data transfer operator between the CPU
and the 8255.
RESET (pin 35):
This is an active high input used to reset 8255, when reset input is high the control register
is cleaned and all the ports are set to the input mode. Usually reset output signal from 8085 or
8051 is used to reset 8255.
A0 and A1 (Pin8, 9):
These input signal and along with read and write inputs control the selection of the control/
states word register or one of the three parts A0 and A1 are generally connected to the A0 ,A1
pins
of the address bus; the 8255 therefore occupies from consecutive location in the input space
Data Bus Buffer
This three state bi-directional 8bit buffer is used to interface the 8255 to the system data bus.
Data is transmitted or received by the buffer upon execution of input or output instructions
by the CPU. Control words and status informations are also transferred through the data bus
buffer
OPERATION MODES
So, computer has to send data with the speed with which printer can accept. This type of
data transfer is achieved by using hand shaking signals along with data signals. Fig 8.3 shows
data transfer between computer and printer using handshaking signals.
These handshaking signals are used to tell computer whether printer is ready to accept the
data or not. If printer is ready to accept the data, then after sending data on data bus,
computer
uses another handshaking signal (STB) to tell printer that valid data is available on the data
bus.
The 8255 mode 1 which supports handshaking has following features.
Two ports (A and B) functions as 8-bit I/O ports. They can be configured either as input
or as output ports.
Each port uses three lines from port C as handshake signals. The remaining two lines of
port C can be used for simple I/O function.
Input and output data are latched.
Interrupt logic is supported.