Microprocessor and Its Applications
Microprocessor and Its Applications
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
Evolution of Microprocessors
Need to learn MuP: The microprocessor is the core of computer systems. Nowadays many communication, digital entertainment, portable devices, are controlled by them. A designer should know what types of components he needs, ways to reduce production costs and product reliable.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 2
Picture of Microprocessor
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
Bus
Memory
Output units
Input units
4
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
MAR PC
IR
Control bus
Data bus
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
Evolution of Computers
First generation (1939-1954) - vacuum tube
Second generation (1954-1959) - transistor Third generation (1959-1971) - IC Fourth generation (1971-present) - microprocessor
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
Evolution of Computers
First generation (1939-1954) - vacuum tube
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
Evolution of Computers
Second generation (1954-1959) - transistor
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
Evolution of Computers
Third generation (1959-1971) - IC
PDP-8, Digital Equipment Corporation
Thanks to the use of ICs, the DEC PDP-8 is the least expensive general purpose small computer in 1960s
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
Evolution of Computers
Fourth generation (1971-present) - microprocessor
In 1971, Intel developed 4-bit 4004 chip for calculator applications.
ROM/RAM buffer Timing Reset
Program counter
http://www.intel.com
4004 chip layout
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
10
P III
8080
P4
P II
80486
MIPS
10000
P4
1000
1000
P4
P II
100 10
P III
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
11
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 12
...
Keyboard
Monitor
Disk
Other peripherals
Bus
Microprocessor Timing & control
...
13
Memory
Interrupt control
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
OSC.
RAM
ROM
CPU
I/O port Timer Interrupt USART A/D, D/A
The 8086 has a 16 bit data bus, so it can read data from or write data to memory and ports either 16 bits or 8 bits at a time.
The 8088 however has an 8 bit data bus, so it can only read data from or write data to memory and ports 8 bits at a time.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
15
The 8086 has 20 bit address bus so it can directly access 2^20 or 1048576 (1Mb) memory location. The 8086 can generate 16 bit I/O address, hence it can access 2^16 = 65536 I/O ports. The 8086 provides fourteen 16 bit registers. 8086 requires one phase clock with a 33% duty cycle to provide optimized internal timing.
Range of clock:
5 MHz for 8086 8Mhz for 8086-2 10Mhz for 8086-1
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 16
8086 has multiplexed address and data bus which reduces the number of pins need but slows down the transfer of data(drawback) 8086 performs arithmetic and logical operations on bit,byte,word and decimal numbers including multiply and divide 8086 operates in 2 modes:
Minimum- one CPU & it issues control signals Maximum- Multiple CPU, DMA controller issues control signals
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 17
Fetches six instructions bytes from memory and stores them in queue to speed up the program execution
Supports powerful instruction set with the following addressing modes. For accessing immediate and register data For accessing data in memory For accessing I/O ports Relative addressing mode Implied addressing mode IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
18
8086 architecture
Divided into two functional units:
Bus interface unit (BIU) Execution unit (EU)
Functions of BIU:
Sends address of the memory or I/O Fetches instructions from memory Reads data from port/memory Writes data into port/memory Supports instruction queuing Provides address relocation facility
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
Instruction queue To speed up the execution BIU fetches 6 instructions ahead of time from memory These prefetched instructions are stored in group of registers called queue.Operates on the principle of FIFO Feature of fetching the next instruction while the current instruction is executing is called pipelining Segment registers: Physical address of the 8086 is 20 bits wide to access1 Mbyte of memory locations. But registers and memory locations contain logical address of 16 bits wide. Hence memory segmentation is used IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
21
Memory is divided into active segments each 64 kbytes. For the selection of the segments 4 16 bit registers are used, they are:
CODE SEGMENT REGISTER (CS) DATA SEGMENT REGISTER (DS) EXTRA SEGMENT REGISTER (ES) STACK SEGMENT REGISTEER (SS)
Starting address of the segment register is called as segment base or base address
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 22
BIU always inserts zeros for the lowest 4 bits in the contents of the segment register to generate 20 bit base address.
For eg, if the code segment register contains 348H then code segment will start at addres 3480H
ES register and DS register hold the upper 16 bits of the starting address of the 2 memory segments which are used for the data. Rules for memory segmentation:
4 segments can overlap. In minimum system starting address 00000H Starting / Ending address must by divisible by 16
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
24
Major Advantages:
Memory addressing capacity of 1Mbyte even though the individual instruction is only 16 bit Separate memory areas for code, data and stack Permits address relocation which is useful in multiprogramming
Instruction pointer
Holds the 16 bit address of the next code byte within the code segment. Value in IP is offset
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 25
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
26
Execution unit:
The EU of 8086 tells the BIU from where to fetch instruction or data decodes instruction and executes instruction. It contains
Control circuitry( directs the internal operation) Instruction decoder ( translates instruc to actions) ALU( 16 bit ,performs Arithmetic and logic operations) Flag register General purpose register Pointers and index registers
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
27
Flag register
A flag is a flip-flop which indicates some condition produced by the execution of an ins or controls certain operations of the EU. The flag register contains nine active flags. Six of them are used to indicate some condition: Carry flag in case of addition this flag is set if there is a carry out of the MSB. The carry flag also serves as a borrow flag for subtraction. In case of sub it is set when borrow is needed. Parity flag it is set to 1 if result of byte operation or lower byte of the word operation contain an even number of ones, otherwise it is zero. Auxiliary flag this flag is set if there is an overflow out of bit 3.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 28
Flag register
Zero flag the ZF sets if the result of operation in ALU is zero and flag resets if the result in nonzero. The zero flag is also set if a certain reg content becomes zero following an inc or dec operation to that reg. Sign flag After the execution of arithmetic or logical operation if the MSB of the result is 1, the sign bit is set. Sign bit 1 indicates the result is negative otherwise it is positive. Overflow flag this flag is set if result is out of range. For addition this flag is set when there is a carry into the MSB and no carry out of the MSB. For sub it is set when MSB needs a borrow and there is no borrow from the MSB.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 29
The three remaining flags are used to control certain operations of the processor:
Trap flag- one way to debug a program is to run the pgm one ins at a time and see the contents of used reg and memory variables after execution of every ins. This process is called single stepping through a pgm. Interrupt flag it is used to allow the interruption of a pgm. If set a certain type of interrupt can be recognized by the 8086, otherwise these interrupts are ignored. Direction flag it is used with string ins. If DF = 0, the string is processed from its beginning with the first element having the lowest address. Otherwise the string is processed from the high address towards the low address.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 30
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
31
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
33
Stack pointer:
Contains the 16 bit offset from the start of the segment to the top of the stack Physical address is produced by adding the contents of stack pointer to the segment base address in SS SS SP (+) Physical address
The BP, index reg DI and SI are used as a general purpose registers.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 34
Base pointer:
For accessing the stack using based addressing mode use BP instead of SP. 20 bit physical address is formed by using BP and SS
Source index
Holds the offset of a data word in the data segment 20 bit physical address is formed by using SI and DS
Destination index
ES register points to the extra segment in which data is stored String instructions always use ES and DI to determine the physical address
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 35
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
36
8086 BUSES
THE 8086 HAS 20 ADDRESS LINES
16 DATA LINES
4-10 CONTROL LINES.
THE INTERNAL ARCHITECTURE OF 8086 CAN BE MAINLY DIVIDED INTO TWO UNITS:
BUS INTERFACE UNIT (BIU)
THE EU CONTAINS THE FOLLOWING 8-BIT REGISTERS: AH & AL (AX-16 BIT) BH & BL (BX-16 BIT) CH & CL (CX-16 BIT) DH & DL (DX-16 BIT)
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
40
AH
AL
BH CH DH
BL CL DL
CS DS ES SS
SP BP SI DI IP
Flags/PSW
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 41
Segment register
These are used to hold the upper 16 bits of the starting addresses of the four memory segments on which 8086 works at a particular time. Starting address the lowest addressed byte in the active code segment. The starting address is also known as base address or segment base. Physical address is calculated from segment address and offset Advantage is instead of maintaining a 20 bit register for a physical address, this maintains two 16 bit registers
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 43
Control Circuitry, Instruction decoder, ALU The control circuitry in the EU directs the internal operation. A decoder translates the ins fetched from memory into a series of actions which the EU performs. ALU is 16 bit. It can add, subtract, AND, OR, XOR, increment, decrements, complement and shift binary numbers.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 46
Flag register
A flag is a flip-flop which indicates some condition produced by the execution of an ins or controls certain operations of the EU. The flag register contains nine active flags. Carry flag in case of addition this flag is set if there is a carry out of the MSB. The carry flag also serves as a borrow flag for subtraction. In case of sub it is set when borrow is needed. Parity flag it is set to 1 if result of byte operation or lower byte of the word operation contain an even number of ones, otherwise it is zero. Auxiliary flag this flag is set if there is an overflow out of bit 3.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 47
Flag register
Zero flag the ZF sets if the result of operation in ALU is zero and flag resets if the result in nonzero. The zero flag is also set if a certain reg content becomes zero following an inc or dec operation to that reg. Sign flag After the execution of arithmetic or logical operation if the MSB of the result is 1, the sign bit is set. Sign bit 1 indicates the result is negative otherwise it is positive. Overflow flag this flag is set if result is out of range. For addition this flag is set when there is a carry into the MSB and no carry out of the MSB. For sub it is set when MSB needs a borrow and there is no borrow from the MSB.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 48
Flag register
Trap flag- one way to debug a program is to run the pgm one ins at a time and see the contents of used reg and memory variables after execution of every ins. This process is called single stepping through a pgm. Interrupt flag it is used to allow the interruption of a pgm. If set a certain type of interrupt can be recognized by the 8086, otherwise these interrupts are ignored. Direction flag it is used with string ins. If DF = 0, the string is processed from its beginning with the first element having the lowest address. Otherwise the string is processed from the high address towards the low address.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 49
Addressing Modes
We have seen how the 8086 fetches code bytes from memory by generating 20 bit physical address with the help of IP and CS. We have also seen how the 8086 accesses the stack using SS and SP. In this section we will see the different ways that an 8086 can access the data. The different ways that a processor can access data are referred to as addressing modes.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 50
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
51
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
53
A. B. C. D. E. F.
Direct addressing mode Register indirect addressing modes Based addressing mode Indexed addressing mode Based indexed addressing modes String addressing modes.
55
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
Example MOV [DI],BX the instruction copies the 16 bit contents of BX into DI
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
57
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
63
INTRASEGMENT MODE
If the location to which the control is to be transferred lies in the same segment, the mode is called intrasegment mode
Intrasegment Direct: Destination location is in same segment and appears directly in the instruction as an immediate displacement value.The effective address is sum of 8 or 16 bit displacement and current content of IP. JMP SHORT label; Label lies within -128 to +127 from the current IP content.
Intrasegment Indirect mode: Destination location is in same segment but it is passed to the instruction indirectly. The branch address is found as the content of a register or a memory location. JMP[BX];Jump to effective address stored in BX
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 66
Types of Instructions
The instructions supported by 8086 are Data transfer instructions Addition, subtraction and comparison instructions Multiplication and Division instructions BCD and ASCII arithmetic instructions Logical instructions Shift compare instructions String compare instructions Jump instructions Machine control and miscellaneous instructions Interrupt instructions
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 69
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
71
MOV instruction
MOV destination, source MOV ins copies a word or a byte of data from some source to a destination. The destination can be a register or a memory location. The source can be a register, a memory location, or an immediate number. The source and destination in a MOV ins must be of same type.
MOV ins does not effect any flags. Example MOV BX,592FH MOV CL [357A]
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 72
PUSH instructions
PUSH source Push ins decrement stack pointer by 2 and copies a word from some source to the location in the stack pointer points. Here the source must be a word. The source of the word can be a general purpose reg, a segment reg or memory Example: PUSH CX; Decrement SP by 2 , copy CX to stack
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 73
POP instructions
POP destination It copies a word from the stack location pointed by the stack pointer to the destination Destination can be general purpose register, segment register or memory location After a word is copied to the specified location, the stack pointer is incremented by 2 Example:
POP CX ; copy a word from top of stack to CX and increment SP by 2
XCHG
XCHG destination , source Exchanges the contents of a register with the contents of another register Or the contents of the register with the contents of a memory location Instruction cannot exchange the contents of the two memory location Example:
XCHG BX, CX ; ;exchange word in BX with word in CX
XLAT
Translate a byte in AL Replaces a byte in the AL register with a byte from a lookup table in memory
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
78
LDS
Load register and DS with words from memory This ins copies a word from two memory locations into the register specified in the ins. It then copies a word from the next memory location. Into the DS. Example:
LDS CX, [391AH] copy contents of memory at displacement of 391AH and 391BH to CX, then copy contents at displacement of 391CH and 391DH into ds.
LES
LES ins- load register and ES with words from memory. This ins loads new values into the specified reg and into the ES reg from four successive memory location. The word from the first two memory location is copied into the specified reg and the word from the next two memory location is copied into the ES reg. Eg: LES CX,[3483H]- copy the contents of memory at displacement of 3483h in ds to cl, contents of 3484h in ds to ch and copy the contents of memory displacement of 3485h and 3486h in ds to es register.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 80
c) Flag transfer
LAHF- load lower byte of flag register in AH SAHF- copy AH register to low byte of flag register PUSHF- push flag register on the stack POPF- pop word from top of stack to flag register
OUT
Send a byte or word to a port
Addressing modes for accessing I/O ports Standard I/O devices uses port addressing modes. For memory mapped I/O memory addressing modes are used. There are two types of port addressing modes they are Direct and Indirect
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
83
Arithmetic instructions
Addition instructions (ADD,ADC,INC,AAA,DAA) Subtraction instructions- (SUB, SBB ,DEC, NEG , CMP , AAS ,DAS) Multiplication instructions- MUL, IMUL, AAM Bit manipulation instructions NOT, AND, OR, XOR, TEST Shift instructions- SAL/ SHL , SHR, SAR
Arithmetic instructions
ADD
This instructions add a number from source to a number from destination and put the result in the destination Example: ADD AL, OF0H ; Add immediate number OF0H to contents of AL
ADC
Adds the status of the carry flag into the result Example: ADC DL,CL ; Dl <- DL+CL+CY
INC
Increment destination This instruc adds 1 to the specified destination. Destination may be register or memory location Example:
INC AL ; Adds 1 to contents of AL
AAA
ASCII adjust for addition Mask upper nibble (3) from the code before addition
DAA
Decimal adjust accumulator If the value of the low-order bits (D0-D3) in the AL is >9 add 6 to the low order 4 bits If the value of the high-order bits (D7-D4) in the AL is >9 add 6 to the high order 4 bits
SUB
SUBTRACTION
This ins subtract the number in the source from the number in the destination and put result in the destination. EG: SUB AL, 0F0H ; subtract immediate number OF0H from contents of AL and stores result in AL
SBB
The SBB instruction also sub the status of carry flag from the result. EG : SBB DL,CL ; DL <- DL-CL- CY
Eg: DEC AL ;Subtracts 1 from the contents of AL NOTE: The carry flag is not affected. AF, OF,PF, SF,ZF flags affected
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 91
NEG
This replaces the number in a destination with the 2s complement of that number AF ,CF , SF, PF, ZF affected EXAMPLE:
NEG AL ; Replaces the number in AL with its 2s complement AL = 0011 0101 35H 2s = 1100 1011 CBH
COMPARISON
The comparison ins (CMP) compares a byte/word from the specified source with a byte/word from the specified destination. The source and destination both must be byte or word not a memory locations The flags are updated by the CMP instruction they are AF,SF,ZF,OF,PF EG: CMP BL,01H ; Compare immediate number 01H with byte in BL.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 93
AAS
ASCII adjust after subtraction Mask upper nibble (3) from the code before subtraction
DAS
Decimal adjust after subtraction If the value of the low-order bits (D0-D3) in the AL is >9 , subtract 6 to the low order 4 bits If the value of the high-order bits (D7-D4) in the AL is >9 subtract 6 to the high order 4 bits
MUL
MUL unsigned multiplication This ins multiplies an unsigned byte from source and unsigned byte in AL or unsigned word from source and unsigned word in AX register. Flags: MUL ins affect AF,PF,SF and ZF flags. Example: MUL BL ; AL * BL result in AX MUL BX ; AX * BX result high word in DX low word in AX
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 95
IMUL
this ins multiplies a signed byte from some source and a signed byte in AL, or a signed word from some source and a signed word in AX. If the upper byte of a 16 bit result or the upper word of 32 bit result contains only copies of the sign bit , then the CF and the OF flags will both be 0s. The AF,PF,SF and ZF flags are undefined after IMUL.
EG:IMUL BL AL * BL RESULT IN AX
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 96
AAM
ASCII adjust after multiply Adjust the product to 2 unpacked BCD digits in AX
Division instructions
DIV ins : DIV source This ins is used to divide an unsigned word by a byte or to divide an unsigned double word by a word. When dividing a word by a byte, the word must be in AX reg. After the division AL will contain an 8 bit quotient and AH will contain an 8 bit remainder.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
98
IDIV instruction
IDIV source This instruction is used to divide a signed word by a signed byte, Or to divide a signed double word by a signed word
AAD
Binary adjust before division
-- AAD converts two unpacked BCD digits in AH and AL to the equivalent binary number in AL. Done before division.
Sign extension
They are
CBW CWD
CBW
Copies the sign of a byte in AL to all bits in AH
CWD
Copies the sign bit of a word in AX to all the bits of the DX register Note: both CBW & CWD affects no flag
Logical instructions
The basic logic instruction include
NOT AND OR EXCLUSIVE OR, (XOR) and TEST
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
102
NOT
This inverts each bit of a byte or a word Affects no flag Example
NOT AL ; AL= 0010 1010 then AL= 1101 0101
AND
AND instuction : AND destination, source
Logically ANDs each bit of the source byte or word with the corresponding bit in the destination and stores result in the destination AND operation with two inputs produces logic 1 only when both the inputs are logic 1 that is Y = A.B The CF and OF flags are both 0 after AND. The PF, SF and ZF are affected, AF is undefined.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
106
Shift instructions
SAL/SHL
SAL/SHL destination, count Shfts each bit in the specified destination to the left and 0 is stored at LSB position MSB is shifted into the carry flag eg: SAL CX,1
SHR
SHR destination, count Shfts each bit in the specified destination to the left and 0 is stored at MSB position LSB is shifted into the carry flag Eg: SHR CX,1
SAR
SAR destination, count Shifts the bits to right Copy of the old MSB is put in the new MSB LSB will be shifted into CF EG: SAR CX,1
Rotate instructions
ROL ROR RCL RCR
ROL instr:
ROL destination , count Rotates bits to left Old MSB is placed as a new LSB and CF Eg: ROL CX,1
ROR instr :
ROR destination , count Rotates bits to right Old LSB is placed as a new MSB and new CF Eg: ROR CX,1
RCL instruc:
RCL destination , count Rotates all the bits in left along with the carry flag MSB is placed as a new carry and previous carry is placed as new LSB EG: RCL CX,1
RCR instruc
RCR destination , count Rotates all the bits in right along with the carry flag LSB is placed as a new carry and previous carry is placed as new MSB EG: RCR CX,1
J cond
These instructions will cause a jump to label given in the instruction if the desired condition(s) occurs in the program before the execution of the instruction Destination range -128 bytes to +127 bytes
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
113
JA / JNBE
Jump if above/ jump if not CF=0 & ZF=0 below or equal Jump if above or equal / jump if not below CF=0 & ZF=1
JAE/ JNB
JB/ JNAE/JC Jump if below/ jump if not CF=1 & ZF=0 above or equal JBE / JNA Jump if below or equal / jump if not above CF=1 & ZF=1
114
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
JE / JZ
Jump if equal / jump if Zero flag Jump if greater / jump if not less than or equal Jump if greater than or equal / jump if not less than
ZF= 1
JG / JNLE
JGE / JNL
SF=0
JL / JNGE
Jump if less than/ jump SF 0 if not greater than or IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 equal
115
JLE / JNG
ZF= 1 or SF 0
CF=0
JNO
JNP / JPO
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
116
JNS
Jump if not sign or jump SF=0 if positive Jump if overflow flag=1 Jump if parity / jump if parity even Jump if sign flag=1 or jump if negative Jump if CX is zero OF=1 PF=1
JO JP / JPE
JS JCXZ
SF=1 CX=0
117
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
LOOP
CX=0
CX=0 or ZF=0
CX=0 or Zf=1
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
119
STI- Sets the interrupt flag to one, enable interrupt CLI- resets the interrupt flag to zero, no interrupt HLT- strop fetching and executing instructions WAIT- enters idle condition, no processing
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 121
ESC instruction:
Used to pass instructions to a coprocessor such as the 8087 math coprocessor which shares the address and data bus with an 8086 Instruc represented as 6 bit code embedded in esc instruc Coprocessor decodes the instruc and carries out the action specified by the 6 bit code
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 122
LOCK
Used in multiprocessor systems This allows a microprocessor to make sure that another processor does not take control of the system bus while it currently executing an critical instruction
NOP
At the time of execution of NOP instruc, no operation is performed except fetch and decode
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
123
Interrupt instructions
INT, INTO, IRET INT
General Format : INT type This instruc causes the 8086 to call a far procedure. Type refers to a number between 0-255 which identifies the interrupt Address of the procedure is taken from the memory whose address is four times the type number
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 124
INTO
If the overflow flag is set, this instruc will cause the 8086 to do an indirect far call to a procedure you write to handle overflow condition
IRET
Used at the end of the ISR to return execution to the interrupted program
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
125
These ins copy a byte from a location in the data segment to a location in the extra segment. The offset of the source byte in the DS must be in the SI reg. the offset of destination in the extra segment must be contained in the DI register. Eg: CLD clear direction flag to auto increment SI and DI MOV DS,AX initialize data segment register to 0 MOV SI,2000H Load offset of start of source string into SI
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 126
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
127
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
128
STOS/STOSB/STOSW
The STOS ins copies a byte from AL or a word to DI, STOS does not affect the flag, STOSB copies byte, and STOSW copies a word.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 129
Assembler directives
Assembler directives There are some instructions in the assembly level language program which are not a part of processor ins set. The instructions are instructions to the assembler, linker and loader. These are referred to as pseudo-operations or as assembler directives. The assembler directives enable to control the way in which a pgm assembles and lists.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 130
Assembler directives
The general format for this number is ALIGN number Where the number can be 2,4,8 or 16
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 131
Assume
The 8086 at any time can directly address four physical segments which include a code segment, DS, SS and ES. The 8086 may contain a number of logical segments.
ASSUME CS:code, DS:Data, SS:Stack
CODE
This directive provides shortcut in def of the CS. General format for this directive is code [name] The name is optional
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 132
Assembler directives
DATA this directive provides shortcut in definition of the data segment. DB DEFINE BYTE DW DEFINE WORD DD DEFINE DOUBLEWORD DQ DEFINE QUADWORD DT DEFINE TEN BYTES
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
133
DUP This DUP directive can be used to initialize several locations and to assign values to these locations
Format name data _ type num DUP (value)
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
134
Assembler directives
END the END directive is put after the last statement of a pgm to tell the assembler that this is the end of the pgm module. EQU this EQU directive is used to redefine a data name or variable with another data name, variable. EVEN- EVEN tells the assembler to advance the location counter if necessary so that the next defined data item or label is aligned on an even storage boundary
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 135
Assembler directives
EXTRN the EXTRN directive is used to inform assembler that the names or labels following the directive are in some other assembly module. GROUP a pgm may contain several segments of the same type. The purpose of the group is to collect them all under one name, so that they reside within one segment usually a data segment
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
136
Assembler directives
LABEL assembler uses a location counter to keep track of how many bytes it is from the start of a segment at any time. The LABEL directive is used to give a name to the current value in the location counter. LENGTH it is an operator which tells the assembler to determine the number of elements in some named data item such as a string or array. TITLE this directive help to control the format of a listing of an assembled pf.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 137
Assembler directives
MACRO and ENDM the macros in the pgm can be defined by MACRO directive. ENDM directive is used along with the MACRO directive. ENDM defines the end of the macro. MODEL it is available in MASM version 5.0 and above. This directive provides shortcut in defining segments. ( small , medium, compact, large) SHORT a short is a operator. It is used to tell the assembler that the only 1 byte displacement is needed to code a jump ins.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 138
Assembler directives
NAME the name directive is used at the start of a source pgm to give specific names to each assembly module. OFFSET it is an operator which tells the assembler to determine the offset or displacement of a named data item from the start of the segment which contains it. ORG the assembler uses a location counter to account for its relative position in a data or code segment.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 139
Assembler directives
PTR it is used to assign a specific type to a variable or to a label. It is used to override the declared type of variable. PAGE this directive help to control the format of a listing of an assembled pgm. At the start of a pgm the PAGE specifies the max number. PROC and ENDP the procedure in the pgm can be defined by PROC directive. This directive is used along with PROC directive . ENDP defines the end of the procedure.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 140
Assembler directives
PUBLIC large pgm are usually written as several separate modules. Each module is individually assembled, tested and debugged. SEGMENT and ENDS an assembly pgm in .EXE format consists of one or more segments. The start of these segment are defined by SEGMENT directive and the ENDS statement indicates the end of the segment. STACK this directive provides shortcut in definition of the stack segment. TYPE it is an operator which tells assembler to determine the type of specified variable.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 141
in 8086 to sort the array stored at location 1100 in ascending order. ALGORITHM: Start program. Get the source. Move CL and [SI] & decrement CL. Move [SI] to CH and decrement CH and increment SI. Compare AL,SI if no carry exchange AL & SI and AL & [SI-1]. If carry, decrement CH and check for zero and repeat the steps. Stop the program.
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 145
Flowchart
START Get the source address Move the [SI] & CL. Decrement CL Move 1100 to SI. Decrement CH, Increment SI Move source to AL. Increment SI Compare AL & SI
no
yes
Exchange AL & [SI] * also AL & [SI-1]
Check for 0
yes
Decrement CL
Check for 0
no
146
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232yes I / PPT/ Version 1.1 /Unit
STOP
PROGRAM:
START ORG MOV MOV DEC MOV MOV DEC INC MOV INC CMP JC XCHG XCHG DEC JNZ DEC JNZ HLT 1000H SI, 1100H CL, [SI] CL SI, 1100H CH, [SI] CH SI AL, [SI] SI AL, [SI] AHEAD AL, [SI] AL, [SI-1] CH REPCOM CL REPEAT
147
REPEAT
REPCOM
AHEAD
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
When the ISR is completed, the original program resumes execution Interrupts provide an efficient way to handle unanticipated events
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 148
Procedures
Can only be initiated by software Can handle anticipated events that are coded into the program Typically loaded along with the program Use meaningful names to indicate their function Do not save the (E)FLAGS register
149
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
150
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
151
Interrupt Processing in Real Mode Uses an interrupt vector table that stores pointers to the associated interrupt handlers.
This table is located at base address zero.
Each entry in this table consists of a CS:IP pointer to the associated ISRs
Each entry or vector requires four bytes:
Two bytes for specifying CS Two bytes for the offset
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
153
Example
For interrupt 2, the memory address is 2 * 4 = 8H The first two bytes at 8H are taken as the offset value The next two bytes (i.e., at address AH) are used as the CS value
154
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
Push CS and IP registers onto the stack Load CS with the 16-bit data at memory address interrupt-type * 4 + 2 Load IP with the 16-bit data at memory address
interrupt-type * 4
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 156
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
157
Dedicated Interrupts
Several Pentium predefined interrupts --called dedicated interrupts These include the first five interrupts:
interrupt type Purpose 0 Divide error 1 Single-step 2 Nonmaskable interrupt (MNI) 3 Breakpoint 4 Overflow
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 159
Single-Step Interrupt
Useful in debugging To single step, Trap Flag (TF) should be set CPU automatically generates a type 1 interrupt after executing each instruction if TF is set Type 1 ISR can be used to present the system state to the user
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 160
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
161
We write our own type 1 ISR that displays the contents of AX and BX registers after each instruction has been executed
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 162
Hardware Interrupts
Software interrupts are synchronous events Caused by executing the int instruction Hardware interrupts are of hardware origin and asynchronous in nature Typically caused by applying an electrical signal to the processor chip Hardware interrupts can be Maskable Non-maskable Causes a type 2 interrupt
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1 163
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
164
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
165
IFET CE/ MCA/ II yr/ III sem/ MuP/MC9232 /Unit I / PPT/ Version 1.1
166