Microprocessors & Microcontrollers Overview
Microprocessors & Microcontrollers Overview
Credits: 4
COMPUTER SYSTEM COMPONENTS
Memory
Stores instructions and data
Input/Output
Called peripherals
Used to input and output instructions and data
https://www.youtube.com/watch?v=_Cdf68NMTZ0.
MICROPROCESSO
R
is a semiconductor device consisting
of electronic logic circuits
manufactured by using various
fabrication schemes
capable of performing computing
functions
capable of transporting data/information
can be divided into 3 segments:
Arithmetic and Logic Unit
Register Unit
History: https://spectrum.ieee.org/the-surprising-story-of-the-first-microprocessors
Control Unit
HISTORY OF INTEL
MICROPROCESSORS
1
CONTENTS
Introduction
4-Bit Microprocessors
8-Bit Microprocessors
16-Bit Microprocessors
32-Bit Microprocessors
64-Bit Microprocessors
2
INTRODUCTION
Fairchild Semiconductors (founded in 1957) invented the first
IC in 1959.
In 1968, Robert Noyce, Gordan Moore, Andrew Grove
resigned from Fairchild Semiconductors.
4
INTEL Introduced in 1971.
4004
It was the first microprocessor by
Intel.
4-bit microprocessor
4 KB main memory
45 instructions
PMOS technology 5
6
8-BIT MICROPROCESSORS
7
INTEL
8008
Introduced in 1972.
It was first 8-bit µP.
Its clock speed was 500 KHz.
Could execute 50,000 instructions
per second.
1
1
Introduced in 1978.
13
.
Intel 8086/8088
It could address 16 MB of
memory.
16
Introduced in 1986.
INTEL It was first 32-bit µP.
80386 Its data bus is 32-bit and
address bus is 32-bit.
It could address 4 GB of
memory.
It had 2,75,000 transistors.
Its clock speed varied from 16
MHz to 33 MHz depending upon
the various versions.
Different versions:
80386 DX
80386 SX
80386 SL
80486 DX
80486 SX
80486 DX2
80486 SL
80486 DX4
8 KB of cache memory was
18
introduced.
Intel 80486
It could address 4 GB of
memory.
Cache memory:
8 KB for instructions.
19
8 KB for data.
Pentium
Year of introduction 1993
32-bit microprocessor, 64-bit data bus and
32-bit address bus
4 GB main memory
Double clocked 120 and 133MHz versions
Fastest version is the 233MHz, Dual integer
processor
16 KB L1 cache (split instruction and data: 8 KB
each)
INTEL PENTIUM
PRO
Introduced in 1995.
It was also 32-bit µP.
It had L2 cache of 256 KB.
It had 21 million transistors.
8 KB for data.
20
on one circuit.
Pentium II
Year of introduction 1997
32-bit microprocessor, 64-bit data bus and
36-bit address bus, MMX
64 GB main memory
32 KB split instruction/data L1 caches (16 KB
each)
Module integrated 512KB L2 cache (133MHz)
A version of P2 called Xeon; specifically designed
for high-end applications
INTEL PENTIUM II
XEON
Introduced in 1998.
46
INTEL CORE
2 Introduced in 2006.
It is a 64-bit µP.
It is a 64-bit µP.
It is a 64-bit µP.
30
INTEL CORE
I3
Introduced in 2010.
It is a 64-bit µP.
It is a 64-bit µP.
It has 64 KB of L1
cache per core, 1 MB
of L2 cache per core
and 13.75 MB of L3
cache.
Assembly language, high level language, low level language,
machine language:
http://www.itrelease.com/2018/07/difference-between-assembly-language-and-high
-level-language/.
MOV A, M : Assembly language
7E (0111 1110): Machine language
▪ Assembly language and machine language are treated as low level languages.
▪ In assembly language programs written for one processor will not run on another type of
processor. In high-level language programs run independently of processor type.
▪ Performance and accuracy of assembly language code are better than a high-level.
▪ High-level languages have to give extra instructions to run code on the computer.
▪ Code of assembly language is difficult to understand and debug than a high-level.
▪ One or two statements of high-level language expand into many assembly language
codes.
▪ Assembly language can communicate better than a high-level. Some type
of hardware actions can only be performed by assembly language.
▪ In assembly language, we can directly read pointers at a physical address which is not
possible in high-level
▪ Working with bits is easier in assembly language.
▪ Assembler is used to translate code in assembly language while the compiler is used to
compile code in the high-level.
▪ The executable code of high-level language is larger than assembly language code so it
takes a longer time to execute.
▪ Due to long executable code, high-level programs are less efficient than assembly
language programs.
▪ High-level language programmer does not need to know details about hardware
like registers in the processor as compared to assembly programmers.
▪ The most high-level language code is first automatically converted into assembly code.
Assembly Programming
• Machine Language
• binary
• hexadecimal
• machine code or object code
• Assembly Language
• mnemonics
• assembler
• High-Level Language
• Pascal, Basic, C
• compiler
Assembly Language Programming
Microprocessor development tools:
https://csenotesforyou.blogspot.com/2016/12/assembly-language-program-develo
pment.html.
Hardware Tools :
Software Tools !
In Circuit Emulator (ICE)
Assembler !
Logic Analyzer
Linker !
Emulator
Loader !
Compiler ! ▪ A debugger is a computer program used by programmers to test
Libraries ! and debug a target program. Debuggers may use instruction-set
Simulator ! simulators, rather than running a program directly on the
Debugger ! processor to achieve a higher level of control over its execution.
Locator ! This allows debuggers to stop or halt the program according to
specific conditions. However, use of simulators decreases
execution speed. When a program crashes, debuggers show the
position of the error in the target program. Most debuggers also
are capable of running programs in a step-by-step mode, besides
stopping on specific points. They also can often modify the state
of programs while they are running.
5. Debugger: - A debugger is a program which allows you to load your object code
program into system memory, execute the program and troubleshoot or debug it.
The debugger allows you to look at the contents of registers and memory locations
after your program runs.
- It allows you to change the contents of registers and memory locations and re-run
the program.
- Some debuggers allow you to stop execution after each instruction so that you can
check or alter after each register contents.
- A debugger also allows you to set a breakpoint at any point in your program. If you
insert a breakpoint at any point in your program, the debugger will run the program
up to the instruction where you put the breakpoint and then stop the execution.
- It is used to test and debug the hardware and software of an external system, such as
the prototype of a microprocessor based instrument. Part of the hardware of an
emulator is a multi wire cable which connects the host system to the system being
developed. Ex: FPGAs, USRPs.
Logic analysers:
https://www.radio-electronics.com/info/t_and_m/logic_analyzer/logic_analyzer.php.
Debugging, Simulator, Emulator, In-circuit
emulator:
https://www.youtube.com/watch?v=4wmDsd53ibE.
ICE:
Embedded systems pose unique debugging challenges. With neither terminal nor
display (in most cases), there's no natural way to probe these devices, to extract the
behavioural information needed to find what's wrong. They let us connect an external
computer to the system being debugged to enable single stepping, breakpoints, and all
of the debug resources enjoyed by programmers of desktop computers.
In-circuit emulation (ICE) is the use of a hardware device or in-circuit emulator used
to debug the software of an embedded system. It operates by using a processor with the
additional ability to support debugging operations, as well as to carry out the main function of
the system. Particularly for older systems, with limited processors, this usually involved
replacing the processor temporarily with a hardware emulator: a more powerful although more
expensive version. It was historically in the form of bond-out processor which has many
internal signals brought out for the purpose of debugging. These signals provide information
about the state of the processor.
An in-circuit emulator (ICE) provides a window into the embedded system. The
programmer uses the emulator to load programs into the embedded system, run
them, step through them slowly, and view and change data used by the system's
software.
WHAT DOES IT MEAN TO
DISASSEMBLE CODE?
Preprocessing
& Compiling
Source Code Assembly Code
Assembly
DLLs
WHAT DOES IT MEAN TO
DISASSEMBLE CODE?
Preprocessing
& Compiling
Source Code Assembly Code
L Y
MB
S E
A S Assembly
S
DI
DLLs
1. Linker :
2. Loader :
The loader is special program that takes input of object code from linker, loads it to
main memory, and prepares this code for execution by computer. Loader allocates
memory space to program. Even it settles down symbolic reference between objects.
It is in charge of loading programs and libraries in operating system.
A locator is a program used to assign the specific addresses of where the
segments of object code are to be loaded into memory.
Subject: ECE3004
Credits: 4
Microprocessor Fifth Generation Pentium
Fourth Generation
During 1980s
Low power version of HMOS technology
(HCMOS)
Third Generation 32 bit processors
During 1978 Physical memory space 224 bytes = 16 Mb
HMOS technology ⇒ Faster speed, Higher Virtual memory space 240 bytes = 1 Tb
packing density Floating point hardware
16 bit processors ⇒ 40/ 48/ 64 pins Supports increased number of addressing
Easier to program modes
Dynamically relatable programs
Processor has multiply/ divide arithmetic Intel 80386
hardware
More powerful interrupt handling
capabilities Second Generation
Flexible I/O port addressing During 1973
NMOS technology ⇒ Faster speed, Higher
Intel 8086 (16 bit processor) density, Compatible with TTL
4 / 8/ 16 bit processors ⇒ 40 pins
First Generation Ability to address large memory spaces
Between 1971 – 1973 and I/O ports
PMOS technology, non compatible with TTL Greater number of levels of subroutine
4 bit processors ⇒ 16 pins nesting
8 and 16 bit processors ⇒ 40 pins Better interrupt handling capabilities
Due to limitations of pins, signals are 2
multiplexed Intel 8085 (8 bit processor)
General Microprocessor Functional blocks
Overview
First 16- bit processor released by
INTEL in the year 1978
4
8086 Microprocessor
Common signals
Address/Data bus
https://www.geeksforgeeks.org/pin-diagram-8086-microprocessor/ 5
8086 Microprocessor
Common signals
MN/ MX
MINIMUM / MAXIMUM
READY
CLK
9
8086 Microprocessor
Minimum mode signals
It stands for Data Enable and is available at pin 26. It is used to enable Transreceiver 8286.
The transreceiver is a device used to separate data from the address/data bus.
ALE
It stands for address enable latch and is available at pin 25. A positive pulse is generated
each time the processor begins any operation. This signal indicates the availability of a valid
address on the address/data lines.
8086 Microprocessor
Minimum mode signals
12
8086 Microprocessor
Maximum mode signals
13
8086 Microprocessor
Maximum mode signals
15
Inside The 8088/8086
• Pipelining
• Registers
Inside The 8088/8086…pipelining
• Pipelining
– Two ways to make CPU process information faster:
• Increase the working frequency – technology dependent
• Change the internal architecture of the CPU
Architecture
Segment Registers >> 20
8086 Microprocessor
Bus Interface Unit (BIU)
Architecture
Segment
Registers
Architecture
Segment Code Segment Register
Registers
• 16-bit
• CS contains the base or start of the current code segment; IP contains the
distance or offset from this address to the next instruction byte to be fetched.
• BIU computes the 20-bit physical address by logically shifting the contents of CS
4-bits to the left and then adding the 16-bit contents of IP.
• That is, all instructions of a program are relative to the contents of the CS
register multiplied by 16 and then offset is added provided by the IP.
23
8086 Microprocessor
Bus Interface Unit (BIU)
Architecture
Segment Data Segment Register
Registers
• 16-bit
• Points to the current data segment; operands for most instructions are fetched
from this segment.
• The 16-bit contents of the Source Index (SI) or Destination Index (DI) or a
16-bit displacement are used as offset for computing the 20-bit physical
address.
24
8086 Microprocessor
Bus Interface Unit (BIU)
Architecture
Segment Stack Segment Register
Registers
• 16-bit
• The 20-bit physical stack address is calculated from the Stack Segment (SS) and
the Stack Pointer (SP) for stack instructions such as PUSH and POP.
• In based addressing mode, the 20-bit physical stack address is calculated from
the Stack segment (SS) and the Base Pointer (BP).
25
8086 Microprocessor
Bus Interface Unit (BIU)
Architecture
Segment
Registers
Extra Segment Register
• 16-bit
• Points to the extra segment in which data (in excess of 64K pointed to by the
DS) is stored.
• String instructions use the ES and DI to determine the 20-bit physical address
for the destination.
26
8086 Microprocessor
Bus Interface Unit (BIU)
Architecture
Segment Instruction Pointer
Registers
• 16-bit
27
8086 Microprocessor
Bus Interface Unit (BIU)
Instruction queue
Architecture
28
8086 Microprocessor
Execution Unit (EU)
A decoder in the EU
control system
translates instructions.
and
Some of the 16 bit registers can be
Index registers (Source used as two 8 bit registers as :
Index, Destination Index)
each of 16-bits AX can be used as AH and AL
BX can be used as BH and BL
CX can be used as CH and CL 29
DX can be used as DH and DL
8086 Microprocessor
Execution Unit (EU)
Architecture
EU Accumulator Register (AX)
Registers
30
8086 Microprocessor
Execution Unit (EU)
Architecture
EU Base Register (BX)
Registers
• Consists of two 8-bit registers BL and BH, which can be
combined together and used as a 16-bit register BX.
31
8086 Microprocessor
Execution Unit (EU)
Architecture
EU Counter Register (CX)
Registers
• Consists of two 8-bit registers CL and CH, which can be
combined together and used as a 16-bit register CX.
Example:
32
8086 Microprocessor
Execution Unit (EU)
Architecture
EU
Registers
33
8086 Microprocessor
Execution Unit (EU)
Architecture
EU Stack Pointer (SP) and Base Pointer (BP)
Registers
• SP and BP are used to access data in the stack segment.
34
8086 Microprocessor
Execution Unit (EU)
Architecture
EU Source Index (SI) and Destination Index (DI)
Registers
• Used in indexed addressing.
35
8086 Microprocessor
Architecture Execution Unit (EU)
Auxiliary Carry Flag
Carry Flag
Flag Register This is set, if there is a carry from the
This flag is set, when there is
lowest nibble, i.e, bit three during
addition, or borrow for the lowest a carry out of MSB in case of
nibble, i.e, bit three, during addition or a borrow in case
subtraction. of subtraction.
This flag is set, when the This flag is set, if the result of This flag is set to 1, if the lower
result of any computation the computation or comparison byte of the result contains even
is negative performed by an instruction is number of 1’s ; for odd number
zero of 1’s set to zero.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
OF DF IF TF SF ZF AF PF CF
Tarp Flag
Over flow Flag If this flag is set, the processor
This flag is set, if an overflow occurs, i.e, if the result of a signed enters the single step execution
operation is large enough to accommodate in a destination
mode by generating internal
register. The result is of more than 7-bits in size in case of 8-bit
signed operation and more than 15-bits in size in case of 16-bit interrupts after the execution of
sign operations, then the overflow will be set. each instruction
8086 registers
categorized 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
into 4 groups OF DF IF TF SF ZF AF PF CF
AX 16-bit Accumulator Stores the 16-bit results of arithmetic and logic operations
AL 8-bit Accumulator Stores the 8-bit results of arithmetic and logic operations
BX Base register Used to hold base value in base addressing mode to access
memory data
CX Count Register Used to hold the count value in SHIFT, ROTATE and LOOP
instructions
DX Data Register Used to hold data for multiplication and division operations
SP Stack Pointer Used to hold the offset address of top stack memory
BP Base Pointer Used to hold the base value in base addressing using SS
register to access data from stack memory
SI Source Index Used to hold index value of source operand (data) for string
instructions
DI Data Index Used to hold the index value of destination operand (data)
for string operations 38
8086 Microprocessor
Addressing Modes
• Every instruction of a program has to operate on a data.
• The different ways in which a source operand is denoted
in an instruction are known as addressing modes.
1. Register Addressing
Group I : Addressing modes for register and
2. Immediate Addressing immediate data
3. Direct Addressing
5. Based Addressing
Group II : Addressing modes for memory data
6. Indexed Addressing
8. String Addressing
1. Register Addressing
Addressing Modes
The instruction will specify the name of the
immediate data
8. String Addressing
40
8086 Microprocessor Group I : Addressing modes for register and
immediate data
1. Register Addressing
Addressing Modes
In immediate addressing mode, an 8-bit or 16-bit
2. Immediate Addressing data is specified as part of the instruction
3. Direct Addressing
Example:
4. Register Indirect Addressing
MOV DL, 08H
5. Based Addressing
The 8-bit data (08H) given in the instruction is
6. Indexed Addressing moved to DL
8. String Addressing
MOV AX, 0A9FH
9. Direct I/O port Addressing
The 16-bit data (0A9FH) given in the instruction is
10. Indirect I/O port Addressing
moved to AX register
11. Relative Addressing
(AX) ← 0A9FH
12. Implied Addressing
41
8086 Microprocessor
Adder
42
8086 Microprocessor
• Supported combinations:
BX SI
+ disp
BP DI 44
8086 Microprocessor Group II : Addressing modes for memory data
12. Implied Addressing This addressing mode is called direct because the
displacement of the operand from the segment
base is specified directly in the instruction.
45
8086 Microprocessor Group II : Addressing modes for memory
(CL) ← (MA)
(CH) ← (MA +1)
46
8086 Microprocessor Group II : Addressing modes for memory
Addressing Modes
In Based Addressing, BX or BP is used to hold the
data
1. Register Addressing
base value for effective address and a signed 8-bit
2. Immediate Addressing or unsigned 16-bit displacement will be specified
in the instruction.
3. Direct Addressing
In case of 8-bit displacement, it is sign extended
4. Register Indirect Addressing to 16-bit before adding to the base value.
(AL) ← (MA)
47
(AH) ← (MA + 1)
8086 Microprocessor Group II : Addressing modes for memory
(CL) ← (MA)
(CH) ← (MA + 1)
48
8086 Microprocessor Group II : Addressing modes for memory
49
8086 Microprocessor Group II : Addressing modes for memory
1. Register Addressing
data.
2. Immediate Addressing
The effective address (EA) of source data is stored
3. Direct Addressing in SI register and the EA of destination is stored in
DI register.
4. Register Indirect Addressing
Segment register for calculating base address of
5. Based Addressing source data is DS and that of the destination data
is ES
6. Indexed Addressing
Addressing Modes
These addressing modes are used to access data
ports
1. Register Addressing
from standard I/O mapped devices or ports.
2. Immediate Addressing
In direct port addressing mode, an 8-bit port
3. Direct Addressing address is directly specified in the instruction.
1. Register Addressing
Addressing Modes
2. Immediate Addressing
3. Direct Addressing
5. Based Addressing
6. Indexed Addressing
Instructions using this mode have no operands.
The instruction itself will specify the data to be
7. Based Index Addressing
operated by the instruction.
8. String Addressing
Example: CLC
9. Direct I/O port Addressing
This clears the carry flag to zero.
10. Indirect I/O port Addressing
53
8086 Microprocessor
Instruction Set
8086 supports 6 types of instructions.
2. Arithmetic Instructions
3. Logical Instructions
https://www.tutorialspoint.com/assembly_programming/assembly_logical_instructions.ht
m
54
8086 Microprocessor
Instruction Set
1. Data Transfer Instructions
Generally involve two operands: Source operand and Destination operand of the
same size.
A 8-bit data can only be moved to 8-bit register/ memory and a 16-bit data can be
moved to 16-bit register/ memory.
55
8086 Microprocessor
Instruction Set
1. Data Transfer Instructions
56
8086 Microprocessor
Instruction Set
1. Data Transfer Instructions
IN AL, [DX] PORT addr = (DX) OUT [DX], AL PORT addr = (DX)
(AL) ← (PORT) (PORT) ← (AL)
IN AX, [DX] PORT addr = (DX) OUT [DX], AX PORT addr = (DX)
(AX) ← (PORT) (PORT) ← (AX)
58
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
ADD A, data
59
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
ADD A, data
60
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
SUB A, data
61
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
SBB A, data
62
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
63
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
64
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
65
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
66
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
67
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
68
8086 Microprocessor
Instruction Set
2. Arithmetic Instructions
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
CMP A, data
69
8086 Microprocessor
Instruction Set
3. Logical Instructions
Mnemonics: AND, OR, XOR, TEST, SHR, SHL, RCR, RCL … ROR, ROL
70
8086 Microprocessor
Instruction Set
3. Logical Instructions
Mnemonics: AND, OR, XOR, TEST, SHR, SHL, RCR, RCL … ROR, ROL
71
8086 Microprocessor
Instruction Set
3. Logical Instructions
Mnemonics: AND, OR, XOR, TEST, SHR, SHL, RCR, RCL … ROR, ROL
72
8086 Microprocessor
Instruction Set
3. Logical Instructions
Mnemonics: AND, OR, XOR, TEST, SHR, SHL, RCR, RCL … ROR, ROL
The TEST instruction
works same as the AND
operation, but unlike
AND instruction, it does
not change the first
operand. So, if we need
to check whether a
number in a register is
even or odd, we can also
do this using the TEST
instruction without
changing the original
number.
73
8086 Microprocessor
Instruction Set
3. Logical Instructions
Mnemonics: AND, OR, XOR, TEST, SHR, SHL, RCR, RCL … ROR, ROL
74
8086 Microprocessor
Instruction Set
3. Logical Instructions
Mnemonics: AND, OR, XOR, TEST, SHR, SHL, RCR, RCL … ROR, ROL
75
8086 Microprocessor
Instruction Set
3. Logical Instructions
Mnemonics: AND, OR, XOR, TEST, SHR, SHL, RCR, RCL … ROR, ROL
76
8086 Microprocessor
Instruction Set
3. Logical Instructions
Mnemonics: AND, OR, XOR, TEST, SHR, SHL, RCR, RCL … ROR, ROL
77
8086 Microprocessor
Instruction Set
4. String Manipulation Instructions
❑ 8086 instruction set includes instruction for string movement, comparison, scan, load and store.
❑ String instructions end with S or SB or SW. S represents string, SB string byte and SW string word.
❑ Offset or effective address of the source operand is stored in SI register and that of the destination
operand is stored in DI register.
78
8086 Microprocessor
Instruction Set
4. String Manipulation Instructions
Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS
Note: Always ‘REPZ’ instruction can be used in association with the string related
operations.
79
8086 Microprocessor
Instruction Set
4. String Manipulation Instructions
Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS
MOVS
(MA E) ← (MA)
(MA E ; MA E + 1) ← (MA; MA + 1)
80
8086 Microprocessor
Instruction Set
4. String Manipulation Instructions
Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS
CMPS
81
8086 Microprocessor
Instruction Set
4. String Manipulation Instructions
Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS
Scan (compare) a string byte or word with accumulator
SCAS
LODS
83
8086 Microprocessor
Instruction Set
4. String Manipulation Instructions
Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS
STOS
84
8086 Microprocessor
Instruction Set
5. Processor Control Instructions
Mnemonics Explanation
STC Set CF ← 1
CLC Clear CF ← 0
NOP No operation
ESC opcode mem/ reg Used to pass instruction to a coprocessor which shares the
address and data bus with the 8086
LOCK Lock bus during next instruction
85
8086 Microprocessor
Instruction Set
6. Control Transfer Instructions
Mnemonics Explanation
CALL reg/ mem/ disp16 Call subroutine
86
8086 Microprocessor
Instruction Set
6. Control Transfer Instructions
• Checks flags
87
8086 Microprocessor
Instruction Set
6. Control Transfer Instructions
Note: Before these instructions comparison instruction is to be used for comparing two operands.88
8086 Microprocessor
Instruction Set
6. Control Transfer Instructions
Mnemonics Explanation
JC disp8 Jump if CF = 1
JP disp8 Jump if PF = 1
JO disp8 Jump if OF = 1
JS disp8 Jump if SF = 1
89
Iteration Control Instructions
These instructions are used to execute the given instructions for number of times.
Following is the list of instructions under this group −
LOOP − Used to loop a group of instructions until the condition satisfies, i.e., CX = 0
Assemble Directives
• Instructions to the Assembler regarding the program being executed.
• Control the generation of machine codes and organization of the program; but no
machine codes are generated for assembler directives.
• Used to :
› specify the start and end of a program
› attach value to variables
› allocate storage locations to input/ output data
› define start and end of segments, procedures, macros etc..
91
8086 Microprocessor
DB
Assemble Directives
• Define Byte
ASSUME • Range : 00H – FFH for unsigned value; 00H – 7FH for
positive value and 80H – FFH for negative value
ORG
END • General form : variable DB value/ values
EVEN
EQU
PROC
FAR Example:
NEAR LIST DB 7FH, 42H, 35H
ENDP
Three consecutive memory locations are reserved for the variable LIST
SHORT and each data specified in the instruction are stored as initial value in
the reserved memory location
MACRO
ENDM 92
8086 Microprocessor
DB
Assemble Directives
• Define Word
PROC
FAR Example:
NEAR ALIST DW 6512H, 0F251H, 0CDE2H
ENDP
Six consecutive memory locations are reserved for the variable ALIST
SHORT and each 16-bit data specified in the instruction is stored in two
consecutive memory location.
MACRO
ENDM 93
8086 Microprocessor
DB
Assemble Directives
• SEGMENT : Used to indicate the beginning of a code/ data/
stack segment
DW
• ENDS : Used to indicate the end of a code/ data/ stack
SEGMENT segment
ENDS
• General form:
ASSUME
ORG
END Segnam SEGMENT
EVEN
…
EQU … Program code
… or
PROC … Data Defining Statements
…
FAR …
NEAR
ENDP Segnam ENDS
SHORT
DB
Assemble Directives
• Informs the assembler the name of the program/ data
segment that should be used for a specific segment.
DW
• General form:
SEGMENT
ENDS
ASSUME segreg : segnam, .. , segreg : segnam
ASSUME
ORG
User defined name of the
END Segment Register
segment
EVEN
EQU
PROC Example:
FAR
NEAR ASSUME CS: ACODE, DS:ADATA Tells the compiler that the instructions of the
ENDP program are stored in the segment ACODE and
data are stored in the segment ADATA
SHORT
MACRO
ENDM 95
8086 Microprocessor
Assemble Directives
• ORG (Origin) is used to assign the starting address (Effective address)
DB
for a program/ data segment
ORG
Examples:
END
EVEN ORG 1000H Informs the assembler that the statements following
EQU ORG 1000H should be stored in memory starting with
effective address 1000H
PROC
FAR
LOOP EQU 10FEH Value of variable LOOP is 10FEH
NEAR
ENDP
_SDATA SEGMENT In this data segment, effective address of memory
SHORT ORG 1200H location assigned to A will be 1200H and that of B will
A DB 4CH be 1202H and 1203H.
EVEN
MACRO B DW 1052H
ENDM _SDATA ENDS 96
LENGTH: LENGTH is an operator, which tells the assembler to determine the number of
elements in some named data item, such as a string or an array. When the assembler reads
the statement MOV CX, LENGTH STRING1, for example, will determine the number of
elements in STRING1 and load it into CX. If the string was declared as a string of bytes,
LENGTH will produce the number of bytes in the string. If the string was declared as a word
string, LENGTH will produce the number of words in the string.
LENGTH: Byte length of a label: This is used to refer to the length of a data array or a
string. Ex : MOV CX, LENGTH ARRAY
OFFSET: offset of a label: When the assembler comes across the OFFSET operator along
with a label, it first computing the 16-bit offset address of a particular label and replace
the string ‘OFFSET LABEL’ by the computed offset address. Ex : MOV SI, offset list
Assemble Directives
• PROC Indicates the beginning of a procedure
DB
• ENDP End of procedure
DW
• FAR Intersegment call
SEGMENT
ENDS • NEAR Intrasegment call
• General form
ASSUME
ORG
procname PROC[NEAR/ FAR]
END
EVEN …
… Program statements of the procedure
EQU
…
Last statement of the procedure
PROC RET
ENDP
FAR procname ENDP
NEAR
DB
Assemble Directives
Examples:
DW
SEGMENT ADD64 PROC NEAR The subroutine/ procedure named ADD64 is declared
ENDS as NEAR and so the assembler will code the CALL
… and RET instructions involved in this procedure as
… near call and return
ASSUME …
RET
ORG
ADD64 ENDP
END
EVEN
EQU CONVERT PROC FAR The subroutine/ procedure named CONVERT is
declared as FAR and so the assembler will code the
… CALL and RET instructions involved in this
PROC … procedure as far call and return
ENDP …
FAR
RET
NEAR CONVERT ENDP
SHORT
MACRO
ENDM 99
8086 Microprocessor
Assemble Directives
DB • Reserves one memory location for 8-bit signed displacement
in jump instructions
DW
Example:
SEGMENT
ENDS
ASSUME JMP SHORT AHEAD The directive will reserve one memory
location for 8-bit displacement named
ORG AHEAD
END
EVEN
EQU
PROC
ENDP
FAR
NEAR
SHORT
MACRO
ENDM 100
8086 Microprocessor
Assemble Directives
DB • MACRO Indicate the beginning of a macro
PROC
ENDP
FAR
NEAR User defined name of the macro
SHORT
MACRO
ENDM 101
Procedures and Macros:
http://www.snjb.org/polytechnic/up-images/downloads/chapter%206-MAPupFile_0
58d4fa990abaa.pdf.
Define procedure : A procedure is group of instructions that usually performs one task. It is a
reusable section of a software program which is stored in memory once but can be used as
often as necessary. A procedure can be of two types. 1) Near Procedure 2) Far Procedure
Near Procedure: A procedure is known as NEAR procedure if is written(defined) in the same
code segment which is calling that procedure. Only Instruction Pointer(IP register) contents
will be changed in NEAR procedure. FAR procedure : A procedure is known as FAR procedure
if it is written (defined) in the different code segment than the calling segment. In this case
both Instruction Pointer (IP) and the Code Segment (CS) register content will be changed.
Directives used for procedure : PROC directive: The PROC directive is used to identify the
start of a procedure. The PROC directive follows a name given to the procedure. After that
the term FAR and NEAR is used to specify the type of the procedure. ENDP Directive: This
directive is used along with the name of the procedure to indicate the end of a procedure to
the assembler. The PROC and ENDP directive are used to bracket a procedure.
CALL instruction and RET instruction :
There are two types of calls. 1)Near Call or Intra segment call. 2) Far call or Inter Segment call
Operation for Near Call : When 8086 executes a near CALL instruction, it decrements the
stack pointer by 2 and copies the IP register contents on to the stack. Then it copies address
of first instruction of called procedure.
Operation for Near Call : When 8086 executes a near CALL instruction, it decrements the
stack pointer by 2 and copies the IP register contents on to the stack. Then it copies address
of first instruction of called procedure.
Operation of FAR CALL: When 8086 executes a far call, it decrements the stack pointer by 2
and copies the contents of CS register to the stack. It the decrements the stack pointer by 2
again and copies the content of IP register to the stack. Finally it loads CS register with base
address of segment having procedure and IP with address of first instruction in procedure.
ASSUME CS:CODE, DS:DATA, SS:STACK_SEG CALL SUBTRACTION
MOV AH, 4CH
DATA SEGMENT INT 21H
NUM1 DB 50H Procedure Example
NUM2 DB 20H ADDITION PROC NEAR
ADD_RES DB ?
program: MOV AL, NUM1
SUB_RES DB ? MOV BL, NUM2
DATA ENDS ADD AL, BL
MOV ADD_RES, AL
STACK_SEG SEGMENT RET
ADDITION ENDP
DW 40 DUP(0) ; stack of 40 words, all initialized to zero
TOS LABEL WORD SUBTRACTION PROC
STACK_SEG ENDS MOV AL, NUM1
MOV BL, NUM2
CODE SEGMENT SUB AL, BL
MOV SUB_RES, AL
START: MOV AX, DATA ; initialize data segment RET
MOV DS, AX SUBTRACTION ENDP
MOV AX, STACK_SEG ; initialize stack segment
MOV SS, AX CODE ENDS
MOV SP, OFFSET TOS ; initialize stack pointer to TOS END START
CALL ADDITION
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM1 DW 1000H
NUM2 DW 2000H
RES DW ?
DATA ENDS MACRO program
CODE SEGMENT Example
ADDITION MACRO NO1, NO2, RESULT
MOV AX, NO1
MOV BX, NO2
ADD AX, BX
MOV RESULT, AX
ENDM
CODE ENDS
END START
Modular programming:
https://en.wikipedia.org/wiki/Modular_programming.
Modular programming is a software design technique that emphasizes separating the
functionality of a program into independent, interchangeable modules, such that each
contains everything necessary to execute only one aspect of the desired functionality.
A module interface expresses the elements that are provided and required by the module.
The elements defined in the interface are detectable by other modules.
The implementation contains the working code that corresponds to the elements declared in
the interface. Modular programming is closely related to structured
programming and object-oriented programming, all having the same goal of facilitating
construction of large software programs and systems by decomposition into smaller pieces,
and all originating around the 1960s. While the historical usage of these terms has been
inconsistent, "modular programming" now refers to high-level decomposition of the code of
an entire program into pieces: structured programming to the low-level code use of
structured control flow, and object-oriented programming to the data use of objects, a kind
of data structure.
107
Linking and relocation:
In computing, a linker or link editor is a computer utility program that takes one or
more object files generated by a compiler and combines them into a
single executable file, library file, or another 'object' file.
Executes ISR
(Available)
Type 1FH Interrupt
00080 (Reserved)
Reserved
H
Table (IVT)
Interrupts
0007C Type 05H Interrupt
H
00014 (27)
(Reserved)
Type 04H Interrupt (Over
H
Flow)03H Interrupt (Break
Type
00010 Point)
Type 02H Interrupt
H
0004F Type 01H Interrupt
(NMI) Dedicated
H
0000C (Trap or Single Interrupts
(05
H step)
CS 0003F
00002
C
Type 00H )
H
00008 Interrupt
IP H
00001
H S
H
00000 (Divide by Zero)
H
I
P
https://www.eeeguide.com/8086-interrupt/
Interrupt Vector Table
2 bytes 00002H
CS LSB C S 00003H
CS MSB
Type 0 or
INT 00
2 bytes 00000H
IP LSB I P 00001H
IP MSB Interrupt
CS LSB MSB
Offset = 02 x 4 = 08
= 00008H
256 Interrupts of 8086 are Divided into 3 Groups
1. Type 00 to Type 04 interrupts -
These are used for fixed operations and hence are
called dedicated interrupts
¬
Type – 2:- Non-Maskable Interrupt
This interrupt is used for executing ISR of NMI pin
(positive edge signal), NMI can’t be masked by S/W.
¬
Type – 3:- One-byte INT instruction interrupt
Used for providing break points in the program
¬
An example of an interrupt generated
due to overflow error in an 8086 system
MICROCONTROLLER-8051
Features & Applications
….. Man’s glory lies in his knowledge,
his upright conduct, his praise-worthy character,
his wisdom, and not in his nationality or rank
--Baha’ullah
(From, the book The 8051 Microcontroller and Embedded systems- Mazidi )
Overview
Introduction
Block Diagram and Pin Description of the
8051
Registers
Memory mapping in 8051
Stack in the 8051
I/O Port Programming
Timers
Interrupts & Applications
Why do we need to learn
Microcontrollers ?
■ Its not an exaggeration if I say that , today
there is no electronic gadget on the earth
which is designed without a Microcontroller.
Ex: communication devices, digital
entertainment, portable devices etc…
O.K ????
Then What is a Microcontroller ?
■ A smaller computer
■ On-chip RAM, ROM, I/O ports...
■ Example : Motorola’s 6811, Intel’s 8051,
Zilog’s Z8 and PIC 16X
CPU RAM ROM
I/O
Serial A single chip
Timer COM
Port Microcontroller
Port
How it is different from a
Microprocessor ??
■ General-purpose microprocessor
CPU for Computers
No RAM, ROM, I/O on CPU chip itself
Example : Intel’s x86, Motorola’s 680x0
Data Bus
CPU
General-
Serial
Purpose RAM ROM I/O Timer COM
Micro- Port
Port
processor
Address Bus
Microprocessor vs. Microcontroller
Microprocessor Microcontroller
■ CPU is stand-alone, RAM, • CPU, RAM, ROM, I/O and
ROM, I/O, timer are separate timer are all on a single chip
■ designer can decide on the • fix amount of on-chip ROM,
amount of ROM, RAM and I/ RAM, I/O ports
O ports.
• Highly bit addressable
■ expansive
• for applications in which cost,
■ versatility
power and space are critical
■ general-purpose
• single-purpose
EVOLUTION
Flashback !!!!
In 1970 and 1971, about the same time Intel was inventing its
microprocessor, Gary Boone, an engineer at Texas Instruments was working on
a similar idea. This extraordinary breakthrough was given the rather humdrum
name of the TMS1802NC. It is named as microcontroller.
TIMERS 2 2 3 2
I/O PINS 32 32 32 32
SERIAL PORTS 1 1 1 1
INTERRUPT 6 6 8 6
SOURCES
EPROM: Erasable programmable read-only memory
Microcontroller Architectures
Memory
0
Address Bus
Program
CPU Data Bus + Data Von Neumann
n
Architecture
2
Memory
0
Address Bus
Program
CPU
Fetch Bus Harvard
Address Bus 0
Architecture
Data Bus Data
Important Features of
8051
■ 4K bytes ROM
■ 128 bytes RAM
■ Four 8-bit I/O ports
■ Two 16-bit timers
■ Serial interface
■ 64K external code memory space
■ 64K data memory space
“Original” 8051 Microcontroller
Oscillator 4096 Bytes 128 Bytes Two 16 Bit
and timing Program Memory Data Memory Timer/Event
(ROM) (RAM) Counters
During Flash Programming, this pin acts as program pulse input (PROG).
Pin 31 (EA/VPP): Pin 31 is the External Access Enable Pin i.e. allows external
program Memory. Code from external program memory can be fetched only if this pin
is LOW. Else internal program memory and external program memory is used.
During Flash Programming, this Pin receives 12V Programming Enable Voltage
(VPP).
Flash memory is just like EEPROM: electrically erasable programmable read only
memory.
PSEN : This is an output pin. PSEN stands for “program store enable.” This pin is
used to read external program memory when ‘0’ else external data memory. If we use
an external ROM for storing the program, then logic 0 appears on it, which indicates
Microcontroller to read data from the memory.
https://www.electronicshub.org/8051-microcontroller-memory-organization/
EA :
▪ A (8-bit Accumulator). B (8-bit register for Mul & Div). PSW (8-bit Program
Status Word). SP (8-bit Stack Pointer). PC (16-bit Program Counter). DPTR
(16-bit Data Pointer)
RAM memory space allocation in the 8051
7FH
30H
2FH
Bit-Addressable RAM
20H
1FH Register Bank 3
18H
17H
Register Bank 2
10H
0FH Register Bank 1
08H
07H
Register Bank 0
00H
Special Function Registers
▪ Almost all modern variants of 8051 Microcontroller have 256B of RAM. In this 256B, the
first 128B i.e. memory addresses from 00H to 7FH is divided into Working Registers
(organized
almost all modern as variants
RegisterofBanks), Bit – Addressable
8051 Microcontroller have Area
256B ofandRAM.
General
In thisPurpose
256B, theRAM first(also
128B
known as
i.e. memory Scratchpad
addresses from area).
00H Into the
7FHfirst 128B in
is divided of to
RAM (fromRegisters
Working 00H to 7FH), the first
(organized 32B i.e.
as Register
memory
Banks), from addresses
Bit – Addressable Area00H
andtoGeneral
1FH consists
Purpose ofRAM
32 Working Registers
(also known that are organized
as Scratchpad area). as
four
In the firstbanks
128Bwith 8 Registers
of RAM (from 00H in each Bank.
to 7FH), the first 32B i.e. memory from addresses 00H to 1FH
consists of 32 Working Registers that are organized as four banks with 8 Registers in each Bank.
• DATA registers
• CONTROL
registers
• Timers
• Serial ports
• Interrupt system
•Analog to Digital converter
Addresses 80h – FFh
•Digital to Analog converter
etc.. Direct Addressing is used to
access SFRs
List of Registers
(*Denotes the SFRs)
Contd…
PSW REGISTER
PORTS OF 8051
■ 8051 has 4 Ports. Port 0, Port1, Port2 , Port3
■ Port 0 is a dual purpose port, it is located from
pin 32 to pin 39 (8 pins). To use this port as both
input/output ports each pin must be connected
externally to a 10 k ohm pull-up resistor.This is
because Port 0 is an open drain.
Simple ex: MOV A, #22
BACK MOV P0 ,A
ACALL DELAY
CPL A
SJMP BACK
Ports….
■ Port 1 is a dedicated I/O port from pin 1 to
pin 8.Upon reset it is configured as outport.
It is generally used for interfacing to
external device thus if you need to connect to
switches or LEDs, you could make use of
these 8 pins,but it doesn’t need any pull-up
resistors as it is having internally
■ Like port 0, port 2 is a dual-purpose port.(Pins 21
through 28) It can be used for general I/O or as
the high byte of the address bus for designs with
external code memory.Like P1 ,Port2 also doesn’t
require any pull-up resistors
Ports contd…
♦ Example:
MOV R0, A
♦ Example:
♦ Examples:
;Store the content of
;accumulator into the memory
MOV @R0,A ;location pointed to by
;register R0. R0 could have an
;8-bit address, such as 60H.
♦ Examples:
ADD A,#030H ;Add 8-bit value of 30H to
;the accumulator register
;(which is an 8-bit register).
;function call:
Timer_Init().
Indexed Addressing
♦ After the execution of the above instructions, the program will branch to
address 1F08H (1F00H+08H) and transfer into the accumulator the data
byte retrieved from that location (from the look-up table)
Keil software introduction:
https://www.youtube.com/watch?v=mhHJV21CDjs.
Introduction
♦ A computer instruction is made up of an operation code
(op-code) followed by either zero, one or two bytes of
operands
♦ The op-code identifies the type of operation to be performed
while the operands identify the source and destination of the
data
♦ The operand can be:
The data value itself
A CPU register
A memory location
An I/O port
♦ If the instruction is associated with more than one operand,
the format is always:
Instruction Destination, Source
1
Instruction Types
♦ The C8051F020 instructions are divided into five functional
groups:
Arithmetic operations
Logical operations
Data transfer operations
Boolean variable operations
Program branching operations
2
Arithmetic Operations
♦ Rn refers to registers
R0-R7 of the currently
selected register bank
3
ADD A,<source-byte> ADDC A,<source-byte>
♦ ADD adds the data byte specified by the source operand to
the accumulator, leaving the result in the accumulator
♦ ADDC adds the data byte specified by the source operand,
the carry flag and the accumulator contents, leaving the
result in the accumulator
♦ Operation of both the instructions, ADD and ADDC, can
affect the carry flag (CY), auxiliary carry flag (AC) and the
overflow flag (OV)
CY=1 If there is a carryout from bit 7; cleared otherwise
AC =1 If there is a carryout from the lower 4-bit of A i.e. from bit 3;
cleared otherwise
OV=1 If the signed result cannot be expressed within the number
of bits in the destination operand; cleared otherwise
4
SUBB A,<source-byte>
♦ SUBB subtracts the specified data byte and the carry flag together from
the accumulator, leaving the result in the accumulator
CY=1 If a borrow is needed for bit 7; cleared otherwise
AC =1 If a borrow is needed for bit 3, cleared otherwise
OV=1 If a borrow is needed into bit 6, but not into bit 7, or into bit 7,
but not into bit 6.
♦ Example:
The accumulator holds 0C1H (11000001B), Register1 holds 40H
(01000000B) and the CY=1.The instruction,
SUBB A, R1
gives the value 70H (01110000B) in the accumulator, with the CY=0 and
AC=0 but OV=1
5
INC <byte>
♦ Increments the data variable by 1. The instruction is used in register,
direct or register direct addressing modes
♦ Example:
INC6FH
If the internal RAM location 6FH contains 30H, then the instruction
increments this value, leaving 31H in location 6FH
♦ Example:
MOVR1, #5E
INCR1
INC@R1
♦ If R1=5E (01011110) and internal RAM location 5FH contains 20H, the
instructions will result in R1=5FH and internal RAM location 5FH to
increment by one to 21H
6
DEC <byte>
♦ The data variable is decremented by 1
7
INC DPTR
♦ Increments the 16-bit data pointer by 1
8
MUL AB
♦ Multiplies A & B and the 16-bit result stored in [B15-8], [A7-0]
♦ The Low order byte of the 16-bit product will go to the accumulator
and the High order byte will go to the B register
♦ If the product is greater than 255 (FFH), the overflow flag is set;
otherwise it is cleared. The carry flag is always cleared.
♦ If ACC=85 (55H) and B=23 (17H), the instruction gives the product
1955 (07A3H), so B is now 07H and the accumulator is A3H. The
overflow flag is set and the carry flag is cleared.
9
DIV AB
♦ Divides A by B
10
DA A
♦ This is a decimal adjust instruction
♦ It adjusts the 8-bit value in ACC resulting from operations
like ADD or ADDC and produces two 4-bit digits (in packed
Binary Coded Decimal (BCD) format)
♦ Effectively, this instruction performs the decimal conversion
by adding 00H, 06H, 60H or 66H to the accumulator,
depending on the initial value of ACC and PSW
♦ If ACC bits A3-0 are greater than 9 (xxxx1010-xxxx1111), or
if AC=1, then a value 6 is added to the accumulator to
produce a correct BCD digit in the lower order nibble
♦ If CY=1, because the high order bits A7-4 is now exceeding
9 (1010xxxx-1111xxxx), then these high order bits will be
increased by 6 to produce a correct proper BCD in the high
order nibble but not clear the carry
11
Logical Operations
♦ Example:
ANLA,R2
If ACC=D3H (11010011) and R2=75H (01110101), the
result of the instruction is ACC=51H (01010001)
♦ Example:
ANLP1,#10111001B
13
ORL <dest-byte>,<source-byte>
♦ This instruction performs the logical OR operation on the
source and destination operands and stores the result in the
destination variable
♦ Example:
ORLA,R2
If ACC=D3H (11010011) and R2=75H (01110101), the
result of the instruction is ACC=F7H (11110111)
♦ Example:
ORLP1,#11000010B
This instruction sets bits 7, 6, and 1 of output Port 1
14
XRL <dest-byte>,<source-byte>
♦ This instruction performs the logical XOR (Exclusive OR)
operation on the source and destination operands and
stores the result in the destination variable
♦ Example:
XRLA,R0
If ACC=C3H (11000011) and R0=AAH (10101010), then the
instruction results in ACC=69H (01101001)
♦ Example:
XRLP1,#00110001
This instruction complements bits 5, 4, and 0 of
output Port 1
15
CLR A and CPL A
CLR A
♦ This instruction clears the accumulator (all bits set to 0)
♦ No flags are affected
♦ If ACC=C3H, then the instruction results in ACC=00H
CPL A
♦ This instruction logically complements each bit of the
accumulator (one’s complement)
♦ No flags are affected
♦ If ACC=C3H (11000011), then the instruction results in
ACC=3CH (00111100)
16
RL A
♦ The 8 bits in the accumulator are rotated one bit to the left.
Bit 7 is rotated into the bit 0 position.
17
RLC A
♦ The instruction rotates the accumulator contents one bit to
the left through the carry flag
♦ Bit 7 of the accumulator will move into carry flag and the
original value of the carry flag will move into the Bit 0
position
18
RR A
♦ The 8 bits in the accumulator are rotated one bit to the right.
Bit 0 is rotated into the bit 7 position.
19
RRC A
♦ The instruction rotates the accumulator contents one bit to
the right through the carry flag
♦ The original value of carry flag will move into Bit 7 of the
accumulator and Bit 0 rotated into carry flag
20
SWAP A
♦ This instruction interchanges the low order 4-bit nibbles
(A3-0) with the high order 4-bit nibbles (A7-4) of the ACC
21
Data Transfer Instructions
Mnemonic Description
MOV @Ri, direct [@Ri] = [direct]
MOV @Ri, #data [@Ri] = immediate data
MOV DPTR, #data 16 [DPTR] = immediate data
MOVC A,@A+DPTR A = Code byte from [@A+DPTR]
MOVC A,@A+PC A = Code byte from [@A+PC]
MOVX A,@Ri A = Data byte from external ram [@Ri]
MOVX A,@DPTR A = Data byte from external ram [@DPTR]
MOVX @Ri, A External[@Ri] = A
MOVX @DPTR,A External[@DPTR] = A
PUSH direct Push into stack
POP direct Pop from stack
XCH A,Rn A = [Rn], [Rn] = A
XCH A, direct A = [direct], [direct] = A
XCH A, @Ri A = [@Rn], [@Rn] = A
XCHD A,@Ri Exchange low order digits
22
MOV <dest-byte>,<source-byte>
♦ This instruction moves the source byte into the destination location
♦ The source byte is not affected, neither are any other registers or flags
♦ Example:
MOVR1,#60;R1=60H
MOVA,@R1 ;A=[60H]
MOVR2,#61;R2=61H
ADDA,@R2 ;A=A+[61H]
MOVR7,A ;R7=A
23
MOV DPTR, #data 16
♦ This instruction loads the data pointer with the 16-bit
constant and no flags are affected
♦ Example:
MOVDPTR,#1032
♦ This instruction loads the value 1032H into the data pointer,
i.e. DPH=10H and DPL=32H.
24
MOVC A,@A + <base-reg>
♦ This instruction moves a code byte from program memory into ACC
♦ The effective address of the byte fetched is formed by adding the original 8-bit
accumulator contents and the contents of the base register, which is either the
data pointer (DPTR) or program counter (PC)
♦ 16-bit addition is performed and no flags are affected
♦ The instruction is useful in reading the look-up tables in the program memory
♦ If the PC is used, it is incremented to the address of the following instruction
before being added to the ACC
♦ Example:
CLRA
LOC1: INCA
MOVC A,@A + PC
RET
Look_up DB 10H
DB 20H
DB 30H
DB 40H
♦ The subroutine takes the value in the accumulator to 1 of 4 values
defined by the DB (define byte) directive
♦ After the operation of the subroutine it returns ACC=20H
25
MOVX <dest-byte>,<source-byte>
♦ This instruction transfers data between ACC and a byte of external data
memory
♦ There are two forms of this instruction, the only difference between them
is whether to use an 8-bit or 16-bit indirect addressing mode to access
the external data RAM
♦ The 8-bit form of the MOVX instruction uses the EMI0CN SFR to
determine the upper 8 bits of the effective address to be accessed and
the contents of R0 or R1 to determine the lower 8 bits of the effective
address to be accessed
♦ Example:
MOV EMI0CN,#10H ;Load high byte of
;address into EMI0CN.
MOV R0,#34H ;Load low byte of
;address into R0(or R1).
MOVX A,@R0 ;Load contents of 1034H
;into ACC.
26
MOVX <dest-byte>,<source-byte>
♦ The 16-bit form of the MOVX instruction accesses the memory location
pointed to by the contents of the DPTR register
♦ Example:
MOVDPTR,#1034H ;Load DPTR with 16 bit
;address to read (1034H).
MOVX A,@DPTR ;Load contents of 1034H
;into ACC.
♦ The above example uses the 16-bit immediate MOV DPTR instruction to
set the contents of DPTR
♦ Alternately, the DPTR can be accessed through the SFR registers DPH,
which contains the upper 8 bits of DPTR, and DPL, which contains the
lower 8 bits of DPTR
27
PUSH Direct
♦ This instruction INCREMENTS THE STACK POINTER (SP) BY 1
♦ Example:
PUSH 22H
PUSH 23H
28
POP Direct
♦ This instruction reads the contents of the internal RAM location
addressed by the stack pointer (SP) and decrements the stack pointer
by 1. The data read is then transferred to the Direct address which is an
internal memory or a SFR. No flags are affected.
♦ Example:
POPDPH
POPDPL
♦ If SP=51H originally and internal RAM locations 4FH, 50H and 51H
contain the values 30H, 11H and 12H respectively, the instructions
above leave SP=4FH and DPTR=1211H
POPSP
♦ If the above line of instruction follows, then SP=30H. In this case, SP is
decremented to 4EH before being loaded with the value popped (30H)
29
XCH A,<byte>
♦ This instruction swaps the contents of ACC with the contents
of the indicated data byte
♦ Example:
XCHA,@R0
30
XCHD A,@Ri
♦ This instruction exchanges the low order nibble of ACC (bits
0-3), with that of the internal RAM location pointed to by Ri
register
♦ The high order nibbles (bits 7-4) of both the registers remain
the same
♦ Example:
XCHDA,@R0
♦ All bit accesses use direct JNC rel Jump if C not set
♦ Example:
CLRP2.7
If Port 2 has been previously written with DCH (11011100),
then the operation leaves the port set to 5CH (01011100)
33
SETB <bit>
♦ This operation sets the specified bit to 1
♦ Example:
SETB C
SETB P2.0
♦ If the carry flag is cleared and the output Port 2 has the
value of 24H (00100100), then the result of the instructions
sets the carry flag to 1 and changes the Port 2 value to 25H
(00100101)
34
CPL <bit>
♦ This operation complements the bit indicated by the operand
♦ Example:
CPLP2.1
CPLP2.2
♦ If Port 2 has the value of 53H (01010011) before the start of
the instructions, then after the execution of the instructions it
leaves the port set to 55H (01010101)
35
ANL C, <source-bit>
♦ This instruction ANDs the bit addressed with the Carry bit and stores the result in
the Carry bit itself
♦ If the source bit is a logical 0, then the instruction clears the carry flag; else the
carry flag is left in its original value
♦ If a slash (/) is used in the source operand bit, it means that the logical
complement of the addressed source bit is used, but the source bit itself is not
affected
♦ Example:
MOVC,P2.0;Load C with input pin
;state of P2.0.
ANLC,P2.7;AND carry flag with ;bit 7
of P2.
MOVP2.1,C;Move C to bit 1 of Port 2.
ANLC,/OV ;AND with inverse of OV flag.
♦ If P2.0=1, P2.7=0 and OV=0 initially, then after the above instructions,
P2.1=0, CY=0 and the OV remains unchanged, i.e. OV=0
36
ORL C, <source-bit>
♦ This instruction ORs the bit addressed with the Carry bit and stores the result in
the Carry bit itself
♦ It sets the carry flag if the source bit is a logical 1; else the carry is left in its
original value
♦ If a slash (/) is used in the source operand bit, it means that the logical
complement of the addressed source bit is used, but the source bit itself is not
affected
♦ Example:
MOV C,P2.0 ;Load C with input pin
;state of P2.0.
ORL C,P2.7 ;OR carry flag with
;bit 7 of P2.
MOV P2.1,C ;Move C to bit 1 of
;port 2.
ORL C,/OV ;OR with inverse of OV
;flag.
37
MOV <dest-bit>,<source-bit>
♦ The instruction loads the value of source operand bit into the destination
operand bit
♦ One of the operands must be the carry flag; the other may be any
directly-addressable bit
♦ Example:
MOVP2.3,C
MOVC,P3.3
MOVP2.0,C
♦ If P2=C5H (11000101), P3.3=0 and CY=1 initially, then after the above
instructions, P2=CCH (11001100) and CY=0.
38
JC rel
♦ This instruction branches to the address, indicated by the label, if the
carry flag is set, otherwise the program continues to the next instruction
♦ Example:
CLRC
SUBB A,R0
JC ARRAY1
MOVA,#20H
♦ The carry flag is cleared initially. After the SUBB instruction, if the value
of A is smaller than R0, then the instruction sets the carry flag and
causes program execution to branch to ARRAY1 address, otherwise it
continues to the MOV instruction.
39
JNC rel
♦ This instruction branches to the address, indicated by the label, if the
carry flag is not set, otherwise the program continues to the next
instruction
♦ Example:
CLRC
SUBB A,R0
JNCARRAY2
MOVA,#20H
♦ The above sequence of instructions will cause the jump to be taken if the
value of A is greater than or equal to R0. Otherwise the program will
continue to the MOV instruction.
40
JB <bit>,rel
♦ This instruction jumps to the address indicated if the
destination bit is 1, otherwise the program continues to the
next instruction
♦ Example:
JB ACC.7,ARRAY1
JB P1.2,ARRAY2
41
JNB <bit>,rel
♦ This instruction jumps to the address indicated if the
destination bit is 0, otherwise the program continues to the
next instruction
♦ Example:
JNBACC.6,ARRAY1
JNBP1.3,ARRAY2
42
JBC <bit>,rel
♦ If the source bit is 1, this instruction clears it and branches to
the address indicated; else it proceeds with the next
instruction
♦ Example:
JBCP1.3,ARRAY1
JBCP1.2,ARRAY2
CJNE @Ri,#data,rel
DJNZ Rn,rel
Decrement and Jump if Not
Zero
DJNZ direct,rel
NOP No Operation
44
ACALL addr11
♦ This instruction unconditionally calls a subroutine indicated by the
address
♦ The operation will cause the PC to increase by 2, then it pushes the
16-bit PC value onto the stack (low order byte first) and increments the
stack pointer twice
♦ The PC is now loaded with the value addr11 and the program execution
continues from this new location
♦ The subroutine called must therefore start within the same 2 kB block of
the program memory
♦ Example:
ACALL LOC_SUB
45
LCALL addr16
♦ This instruction calls a subroutine located at the indicated address
♦ The PC is then loaded with the value addr16 and the program execution
continues from this new location
♦ Example:
LCALL LOC_SUB
♦ RET pops the high byte and low byte address of PC from
the stack and decrements the SP by 2
47
RETI
♦ This instruction returns the program from an interrupt
subroutine
♦ RETI pops the high byte and low byte address of PC from
the stack and restores the interrupt logic to accept additional
interrupts
♦ SP decrements by 2 and no other registers are affected.
However the PSW is not automatically restored to its
pre-interrupt status
♦ After the RETI, program execution will resume immediately
after the point at which the interrupt is detected
♦ Suppose SP=0BH originally and an interrupt is detected
during the instruction ending at location 0213H
Internal RAM locations 0AH and 0BH contain the values 14H and
02H respectively
The RETI instruction leaves SP=09H and returns
48
program execution to location 0234H
AJMP addr11
♦ The AJMP instruction transfers program execution to the
destination address which is located at the absolute short
range distance (short range means 11-bit address)
♦ Example:
AJMP NEAR
49
LJMP addr16
♦ The LJMP instruction transfers program execution to the
destination address which is located at the absolute long
range distance (long range means 16-bit address)
♦ Example:
LJMP FAR_ADR
♦ This will be the new address where the program would branch
to unconditionally
♦ Example:
SJMP RELSRT
♦ Example:
MOVDPTR, #LOOK_TBL
JMP@A + DPTR
LOOK_TBL:AJMP LOC0
AJMP LOC1
AJMP LOC2
If the ACC=02H, execution jumps to LOC1
♦ Example:
SUBB A,#20H
JZ LABEL1
DECA
53
JNZ rel
♦ This instruction branches to the destination address if any bit
of ACC is a 1; else the program continues to the next
instruction
♦ Example:
DECA
JNZLABEL2
MOVRO, A
54
CJNE <dest-byte>,<source-byte>,rel
♦ This instruction compares the magnitude of the dest-byte and the
source-byte and branches if their values are not equal
♦ The carry flag is set if the unsigned dest-byte is less than the unsigned
integer source-byte; otherwise, the carry flag is cleared
♦ Example:
CJNE R3,#50H,NEQU
… … ;R3 = 50H
NEQU: JC LOC1 ;If R3 < 50H
… … ;R7 > 50H
LOC1: … … ;R3 < 50H
55
DJNZ <byte>,<rel-addr>
♦ This instruction is ”decrement jump not zero”
♦ It decrements the contents of the destination location and if the resulting
value is not 0, branches to the address indicated by the source operand
♦ An original value of 00H underflows to FFH
♦ No flags are affected
♦ Example:
DJNZ 20H,LOC1
DJNZ 30H,LOC2
DJNZ 40H,LOC3
♦ If internal RAM locations 20H, 30H and 40H contain the values 01H,
5FH and 16H respectively, the above instruction sequence will cause a
jump to the instruction at LOC2, with the values 00H, 5EH, and 15H in
the 3 RAM locations.
Note, the first instruction will not branch to LOC1 because the [20H] = 00H,
hence the program continues to the second instruction
Only after the execution of the second instruction (where the
location [30H] = 5FH), then the branching takes place
56
NOP
♦ This is the no operation instruction
♦ The instruction takes one machine cycle operation time
♦ Hence it is useful to time the ON/OFF bit of an output port
♦ Example:
CLRP1.2
NOP
NOP
NOP
NOP
SETB P1.2
57
However, the relation between machine cycle and clock cycle depends upon the
manufacturer. Ex: AT89C51 by Atmel consider 12 clock cycles per machine cycle. But,
other 8051 manufactured by Dallas consider 1 clock cycles per machine cycle.
Instruction cycle: Fetch, decode, execute, write back into memory and etc.
•Note: Mnemonic will occupy 1
byte memory.
As shown in the above figure, 8-bit of THx and lower 5-bit of TLx used to form a total 13-bit timer. Higher
3-bits of TLx should be written as zero while using timer mode0, or it will affect the result.
Example: Let's generate a square wave of 2mSec period using an AT89C51 microcontroller with timer0 in
mode0 on the P1.0 pin of port1. Assume xtal oscillator frequency of 11.0592 MHz. As the Xtal oscillator
frequency is 11.0592 MHz we have a machine cycle of 1.085uSec. Hence, the required count to generate a
delay of 1mSec. is,
Count =(1×10^-3)/(1.085×10^-6) ≈ 921
The maximum count of Mode0 is 2^13 (0 - 8191) and the Timer0 count will increment from 0 – 8191. So we
need to load value which is 921 less from its maximum count i.e. 8191. Also, here in the below program, we
need an additional 13 MC (machine cycles) from call to return of delay function. Hence value needed to be
loaded is,
Value=(8191-Count)+Function_MCycles+1 =7284= 0x1C74
So we need to load 0x1C74 value in Timer0.
1C74 = 0001 1100 0111 0100 b, now load lower 5-bit in TL0 and next 8-bit in TH0
so here we get, TL0 = 0001 0100 = 0x14 and TH0 = 1110 0011 = 0xE3
Mode 1 Programming
⚫ The following are the characteristics and
operations of mode1:
◦ It is a 16-bit timer
● It allows value of 0000 to FFFFH to be loaded into
the timer’s register TL and TH
◦ After TH and TL are loaded with a 16-bit
initial value, the timer must be started
● This is done by SETB TR0 for timer 0 and SETB
TR1 for timer 1
◦ After being started, it starts to count up
● It counts up until it reaches its limit of FFFFH
Mode 1 Programming (cont.)
● When it rolls over from FFFFH to 0000, it sets high
a flag bit called TF (timer flag)
● Each timer has its own timer flag: TF0 for timer 0, and TF1
for timer 1
● This timer flag can be monitored
● When this timer flag is raised, one option would be
to stop the timer with the instructions CLR TR0 or
CLR TR1, for timer 0 and timer 1, respectively
◦ In order to repeat the process
● TH and TL must be reloaded with the original value
● TF must be reloaded to 0
Steps to Mode 1 Program
⚫ Load the TMOD value register
◦ Indicating which timer (timer 0 or timer 1) is
to be used and which timer mode (1 or 2) is
selected
⚫ Load registers TL and TH with initial
count value
⚫ Start the timer
⚫ Keep monitoring the timer flag (TF)
◦ With the JNB TFx,target instruction to see if
it is raised
Steps to Mode 1 Program (cont.)
◦ Get out of the loop when TF becomes high
⚫ Stop the timer
⚫ Clear the TF flag for the next round
⚫ Go back to Step 2 to load TH and TL
again
https://www.electronicwings.com/8051/8051-timers
Finding the Loaded Timer Values
⚫ To calculate the values to be loaded into
the TL and TH registers:
◦ Assume XTAL = 11.0592 MHz
● Divide the desired time delay by 1.085 us
● Perform 65536 – n, where n is the decimal value
we got in Step1
● Convert the result of Step2 to hex, where yyxx is
the initial hex value to be loaded into the timer’s
register
● Set TL = xx and TH = yy
SJMP HERE
Mode 2 Programming
⚫ The following are the characteristics and
operations of mode 2:
◦ It is an 8-bit timer
● It allows only values of 00 to FFH to be loaded into
the timer’s register TH
◦ After TH is loaded with the 8-bit value, the
8051 gives a copy of it to TL
● Then the timer must be started
● This is done by the instruction SETB TR0 for timer 0 and
SETB TR1 for timer 1
Mode 2 Programming (cont.)
◦ After the timer is started, it starts to count
up by incrementing the TL register
● It counts up until it reaches its limit of FFH
● When it rolls over from FFH to 00, it sets high the
TF (timer flag)
◦ When TF is set to 1, TL is reloaded
automatically with the original value kept by
the TH register
◦ To repeat the process, we must simply clear
TF and let it go without any need by the
programmer to reload the original value
Mode 2 Programming (cont.)
⚫ Mode 2 can auto-reload, in contrast with
mode 1 in which the programmer has to
reload TH and TL
Steps to Mode 2 Program
⚫ Load the TMOD value register
◦ Indicating which timer (timer 0 or timer 1) is
to be used, and the timer mode (mode 2) is
selected
⚫ Load the TH registers with the initial
count value
⚫ Start timer
⚫ Keep monitoring the timer flag (TF)
◦ With the JNB TFx,target instruction to see
whether it is raised
Steps to Mode 2 Program (cont.)
◦ Get out of the loop when TF goes high
⚫ Clear the TF flag
⚫ Go back to Step 4
◦ Since mode 2 is auto-reload
(253
The number 200 is )
the timer count till
the TF is set to 1
Counter Programming
⚫ Timers can also be used as counters
◦ Counting events happening outside the 8051
◦ A pulse outside of the 8051 increments the
TH, TL registers
◦ TMOD and TH, TL registers are the same as
for the timer
● Programming the timer also applies to
programming it as a counter
● Except the source of the frequency
◦ The C/T bit in the TMOD registers decides
the source of the clock for the timer
Counter Programming (cont.)
● When C/T = 1, the timer is used as a counter and
gets its pulses from outside the 8051
◦ The counter counts up as pulses are fed from
pins 14 and 15
● These pins are called T0 (timer 0 input) and T1
(timer 1 input)
Case of GATE = 1
⚫ The start and stop of the timer are done
externally through pins P3.2 and P3.3 for
timers 0 and 1, respectively
◦ Allows to start or stop the timer externally at
any time via a simple switch
Serial v/s Parallel
Communication
Serial v/s Parallel
Communication
Parallel Communication Serial Communication
Often 8 or more lines (wire conductors) The data is sent one bit at a time on a
are used to transfer data. Multiple bits single line (wire)
are transferred at a time.
Preferred for short-distance Preferred over long-distance
communication communication
Costly as more resources are required Comparatively cheaper
Speed of data transfer is high Slow
Example: SPI, I2C, UART Example: PCI
Basics of Serial Communication
• Serial communication uses single data line making it much
cheaper
• Enables two computers in different cities to communicate over the
telephone
• Byte of data must be converted to serial bits using a
parallel-in-serial- out shift register and transmitted over a single
data line
• At the receiving endthere must be a serial-in-parallel-out
shift register
• If transferred on the telephone line, it must be converted to audio
tones by modem for short distance
Modes of Serial Communication
Modes of Serial Communication
• In simplex transmissions, the computer can only send data. There
is only one wire.
• If the data can be transmittedandreceived, then it is a
duplex transmission
• Duplex transmissions can be half or full duplex depending
on whether or not the data transfer can be simultaneous
• If the communication is only one way at a time, it is half duplex
• If both sides can communicate at the same time, it is full duplex
✔ Full duplex requires two wire conductors for the data
lines (in addition to the signal ground)
Basics of Serial Communication
• Serial Communication can be
✔ Asynchronous
✔ Synchronous
Synchronous Communication
• Synchronous methods transfer a block of data (characters) at a
time
• The events are referenced to a clock
• Example: SPI bus, I2C bus
Asynchronous Communication
• Asynchronous methods transfer a single byte at a time
• There is no clock. The bytes are separated by start and stop bits.
• Example: UART
Basics of Serial Communication
• To support serial communication, special interfaces are built in
the microcontroller.
• The microcontrollers use special IC chips called UART
(universal asynchronous receiver-transmitter) and USART
(universal synchronous asynchronous
receiver-transmitter)
• 8051 chip has a built-in UART
Data Framing in Asynchronous Serial
Communication
• Data is transmitted in 0s and 1s
• To have a sense of synchronization between transmitter and
receiver and to make sense of the data, transmitter and
receiver agree on a set of rules i.e protocol, which describes
✔ how the data is packed
✔ how many bits constitute a character
✔ when the data begins and ends
Data Framing in Asynchronous Serial
Communication
Start and stop bits
• Each character is placed between start and stop bits. This is
called framing.
• Start bit is always one bit, stop bit can be one, two or one and
half bits
• In 8051 serial port, when there is no transmission, the TxD
line is held high. This is called mark.
• Start bit is always a 0 (low), stop bit(s) is 1 (high)
• LSB is sent out first
Data Framing in Asynchronous Serial
Communication
Framing ASCII
A
RI (receive interrupt)
• When the 8051 receives data serially via RxD, it places the
byte in the SBUF register then 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
Programming the 8051 to transfer
character bytes serially
1. TMOD register is loaded with the value 20H, indicating the use of
timer 1 in mode 2 (8-bit auto-reload) to set baud rate
2. The TH1 is loaded with one of the value to set baud rate for serial
data transfer
3. The SCON register is loaded with the value 50H, indicating serial
mode 1, where an 8- bit data is framed with start and stop bits
4. TR1 is set to 1 to start timer 1
5. TI is cleared by CLR TI instruction
6. The character byte to be transferred serially is
written intoSBUF register
7. The TI flag bit is monitored with the use of instruction JNB TI,xx
to see if the character has been transferred completely
8. To transfer the next byte, go to step 5
Steps that 8051 goes through in
transmitting a character via TxD
1. The byte character to be transmitted is written into the SBUF
register
2. The start bit is transferred
3. The 8-bit character is transferred on bit at a time
4. The stop bit is transferred
✔ It is during the transfer of the stop bit that 8051 raises the TI
flag, indicating that the last character was transmitted
5. By monitoring the TI flag, we make sure that we are not
overloading
the SBUF
✔ If we write another byte into the SBUF before TI is raised, the
untransmitted portion of the previous byte will be lost
6. After SBUF is loaded with a new byte, the TI flag bit must be
forced to
0 by CLR TI in order for this new byte to be transferred
Importance of TI Flag
• By checking the TI flag bit, we know whether or not
the 8051 is ready to transfer another byte
• If we write a byte into SBUF before the TI flag
bit is raised, we risk the loss of a portion of the
byte being transferred
Programming the 8051 to receive
character bytes serially
1. TMOD register is loaded with the value 20H, indicating the use of
timer 1 in mode 2 (8-bit auto-reload) to set baud rate
2. The TH1 is loaded with one of the value to set baud rate for serial
data transfer
3. The SCON register is loaded with the value 50H, indicating serial
mode 1, where an 8- bit data is framed with start and stop bits
4. TR1 is set to 1 to start timer 1
5. RI is cleared by CLR RI instruction
6. The RI flag bit is monitored with the use of instruction JNB RI,xx
to see if the entire character has been received yet
7. When RI is raised, SBUF has the byte, its contents are moved into
a safe place
8. To transfer the next byte, go to step 5
Steps that 8051 goes through in
receiving a character via RxD
1. It receives the start bit
✔ Indicating that the next bit is the first bit of the character byte it is
about
to receive
2. The 8-bit character is received one bit at time
3. The stop bit is received
✔ When receiving the stop bit 8051 makes RI = 1, indicating that an
entire character byte has been received and must be picked up before
it gets overwritten by an incoming character
4. By checking the RI flag bit when it is raised, we know that a character
has been received and is sitting in the SBUF register
✔ We copy the SBUF contents to a safe place in some other register or
memory before it is lost
5. After the SBUF contents are copied into a safe place, the RI flag bit
must be forced to 0 by CLR RI in order to allow the next received
character byte to be placed in SBUF
✔ Failure to do this causes loss of the received character
Importance of RI Flag
• By checking the RI flag bit, we know whether or not
the 8051 received a character byte
• If we copy SBUF into a safe place before the RI flag
bit is raised, we risk copying garbage
Steps that 8051 goes through in
receiving a character via RxD
1. It receives the start bit
✔ Indicating that the next bit is the first bit of the character byte it is
about
to receive
2. The 8-bit character is received one bit at time
3. The stop bit is received
✔ When receiving the stop bit 8051 makes RI = 1, indicating that an
entire character byte has been received and must be picked up before
it gets overwritten by an incoming character
4. By checking the RI flag bit when it is raised, we know that a character
has been received and is sitting in the SBUF register
✔ We copy the SBUF contents to a safe place in some other register or
memory before it is lost
5. After the SBUF contents are copied into a safe place, the RI flag bit
must be forced to 0 by CLR RI in order to allow the next received
character byte to be placed in SBUF
✔ Failure to do this causes loss of the received character
Note: No need to use timers in case of Mode-0 and Mode-2 operation of
serial communication since timers do not control the baud rate.
Doubling the Baud Rate in 8051
• There are two ways to increase the baud rate of data transfer
✔ To use a higher frequency crystal
✔ To set the SMOD bit in the PCON register
• PCON register is an 8-bit register, whose MSB is SMOD
• When 8051 is powered up, SMOD is zero
• We can set it to high by software and thereby double the baud
rate
• PCON is not bit-addressable register. Hence, we cannot set
SMOD bit MOV A, This;place
directly. maya be
copydone as: in
of PCON
PCON ACC
SETB ;make D7=1
ACC.7 ;changing any other bits
MOV
Doubling the Baud Rate in
8051
8051 Interrupts
❑ By Mov instruction
IP.7: reserved
IP.6: reserved
IP.5: Timer 2 interrupt priority bit (8052 only)
IP.4: Serial port interrupt priority bit
IP.3: Timer 1 interrupt priority bit
IP.2: External interrupt 1 priority bit
IP.1: Timer 0 interrupt priority bit
IP.0: External interrupt 0 priority bit
Interrupt Priorities Example
--- --- PT2 PS PT1 PX1 PT0 PX0