Module1 Module3 All in One
Module1 Module3 All in One
× × × × OF DF IF TF SF ZF × AF × PF × CF
× × × × OF DF IF TF SF ZF × AF × PF × CF
This flag is specifically Interrupt Flag (I) – Trap Flag (T) –If TF=1,
used in string This flag is for the CPU automatically
instructions. interrupts. generates an internal
If DF=1, then access If IF=1, the interrupt after each
the string data from microprocessor will instruction, allowing a
higher memory recognize interrupt program to be inspected
location towards lower requests from the as it executes instruction
memory location. peripherals. by instruction.
If DF=0, then access If IF=0, the If TF=0, no function is
the string data from microprocessor will performed.
lower memory not recognize any
location towards interrupt requests and
higher memory will ignore them.
location.
The Execution Unit contd..
Solved Example on carry Flag
Q. Find the status of the flags CF, SF, AF after the following instructions are executed.
MOV AL, 35H
ADD AL, 0CEH
35H 0011 0101
+ CEH 1100 1110
103H 1 0000 0011
CF = 1 since there is a carry out from D7.
SF = 0 since the sign bit (MSB) of the 8-bit destination is 0.
AF = 1 since there is an overflow from D3 to D4.
Control Flags There are three control flags ; the Trap flag, Direction flag &Interrupt flag. The
control flags have to be deliberately set or reset according to the requirements of the
program. The Trap flag (TF) is set to perform step by step execution, during debugging. The
Interrupt flag (IF) is set to enable interrupts. The Direction flag (DF) is used in string
operations.
Arithmetic Logic Unit It is the part of a computer that performs all arithmetic and logic
computations. The ALU is the most important unit of the processor. Instructions that are
fetched and decoded, are executed in the ALU. Thus the ALU has direct access to the general
purpose registers and flags.
Bus Interface Unit (BIU)
This unit BIU is responsible for address calculations, pre-fetching instructions for the
queue and sequencing instructions one by one.
The Instruction Queue
Instructions are found in memory, from where they are fetched and decoded as
and when they need to be executed.
This pre-fetching belongs to a class of ideas called pipelining, which means that
both execution and fetching take place at the same time i.e., while the execution
of one instruction is going on, the fetching of another one can be done.
EU
BIU
Processor
Memory Segmentation
The stack is used to keep aside address and data
temporarily, when a subprogram is called
Base address Extra segment use to store data in a special way
SS
A0000H
Segment Register
ES These registers are all 16-
90000H A000H SS
bit in size. Each register
9000H ES stores the base address of
7000H DS the corresponding
DS Segment.
70000H 5000H CS
Address byte
22236H
-
Physical address -
=22220H + -
0016H - Data
22236H
OFFSET=0016H (displacement) Segment
BYTE-2
BYTE-1
Base address
BYTE-0 2222H DS Register
22220H
Memory Segmentation contd…
The Code Segment and the Instruction Pointer
The code segment is the area of memory where code alone is stored.
The offsets within the code segment are referenced using the Instruction Pointer (IP),
which is a 16-bit register.
The IP sequences the instructions, and always points to the next instruction to be
executed.
Whenever an instruction byte has to be fetched from memory, the bus interface unit (BIU)
performs the address calculation using the contents of CS register and the IP.
This 20-bit address is then placed on the address bus and the instruction byte is fetched.
Thus the logical address for an instruction bye is of the form CS : IP.
Example Address byte
The contents of the following segment registers are as -
given. -
CS = 1111H -
-
IP = 1232H.
Calculate the corresponding physical addresses for the BYTE-2
addressed byte in a) CS BYTE-1
Base address
Ans: The base address of the code segment is 11110H. 11110H BYTE-0 1111H
The address of the next instruction to be CS Register
executed is referenced by CS and IP which is given by
11110H + 1232H = 12342H.
Memory Segmentation contd…
The Stack Segment and the Stack Pointer
The stack is an area of memory that is used in a special way.
There is a 16-bit register called Stack Pointer (SP) which points to the top of the stack.
The upper 16 bits of its base address is available in the SS register.
A stack address of the form 4466H : 0122H means that the SS register contains 4466H, the
stack pointer (SP) contains the number 0122H, and the physical address of the top of the
stack is 44660H + 0122H = 44782H
The 8086 has a stack which grows downwards (i.e., to lower memory addresses).
Thus the format SS : BP is also a logical address.
Memory Segmentation contd…
The Data Segment and Extra Segment
Both these segments store data, but in certain special cases (string instructions),
it may be necessary to list them separately.
There is an Extra Segment (ES) register to store the upper 16 bits of the base
address of the extra segment.
The offset within the data segment is also termed as an ‘effective address’.
1. It allows all address registers to have the same size as the data registers (16-bit),
while allowing the use of 20-bit physical addresses.
2. All addresses in memory are re-locatable. This means that any program or data
can be loaded in any address in memory. A re-locatable program is one which
can be placed in any area of memory and executed without change. Data is also
re-locatable. Changing the base address of the corresponding segment is the
only action we need to perform, in order to re-locate. All addresses within the
program are relative to the base address, as they are of the form Base address:
off set. All processors in the x86 family have this kind of segmentation.
Addressing Modes
For computations in assembly language, we need an opcode and operands
The way in which operands are specified in an assembly language instruction is called its
addressing mode.
Let us use the MOV instruction for understanding these modes. This has the format
MOV destination, source ………. source data is copied into the destination.
Here both the source and destination are registers. No memory access is involved.
See the following instructions.
MOV AL, AH ;copy the content of AH to AL
MOV CH, BL ;copy the content of BL to CH
1. Direct Offset DS
v. XLAT :
IN and OUT instructions
ix. LDS :
This instruction loads the register and DS with words from the memory.
Format: LDS register, memory address of first word
The LDS instruction copies a word from the memory location specified in the instruction into
the register, and then copies a word from the next memory location into the DS register.
LDS instruction is useful in initializing the SI and DS register at the start of a string before
using one of the string instruction.
Example: [2000H]
LDS SI, [2000H] ; SI= [2001H]
[2002H]
DS= [2003H]
1. Data transfer instructions contd…
x. LES and LSS:
The LES and LSS instructions are similar to the LDS instruction, except that instead of the DS
register, the ES and SS registers are loaded, along with the register specified in the instruction
xi. LAHF:
This instruction copies the lower-byte of the flag register into AH.
FLAG register AH AL
AX
xii. SAHF:
This instruction copies the content of AH in the lower-order byte of the flag register.
FLAG register AH AL
AX
2. Arithmetic Instructions
The arithmetic instructions in 8086 are used to perform
o Addition
o Addition with carry
o Subtraction
o Subtraction with borrow
o Increment
o Decrement
o Negation (changing sign)
o Comparison
o Multiplication
o Division
o Decimal-adjust after addition
o Decimal-adjust after subtraction
o Processing of ASCII data
2. Arithmetic Instructions contd..
i. ADD:
Format: ADD destination, source
The source may be immediate number, register or a memory location.
The destination can be register or a memory location.
Note: Source and destination both cannot be memory location.
Example:
ADD BX, 80H ; Add the immediate data 80H to BL.
ADD CX, 12B0H ; Add the immediate data 12B0H to CX.
ADD AX, CX ; Add the content of AX and CX and store the result in AX.
ADD AL, [BX] ; Add the content of AL and the byte from the memory at [BX] and store
the result in AL.
ii. ADC:
This instruction add the data source and destination with the content of carry flag and
stores the result in the destination.
iii. SUB:
Format: SUB destination, source
It subtract the number in source from number in the destination and stores the result in
destination.
Example:
SUB AL, BL ; Subtract BL from AL and store result in AL
Example of ADC
2. Arithmetic Instructions contd..
iv. SBB: Subtract with borrow
Format: SBB destination, source
The SBB instruction subtracts the content of the source and carry flag from the content of
the destination and stores the result in the destination.
In this above example, the subtraction of 62H and F1H will result in 71H with a borrow
of 1. Next, we have to subtract 44H and 13H along with this borrow value of 1.
To facilitate this, SBB instruction has been provided to subtract two numbers along with
the borrow value
SBB E as an example instruction of this type Before After
Let us suppose, Accumulator’s
(E) 13H 13H
initial content is 44H and E
register’s initial content is 13H and (A) 44H 30H
Cy is set to 1
2. Arithmetic Instructions contd..
v. INC:
The increment (INC) instruction adds 1 to the content of a specified register or a
memory location.
Example:
INC CL; Increment the content of CL by 1.
INC BYTE PTR [BX] ; Increment the byte in the memory at [BX] by
1.
INC WORD PTR [SI] ; Increment the word in the memory at [SI] by 1.
In these examples the term BYTE PTR and WORD PTR are assembler directives,
which are used to specify the type of data (byte or word) to be incremented in
the memory.
vi. DEC:
The decrement instruction subtract 1 from the content of a specified register or
memory location.
vii. NEG:
The negate instruction replaces the byte or word in the specified register or
memory location by it’s 2 compliment (i.e. changes the sign of a data)
2. Arithmetic Instructions contd..
Example:
NEG AL ; Take 2’s compliment of the data in AL and store it in AL.
NEG BYTE PTR [BX] ; Take 2’s complement of the byte in the memory at [BX] and store
the result in the same place.
NEG WORD PTR [BX] ; Take 2’s complement of the word in the memory at [SI] and store
the result in the same place.
viii. CMP:
Format; CMP destination, source
This instruction compare a byte or word in the source with a byte word in the destination.
The comparison is done by subtracting the content of source from that of the destination.
Example:
After the instruction CMP AX, DX is executed, the status of CF, ZF and SF will be as follows
CF ZF SF
If Ax=DX 0 1 0
If AX>DX 0 0 0
If AX<DX 1 0 1
2. Arithmetic Instructions contd..
ix. MUL:
The multiply instruction is used for multiplying two unsigned bytes or words.
Format: MUL source
The source can be memory location or register which is considered as multiplier.
The multiplicand is taken by default from AL and AX for byte and word type data
respectively.
Note: The result of multiplication is stored in AX for byte data type.
For the word data type, the result of multiplication is stored in AX-DX (i.e. the most
significant word of the result in DX and the least significant word of the result in AX)
Example: byte with byte multiplication
MOV AL, 23H 35×15=525=20DH;
MOV BH, FH After multiplication AX= 20DH
MUL BH
Example: word with word multiplication
The two words of 16-bits on multiplication can produce a 32-bit word.
Lower bytes of word are stored in AX and higher bytes are stored in DX
MOV AX, 90C3H
MOV BX, 12DAH 90C3×12DA=AA8FC0E;
MUL BX After multiplication AX=FC0E & DX=AA8
2. Arithmetic Instructions contd..
x. IMUL
IMUL instruction are is used for multiplying the signed byte or word in a register or memory
location with AL or AX, and store the result in AX or AX-DX, respectively.
Example:
IMUL BL ; Multiply AL with BL and store the result in AX
IMUL AX ; Multiply AX with AX and store the result in DX-AX.
xi. DIV
The divide (DIV) instruction is used for dividing unsigned data.
Format: DIV source.
Source is a divisor which can be byte or word in a register or memory location.
The dividend is taken by default from AX and DX-AX for byte and word type data division.
Dividend Divisor Quotient Remainder
AX (16-bit) Source (8-bit) AL (8-bit) AH (8-bit)
DX-AX (32-bit) Source (16-bit) AX (16-bit) DX (16-bit)
Example:
DIV DL ; Divide the word in AX by the byte in DL. The quotient is stored in AL
and the remainder in AH
DIV CX ; Divide the double word (32-bits) in DX-AX by the word in CX. The
quotient is stored in AX and the remainder in DX
2. Arithmetic Instructions contd..
xii. IDIV
The IDIV instruction is used for dividing signed data. The general forms and rules for IDIV is
same as those for DIV instruction.
ii. OR:
Format: OR destination, source
The OR instruction performs a logical OR operation between the corresponding bits in the
source and destination and stores the result in the destination.
iii. XOR:
Format: XOR destination, source
The XOR instruction performs a logical XOR operation between the corresponding bits in
the source and destination and stores the result in the destination.
iv. NOT:
The NOT instruction inverts each bit (i.e. performs 1’s compliment of the byte or word at a
specified destination). The destination can be register or a memory location.
Example:
NOT AL ; Take 1’s compliment of AL.
3. Logical Instruction contd…
v. TEST:
This instruction ANDs the content of a source byte or a word with the content of the
specified destination byte or word. The TEST instruction is used to set flags before a
conditional jump instruction.
Format: TEST destination, source
Example:
Let AL=0111 1111=7FH
TEST AL, 80H ; AL=7FH (unchanged)
ZF=1 since (AL) AND (80H)=00H;SF=1
4. Flag manipulation instruction
The 8086 has a few instructions exclusively for performing operations on the flags in the
flag register. They are used to set or clear specific flags in the flag register, to push or pop
the flag register content to the AH register and vice-versa. Following are flag manipulation
instructions in 8086:
LAHF: Load the low-order byte of the flag register in AH
SAHF: Store AH in the low-order byte of the flag register.
PUSHF: Push the flag register’s content onto the stack.
POPF: Pop the top word of the stack onto the flag register.
CMC: Compliment the carry flag.
CLC: Clear the carry flag (CF=0)
STC: Set the carry flag (CF=1).
CLD: Clear the direction flag (DF=0)
STD: set the direction flag (DF=1)
CLI: Clear the interrupt flag (IF=0)
STI: Set the interrupt flag (IF=1)
5. Control Transfer instructions
MUL DL
RET
5. Control Transfer instructions contd…
Unconditional Transfer Instruction
1. CALL Instruction :
The CALL instruction is used to Program Execution Transfer Instructions in 8086
Microprocessor to a subprogram or procedure.
i. SAL/SHL:
Format: SAL/SHL destination, count
The designation can be register or a memory location and a byte or a word.
The instruction shifts each bit in the designation a specified number of bit position to the
left.
As a bit is shifted out of LESB position, 0 is placed in the LSB position. The MSB is shifted
into the carry flag (CF).
CF MSBLSB0
Example:
SAL AL, 1 ; shift left the content of AL by one bit.
AL
CF 0 1 0 1 0 1 1 0 0 Content of AL before SAL
CF before
SAL
×8
The content of AL after SAL, 3 0 0 0 0 1 0 0 0 AL=8
6. Shift/Rotate contd...
ii. SAR:
Format: SAR destination, count
The designation can be register or a memory location and a byte or a word.
The instruction shifts each bit in the designation a specified number of bit position to the
right.
As a bit is shifted out of MSB position, a copy is put into the MSB position (i.e. sign bit is
copied into the MSB). The LSB is shifted into the carry flag (CF).
MSB MSBLSBCF
Example:
SAR AL, 1 ; shift right the content of AL by one bit.
AL
0 1 0 1 0 1 1 0 CF Content of AL before SAR
Example:
SAR AL, 1 ; shift left the content of AL by one bit.
AL
0 0 1 0 1 0 1 1 0 CF Content of AL before SHR
Example:
ROR CH, 1 ; Rotate right the byte in CH by one bit position.
v. ROL:
ROL rotate all the bits in a byte or word in the destination to the left, by one or more bit
position, using CL, as follows:
CFMSBLSB
The data bit moved out of the MSB is copied into CF.
vi. RCR:
RCR rotates byte or word in the destination to the right, through the carry flag CL, either by
one bit position or by the number of bit position given by CL:
CFMSBLSB
vii. RCL:
RCR rotates byte or word in the destination to the left, through
the carry flag CL, either by one bit position or by the number of
bit position given by CL: CFMSBLSB
7. String Instruction
String is a series of data byte or word available in memory at consecutive locations. It is
either referred as byte string or word string. Their memory is always allocated in a
sequential order.
Instructions used to manipulate strings are called string manipulation instructions.
The register SI contains the offset address of an element (byte or word) in the source
string, which is present in the data segment.
The register DI contains the offset address of an element (byte or word) in the
destination string.
The source string is in data segment at the offset address given by SI
The destination string is in extra segment at the offset address given by DI.
After each string operation, SI and/or DI are automatically incremented or decremented
by 1 or 2 (for byte or word operation) according to D flag in the register (see slide 70)
If D=0, SI and or DI are automatically incremented and D=1, SI/DI are automatically
decremented.
7. String Instruction contd…
Mnemonics Function
MOVSB Move the string byte from DS:[SI] to ES:[DI]
MOVSW Move the string word from DS:[SI] to ES:[DI] (see example in next slide)
CMPSB Compare string bytes (done by subtracting the bytes at ES:[DI] from the bytes at DS:
[SI])
CMPSW Compare string words (done by subtracting the bytes at ES:[DI] from the bytes at DS:
[SI])
LODSB Load the string byte at DS:[SI] into AL
LODSW Load the string word at DS:[SI] into AX
SCASB Compare string bytes (done by subtracting the bytes at ES:[DI] from the bytes at AL)
SCASW Compare string word (done by subtracting the word at ES:[DI] from the word at AX)
REP Decrement the CX and repeat the following string operation if CX0 (next slide)
REPE/REPZ Decrement the CX and repeat the following string operation if CX0 and ZF=1
REPNE/REPNZ Decrement the CX and repeat the following string operation if CX0 and ZF=0
STOSW Load the string word in AX at ES:[DI]
STOSB Load the string byte in AL at ES:[DI]
7. String Instruction contd…
The REP (repeat) prefix placed before a string instruction causes the
Start
string instruction to be executed CX times.
Example:
MOV CX, 32H ; Load 32H (=decimal 50) in CX
MOV 32H (50) into
REP MOVSW ; Execute MOVSW instruction 50 times
register CX
Execution of these two instructions causes the moving of a string
having 50 words from the data segment to the extra segment.
Decrement the
SS content of CX reg.
ES data will be
moved from
word-50 Move a string word
word-
word-
Data segment
from DS to ES
(DS) to Extra
2
1
segment (ES)
DS 50 times
Is
NO CX=0?
YES
CS
End
7. String Instruction contd…
Memory address
30000H When 1-byte
data is moved
30001H then SI/DI is
30002H decremented or
ES 30003H incremented by
1.
30004H For 2-byte
DI contains offset
30005H (word) data
address of
movement ,
destination string
SI/DI is
70000H incremented or
8-bit data 70001H decremented by
70002H 2.
8-bit data
DS 8-bit data 70003H
For 4-byte(2
word) data
8-bit data 70004H movement, SI/DI
70005H SI contains offset is increment or
8-bit data (1-byte)
address of source decrement by 4.
string
8. Machine control instruction
The machine/processor control instructions in the 8086 include HLT, LOCK, NOP, ESC, and
WAIT.
i. HALT:
The halt instruction stops the execution of all instructions and places the processor in the
halt state. An interrupt or a Reset signal causes the processor to resume execution from the
halt state.
ii. LOCK:
The lock instruction provides the processor an exclusive hold on the use of the system bus. It
activates an external locking signal () of the processor and is placed as a prefix to the
instruction for which a lock is to be asserted. The lock functions only with the XCHG, ADD,
OR, ADC, SBB, AND, SUB, XOR, NOT, NEG, INC and DEC instruction.
iii. NOP:
No operation– This instruction is used to insert a delay in software delay programs
iv. ESC:
This instruction is used to pass instructions to a coprocessor such as 8087, which shares the
address and data bus with an 8086.
v. WAIT:
When this instruction is executed, the 8086 checks the status of its TEST input and if the
TEST input is high, it enters an idle condition during which it does not do anything.
PIN Details of 8086
Description
Intel 8086 is a 16-bit HMOS
microprocessor.
It is available in 40 pin DIP chip.
It uses a 5V DC supply for its
operation.
The 8086 uses 20-line address bus.
It has a 16-line data bus.
The 20 lines of the address bus
operate in multiplexed mode. The
16-low order address bus lines have
been multiplexed with data and 4
high-order address bus lines have
been multiplexed with status signals
It is also seen that some pins have dual functions, and this corresponds to two
modes of operation – minimum and maximum modes
PIN Details of 8086 contd…
Modes of operation of 8086
Applications of 8086:
•Microcomputer are built using 8086. For example : IBM PC, used the Intel
8088, a version of the 8086 with 8-bit data bus.
•It is used in calculators.
•It is used for control purposes like in traffic signals(uses micro controllers
which are nothing but contains one or more CPUs along with memory
and programmable i/o peripherals).
Min Mode
8286 (8
bits)
transceiver
s:
Control
signals
provided by
8086 for
memory
operations
and i/o
interfacing
Min Mode
Control signals for all operations are generated by decoding M/IO’, RD’,
WR’. They are decoded by 74138 3:8 decoder
The address
values is saved
in LATCH when
ALE=1
These lines
are free to
carry data
when ALE=0
S0, S1, S2
These are the status signals that provide the status of operation, which is
used by the Bus Controller 8288 to generate memory & I/O control
signals. These are available at pin 26, 27, and 28. Following is the table
showing their status −
S2 S1 S0 Status
0 0 0 Interrupt
acknowledge
ment
0 0 1 I/O Read
0 1 0 I/O Write
0 1 1 Halt
1 0 0 Opcode fetch
1 0 1 Memory read
1 1 0 Memory
write
1 1 1 Passive
PIN Details of 8086 contd…
Minimum Mode Pin Functions
Pin No. Designation Function Type
16 to 2, 39 AD0–AD14, 16 multiplexed address / data lines Bidirectional
AD15 which carry address when ALE is high,
and later functions as data lines
D0–D15, when ALE is low
S4 S3
0 0 Extra Segment
0 1 Stack Segment
1 0 Code or no Segment
1 1 Data Segment
PIN Details of 8086 contd…
Minimum Mode Pin Functions
Pin No. Designation Function Type
32 When this signal is low, data can be received from Output
memory or input devices
25 ALE Address Latch Enable’ – this signal goes high in the Output
beginning of a bus cycle and indicates that the
multiplexed address bus contains address
information
Microprocessor Instruction
Step-2: RD signal is sent to Memory chip codes are
(Instruction
code is decoded stored inside
and executed) Memory-chip
Step-3: Content of the address location is placed
on data bus from memory chip
In T1, the address is placed on
the lines AD0 to AD15 and
A16 to A19 .
BHE is high or low depending
on the type of data (byte /
word) to be accessed. ALE
goes high.
M / IO signal is high from T1
to T4.
Also the DT/ R is low from T1
to T4 indicating that data is to
be ‘received’ by the processor.
In T2, the address information is removed from AD0 to AD15. The address information is removed from
A16 / S3 to A19 / S6 and BHE / S7 also, and these lines now carry status information. In T2, the RD
signal (which is the READ control signal) is made low. In T2, DEN goes low to enable the data bus buffer
outputs
The clock cycle T3 is to allow ‘access time’ for bringing data from memory / IO and put it on the data
bus. Thus, after the data appears on the data bus, it is transferred to the 8086 at the end of T3.
In T4, all the bus signals are de-activated in preparation for the next bus cycle. This machine cycle ends
with T4, and the next machine cycle is scheduled to start.
Timing Diagrams contd…
Write Machine Cycle
The steps in a write machine cycle are :
1. Place on the address bus, the address of the location to which data is to be written.
2. On the data bus, place the data to be written.
3. Assert the write control signal which is part of the control bus.
4. Wait until the data is stored in the addressed location.
5. De-activate the memory write signal. This ends the memory write operation.
For the 8086, the control signals for a write machine cycle is WR. Also the DT / R signal will
be high (for ‘data transmit’) for writing.
The write bus cycle also uses 4 T states normally.
This mode of operation was designed by Intel for allowing the 8086 to communicate
with other processors like the arithmetic co-processor (8087) and the input /output
processor 8089.
It was also used to allow 8086 to be used in large loosely coupled multiprocessor
systems. This mode was dropped from Intel’s designs from 80286 onwards.
Later processors (80486 onwards) had the arithmetic co-processor integrated on the
processor chip itself.
PIN description for Maximum mode
Min
mode (Max mode)
√
√
√
Problem – Write a program to transfer a block of 4 bytes, starting address is 0500 and transfer
the block at address 0600 by using string instructions
04 18 AD 05 04 18 AD 05
Mnemonics Comments
MOV SI, 500 SI500 Move the offset address 500 of Data segment to SI
MOV DI, 600 DI600 Move offset address 600 of Extra segment to DI
MOV DS, AX Move 0000H to DS register so that base address of data segment is
0000H
MOV ES, AX Move 0000H to ES register so that base address of Extra segment is
0000H
0A
04 03 02 01
0600
0500 0501 0502 0503
Mnemonics Comments
HLT Stop
1. Program to find average of ‘n’ numbers
Problem – Write a program to find average of ‘n’ numbers. Assume four numbers stored in
memory address 500, 501, 502, 503. Store the result in address 600
2.5
04 03 02 01
0600
0500 0501 0502 0503
Mnemonics Comments
HLT Stop
1. Program for Sorting an array in ascending order
D0
A0
: Memory write
AN-1 DM-1
𝑊𝐸 𝐶𝑆 𝑂𝐸
𝑴𝑬𝑴𝑾𝑹
𝑆𝐸𝐿𝐸𝐶𝑇 𝑴𝑬𝑴𝑹𝑫
Active low signals is to be connected to the signal from the processor side for the write
operation to be done in the addressed location. For reading, the pin (from the processor side)
is to be connected to the OE (output enable) pin of the memory chip.
The number of address lines (A0 to AN-1) depends on the number of locations it contains
20 parallel wires
A0 D0
Address Data
Lines Lines
RAM
A19
D15
𝑾𝑬 𝑪𝑺𝑶𝑬
wire 𝑺𝑬𝑳𝑬𝑪𝑻
wire
Note:
A ROM chip does not have the
WE pin, as it can only be read
from
256×8 RAM, means that it has 8 data lines (D0 to D7) and a storage capacity of 256
locations – thus it needs 8 address lines (A0 to A7) as 256=28.
A0 D0
Address
Data
Lines
RAM Lines
A7 256=28
D7 Address
𝑾𝑬 𝑪𝑺𝑶𝑬 locations 8-bit storage
𝑺𝑬𝑳𝑬𝑪𝑻
Think of the following cases then – a 1K×8 RAM needs 10 address lines, as 1K=1024=210, a
2K×8 RAM has 11 address lines and so on. Thus, it is the capacity of the chip which
decides the number of address lines
Example:
How many address and data lines are needed for the memory chips with the following
organization?
1. 256×4
2. 512×8
3. 1K×16 Home work
4. 32K×8 Home work
5. 128K×8
Solution:
1. 256×4 means, it has 4 data lines, and 256 memory locations, each of width 4 bits. 256=2 8.
Thus, 8 address lines are needed
2. 512×8
This chip has 8 data lines.
512=29. Thus, it has 9 address lines
5. 128K×8
it has 8 data lines
128K=128×1024=27×210 =217
So, it has 17 address lines
Memory Address Decoding
Fig 1 shows a 2K×8 memory chip A0
A0-A10
This memory chip has 11 address lines, A10
Thus, it is obvious that the chip is selected (enabled) only if all the input lines of the
NAND gate are high – which means that A11-A19 of the address has to be high for the
memory chip to be selected and made active.
The NAND gate thus functions as the address decoder for the memory.
Example
i) Find out the range of addresses that the memory chip in Fig 1 contains.
Solution
A0-A10 The address lines from to A0-A10 can
vary from 000 0000 0000 to 111 1111
Memory
1111.
A11 2K×8
The address A11-A19 lines must always
A19 be 1111 1111 1 for this chip to be
𝑪𝑺 selected.
As such, the lower and upper range of
the addresses in the chip are seen as:
𝑺𝑬𝑳𝑬𝑪𝑻
A19 A18 A17 A16 A15 A14 A13 A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 A0
1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 0 0
The first address on the chip: 1 1 0 0 0
1 1 0 0 0
The last address on the chip: 11 11 1111
0 1111
0 1111
0
1 1
1 1
Thus, the address range of the chip1 is FF800H to
1 FFFFFH i.e., 2048 bytes i.e., 2 K bytes
Address Decoding Concepts
Thus the basic idea of address decoding is to decode the extra unused address lines of the
processor to specify the address range.
When more chips are to be interfaced, decode the extra address lines to a different range
for each group.
Any logic /logic gate can be used to perform address decoding. From the address decoder
output, the right logic value should be obtained to turn the chip on. Thus the ‘select’ pulse
obtained from the address decoding logic can be high or low, depending on the
requirement of the chip select pin of the memory chip.
In general, address decoders can be built using:
– Random logic (simple gates, like NAND fate , OR gate)
– Block decoders (e.g. 2 × 4, 3 × 8 …)
– Programmable logic (PLAs, CPLDs, FPGAs, …)
The first two types of address decoders will be discussed in detail here.
Example 7.3
Design an address decoder using OR logic for a 32 K × 8 RAM. Find the address space of this
memory chip.
Solution
32K=1015, it has 15 address lines. Thus the lower 15 lines of the address bus of the processor
are connected to the address lines of the RAM. The upper 5 lines A15–A19 are used for
address decoding. Here the lines are given as input to the OR gate. When the address on the
address bus corresponds to A15 to A19 being all at logic 0, the decoder gives the SELECT
pulse to the CS pin of the memory chip and thus it is selected.
D0
A0 Data bus
D7
Address bus RAM
A14
A15 𝑪𝑺
OR
A19 𝑺𝑬𝑳𝑬𝑪𝑻
Fig. 1 Address decoding using OR logic
0000 0000 0 0 0
The first address on the chip: 0 0 0
0 0 0
The last address on the chip: 0000 0 1111
0 1111
0 1111
0
1
1
Thus, the address range of the chip is 00000H to
1 07FFFH
Example
Find the address space of the following chips with the address decoding circuitry as shown in
the Fig.1 for 8K×8 EPROM
Solution
A0 D0 8K=213 memory locations has 13 address
Address
lines. The remaining 7 lines of the
Data
Lines EPROM Lines
processor system bus are used for
8K×8 address decoding.
A12 For the EPROM chip to be selected, the
D7 values of A19 to A13 are to be 1111 101.
𝑪𝑺𝑶𝑬
Thus, the lowest and highest addresses in
1=A19 the chip:
1=A18
1=A17 1111 1010 0 0 0
1=A16 0 0 0
0 0 0
1=A15 0 to0 0
0=A14 1111 1011 1111 1111 1111
𝑺𝑬𝑳𝑬𝑪𝑻
1=A13 For example, FA000H to FBFFFH.
Fig. 1 shows a case when two 32 K × 8 RAMs form the16-bit memory. Thus the total memory is
64 K × 8 bytes. 32 K RAM needs 15 address lines. Of the 20 address bits from the address bus
of the processor, A0 is used to enable the even bank. A1 to A15 are connected to the address
pins A0 to A14 of each RAM chip. The rest of the address lines are used for address decoding.
A0 and are used for selecting each of the banks separately.
Programmable Peripheral Interface (PPI)-8255
8086 processor computes what is expected of it and transfers it to the outside world – in
essence, the outside world is external to the processor – memory and I/O devices are
external to the processor
However, I/O devices are of different varieties, functions, features, specifications and so on.
For example, a keyboard is very different from a video monitor. The point is that the 8086
does not have the requisite hardware within it to manage each of them according to their
special requirements and specifications
8086
Processor
Interfacing chip
(8255) √
No direct
connection
I/O devices
(Keyboard, LED display)
8255 contd…
Thus, I/O devices need some extra hardware to let them be interfaced to the processor.
It is in this context that various interfacing chips have been designed.
All these chips are programmable in the sense that they can be made to work in different
ways as required, by ‘programming’ them, using 8086 instructions.
Thus, there are separate chips for managing parallel data transfer, serial data transfer,
keyboard, interrupts and timers for example.
This 8255 chip is also called a parallel port chip and it eases out the problems and issues
related to parallel data transfer.
We have transferred 8/16 bits at one go either to/from memory or I/O devices. The I/O
devices we have discussed were quite simple and needed only a read/write control signal
and a select pulse generated during the read/write cycle.
However, when I/O devices are more versatile and have more features, a PPI will be very
useful, especially when more than one I/O device is to be interfaced to the processor
8255 contd…
Port A Keyboard
Group A and B Controls Functionally this chip has been divided as Group A and B and they
have their corresponding controls. The way the chip is to act is decided by a register called the
control/status register which can be written to and read from.
1. MP doing some work Interrupt 2. Interrupted by
external device
wire
Peripheral
2000 MVI AL,5
3. Go to the (Keyboard)
2001 MVI BL,4 address of
2002 ADD AL,BL Interrupt 3000 Interrupt
Service
2003 Next 3001 Routine
instruction 3002 (ISR)
3003
4. MP spend some
time in executing
8086 PROCESSOR ISR & return back
to main program 3004 IRET
1st option--Processor continually keeps checking if a key has been pressed (polling)
When a key is pressed, the processor goes on to the task of identifying the key.
However, if the processor is ‘polling’ for a key press, it is obviously waiting and doing nothing
else. This is just a waste of processor time.
2nd option--A better way of organizing the setup would be to let the processor do some task,
and ‘interrupt’ it only when a key is pressed
1. Working inside home 2. Doorbell rings
When an interrupt occurs, the processor suspends the execution of its current task and takes
on another task as required by the interrupting source. This program, or routine as it may be
called, is designated as an ‘interrupt service routine’
This means that for any interrupt that occurs, there is a particular interrupt service routine (ISR).
Where is this ISR available?
Thus, the interrupt vector for any interrupt has 4 bytes – two for the CS value and two for the
IP value
Thus, if the interrupt vector for a particular interrupt is obtained, control can be transferred
to the new location by using the new values of CS and IP specified as the ‘interrupt vector’.
Interrupt Vector Table
The 8086 has 256 interrupt vectors and since each vector is specified by 4 bytes, it implies
that 256 × 4 = 1024 bytes (1K) of memory are allocated to store the interrupt vectors
These 256 vectors are stored in a table called the ‘Interrupt Vector Table’ (IVT) in system
RAM from locations 00000 to 003FFH i.e., up to 0000: 03FF
These numbers thus turn out to be what can CS INT 255 VECTOR
3FC
be called as the ‘type numbers’ of the IP
interrupts.
Thus, there are interrupts designated as INT 0,
INT 1, INT 2 …… INT 255
On division, if the quotient register is not large enough to contain the quotient, this
interrupt is generated automatically
Thus, the ISR for this error generated interrupt is written in the address 0000:0000
The ISR for viewing the register and memory contents will be pointed by the vector of
interrupt type 1.
However, an important issue in this is how to set the trap flag. No such instruction has been
encountered so far.
D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
U U U U OF DF IF TF SF ZF U AF U P U CF
Hardware interrupt service routines can be tested using software interrupts. Take the case
of NMI which is a hardware interrupt. This is vectored to location 0000:0008 i.e., it
corresponds to type number 2. The routine corresponding to this hardware interrupt can be
tested by using the instruction INT 2. In effect, a hardware interrupt on the NMI pin and the
software instruction INT 2 takes us to the same absolute address
Hardware Interrupts
There are two pins on which interrupt requests can be received – they are the INTR pin and
the NMI pin.
NMI
This is a +v edge triggered interrupt, but it is also required to have a duration in the high state
of more than two clock cycles. Any high going transition of NMI is latched on-chip and will be
serviced at the end of the current instruction. This is a non maskable interrupt, in that it does
not depend on the setting of the interrupt flag (IF). It cannot be masked or prevented from
being activated. It is a Type 2 interrupt, meaning that its vector is obtained from the
corresponding location in the interrupt vector table.
INTR
This is the non-vectored interrupt pin of the 8086, which means that when an interrupt
request is received on this pin, it does not get automatically directed towards any particular
entry in the interrupt vector table. Another feature of this interrupt is that the interrupt flag
(IF) is required to be set for an interrupt request on the INTR line to be honored. When the
processor is reset, all flags are found to be cleared and so is the IF. It is to be ensured that the
IF is set by the instruction STI if interrupts on the INTR lines are to be acknowledged
Syllabus
Evolution of Microcontrollers
Architecture of 8051 Microcontroller
Pin diagram of 8051
External memories
Counters & Timers
Serial Communication
Interrupts
Instruction set of 8051
Addressing Modes of 8051
Assembly Language Programming examples using 8051
Difference between General purpose processor & Microcontrollers
Difference between MP & MC
MP MC
Structure It has only the CPU in the chip. CPU, Memory, I/O port and all
Other devices like I/O port, other devices are connected on
memory, timer is connected the single chip.
externally.
Clock speed High. It is in terms of the GHz. It Less. It is in terms of the MHz.
ranges between 1 GHz to 4 GHz. it ranges between 1 MHz to 300
MHz.
complexity It’s complex and expensive, with It’s simple and inexpensive
a large number of instructions to with less number of
process
instructions to process
Difference between MP & MC
MP MC
Programming The program for the The program for the
microprocessor can be microcontroller is fixed once it is
changed for different designed.
applications
Bit size It is available in 32-Bit and 64- It is available in 8-bit, 16-bit, and
bit. 36-bit.
Cost The cost of the microprocessor It is cheaper.
is high compared to the
microcontroller.
Power The power consumption for The power consumption for the
consumption the microprocessor is high. microcontroller is less.
Size The overall size of the The overall size of the system is
system is large. small.
Microprocessor has a Microcontroller has more
smaller number of register. Hence the programs
registers, so more are easier to write.
operations are memory-
based.
Examples where Microcontrollers are used
Telephone, Security system, TVs, VCR, Cable TV Tuner, Home computers, Remote
controller, Video games, Paging, Camera, Cellular Phones, Fax machine , Microwave
oven, Laser printer, Color printer
8031 Microcontroller
Another member of 8051 family is the 8031 chip.
This chip is often referred to as a ROM-less 8051 since it has 0K bytes of on-chip ROM.
To use this we have to add external ROM to it.
This external ROM must contain the program that 8031 will fetch and execute.
In process of adding external ROM, we loose two ports.
That leaves only 2 ports (of the 4 ports) for I/O operations.
To solve this problem, we can add external I/O to the 8031.
8751 Microcontroller
The UV-EPROM version of 8051 is the 8751.
8751 chip has 4K bytes of on-chip UV-EPROM.
Using this chip for development requires access to a PROM burner, as well as a UV-
EPROM eraser to erase the contents of UV-EPROM inside the 8751 chip before we can
program it again.
It takes around 20 minutes to erase the 8751 before it can be programed again.
This has led many manufacturers to introduce flash NV-RAM version of 8051
Interrupt is a subroutine call that interrupts the microcontrollers main operations and
causes it to execute any other program, which is more important at the time of
operation.
The feature of Interrupt is very useful as it helps in case of emergency operations.
An Interrupts gives us a mechanism to put on hold the ongoing operations, execute a
subroutine and then again resumes to another type of operations.
The Microcontroller 8051 can be configured in such a way that it temporarily terminates
or pause the main program at the occurrence of interrupts.
When a subroutine is completed, Then the execution of main program starts.
Generally five interrupt sources are there in 8051 Microcontroller. There are 5 vectored
interrupts are shown in below
INTO
TFO
INT1
TF1
R1/T1
Memory
Microcontroller requires a program which is a collection of instructions. This program tells
microcontroller to do specific tasks. These programs require a memory on which these can
be saved and read by Microcontroller to perform specific operations of a particular task. The
memory which is used to store the program of the microcontroller is known as code memory
or Program memory of applications. It is known as ROM memory of microcontroller also
requires a memory to store data or operands temporarily of the micro controller. The data
memory of the 8051 is used to store data temporarily for operation is known RAM memory.
8051 microcontroller has 4K of code memory or program memory, that has 4KB ROM and
also 128 bytes of data memory of RAM.
BUS
Basically Bus is a collection of wires which work as a communication channel or medium for
transfer of Data. These buses consists of 8, 16 or more wires of the microcontroller. Thus,
these can carry 8 bits,16 bits simultaneously.
Address Bus:
Microcontroller 8051 has a 16 bit address bus for transferring the data. It is used to
address memory locations and to transfer the address from CPU to Memory of the
microcontroller.
Data Bus:
Microcontroller 8051 has 8 bits of the data bus, which is used to carry data of particular
applications.
Oscillator
It requires clock pulses for its operation of microcontroller applications.
For this purpose, microcontroller 8051 has an on-chip oscillator which works as a clock
source for Central Processing Unit of the microcontroller.
The output pulses of oscillator are stable.
Therefore, it enables synchronized work of all parts of the 8051 Microcontroller.
Input/output Port
Normally microcontroller is used in embedded systems to control the operation of
machines in the microcontroller.
Therefore, to connect it to other machines, devices or peripherals we require I/O
interfacing ports in the microcontroller interface.
For this purpose microcontroller 8051 has 4 input, output ports to connect it to the other
peripherals
Timers/Counters
8051 microcontroller has two 16 bit timers and counters.
These counters are again divided into a 8 bit register.
The timers are used for measurement of intervals to determine the pulse width of pulses
Different Registers and Register Bank of 8051
The most widely used registers of 8051 are
Registers Function
A: Accumulator (8-bit): To hold one of the operand in all most all arithmetic and logical
operations, accumulator A is used
B (8-bit): This is general purpose 8-bit register
R0,R1,R2,R3,R4,R5,R6,R7 These are also 8bit general purpose registers.
(8-bit):
DPTR-Data Pointer This register is used as pointer to memory address
register (16-bit):
PC- Program Counter (16- PC points to the address of the next instruction to be executed.
bit): As the CPU fetches the opcode from the program ROM, the PC is
incremented to point to the next instruction
Flag register To indicate the arithmetic conditions such as carry bit
In 8051, Flag register is
called as program status CY AC F0 RS1 RS0 OV ---- P PSW
word (PSW) : 8-bit
IE: Interrupt Enable control To enable or unmask a particular interrupt using instructions
TMOD: Timer/Counter mode control To select the operating mode and the timer/counter operation of the
timers
TCON: Timer/Counter control To control operations of counter and timers
T2CON: Timer/Counter 2 control This register contains bits controlling the operation of timer 2
T2MOD: Timer/Counter mode control Timer/Counter Mode Control
SCON: serial counter The Serial Control or SCON is used to control the 8051 Microcontroller’s
Serial Port
SBUF: Serial data buffer SBUF register holds data which is to be transmitted or received.
PCON: Power control To force the 8051 microcontrollers into power-saving mode
Register bank and Stack
8051 MC as a total 128 bytes of RAM which are assigned address from 00H to 7FH
These 128 bytes are divide into three different groups as follows:
D4 (PSW. 4) and D3 (PSW. 3) can be accessed by bit-addressable instructions SETB and CLR
Ex. SETB PSW. 3 will make PSW.3=1 and select bank register 1.
Pin 29 (PSEN): Pin 29 is the Program Store Enable Pin (PSEN). It is an output pin and using this
pin, external Program Memory can be read.
Pin 30 (ALE/PROG): Pin 30 is the Address Latch Enable Pin. It is an output pin and using this Pin,
external address can be separated from data (as they are multiplexed by 8051).
Pin 31 (EA/VPP): Pin 31 is the External Access Enable Pin i.e. allows external Program Memory. It
is an input pin and must be connected from VCC or GND. If we want to access the code from
external program memory, it must be connected with GND. If we want to use on-chip memory, it
must be high (connected with VCC).
Pins 32–39:
Port 0 uses the Pins 32 to 39. Each pin is bidirectional Input /Output but without internal pull –
up resistors. So we need to use an external pull-up before using Port0 as Input /Output.
Pin 40 (VCC): Pin 40 is the power supply pin of the 8051 Microcontroller. It should be +5V.
Addressing mode of 8051
In 8051 there are six types of addressing modes.
1. Immediate Addressing Mode
2. Register Addressing Mode
3. Direct Addressing Mode
4. Register Indirect Addressing Mode
5. Indexed Addressing Mode
6. Implied Addressing Mode
MOV R0, A;
Mnemonic Description
ADD Addition without Carry
ADDC Addition with Carry
SUBB Subtract with Carry
INC Increment by 1
DEC Decrement by 1
MUL Multiply
DIV Divide
DA A Decimal Adjust the Accumulator (A Register)
The arithmetic instructions have no knowledge about the data format i.e., signed, unsigned,
ASCII, BCD, etc. Also, the operations performed by the arithmetic instructions affect flags
like carry, overflow, zero, etc. in the PSW Register.
Instruction contd…
Logical Instructions
The next group of instructions are the Logical Instructions, which perform logical operations
like AND, OR, XOR, NOT, Rotate, Clear and Swap. Logical Instruction are performed on Bytes
of data on a bit-by-bit basis.
Mnemonic Description
ANL Logical AND
ORL Logical OR
XRL Ex-OR
CLR Clear Register
CPL Complement the Register
RL Rotate a Byte to Left
RLC Rotate a Byte and Carry Bit to Left
RR Rotate a Byte to Right
RRC Rotate a Byte and Carry Bit to Right
SWAP Swap Accumulator Nibbles
Syntax: SWAP A; SWAP swaps bits 0-3 of the Accumulator with bits 4-7
of the Accumulator
These instructions can perform set, clear, and, or, complement etc. at bit level.
Instruction contd…
Instructions Used The instruction used are LDA and STA, etc. The instruction used are IN and OUT.
Any register can communicate with the IO Only Accumulator can communicate with
Registers Communicating
device in case of Memory Mapped IO. IO devices in case of IO Mapped IO.
216 IO ports are possible to be used for Only 256 I/O ports are available for
Space Involved
interfacing in case of Memory Mapped IO. interfacing in case of IO Mapped IO.
During writing or read cycles (IO/M` = 0 ) During writing or read cycles (IO/M` = 1) in
IO/M` signal
in case of Memory Mapped IO. case of IO Mapped IO.
Arithmetic and logical operations are Arithmetic and logical operations cannot be
Arithmetic and Logical operations performed directly on the data in the case of performed directly on the data in the case of
Memory Mapped IO. IO Mapped IO.