Microprocesror and Programming Theory-KM-S4
Microprocesror and Programming Theory-KM-S4
(Theory)
CST/4/401
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
Unit 1
Basics of Microprocessor
1. Microprocessor
Microprocessor is a multipurpose programmable integrated device that has computing and
decision making capability. This semiconductor IC is manufactured by using LSI technique. It
includes all the functional elements of a CPU (Registers, ALU, CU) on a single chip.
2. Features of Microprocessor
(a) Cost
(b) Size - Small in size due to of LSI technique
(c) Power consumption - Low power consumption due to MOS technology
(d) Versatility - Keeping the some basic hardware, a microprocessor has wide versatility
depending on simple change in software program
(e) Reliability - High reliability due to lower chip count in the system
4. Micro controller
It is a device that includes microprocessor, memory and I/O signal lines on a single chip
fabricated using VLSI technique.
5. Micro Computer
It is a computer that is designed using a microprocessor as its CPU. It includes microprocessor,
memory and I/O.
6. Monitor program
It is a program that interprets the input from a keyboard and converts the input into its binary
equivalent.
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
7. First Microprocessor
INTEL 4004 was the first microprocessor introduced by Intel in 1971.
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
9. Functional Block diagram of 8085 microprocessor
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
(ii) INTA’ - Interrupt acknowledge is used to acknowledge an interrupt. Microprocessor
has two or more control signals which suspend the execution of the current program and
jump to a subroutine at a predetermined location.
(iii) RST7.5, RST6.5, RST5.5 - These restart interrupts transfer the program control to
specific memory locations. These vector interrupts has higher priorities than INTR.
(iv) TRAP - This is a non-maskable interrupt and has the highest priority.
(v) HOLD - This signal indicates DMA controller is requesting the use of the AB and
DB.
(vi) HLDA - HOLD acknowledge signal acknowledges the HOLD request.
(vii) READY - This signal is used to delay microprocessor R/W cycles until a slow
responding peripheral is ready to send or accept data.
(viii) RESTIN’ - When the signal on this pin goes low, the PC is set to 0, the buses are
tri-stated and microprocessor is reset.
(viii) RESTOUT’ - This signal is used to reset all the connected devices when the
microprocessor is reset.
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
(ii) Memory Buffer Register (MBR) - Contains a word of data when it is to be written in
the memory.
(iii) Instruction Register (IR) - Fetch instruction from memory to IR temporarily for
decoding.
(iv) Temporary Register (TR) - Every microprocessor contains some TR for proper
functioning of ALU.
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
Unit 2
16 bit microprocessor 8086
AX/AL - AX/AL is used as the accumulator. It is used in the multiplication, division, I/O
operations, decimal and ASCII adjustment instruction.
CX/CL- CX is used to hold the count value while executing the repeated string instructions
and the loop instructions. CL is used to hold the count value while executing the shift/rotate
instructions. The count value indicates the number of times the same code has to be executed
when the loop instruction is used and the number of times the data item has to be
shifted/rotated when the SHIFT/ROTATE instruction is used.
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
DX - DX is used to hold a part of the result during a multiplication operation and a part of the
dividend before a division operation. It‟s also used to hold the I/O device address while
executing the IN & OUT instructions.
Stack Pointer (SP) -SP is used to hold the offset address of the data stored at the top of the
top segment, SP is used along with the SS register to decide the address at which the data is
to be pushed or popped during the execution of instruction respectively.
Base Pointer (BP) -To hold the offset address of the data to be read from or write into the
stag segment.
Source Index (SI) -SI register is used to hold the offset address of the source data in the data
segment while executing string instructions.
Destination Index (DI) -DI register is used to hold the offset address of the destination data in
the extra segment while executing string instructions.
Flag Register (FR) - In the 16 bit flag register 6 bits are used as conditional flags, 3 bits are
used as control flags and rest 7 bits are unused.
- - - - OF DF IF TF SF ZF - AF - PF - CF
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
(i) Carry Flag (CF) - CF holds the carry after addition / subtraction.
(ii) Parity Flag (PF) - If the lower 8bits of the result have an odd parity then PF is set to „0‟
otherwise it set to „1‟.
(iii) Auxiliary carry Flag (AF) - It indicates when an arithmetic carry or borrow has been
generated out of the four least significant bits, or lower nibble.
(iv) Zero Flag (ZF) - It indicates that the result of an arithmetic or logical operation is zero. If
Z=1 the result is zero, if Z=0 the result is not zero.
(v) Sign Flag (SF) - SF holds the arithmetic sign of the result after an arithmetic or logic
instruction is executed. If S=0 then the sign bit is „0‟ and the result is positive.
(vi) Trap Flag (TF) - It is used to debug a program using a single step technique. If TF=1
then 8086 microprocessor gets interrupted after the execution of each instruction in the
program. If TF=0 the trapping or debugging feature is disabled.
(vii) Interrupt Flag (IF) - It controls the operation of INTR interrupt pin of the 8086
microprocessor. If IF=0 then the INTR pin is disabled. If IF=1, then the INTR pin is enabled.
This flag can be set and cleared using the STI and CLI instruction respectively.
(viii) Direction Flag (DF) - It selects either the increment or decrement for the DI and/or SI
register during the execution of the string instructions. If DF=1 the registers are automatically
decremented, if DF=0 then the registers are automatically incremented.
(ix) Overflow Flag (OF) - If the result of an operation is too large for a positive number or
too small for a negative number to fit in the destination operand then the OF is set, otherwise
reset.
Arithmetic Logic Unit (ALU) - ALU is a part of microprocessor, which performs arithmetic
operations (Addition, Subtraction, Increment, Decrement, Compare) and logical operations
(OR, AND, NOT, SHIFT/ROTATE, CLEAR).
Control Unit (CU) - CU controls and coordinates all the sections of microprocessor and other
devices connected to the interface section of microprocessor.
Stack Segment (SS) - It holds the stack of the program which is needed while executing the
CALL and RET instructions and also to handle interrupts.
Extra Segment (ES) - It is a data segment that is used by some string instructions.
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
Instruction Queue (IQ) - 8086 microprocessor fetches instructions ahead of the execution
time and places them in a 6 Byte instruction queue. This pre-fetching is done when the buses
are free.
Instruction Pointer (IP) - The offsets within the code segment are referenced using the IP.
The IP sequences the instructions and always points to the next instruction to be executed.
GND : Ground
AD0-AD15 : Bi-directional AD/DB. There are low order AD bus are multiplied with data
S1, DT/R‟ : Data Transmit/Receive. When it is high data are send out, when it is low data
are received.
S2, M/IO‟ : Memory or I/O access. When it is high then CPU wants to access memory, when
it is low CPU wants to access I/O devices.
LOCK‟ : When it is low all interrupts are masked and no HOLD request is granted.
RQ‟/GT1‟, RQ‟/GT0‟ : Bi-directional local bus priority control. Other processors ask the CPU
through this line to release the local bus.
HLDA : Hold acknowledge. It is issued by the processor when it receives hold signal. It is
active high signal. When hold request is removed HLDA goes low.
MN/MX‟ : It decides the operating mode of 8086 microprocessor. When it is high the CPU
operates in minimum mode, when it is low the CPU operates in maximum mode.
A16-A19 : High order Address Bus. These are multiplexed with status signals.
(ii) Fetching of data from the memory using the DS and BX register
Let us assume, DS=1000H, BX=3000H and the instruction is MOV AX,[BX] where the data
is obtained from the memory indicated by BX register is moved to the AX register.
Base address of the data segment = DS10H = 10000H
Offset values = 3000H
Effective address from where the data is taken = CS10H + IP = 13000H
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
generated by the microprocessor. The pins are used in the minimum mode are – M/IO‟, WR‟,
DT/R‟, DEN‟, ALE, INTA‟, HOLD, HLDA.
Maximum mode operation - In this operation some control signals are externally
generated by 8288 bus controller. There are not enough pins on the 8086 microprocessor for
bus control during maximum mode because new pins and new features have replaced some
of them. Maximum mode is used only when the system contains external coprocessor such as
8087 arithmetic coprocessor. The pins are used in the maximum mode are – S2‟, S1‟, S0‟,
LOCK‟, RQ‟/GTO‟, RQ‟/GT1‟, QS1, QS0.
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
Unit 3
8086 Instruction Set
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
MOV DL, 5[BX][DI] [DL] M[DS]10+[BX]+[DI]+5]
MOV 5[BP][SI],AX M[DS]10+[BP]+[DI]+5] [AX]
2. Find the address of Physical memory for the following instructions if the
content of the registers are SS=2344H, DS=4022H, BX=0200H, BP=1402H,
SI=4442H.
(i) MOV CL, 1234H[SI] // Register Relative Addressing Mode
Here the effective address is obtained from the instruction to be the sum of the displacement
and SI i.e. the effective address is 1234H+4442H=5676H. The segment base address is
obtained from DS. The physical address is the sum of the segment base address and the
effective address i.e. 4022H×10 + 5676H=45896H
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
POP DS [DS][TOS]
POP [SI] M[SI][TOS]
(iv) XCHG dest, src Exchange the contents of the destination and source.
XCHG AL,BL [AL][BL]
XCHG CX,BX [CX][BX]
XCHG AX,[BX] [AX]M[[DS] ×10+[BX]]
(v) XLAT Replaces the byte in AL with byte from a user table, addressed
by BX.
(vi) IN Copies data from a port to the AL or AX register.
IN AL,80 [AL] Port[80] // Byte transfer
IN AX,80 [AX] Port[80] // Word transfer
(vii) OUT Transfer a byte from AL or a word from AX to the specified port.
OUT 48,AL Port[48] [AL] // Byte transfer
IN F0,AX Port[F0] [AX] // Word transfer
(viii) LEA reg16,mem Load effective address i.e. load the offset of specified memory
location in the 16-bit register.
LEA BX, ABC Load BX with the offset address of ABC in the data
segment where ABC is the name assigned to a memory
location in the data segment.
LEA CX, [BX],[SI] Load CX with the value= [BX]+[SI]
(ix) LDS reg16,mem Copies a word from the memory location specified in the
instruction into the register and then copies a word from the
next memory location in the DS register.
LDS SI, 2000 Copy the content of the memory at the offset address 2000
in DS to the lower order byte of SI and the content of 2001
to the higher order byte of SI. Copy the content at the offset
address 2002 in the data segment to the lower order byte of
DS and the content of 2003 to the higher order byte of DS.
(x) LES reg16,mem Load far pointer to ES and register.
(xi) LSS reg16,mem Load far pointer to SS and register.
(xii) LAHF Copies the lower order byte of the flag registers into AH.
(xiii) SAHF Stores the content of AH in the lower order byte of the flag
register.
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
(xxx) JP addr Jump if parity to addr (Jumps if PF=1).
(xxxi) JPE addr Jump if parity is even to addr (Jumps if PF=1).Jump if parity is
even to addr (Jumps if PF=1).
(xxxii) JPE addr Jump if parity is odd to addr (Jumps if PF=0).
(xxxiii) JS addr Jump if sign to addr (Jumps if SF=1).
(xxxiv) JZ addr Jump if zero to addr (Jumps if ZF=1).
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
from the byte at DS:[SI]). Only flags are affected, the content of
the bytes compared is unaffected.
(iv) CMP SW Compare string words (done by subtracting the byte at ES:[DI]
from the byte at DS:[SI]). Only flags are affected, the content of
the words compared is unaffected.
(v) LODSB Load the string byte at DS:[SI] into AL.
(vi) LODSW Load the string word at DS:[SI] into AX.
(vii) STOSB Store the string byte in AL at ES:[DI]
(viii) STOSW Store the string word in AX at ES:[DI]
(ix) SCASB Compare string bytes (done by subtracting the byte at ES:[DI]
from the byte at AL). Only flags are affected, the content of the
byte compared is unaffected.
(x) SCASW Compare string word (done by subtracting the byte at ES:[DI]
from the byte at AX). Only flags are affected, the content of the
bytes compared is unaffected.
(xi) REP Decrement CX and repeat the following string operation of
CX≠0
(xii) REPE or REPZ Decrement CX and repeat the following string operation of
CX≠0 and ZF=1
(xiii) REPNE or REPNZ Decrement CX and repeat the following string operation of
CX≠0 and ZF=0
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
Unit 4
The Art of Assembly Language Programming
1. Line Assembler
It converts each mNemonic of an instruction immediately into an opcode as it is entered in the
system and this type of assembler is used in microprocessor trainer kit. The line assembler is
stored in any one of the ROM type memories in the trainer kit. The assembler needs a personal
computer for generating the opcodes of an assembly language program. The generated opcodes
can be downloaded to the microprocessor based system such as the microprocessor trainer kit or
the microprocessor based prototype hardware through the serial or parallel port of the computer.
2. Assembler Directive
MASM (Microsoft Macro Assembler), TASM (Turbo Assembler) and DOS Assembler are used
to convert the 8086 assembly language program into machine language program. Assembler
directives are commands to the assembler to indicate the size of a variable (either byte or word),
number of bytes or words to be reserved in the memory, value of a content, name of a segment
etc. in a program. Assembler directives are not converted directly into opcode, but are used to
generate the proper opcode of an instruction.
Model Description
Tiny Code and data together may not be greater than 64k
Small Neither code nor data may be greater than 64k
Medium Only the code may be greater than 64k
Compact Only the data may be greater than 64k
Large Both code and data may be greater than 64k
Huge All available memory can be used for code and data
4. Linker program
The linker program, which executes of the second part of machine language, reads the object
files, created by the assembler programs and links them into a single execution file.
5. Debugger
A debugger is a software supported by IDE and is used to test, run and debug the user written
programs. The application code developed by the programmer can be examined by running or
executing the code on an Instruction Set Simulator (ISS). The debugger allows the user to halt
the program simulation when specific conditions are encountered. Debugger provides the facility
to run a program step by step (single stepping) and stopping (breaking) the program at user
define points, modify state of the program.
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
Unit 5
Procedure and Macro
1. Procedure
When a group of instructions are to be used several times to perform a same function in a
program then we can write them as a separate sub program called procedure or subroutine.
The procedures are written or assembled as separate program modules and stored in
memory. When a procedure is called in main program, the program control is transferred to
procedure and after executing the procedure the program control is transferred back to the main
program.
In 8086 microprocessor, the instruction CALL is used to call a procedure in the main
program and the instruction RET is used to return the control to the main program.
A procedure begins with the PROC directive and ends with the ENDP directive. Each directive
appears with the name of the procedure. The PROC directive is followed by the type of
procedure- NEAR or FAR.
The main advantage of using a procedure is that the machine codes for the group of
instructions in the procedure has to be put in memory only once. The disadvantages of using the
procedure are the need for a stack, and the overload time required to call the procedure and
return to the calling program.
2. CALL
The CALL instruction transfers the flow of the program to the procedure. The CALL instruction
defers from the JUMP instruction because a call saves a return address on the stack. The return
address returns control to the instruction that immediately follows the call in the program when a
RET instruction executes.
8086 microprocessor has two types of CALL instructions and they are – Intransigent
CALL (NEAR CALL) and Intersegment CALL (FAR CALL). A procedure can be called using
NEAR CALL instruction if it is stored in the same segment where the main program is also
stored. A procedure can be called using FAR CALL instruction if the procedure and main
program are stored in different memory segment.
NEAR CALL instruction is 3B long, the first byte contains opcode, 2nd and 3rd byte
contain the displacement. When the NEAR CALL executes, it first pushes the offset address of
the next instruction on the stack. The offset address of the next instruction appears in the
Instruction Pointer (IP). After saving the return address, it then adds the displacements from
bytes 2 and 3 to the IP to transfer control to the procedure.
FAR CALL instruction calls a procedure to store in any memory location in the system.
The FAR CALL is a 5B instruction that contains an opcode followed by the next value for the IP
and CS registers. Bytes 2 and 3 contain the new contents of the IP and bytes 4 and 5 contain the
new contents of CS. FAR CALL places the contents of both IP and CS on the stack before
jumping to the address indicated by bytes 2-5 of the instruction. This allows the FAR CALL to
call a procedure located any where in the memory and return from the procedure.
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
3. RET
RET instruction is placed at the end of the procedure to return the CPU to the calling location. If
the calling location is in the same segment, RET pops the stack into IP and increments SP. This
causes the program to continue execution at the new address in IP.
8086 microprocessor has two types of RET instruction and they are - NEAR RETURN
and FAR RETURN. The NEAR RETURN instruction is used to terminate a procedure stored in
the same segment. The FAR RETURN instruction is used to terminate a procedure stored in a
different segment.
4. MACRO
When a group of instruction are to be used several times to perform a same function in a program
and they are too small to be written as a procedure, then they can be defined as a MACRO.
MACRO is a small group of instructions enclosed by the assembler directives macro and
ENDM. Macros are identified by their names and usually defined at the start of a program.
The macro is called by its name in the program. Whenever a macro is called in a
program, the assembler will insert the defined group of instructions in place of the CALL.
The disadvantage of using macro is that the program may take up more memory space
due to insertion of the machine codes in the program at the place of Macros. Hence, the macros
should be used only when its body has a few program statements.
6. DOS interrupt
DOS provides a large number of procedures to access devices, files, memory and process control
services. These procedures can be called in any user program using software interrupts “INT n”
instruction. The various DOS interrupts are as follows-
Interrupt Service provide by interrupt Interrupt Service provide by interrupt
type type
INT-20H Program Terminate INT- 26H Absolute Disk Write
INT-21H DOS System Call INT-27H Terminate and Stay Resident (TSR)
INT -22H Terminate Address INT-28H DOS time slice
INT-23H Control Break Address INT-2EH Perform DOS command
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
INT-24H Critical Error Handler Address INT-2FH Multiplexed Interrupts
INT-25H Absolute Disk Read
Unit 6
System Interfacing
(i) 20-bit addresses are provided for I/O (i) 8-bit or 16-bit addresses are provided for
devices. I/O devices.
(ii) The I/O ports or peripherals can be (ii) Only IN and OUT instructions can be
treated like memory locations and so all used for data transfer between I/O device and
instructions related to memory can be used the processor.
for data transfer between I/O device and the
processor.
(iii) In memory mapped ports, the data can be (iii) In I/O map ports the data transfer can
moved from any register to the ports and take place only between the accumulator and
vice versa. the ports.
(iv) When memory mapping is used for I/O (iv) When I/O mapping is used for I/O
devices the full memory address space devices, then the full memory address can be
cannot be used for addressing memory. used for addressing memory. Hence, it is
Hence, memory mapping is useful only for suitable for systems which requires large
small systems, where the memory memory capacity.
requirement is less.
(v) For accessing the memory mapped (v) For accessing the I/O mapped devices the
devices, the processor executes memory read processor executes I/O read or write cycle.
or write cycle. During this cycle M/IO‟ During This cycle M/IO‟ becomes low.
becomes high.
Features-
(i) It has three 8 bit ports- A, B and C connected to the output pins.
(ii) Port C is divided into two groups, Port C upper (PCU) and Port C lower (PCL) of 4 bits
each. Each of them can be programmed independently or as 4 bit ports for input and output
operations.
(iii) All the ports can be programmed for simple I/O or handshake I/O in the I/O mode.
(iv) Each port C can be set/reset individually in bit set/reset mode.
(v) The bits of Port A and PCU are grouped as group A (GA).
(vii) The bits of Port B and PCU are grouped as group B (GB).
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
The three operating modes are-
(i) Mode-0: In this mode all the three ports can be programmed either as input or output port.
The outputs are latched and the inputs are not latched. The ports do not have handshake or
interrupt capability. The ports in mode-0 can be used to interface DIP switches, Hex-keypad,
LEDs and 7-segment LEDs to the processor.
(ii) Mode-1: In this mode only ports A and B can be programmed either as input or output port.
Handshake signals are exchanged between the processor and peripheral prior to data transfer.
The port C pins are used for handshake signals, Input and output data are latched. Interrupt
driven data transfer is possible.
(iii) Mode-2: In this mode port will be a bi-directional port i.e. the processor can perform both
read and write operations with and I/O device connected to a port in mode-2. Only port A can be
programmed work in mode-2. Five-pins of Port C are used for handshake signals. This mode is
used primarily in applications such as data transfer between two computers or floppy disk
controller interface.
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
3. Programmable Interrupt Control (IC-8259)
For applications that used interrupt from multiple sources, the hardware can use an external
device called Programmable Interrupt Controller or Priority Interrupt Controller (8259).
Features-
(i) It is programmed to work with either 8085 or 8086 processor.
(ii) It manages 8 interrupts according to the instructions written into its control register.
(iii) In an 8086 processor based system, it supplies the type number of the interrupt and the
type number is programmable. In an 8085 processor based system, it vectors an interrupt
request anywhere in the memory map and the interrupt vector address is programmable.
(iv) The priorities of the interrupts are programmable. The different operating modes which
decide the priorities are automatic rotation mode, specific rotation mode and fully nested
mode.
(v) The interrupts can be masked or unmasked individually.
(vi) 8259 is programmed to accept either level-triggered interrupt signal or edge-triggered
interrupt signal.
(vii) 8255 provides the status of the pending interrupts, masked interrupts and the interrupt
being serviced.
(viii) 8259 can be cascaded to accept a maximum of 64-interrupt.
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School
4. DMA Controller (8237/8257)
Direct Memory Access (DMA) is used to provide high-speed data transfer directly between the
memory and peripheral devices without the intervention of the microprocessor. This method is
often used when a huge volume of data is to be transferred. DMA data transfer is controlled
using a separate DMA controller. The microprocessor must be disabled during the DMA data
transfer process. To start the DMA process, the microprocessor loads an external register in the
DMA controller with the data files starting address and the terminal count register with the total
number of bytes to be transferred. The microprocessor disabled the address and data buses and
gives memory system control to the DMA controller. The DMA controller places sequential
addresses on the microprocessor‟s memory bus and issues the read/write pulses. As each byte is
transferred the terminal count register is decremented. When the register is decremented to 0, it
tells the external device that the data transfer is complete.
Features-
(i) 4 independent DMA channels.
(ii) Enable or disable control of individual requests.
(iii) Possibility of memory to memory transfer.
(iv) Address increment or decrement.
(v) Cascading into expand to any number of DMA channels.
Prepared by – Kinshuk Majumder, Lecturer, CST Dept., The Calcutta Technical School