0% found this document useful (0 votes)
4 views

modif

Uploaded by

BATTU DEEPA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

modif

Uploaded by

BATTU DEEPA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 49

COURSE MATERIAL

SUBJECT IMAGE PROCESSING (19A04703d)

UNIT 5

COURSE B.TECH

ELECTRONICS AND COMMUNICATION


DEPARTMENT
ENGINEERING

SEMESTER 4-1

PREPARED BY
(Faculty Name/s) B Chandrakala
Assistant Professor

VERSION 1.0

PREPARED / REVISED DATE 10-10-2022

|MPMC UNIT II
BTECH_ECE-SEM 32
TABLE OF CONTENTS – UNIT 2
SNO CONTENTS PAGE
1 COURSE OBJECTIVES 1
2 PREREQUISITES 1
3 SYLLABUS 1
4 COURSE OUTCOMES 1
5 CO - PO/PSO MAPPING 1
6 LESSON PLAN 2
7 ACTIVITY BASED LEARNING 2
8 LECTURE NOTES 3
8.1 Introduction 3
8.2 Color Models 3
8.2.1 RGB color model 4
8.2.2 HSI color model 4
8.2.3 YUV color model 4
8.3 Color Transformations 4
8.3.1 Formulation 4
8.3.2 Color complements 5
8.3.3 Color Slicing 5
8.3.4 Tone and Color Corrections 5
8.3.5 Smoothing and Sharpening 5
8.3.6 Color Segmentation 6
9 PRACTICE QUIZ 43
10 ASSIGNMENTS 44
11 PART A QUESTIONS & ANSWERS (2 MARKS QUESTIONS) 44
12 PART B QUESTIONS 46
13 SUPPORTIVE ONLINE CERTIFICATION COURSES 46
14 REAL TIME APPLICATIONS 46

15 CONTENTS BEYOND THE SYLLABUS 46

16 PRESCRIBED TEXT BOOKS & REFERENCE BOOKS 47

17 MINI PROJECT SUGGESTION 47

|MPMC UNIT II
BTECH_ECE-SEM 32
1. COURSE OBJECTIVES
The objectives of this course is to
 To introduce fundamentals of Image Processing.

 To expose various intensity transformations in spatial and frequency domains.

 To dissimilate various segmentation techniques for images.

 To impart concepts of wavelets and various coding techniques for image


compression.

 To teach various color models and to introduce the concepts of color image
segmentation.

2. PREREQUISITES
Students should have knowledge on
1. Preliminary Mathematics
2. Principals of Signals and systems

1. SYLLABUS
UNIT V :
Color Image Processing-color models- RGB, YUV,HIS; Color transformations-
formulation, color complements, color slicing, tone and color corrections; Color
image smoothing and sharpening; Color Segmentation.

2. COURSE OUTCOMES
1. Analyze various types of images mathematically.
2. Compare image enhancement methods in spatial and frequency domains.
3. Demonstrate various segmentation algorithms for given image.
4. Justify DCT and wavelet transform techniques for image compression.
5. Describe various color models for color image processing.

3. CO-PO / PSO Mapping


PO PO PO PO PO PO PO PO PO PO PO PO PS PS
1 2 3 4 5 6 7 8 9 10 11 12 O1 O2
CO1 3 3 2 3 3
CO2 3 3 2 2 3 3 3
CO3 3 3 3 2 2 3 3 3
CO4 3 3 3 3 2 2 3 3 3
CO5 3 3 2 3 3 3

|MPMC UNIT II
BTECH_ECE-SEM 32
4. LESSON PLAN
LECTU WEE REFEREN
TOPICS TO BE COVERED
RE K CES
1. Instruction set of 8086 MicroProcessor T1,T2,R2
2. Assemble Directives of 8086 processor T1,T2,R2
1
3. Simple Example of Assembler Directives T1,T2,R2
4. 8086 Procedures and it different types T1,T2,R2
5. Simple Programs on Procedures T1,T2,R2
6. weekly test conducting T1,T2,R2
2
7. Introduction to Macros of 8086 T1,T2,R2
8. simple Programs on Macros T1,T2,R2
9. Introduction to Assembly Language Programming T1,T2,R2
10. Arithmetic and Logical expressions using ALP T1,T2,R2
3
11. Branch and Call Instructions using ALP T1,T2,R2
String Manipulation and Sorting evaluation programs
12. T1,T2,R2
using ALP
13. Simple programs on all syntax and Instruction set T1,T2,R2
4
14. slip Test on ALP T1,T2,R2

7. ACTIVITY BASED LEARNING

1. HOME BASED AUTOMATION.


2. 8085/8085/MSP430 PROJECTS.

|MPMC UNIT II
BTECH_ECE-SEM 32
8. LECTURE NOTES

2.1 PROGRAMMING SYSTEM

MACHINE LANGUAGE
Machine language is the lowest level programming language. In this
language, every instruction is described by binary patterns. This is the form
in which instructions are stored in memory. This is the only form that the
microprocessor understands.
ASSEMBLY LANGUAGE
In this language, the binary patterns are assigned mnemonics (short
abbreviated names). There is usually one assembly language instruction for
each machine language instruction.
Assemblers are software programs that convert assembly language into
machine language.
HIGH LEVEL LANGUAGES
These are languages like C, PASCAL and FORTRON. These are more natural
for humans to use than assembly or machine languages. They are also more
compact (i.e. it takes less statement to write the program).One high level
instruction translates into many assembly or machine language instructions.
Compilers are software programs that convert high level language into
machine language.
ULTRA HIGH LEVEL LANGUAGES
These include C++, and JAVA. Here a single instruction may translate into
hundreds of assembly or machine language instructions.

2.2 ADDRESSING MODES OF 8086

Addressing modes describe the types of operands and the way they are
accessed for executing an instruction.

|MPMC UNIT II
BTECH_ECE-SEM 32
2.2.1 IMMEDIATE ADDRESSING MODE
In these types of addressing, immediate data is a part of instruction, and
appears in the form of successive byte or bytes.
Example: MOV CX, 0005H.
In the above example, 0005H is the immediate data. The immediate data
may be 8-bit or 16-bit in size.

2.2.2 DIRECT MEMORY ADDRESSING MODE


In the direct addressing mode, a 16-bit memory address (offset) is directly
specified in the instruction as a part of it.
Example: MOV DX,[5000H]
Here, data resides in a memory location in the data segment, whose
effective address may be computed using 5000H as the offset address and
content of DS as segment address. The effective address, here, is
10H*DS+5000H.

2.2.3 REGISTER ADDRESSING MODE


In register addressing mode, the data is stored in a register and it is referred
using the particular register. All the registers, except IP, may be used in this
mode.
Example: MOV AX,BX

2.2.4 REGISTER INDIRECT ADDRESSING MODE


In this addressing mode, the offset address of data is in either BX or BP or SI
or DI registers. The default segment is either DS or ES. The data is supposed
to be available at the address pointed to by the content of any of the above
registers in the default data segment.
Example: MOV AL,[BX].
Here, data is present in a memory location in DS whose offset address is in
BX. The effective address of the data is given as 10H*DS+[BX].

2.2.5 REGISTER RELATIVE ADDRESSING MODE


In this addressing mode, the data is available at an effective address formed
by adding an 8-bit or 16-bit displacement with the content of any one of the

|MPMC UNIT II
BTECH_ECE-SEM 32
registers BX, BP, SI and DI in the default (either DS or ES) segment. The
example given below explains this mode.
Example: MOV AL,[BX+50H].
Here, the effective address is given as 10H*DS+50H+[BX].

2.2.6 BASE PLUS INDEX REGISTER ADDRESSING MODE


The effective address of data is formed, in this addressing mode, by adding
content of a base register (any one of BX or BP) to the content of an index
register (any one of SI or DI). The default segment register may be ES or DS.
Example : MOV CL, [BX+SI].
Here, BX is the base register and SI is the index register. The effective
address is computed as 10H*DS+[BX]+[SI].

2.2.7 BASE PLUS INDEX RELATIVE ADDRESSING MODE


The effective address is formed by adding an 8 or 16-bit displacement with
the sum of contents of any one of the base registers (BX or BP) and any one
of the index registers, in a default segment.
Example: MOV DL,[BX+SI+50H].
Here, 50H is an immediate displacement, BX is a base register and SI is an
index register. The effective address of data is computed as 10H*DS+[BX]+
[SI]+50H.

2.2.8 STRING ADDRESSING MODE


The segment register DS and the index register SI are used for source string
whereas fro destination string, the segment register ES and the index
register DI are used. If direction flag DF=0, both SI and DI are incremented
and If DF=1, both Si and Di are decremented.
Example: MOVSB

2.3 INSTRUCTION SET OF 8086


The 8086/8088 instructions are categorized into the following main types.
Data Copy/Transfer Instructions
Arithmetic Instructions
Logical Instructions
Control transfer Instructions
Miscellaneous Instructions
|MPMC UNIT II
BTECH_ECE-SEM 32
2.4 DATA COPY/TRANSFER INSTRUCTION
These types of instructions are used to transfer data from source operand to
destination operand. All the store, move, load, exchange, input and output
instructions belong to this category. These instructions do not affect any
flags.

2.4.1 MOV
Move this instruction transfers data from one register/memory location to
another register/ memory location. The source may be any one of the
segment registers or other general or special purpose registers or a
memory location or immediate number and another register or memory
location may act as destination. Both source and destination operands
cannot be memory.
Example:
 MOV AX, 5000H; Immediate
 MOV AX, BX; Register
 MOV AX, [SI]; Indirect
 MOV AX, [2000H]; Direct
 MOV AX, 50H [BX]; Based relative, 50H Displacement

2.4.2 PUSH

Push to stack this instruction pushes the contents of the specified register/
memory location on to the stack. The stack pointer is decremented by 2,
after each execution of the instruction. The higher byte is pushed first and
then the lower byte.
Example:
1. PUSH AX
2. PUSH DS
3. PUSH [5000H] ; Content of location 5000H and 5001H in DS are pushed
onto the stack.

2.4.3. PUSHF

|MPMC UNIT II
BTECH_ECE-SEM 32
Push Flags to Stack The push flag instruction pushes the flag register on to
the stack; first the upper byte and then the lower byte will be pushed on to
the stack. The SP is decremented by 2, for each push operation.

2.4.4. POP
Pop from Stack This instruction when executed, loads the specified register/
memory location with the contents of the memory location of which the
address is formed using the current stack segment and stack pointer as
usual. The stack pointer is incremented by 2.
Example
1. POP AX
2. POP DS
3. POP [5000H]

2.4.5. POPF
Pop Flags from Stack The pop flags instruction loads the flag register
completely (both bytes) from the word contents of the memory location
currently addressed by SP and SS. The SP is incremented by 2 for each pop
operation.

2.4.6. XCHG
Exchange This instruction exchanges the contents of the specified source
and destination operands, which may be registers or one of them, may be a
memory location.
Example
 XCHG [5000H], AX ;

This instruction exchanges data between AX and a memory location [5000H]


in the data segment.

 XCHG BX,AX ;

This instruction exchanges data between AX and BX

2.4.7. IN
Input the port This IN instruction will copy data from a port to the AL or AX
register.

|MPMC UNIT II
BTECH_ECE-SEM 32
For the Fixed port IN instruction, the 8 – bit port address of a port is
specified directly in the instruction.

Example:
IN AL, 0C8H ;
Input a byte from port 0C8H to AL
IN AX, 34H ;
Input a word from port 34H to AX
For a variable port IN instruction, the port address is loaded in DX register
before IN instruction.
Port address can range from 0000H – FFFFH.
Example:
MOV DX, 0FF78H ;
Initialize DX point to port
IN AL, DX ;
Input a byte from a 8 bit port 0FF78H to AL
IN AX, DX ;
Input a word from 16 bit port to 0FF78H to AX.

2.4.8. OUT
Output to the Port This instruction is used for writing to an output port.
Contents of AX or AL are transferred to a port after execution of this
instruction.
Example
For the Fixed port OUT instruction, the 8 – bit port address of a port is
specified directly in the instruction.
OUT 0C8H, AL ;
Sends a byte from AL to port 0C8H OUT 34H, AX ; Output a word from AX to
port 34H
For a variable port OUT instruction, the port address is loaded in DX
register before OUT instruction. Port address can range from 0000H –
FFFFH.
Example:
MOV DX, 0FF78H ;
Initialize DX point to port
|MPMC UNIT II
BTECH_ECE-SEM 32
OUT DX, AL ;

Output a byte from AL to a 8 bit port 0FF78H OUT DX, AX ; Output a word
from AX to 16 bit port 0FF78H

2.4.9. XLAT
Translate Replaces the byte in AL from a user table addressed by BX.
AL ← [AL+BX]

2.4.10. LEA
Load Effective Address The load effective address instruction loads the
offset of an operand in the specified register.
For example, the instruction
LEA BX, ADR loads the offset of the label ADR in BX

2.4.11. LDS/LES
Load Pointer to DS/ES The instruction, Load DS/ES with pointer, loads the
DS or ES register and the specified destination register in the instruction
with the content of memory location specified as source in the instruction.

2.4.12. LDS
LDS BX, [1100] loads the content of offset address 1100 &1101 in BX and
1102 &1103 in DS.

2.4.13. LAHF
Load AH from Lower Byte of Flag This instruction loads the AH register with
the lower byte of the flag register. This instruction the source string is
10H*DS+[SI], while the starting address of the destination string is
10H*ES+[DI]. may be used to observe the status of all the condition code
flags (except over flow) at a time.

2.4.14. SAHF

|MPMC UNIT II
BTECH_ECE-SEM 32
Store AH to Lower Byte of Flag Register This instruction sets or resets the
condition code flags (except overflow) in the lower byte of the flag register
depending upon the corresponding bit positions in AH.

2.4.15. REP
Repeat Instruction Prefix the instruction to which the REP prefix is provided,
is executed repeatedly until the CX register becomes zero (at each iteration
CX is automatically decremented by one). There are two more options of
the REP instruction.
The first is REPE/REPZ, i.e. repeat operation while equal/zero.
The second is REPNE/REPNZ allows for repeating the operation while not
equal/not zero.
These options are used for CMPS, SCAS instructions only, as instruction
prefixes.

2.4.16. MOVSB/MOVSW
Move String Byte or String Word
The MOVSB/MOVSW instruction moves a string of bytes/ words pointed to
by DS: SI pair (source) to the memory location pointed to by ES: DI pair
(destination).
SI and DI are incremented when the direction flag is clear and decremented
when the direction flag is set.
The REP instruction prefix is used with MOVS instruction to repeat it by a
value given in the counter (CX).

2.4.17. LODSB/LODSW
Load String Byte or String Word
The LODS instruction loads the AL/AX register by the content of a string
pointed to by DS:SI register pair.
The SI is incremented when the direction flag is clear and decremented
when the direction flag is set. Use with REP instruction prefix.

2.4.18. STOSB/STOSW
Store String Byte or String Word The STOS instruction stores the AL/AX
register contents to a location in the string pointed by ES : DI register pair.
|MPMC UNIT II
BTECH_ECE-SEM 32
The DI is incremented when the direction flag is clear and decremented
when the direction flag is set. Used with REP instruction prefix.

2.5 ARITHMETIC INSTRUCTIONS


These instructions perform the arithmetic operations, like addition,
subtraction, multiplication and division along with the respective ASCII and
decimal adjust instructions. The increment and decrement operations also
belong to this type of instructions. The arithmetic instructions affect all the
condition code flags.

2.5.1 ADD
Add this instruction adds an immediate data or contents of a memory
location or a register (source) to the contents of another register
(destination) or memory location. The result is in the destination operand.
However, both the source and destination operands cannot be memory
operands. Examples
 ADD AX, 0100H Immediate
 ADD AX, BX Register
 ADD AX, [SI] Register indirect
 ADD AX, [5000H] Direct
 ADD [5000H], 0100H Immediate

2.5.2 ADC
Add with Carry This instruction adds an immediate data or contents of a
memory location or a register (source) to the contents of another register
(destination) or memory location and carry flag. The result is in the
destination operand.
Example
 ADC AX, 0100H Immediate
 ADC AX ,BX Register
 ADC AX, [SI] Register indirect
 ADC AX, [5000H] Direct
 ADC [5000H], 0100HImmediate

2.5.3 INC – INCREMENT

|MPMC UNIT II
BTECH_ECE-SEM 32
These instructions increment the contents of the specified register or
memory location by 1. All the condition code flags are affected except the
carry flag CF.

Example
 INC AX Register
 INC [BX] Register indirect
 INC [5000H] Direct

2.5.4 DEC-DECREMENT
The decrement instruction subtracts 1 from the contents of the specified
register of memory location. All the condition code flags except carry flag
are affected.
Example
 DEC AX Register
 DEC [5000H] Direct

2.5.5 SUB- SUBTRACT


The subtract instruction subtracts the source operand from the
destination operand and the result is left in the destination operand.
Source operand may be a register, memory location or immediate data
and the destination operand may be a register or a memory location, but
source and destination operands both must not be memory operands.
Example
 SUB BX,0100H Immediate
 SUB AX, BX Register
 SUB AX, [5000H] Direct
 SUB [5000H], 0100 Immediate

2.5.6 SBB: SUBTRACT WITH BORROW


The subtract with borrow instruction subtracts the source operand and the
borrow flag (CF), from the destination operand. The result is stored in the
destination operand.
Example
 SBB BX, 0100H Immediate

|MPMC UNIT II
BTECH_ECE-SEM 32
 SBB AX, BX Register
 SBB AX, [5000H] Direct
 SBB [5000H], 0100H Immediate

2.5.7 AAA- ASCII ADJUST AFTER ADDITION


The AAA instruction is executed after an ADD instruction that adds two
ASCII coded operands to give a byte of result in AL. The AAA instruction
converts the resulting contents of AL to unpacked decimal digits.
If 35 (ASCII for 5) and 39 (ASCII for 9) are added, then AL will be having
6E. AAA instruction puts 01 in AH and 04 in AL. (9+5=14BCD). This is
done by adding 6 to lower order nibble and putting zeros in higher order
nibble. Also it increments AH.

2.5.8 AAS: ASCII ADJUST AL AFTER SUBTRACTION


AAS instruction corrects the result in AL register after subtracting two
unpacked ASCII operands. The result is in unpacked decimal format.
If 39 (ASCII for 9) is subtracted from 35 (ASCII for 5), then AL will be
having FC (- 4 in 2’s complement form) and CF =1. AAS instruction puts
04 in AL.

2.5.9 AAM : ASCII ADJUST FOR MULTIPLICATION


This instruction, after execution, converts the product available in AL into
unpacked BCD format. This follows a multiplication instruction.
If a product is available in AL = 5D. AAM instruction will form unpacked
BCD result in AX. “D” is greater than 9, so add 6 (0110) to it D+6 = 13H.
LSD of 13H is the lower unpacked byte for the result. Increment AH by 1,
5+1 =6 will be the upper unpacked byte of the result. Thus after the
execution, AH = 06 and AL = 03.

2.5.10 AAD: ASCII ADJUST FOR DIVISION


The AAD instruction converts two unpacked BCD digits in AH and AL to the
equivalent binary number in AL. This adjustment must be made before
dividing the two unpacked BCD digits in AX by an unpacked BCD byte

|MPMC UNIT II
BTECH_ECE-SEM 32
Let AX contain 0508 unpacked BCD for 58 decimal. The result of AAD
execution will give the hexadecimal number 3A in AL ( 58D =3AH) and 00
in AH.

2.5.11 DAA: DECIMAL ADJUST ACCUMULATOR


This instruction is used to convert the result of the addition of two packed
BCD number to a valid BCD number. The result has to be only in AL. If the
lower nibble is greater than 9, after addition or if AF is set, it will add 06 to
the lower nibble in AL. If the upper nibble of AL is greater than 9 or if carry
flag is set, DAA instruction adds 60H to AL.

Example
AL = 53 CL = 29
ADD AL, CL ; 7C ← 53 + 29
DAA ; AL =82 7C + 06 (as C>9)

2.5.12 DAS: DECIMAL ADJUST AFTER SUBTRACTION


This instruction converts the result of subtraction of two packed BCD
numbers to a valid BCD number. The result has to be in AL only. If the
lower nibble of AL is greater than 9, this instruction will subtract 06 from
lower nibble of AL. If the result of subtraction sets the carry flag or if upper
nibble is greater than 9, it subtracts 60H from AL.
AL = 75 BH = 46
SUB AL, BH ; 2 F =75- 46
DAS ; AL =29 (as F> 9, F – 6 = 9)

2.5.13 MUL SOURCE : UNSIGNED MULTIPLICATION BYTE OR WORD


This instruction multiplies an unsigned byte or word by the contents of AL
or AX. The unsigned byte or word may be in any one of the general
purpose registers or memory locations. If the source is byte, then AL is
multiplied with source and the result is stored in AX. If the source is word,
then AX is multiplied with source and the result is placed in DX and AX.
 MUL BH ; (AX) ←(AL) x (BH)

|MPMC UNIT II
BTECH_ECE-SEM 32
 MUL CX ; (DX) (AX)←(AX) x (CX)
 MUL WORD PTR [SI] ; (DX) (AX)←(AX) x ([SI])

2.5.14 IMUL SOURCE: SIGNED MULTIPLICATION


This instruction multiplies a signed byte in source operand by a signed
byte in AL or signed word in source operand by signed word in AX. The
source can be a general purpose register, memory operand, index register
or base register, In case of 32-bit results, the higher order word (MSW) is
stored in DX and the lower order word is stored in AX.
 IMUL BH
 IMUL CX
 IMUL [SI]

2.5.15 CBW: CONVERT SIGNED BYTE TO WORD


This instruction converts a signed byte in AL to a signed word in AX by
extending the sign of AL throughout the register AH.

2.5.16 CWD: CONVERT SIGNED WORD TO DOUBLE WORD


This instruction copies the sign bit of AX to all the DX register.

2.5.17 DIV SOURCE: UNSIGNED DIVISION


It performs unsigned division of the accumulator by source. Source may
be a register or a memory location. If the source divisor is a byte value,
AX is divided by source and the quotient will be in AL while AH will contain
the remainder. If the source is a word, DX:AX is divided by source and the
quotient will be in AX and the remainder DX. The instruction does not
affect any flag.
DIV BL ; (AX) ← (AX)/(BL)
DIV BX ; (DX)(AX) ← (DX)(AX) /BX

2.5.18 IDIV: SIGNED DIVISION


It performs signed division of the accumulator by source. If the source
divisor is a byte value, AX is divided by source and the quotient will be in
AL while AH will contain the remainder. If the source is a word, DX:AX is
divided by source and the quotient will be in AX and the remainder DX.
The instruction does not affect any flag.
|MPMC UNIT II
BTECH_ECE-SEM 32
IDIV BL ; (AX) ← (AX)/(BL)
IDIV BX ; (DX)(AX) ← (DX)(AX) /BX

2.5.19 CMP: COMPARE


This instruction subtracts the source operand, with a destination operand
but does not store the result anywhere. The source may be a register or
an immediate data or a memory location and the destination may be a
register or a memory location If both of the operands are equal, zero flag
is set. If the source operand is greater than the destination operand, carry
flag is set or else, carry flag is reset.
Example
 CMP BX, 0100HImmediate
 CMP [5000H], 0100H Direct
 CMP BX, [SI] Register indirect
 CMP BX, CX Register

2.5.20 CMPSB/CMPSW: COMPARE STRING BYTE OR STRING WORD


The CMPS instruction compares two strings of bytes or words pointed by
DS:SI and ES:DI. If both of the operands are equal, zero flag is set. If the
source operand is greater than the destination operand, carry flag is set or
else, carry flag is reset. SI and DI are incremented when the direction flag
is clear and decremented when the direction flag is set. The length of the
string must be stored in the register CX. The REP instruction prefix is used
to repeat the operation till CX (counter) become zero.

2.5.21 SCASB/SCASW : SCAN STRING BYTE OR STRING WORD


This instruction scans a string of byte or word for an operand byte or word
specified in the register AL or AX. The string is pointed to by ES:DI register
pair. DI is incremented when the direction flag is clear and decremented
when the direction flag is set. The length of the string is stored in CX.
Whenever a match to the specified operand is found in the string,
execution stops and the zero flag is set. If no match is found, the zero flag
is reset. The REP prefixes are used with the SCAS instruction.

|MPMC UNIT II
BTECH_ECE-SEM 32
2.6 LOGICAL INSTRUCTIONS
These type of instructions are used for carrying out the bit by bit shift,
rotate, or basic logical operations. All the condition code flags are affected
upon the result.

2.6.1 AND-LOGICAL AND


This instruction bit by bit ANDs the source operand that may be an
immediate, a register or a memory location to the destination operand
that may be a register or a memory location. The result is stored in the
destination operand.
Example
 AND AX, 0008H
 AND AX, BX
 AND AX, [5000H]
 AND [5000H], DX
If the content of AX is 3F0FH, the first example instruction will carry out
the operation as given below. The result 0008H will be stored in the AX
register.
= 3F0F H
0 0 11 1111 0000 1111
[AX]
AND

0000 0000 0000 0000 = 0008 H


= 0008 H
0000 0000 0000 1000
[AX]

2.6.2 OR: LOGICAL OR


This instruction bit by bit ORs the source operand that may be an
immediate, a register or a memory location to the destination operand
that may be a register or a memory location. The result is stored in the
destination operand
Example
OR AX,0098H
OR AX,BX
OR AX,[5000H]

|MPMC UNIT II
BTECH_ECE-SEM 32
OR [5000H],0008H
If the contents of AX are 3F0FH, then the first example instruction will be
carried out as given below. Thus the result 3F9FH will be stored in the AX
register
0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 = 3F0F H [AX]OR
0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 1 = 0098 H
0 0 1 1 1 1 1 1 1 0 0 1 1 1 1 1 = 3F9F H [AX] =Result

2.6.3 XOR: Logical Exclusive OR


This instruction bit by bit XORs the source operand that may be an
immediate, a register or a memory location to the destination operand
that may be a register or a memory location. The result is stored in the
destination operand. The XOR operation gives a high output, when the 2
input bits are dissimilar. Otherwise, the output is zero.
Example
 XOR AX, 0098H
 XOR AX, BX
 XOR AX, [5000H]
If the content of AX is 3FOFH, then first example instruction will be
executed as explained. The result 3F97H will be stored in AX.
0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 = 3F0F H [AX]OR
0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 1 = 0098 H
0 0 1 1 1 1 1 1 1 0 0 1 0 1 1 1 = 3F97 H [AX] =Result

2.6.4 TEST: LOGICAL COMPARE INSTRUCTION


The TEST instruction performs a bit by bit logical AND operation on the
operands. The result of this AND-ing operation is not available for further
use, but flags are affected.
Example
1. TEST AX, BX
2. TEST [0500], 06H
3. TEST [BX] [DI], CX

|MPMC UNIT II
BTECH_ECE-SEM 32
2.6.5 NOT: LOGICAL INVERT
The NOT instruction complements (inverts) the contents of an operand
register or a memory location, bit by bit.
Example
NOT AX
NOT [5000 H]
If the content of AX is 200FH, the first example instruction will be
executed as shown. The result DFF0H will be stored in the destination
register AX.
AX = 0010 0 0 0 0 0000 1111
invert

AX
1 1 0 1 1 1 1 1 11 1 1 0 0 0
0

2.6.6 NEG DESTINATION NEGATE


The negate instruction forms 2’s complement of the specified destination
in the instruction. For obtaining 2’s complement, it subtracts the contents
of destination from zero. The result it stored back in the destination
operand which may be a register or a memory location.

2.6.7 SHL/SAL DESTINATION, COUNT


Shift logical/Arithmetic Left These instructions shift the operand word or
byte bit by bit to the left by the specified count and insert zeros in the
newly introduced least significant bits. The count is either 1 or specified
by register CL. The operand may reside in a register or a memory

location .

2.6.8 SHR DESTINATION, COUNT


Shift Logical Right This instruction performs bit-wise right shifts on the
operand word or byte by the specified count in the instruction and inserts

|MPMC UNIT II
BTECH_ECE-SEM 32
zero in the shifted position. The count is either 1 or specified by register
CL. The operand may reside in a register or a memory location.

2.6.9 SAR DESTINATION, COUNT


Shift Arithmetic Right This instruction performs right shifts on the operand
word or byte by the specified count in the instruction and inserts the most
significant bit of the operand in the newly inserted positions. The count is
either 1 or specified by register CL. This shift operation shifts the operand
through carry flag.

2.6.10ROR DESTINATION,COUNT
Rotate Right without Carry This instruction rotates the contents of the
destination operand to the right (bit-wise) either by one or by the count
specified in CL, excluding carry. The least significant bit is pushed into the
carry flag and simultaneously it is transferred into the most significant bit
position at each operation. The remaining bits are shifted right by the
specified position.

2.6.11ROL DESTINATION,COUNT

|MPMC UNIT II
BTECH_ECE-SEM 32
Rotate Left without Carry This instruction rotates the content of the
destination operand to the left by the specified count (bit-wise) excluding

carry. The

most significant bit is pushed into the carry flag as well as the least
significant bit position at each operation. The remaining bits are shifted
left subsequently by the specified count positions.

2.6.12RCR DESTINATION,COUNT
Rotate Right through Carry This instruction rotates the contents (bit-wise)
of the destination operand right by the specified count through carry flag
(CF). For each operation, the carry flag is pushed into the MSB of the
operand, and the LSB is pushed into carry flag. The remaining bits are
shifted right by the specified count positions.

2.6.13RCL DESTINATION,COUNT
Rotate Left through Carry This instruction rotates (bit-wise) the contents
of the destination operand left by the specified count through the carry
flags (CF). For each operation, the carry flag is pushed into LSB, and the
MSB of the operand is pushed into carry flag. The remaining bits are
shifted left by the specified positions. The SF, PF, ZF are left unchanged.
The operand may be a register or a memory location. The count for
rotation or shifting is either 1 or is specified using register CL, in case of
all the shift and rotate instructions.

|MPMC UNIT II
BTECH_ECE-SEM 32
2.7 CONTROL TRANSFER OR BRANCHING INSTRUCTION
The control transfer instructions transfer the flow of execution of the
program to a new address specified in the instructions directly or indirectly.
 Unconditional Control Transfer (Branch) Instruction In these
instructions, the execution control is transferred to the specified
location independent of any status or conditions. The CS and IP are
unconditionally modified to the new CS and IP.
 Conditional Control Transfer (Branch) Instructions In these
instructions, the control is transferred to the specified location
provided the result of the previous operation satisfies a particular
condition, otherwise, the execution continues in normal flow sequence.

2.7.1 UNCONDITIONAL BRANCH INSTRUCTIONS- CALL


Unconditional Call This instruction is used to call a procedure from a
main program. There are two types of procedures depending upon
whether it is available in the same segment (Near CALL, i.e. = 32K
displacement) or in another segment (Far CALL, i.e. anywhere outside
the segment). The modes for them are respectively called as
intrasegment and intersegment addressing modes. On execution, this
instruction stores the incremented IP (i.e. address of the next
instruction) and CS onto the stack along with the flags and loads the
CS and IP registers, respectively, with the segment and offset
addresses of the procedure to be called.

2.7.2 UNCONDITIONAL BRANCH INSTRUCTIONS- RET


Return from the Procedure At each CALL instruction, the IP and CS of
the next instruction is pushed onto stack, before the control is
transferred to the procedure. At the end of the procedure, the RET
instruction must be executed. When it is executed, the previously
stored contents of IP and CS along with flags are retrieved into the CS,
IP and flag registers from the stack and the execution of the main
program continues further. The procedure may be a near or a far
procedure. In case of a FAR procedure, the current contents of SP
|MPMC UNIT II
BTECH_ECE-SEM 32
points to IP and CS at the time of return. While in case of a NEAR
procedure, it points to only IP

2.7.3 UNCONDITIONAL BRANCH INSTRUCTIONS- INT N


Interrupt Type N In the interrupt structure of 8086, 256 interrupts are
defined corresponding to the types from 00H to FFH. When an INT N
instruction is executed, the TYPE byte N is multiplied by 4 and the
contents of IP and CS of the interrupt service routine will be taken from
the hexadecimal multiplication (Nx4) as offset address and 0000 as
segment address.

2.7.4 UNCONDITIONAL BRANCH INSTRUCTIONS- INT O


Interrupt on Overflow This is executed, when the overflag OF is set.
The new contents of IP and CS are taken from the address 0000:0010.
This is equivalent to a Type 4 interrupt instruction.

2.7.5 UNCONDITIONAL BRANCH INSTRUCTIONS- JMP


Unconditional Jump This instruction unconditionally transfers the
control of execution to the specified address using an 8-bit or 16-bit
displacement (intrasegment relative, short or long) or CS: IP
(intersegment direct far).
JMP DISP 8-bit Intrasegment, relative, near/short jump
JMP DISP 16-bit Intrasegment, relative, Far jump
JMP IP CS Intersegment, direct, far jump

2.7.6 UNCONDITIONAL BRANCH INSTRUCTIONS- IRET


Return from ISR When an interrupt service routine is to be called,
before transferring control to it, the IP, CS and flag register are stored
on to the stack to indicate the location from where the execution is to
be continued, after the ISR is executed. So, at the end of each ISR,
when IRET is executed, the values of IP, CS and flags are retrieved
from the stack to continue the execution of the main program.
|MPMC UNIT II
BTECH_ECE-SEM 32
2.7.7 UNCONDITIONAL BRANCH INSTRUCTIONS- LOOP
Loop Unconditionally This instruction executes the part of the program
from the label or address specified in the instruction up to the loop
instruction, CX number of times. At each iteration, CX, is decremented
automatically.
Example
MOV CX, 0005 ; Number of times in CX Label : MOVSB
Loop Label

2.7.8 CONDITIONAL BRANCH INSTRUCTIONS- CONDITIONAL JUMP


When these instructions are executed, they transfer execution control
to the address specified relatively in the instruction, provided the
condition implicit in the opcode is satisfied, otherwise, the execution
continues sequentially. The displacement must be 8-bit(–128 to 127
bytes) from the address of the branch instruction.
Example
 Mnemonic Jump condition
 JZ/JE Label ZF=1
 JNZ/JNE Label ZF=0
 JC/JB/JNAE Label CF=1
 JNC/JNB/JAE Label CF=0
 JS Label SF=1
 JNS Label SF=0
 JP/JPE Label PF=1
 JNP Label PF=0
 JL/JNGE Label SF≠1 and OF≠1 for signed numbers
 JNL/JGE Label SF≠0 and OF≠0

2.7.9 CONDITIONAL BRANCH INSTRUCTIONS- CONDITIONAL LOOP


LOOPZ / LOOPE Label
Loop through a sequence of instructions from ‘Label’ while ZF=0 and
CX = 0.
LOOPNZ / LOOPNE Label

|MPMC UNIT II
BTECH_ECE-SEM 32
Loop through a sequence of instructions from ‘Label’ while ZF=0 and
CX ≠ 0.

2.7.10MISCELLANEOUS INSTRUCTIONS:
CLC - Clear carry flag
CMC - Complement carry flag
STC - Set carry flag
CLD- Clear direction flag
STD - Set direction flag
CLI - Clear interrupt flag
STI - Set interrupt flag

2.7.11 WAIT
The processor goes on inserting WAIT states in the instruction cycle,
till the TEST pin goes low. Once the TEST pin goes low, it continues
further execution.
2.7.12 HLT
Halt the processor until RESET line is activated.
2.7.13 NOP
No operation. The processor does not perform any operation till 4 clock
cycles, except incrementing the IP by one
2.7.14 ESC
Escape to external device like NDP (numeric co-processor)
2.7.15 LOCK
This instruction is a prefix that causes the CPU assert bus lock signal
during the execution of the next instruction.
2.7.16 FALC
Fills AL with carry. If CF=0, AL =00H. If CF=1,AL=FFH

2.8 ASSEMBLER DIRECTIVES AND OPERATORS


An assembler is a program used to convert an assembly language
program into the equivalent machine code modules which may further
be converted to executable codes. Assembler directives help the
assembler to correctly understand the assembly language programs to
prepare the codes.
|MPMC UNIT II
BTECH_ECE-SEM 32
DB: Define Byte The DB directive is used to reserve byte of memory
locations in the available memory. While preparing the EXE file, this
directive directs the assembler to allocate the specified number of
memory bytes to the said data type may be a constant, variable,
string, etc. Example RANKS DB 01H,02H,04H
This statement directs the assembler to reserve for memory location
for a list named RANKS and initialize them with the above specified
four values.
MESSAGE DB ‘GOOD MORNING’
This makes the assembler reserve the number of bytes of memory
equal to the number of characters in the string named MESSAGE and
initialize those locations by the ASCII equivalent of these characters.
VALUE DB 50H
This statement directs the assembler to reserve 50H memory bytes
and leave them uninitialised for the variable named VALUE.

DW: Define Word The DW directive serves the same purposes as the
DB directive, but it now makes the assembler reserve the number of
memory word (16-bit) instead of bytes. Some example are given to
explain this directive.
Example WORDS DW 1234H, 78ABH, 045CH,
This makes the assembler reserve four words in memory (8bytes), and
initialize the word with the specified values in the statement, During
initialsation, the lower bytes are stored at the lower memory
addresses, while the upper bytes are stored at the higher addresses.
Another option of the DW directive is explained with the DUP operator.
WDATA DW 5 DUP (6666H)
This statement reserves five words, i.e. 10-bytes of memory for a word
lable WDATA and initialises all the word locations with 6666H.

DQ: Define Quadword This directors is used to direct, the assembler


to reserve 4 words (8bytes) of memory for the specified variable and
may initialise it with the specified values.
|MPMC UNIT II
BTECH_ECE-SEM 32
DT: Define Ten Bytes The DT directive directs the assembler to
define the specified variable requiring 10-bytes its storage and
initialise the 10-bytes with the specified values. The directive may be
used in case of variable facing heavy numerical calculations, generally
processed by numerical processors.

ASSUME: Assume Logical Segment Name The ASSUME directive is


used to inform the assemble, the name of the logicals segments to be
assumed for different segments used in the program. The statement
ASSUME CS : CODE directs the assembler that the machine code are
available in a segment named CODE, and hence the CS register to be
loaded with the address (segment) allotted by the operating system
for the label CODE, while loading. Similary, ASSUME DS: DATA
indicates to the assembler that the data items related to the program,
are available in a logical segment named DATA, and the DS register is
to be initialised by the segment address value decided by the
operating system for the data segment, while loading.

END: END of Program The END directed marks the end of an


assembly language program. When the assembler comes across this
END directive, it ignores the source lines available later on.

ENDP: END of Procedure In assembly language programming, the


subroutines are called procedures. The ENDP directive is used to
indicate the end of a procedure. A procedure is usually assigned a
name, i.e. label. To mark the end of a particular procedure, the name
of the procedure, i.e. label may appear as prefix with the directive
ENDP.
PROCEDURE STAR
:
: STAR ENDP

|MPMC UNIT II
BTECH_ECE-SEM 32
ENDS: END of Segment This directive marks the end of a logical
segment. The logical segments are assigned with the names using the
ASSUME directive. The names appear with the ENDS directive as
prefixes to mark the end of those particular segments.
DATA SEGMENT:
DATA ENDS
ASSUMECS : CODE,
DS : DATA CODE SEGMENT
:
CODE ENDS END

EVEN: Align on Even Memory Address The assembler, while


starting the assembling procedure of any program, initialises a location
counter and goes on updating it, as the assembly proceeds. It goes on
assigning the available addresses, i.e. the contents of the location
counter, sequentially to the program variables, constants and modules
as per their requirements, in the sequence in which they appear in the
program. The EVEN directive updates the location counter to the next
even address, if the current location counter contents are not even,
and assigns the following routine or variable or constant to that
address.
EVEN
PROCEDURE ROOT
:
ROOT ENDP
The above structure shows a procedure ROOT that is to be aligned at
an even address.

EQU: Equate The directive EQU is used to assign a label with a value
or a symbol. The use of this directive is just to reduce the recurrence
of the numerical values or constants in a program code. The recurring
value is assigned with a label, and that label is used in place of that
numerical value, throughout the program.
Example
LABEL EQU 0500H
|MPMC UNIT II
BTECH_ECE-SEM 32
ADDITION EQU ADD
The first statement assigns the constant 500H with the label LABEL,
while the second statement assigns another label ADDITION with
mnemonic ADD.

EXTRN: External and PUBLIC: Public The directive EXTRN informs


the assembler that the names, procedures and labels declared after
this directive have already been defined in some other assembly
language modules. While in the other module, where the names,
procedures and labels actually appear, they must be declared public,
using the PUBLIC directive. If one wants to call a procedure FACTORIAL
appearing in MODULE1 from MODULE 2; in MODULE1; it must be
declared PUBLIC using the statement PUBLIC FACTORIAL and in
module2; it must be declared external using the declaration EXTRN
FACTORIAL.
MODULE1 SEGMENT
PUBLIC FACTORIAL FAR
MODULE1 ENDS
MODULE2 SEGMENT
EXTRN FACTORIAL FAR
MODULE2 ENDS

GROUP: Group the Related Segments The directive is used to form


logical groups of segments with similar purpose or types. The
assembler passes information to the linker/loader to form the code
such that the group declared segments or operands must lie within a
64Kbyte memory segment. Thus all such segments and labels can be
addressed using the same segment base.
PROGAM GROUP CODE, DATA, STACK
The above statement directs the loader/linker to prepare an EXE file
such that CODE, DATA and STACK segment must lie within a 64Kbyte
memory segment that is named as PROGRAM.

LABEL :Label The Label directive is used to assign a name to the


current content of the location counter. At the start of the assembly
|MPMC UNIT II
BTECH_ECE-SEM 32
process, the assembler initialises a location counter to keep track of
memory locations assigned to the program. As the program assembly
proceeds, the contents of the location counter are updated. During the
assembly process, whenever the assembler comes across the LABEL
directive, it assigns the declared label with the current contents of the
location counter. The type of the label must be specified, i.e. whether
it is a NEAR or a BYTE or WORD label, etc.A LABEL directive may be
used to make a FAR jump as shown below. A FAR jump cannot be
made at a normal label with a colon. The labels CONTINUE can be used
for a FAR jump, if the program contains the following statement.
CONTINUE LABEL FAR

LENGTH: Byte Length of a Label This is used refer to the length of


a data array or a string.MOV CX, LENGTH ARRAY
This statement, when assembled, will substitute the length of the array
ARRAY in bytes, in the instruction.

LOCAL The labels, variables, constants or procedures declared LOCAL


in a module are to be used only by that module. At a later time, some
other module may declare a particular data type LOCAL, which is
previously declared LOCAL by an other module or modules.
LOCAL a, b, DATA, ARRAY, ROUTINE

NAME: Logical Name of a Module The NAME directive is used to


assign a name to an assembly language program module. The module,
may now be referred to by its declared name. The names, if selected
to be suggestive, may point out the functions of the different modules
and hence may help in the documentation.

OFFSET: Offset of a Label When the assembler comes the OFFSET


operator along with a label, it first computes the 16-bit displacement
(also called as offset interchangeably) of the particular label, and

|MPMC UNIT II
BTECH_ECE-SEM 32
replaces the string ‘OFFSET LABEL’ by the computed
displacementExample
CODE SEGMENT MOV SI, OFFSET LIST CODE ENDS
DATA SEGMENT LIST DB 10H DATA ENDS

ORG: Origin The ORG directive directs the assembler to start the
memory allotment for the particular segment, block or code from the
declared address in the ORG statement. While starting the assembly
process for a module, the assembler initialises a location counter to
keep track of the allotted addresses for the module. If ORG statement
is not written in the program, the location counter is initialised to 0000.
If the ORG 200H statement is present at the starting of the code
segment of that module, then the code will start from 200H address in
code segment

PROC: Procedure The PROC directive marks the start of a named


procedure in the statement. Also, the types NEAR or FAR specify the
type of the procedure, i.e. whether it is to be called by the main
program located within 64K of physical memory or not. For example,
the statement RESULT PROC NEAR marks the start of a routine
RESULT, while is to be called by a program located in the same
segment of memory. The FAR directive is used for the procedures to
be called by the programs located in different segment of memory.
The example statement are as follows:
Example
RESULT PROC NEAR ROUTINE PROC FAR

PTR: Pointer The pointer operator is used to declare the type of a


label, variable of memory operand. The operator PTR is prefixed by
either BYTE or WORD. If the prefix is BYTE, than the particular label,
variable or memory operand is treated as an 8-bit quantity, while if
WORD is the prefix, then it is treaded as a 16-bit quantity.
Example

|MPMC UNIT II
BTECH_ECE-SEM 32
MOV AL, BYTE PTR [SI] - Moves content of memory location addressed
by SI (8-bit) to AL INC BYTE PTR [BX] - Increments byte contents of
memory location addressed by BX

SEG: Segment of a Label The SEG operator is used to decide the


segment address of the label, variable, or procedure and substitutes
the segment base address in place of ‘SEG label”. The example given
below explain the use of SEG operator.
Example
MOV AX, SEG ARRAY ; This statement moves the segment address
of ARRAY in MOV DS, AX ; which it is appearing, to register AX and
then to DS. SEGMEN:

Logical Segment The SEGMENT directive marks the starting of a


logical segment. The started segment is also assigned a name, i.e.
label, by this statement. The SEGMENT and ENDS directive must
bracket each logical segment of a program

SHORT The SHORT operator indicates to the assemble that only one
byte is required to code the displacement for a jump (i.e. displacement
is within –128 to + 127 bytes from the address of the byte next to the
jump opcode). This method of specified the jump address saves the
memory. Otherwise, the assembler may reserve two bytes for the
displacement. The syntax of the statement is as given below. JMP
SHORT LABEL

TYPE The TYPE operator directs the assembler to decide the data type
of the specified label and replaces the ”TYPE label’ by the decided data
type. For the word type variable, the data type is 2, for double word
type, it is 4, and for byte type, it is 1. Suppose, the STRING is a word
array. The instruction MOV AX, TYPE STRING moves the value 0002H in
AX.

|MPMC UNIT II
BTECH_ECE-SEM 32
GLOBAL The labels, variable, constants or procedures declared
GLOBAL may be used by other modules of the program. Once a
variable is declared GLOBAL, it can be used by any module in the
program. The following statement declares the procedure ROUTINE as
a global label.
ROUTINE PROC GLOBAL

2.9 MODULAR PROGRAMMING


Program is composed from several smaller modules. Modules could be
developed by separate teams concurrently. The modules are only
assembled producing .OBJ modules (Object modules). Each module is
produced from a separate Assembly Language program.
. The .OBJ modules so produced are combined using a LINK program.
. Another concept that is useful is that of a Library of Object Modules.
Frequently used procedures could be assembled into object modules
and these object modules are placed in a Library file that is linked into
the application. Note that only the required object modules are pulled
from the Library to be linked to the final application.
.Ex: Let us say, in Module A , we defined num DB 10 DUP (?)
. In Module B , we wish to access BUF1, say as in
MOV DX, OFFSET num
. num is not defined in Module B. Thus, when assembling Module B, we
will get the assembly error of .Undefined Symbol.. Note that the
symbol is actually defined in Module A. But, the two modules are
assembled independently!
. Problem: How to make BUF1, defined in Module A, accessible to
Module B?
. Solution:
. Declare num as public in Module A
(Interpretation: Defined in this module, may be used in other
modules.)
. Declare num as external in Module B
(Interpretation: Used in this module, but defined in some other
module.)
num becomes a global symbol.
|MPMC UNIT II
BTECH_ECE-SEM 32
Its use is resolved by a Linker program.
What happens if num is declared as external in one module; but Linker
does not find its definition in any of the modules being linked
together? This will be a Link Error, something like: .Globally Unresolved
Symbol.
. Properly used, Public and Extrn facilities allow modules to
communicate with each other, which is essential for Modular
Programming
2.10 PUBLIC AND EXTRN DIRECTIVES
The following example illustrates the use of Public and External
directives to achieve communication among independently assembled
modules.PROG1.ASM, defines two symbols BUF1 and BUF2 and
declares them as PUBLIC. It also defines a Far Procedure called RDKEY
and declares this also as PUBLIC. Such a declaration allows these
symbols to be accessed from other modules. Thus, BUF1, BUF2
and RDKEY can be accessed from other modules.
File 1: PROG1.ASM
.MODEL SMALL
.DATA PUBLIC BUF1 PUBLIC BUF2
BUF1 DB 10 DUP (?)
Modular Programming in 8086
BUF2 DW 10 DUP (?)
.CODE
.STARTUP PUBLIC RDKEY
RDKEY PROC FAR ; function definition here MOV AH, 01
INT 21H
RDKEY ENDP END
In another program PROG2.ASM, we make use of the symbols BUF1,
BUF2 and RDKEY.We declare them as EXTRN. Such a declaration
indicates that the definitions for these symbols are not in this module;
rather they are expected to be defined in other modules which will be
specified during LINK time.
. Note that the type of the EXTRN data items must be declared for
proper assembly. Similarly, the FAR/NEAR nature of an EXTRN

|MPMC UNIT II
BTECH_ECE-SEM 32
procedure also must be declared for the Assembler to generate correct
code.

File 2: PROG2.ASM
.MODEL SMALL
.DATA
EXTRN BUF1:BYTE EXTRN BUF2:WORD
.CODE
EXTRN RDKEY:FAR
.STARTUP
MOV DX, OFFSET BUF1 MOV CX, 10
L1: CALL RDKEY STOSB
LOOP L1
MOV BUF2, AX
.EXIT END
. We now have 2 Files: PROG1.ASM and PROG2.ASM
. These can be assembled and Linked by the following command
ML PROG1.ASM PROG2.ASM
Now assemble the two programs separately, getting the two .OBJ files.
MASM PROG1; MASM PROG2;
Run LINK utility and specify the two .OBJ files as inputs.
LINK PROG1.OBJ PROG2.OBJ

2.11 LIBRARIES
. Frequently used procedures for a given application domain may be
placed in a .Library File. as .OBJ files.
. The Library file can be specified at LINK time.
Modular Programming in 8086
. Only the required .OBJ files are extracted from the Library File and
linked to the program.
. Thus, Libraries provide a powerful reuse mechanism.
LIB Command:
The LIB command provides the following facilities:
. Create a new Library file
. Add .OBJ file to a Library file.
|MPMC UNIT II
BTECH_ECE-SEM 32
. Delete .OBJ file from a Library file.
. Replace an existing .OBJ file in the Library with another .OBJ file with
the same name (equivalent to Delete followed by Add)
. The LIB command is used as follows:
LIB library file name
If the named Library file does not exist, the system prompts whether
to create? Type Y
It prompts for operation (operation could be specified on command
line also). The Operation can be: + (add) ; - (remove) ; -+ (replace)
Example:
Create a Library file called MYP1.LIB and add the module PROG1 to the
library:
> LIB MYP1.LIB
; some messages are displayed Library file does not exist. Create? Y
Operations: + PROG1
List File: MYP1
To the Library file called MYP1.LIB, add the module PROG2:
> LIB MYP1.LIB
; some messages are displayed
Operations: +PROG2
>
To the Library file called MYP1.LIB add the module PROG3:
>LIB MYP1.LIB
; some messages are displayed
Operations: +PROG3
>
Alternatively, the 3 modules could be added to the Library file as
shown below:
>LIB MYP1.LIB
; some messages are displayed from the utility Library file does not
exist. Create? Y Operations: PROG1 + PROG2 + PROG3
List File: MYP1
>
From the Library file called MYP1.LIB, remove the module PROG2:
|MPMC UNIT II
BTECH_ECE-SEM 32
> LIB MYIO1.LIB
Copyright messages etc from the utility
Operations: -PROG2
>
Modular Programming in 8086
. List file shows:
. Sizes and names of the files in the Library
. Public labels available in the Library
. Once we create Library files, we can use them to link the required
modules into the application program by specifying the Library files to
the Linker. With ML or with LINK, specify the Library files required in
response to the prompt
Libraries [.lib]:
. Library files have .lib as the default extension.

2.12 PROCEDURES
. Procedure or a subroutine or a function is a key concept for
modular programming, the essential way to conquer complexity.
. A procedure is a reusable set of instructions that has a name.
. Only one copy of the procedure is stored in the memory; and it can
be .called. as many times as needed.
. As only one copy is stored, it saves memory; but has execution time
overhead for the
.call. and .return. operations.
. Macros are faster but consume more space.
. .CALL. transfers control to the procedure like with a jump; but unlike
a jump, procedure has a .RETURN. instruction which returns control to
the instruction following the CALL instruction! In order to implement
such a return, the necessary information is stored on a stack, before
transferring control to the procedure. . Further, nested procedures
calls are possible. In other words, Procedure A can call Procedure B
which in turn calls Procedure C. After completing Procedure C, control
returns to Procedure B and after completing Procedure B, control
returns to Procedure

|MPMC UNIT II
BTECH_ECE-SEM 32
A. Logically, such a nesting of calls can be to any level, though in
practice Assemblers impose implementation-dependent limits on the
nesting depth.
. It is also possible for a Procedure to call itself (a recursive procedure).
Of course, to avoid infinite regress, the procedure would have an
alternative that would not involve recursion. Examples of recursive
procedures are described in later sessions.
. We see that return addresses are known in one order and are used
for implementing
.return. in exactly the reverse order. Thus a Stack would be the most
convenient data structure for storing return addresses

2.13 PROCEDURE - RELATED INSTRUCTIONS/DIRECTIVES


. A procedure may be in the same code segment as that of the main
program (Intra-segment).In such a case, we specify only IP as relative
distance or indirectly as actual value. This is known as NEAR CALL.
. A procedure may be in a different code segment (Inter segement). In
such a case, we need to specify both IP and CS (directly or indirectly).
This is known as a FAR CALL
. In program, procedure starts with PROC directive and ends with ENDP
directive.
. Each directive follows the name of the procedure.
. PROC directive is followed by the type of procedure: NEAR or FAR.
. NEAR or FAR can be followed by USES statement.
. USES statement allows specification of registers which are
automatically pushed onto the stack and popped from the stack within
the procedure.
Modular Programming in 8086
Near CALL Instruction:
. Similar to Near Jump except that current IP is saved on the stack
before transferring control to the new IP with CS remaining the same.
. Displacement is specified as 16. bit signed integer. Target range is
thus -32768 bytes to +32767 bytes and consequently, target can be
anywhere in the current code segment. . This Instruction is 3 . Byte
long
|MPMC UNIT II
BTECH_ECE-SEM 32
Main program:
|
..
CALL MULT ; calling a procedure. Transfer control to procedure named
as MULT
..
; Procedure definition
MULT PROC NEAR USES BX MOV AX, 1
ADD AX, BX RET
MULT ENDP
...
CALL SR1

2.14 FAR CALL INSTRUCTION


. FAR CALL is like FAR JMP in the sense that it can call a procedure
available anywhere in the code space.
. In this case, the target address is directly specified as new CS:IP.
. Both current IP and CS are saved on the stack and then control is
transferred to the new CS:IP
. The instruction is 5 . Byte long. The first byte specifies the opcode,
the next two bytes
specify the new IP value and the next two bytes specify the new CS
value. The format is shown below:
. Example:
SUM1 PROCFAR
......
SUM1 ENDP
E8 Low Byte High Byte
OPcode IP
Low Byte High Byte CS

Modular Programming in 8086


Now, a CALL to SUM1 is assembled as FAR CALL.
CALL with Register Operands:

|MPMC UNIT II
BTECH_ECE-SEM 32
. As with JMP, 16-Bit register may be used as the operand for the CALL
instruction. (Indirect CALL)
. Again, as with JMP, contents of the register are transferred directly
into IP (not relative displacement).
. This is always a Near CALL.
. Example: Assume BX = 0080H. Now the execution of the instruction:
CALL BX ; calls the procedure at 0080H in the current code segment

2.15 INDIRECT CALL USING INDEX


. Like the corresponding JMP instruction, this instruction uses [ ] form
of addressing to directly access a table of CALL addresses.
. This form is called Indirect Memory CALL also.
. Example: CALL TABLE [SI]
Fetch the word, using indexed addressing, at the offset of TABLE [SI]
from the current code segment and CALL the near procedure at that
address.
. FAR PTR directive indicates a far CALL (the table is assumed to
contain double words giving CS, IP values)
. Otherwise a near CALL is indicated. (the table is assumed to contain
words giving IP values)
. Example: CALL FAR PTR [SI]
Fetch the double word, using indexed addressing, at the offset of [SI]
from the current code segment and CALL the far procedure at that
address.
Return from Procedure:
. We use a RET instruction to .return. from the called procedure.
. The control returns to the instruction following the CALL instruction in
the calling program. Corresponding to the two varieties of CALL
instructions (near & far), two forms of RET instructions (near & far)
exist.
. Near RET instruction pops a 16-Bit word from the stack and places it
in the IP.
. Far RET instruction pops two 16-Bit words from the stack and places
them in IP & CS.

|MPMC UNIT II
BTECH_ECE-SEM 32
. In ALP, RET is written within the procedure, before the ENDP directive
and the Assembler will automatically select the proper (Near or Far)
RET instruction!
. Another form: RET immediate value
Example: RET 6
In this case, after popping return address from the stack, the specified
number (in this case, 6) is added to SP, effectively removing some (in
this case 3) words from the stack! These words could have been
pushed onto stack before calling. This is the scheme followed by
compilers.

2.16 MACRO
Macros provide several powerful mechanisms useful for the
development of generic programs.
Modular Programming in 8086
MACRO is a group of instructions with a name.
When a macro is invoked, the associated set of instructions is inserted
in place into the source, replacing the macro name. This .macro
expansion. is done by a Macro
Preprocessor and it happens before assembly. Thus the actual
Assembler sees the .expanded. source !
We could consider the macro as shorthand for a piece of text.

2.17 MACROS VS PROCEDURES


Procedure:
 Only one copy exists in memory. Thus memory consumed is less.
 .Called. when required;
 Return address (IP or CS:IP) is saved on stack before transferring
control to the subroutine thro. CALL instruction. It should be
popped again when control comes back to calling program with
RET instruction.
 Execution time overhead is present because of the call and
return instructions.
 If more lines of code, better to write a procedure
MACRO
|MPMC UNIT II
BTECH_ECE-SEM 32
 When a macro is .invoked., the corresponding code is .inserted.
into the source. Thus multiple copies of the same code exist in
the memory leading to greater space requirements.
 However, there is no execution overhead because there are no
additional call and return instructions. The code is in-place.
 Good if few instructions are in the Macro body.
 No use of stack for operation

2.18 MACRO DEFINITION


A macro has a name. The body of the macro is defined between a pair
of directives, MACRO and ENDM.
Examples of Macro Definitions:
; Definition of a Macro named SAVE
SAVE MACRO
PUSH AX
PUSH BX
PUSH CX
ENDMHKJS;
Another Macro named RETRIEVE is defined here RETRIEVE MACRO
POP CX POP BX POP AX ENDM
Examples of Macro usage
The following examples illustrate the use of macros. We first show the
source with macro invocation and then show how the expanded source
looks.

9. Practice Quiz
1. Find the odd instruction out of these.
(a)AAA (b) AAS (c) AAM (d) AAD
2. In 8086 microprocessor one of the following statements is not
true.
(a)Coprocessor is interfaced in MAX mode
(b)Coprocessor is interfaced in MIN mode
|MPMC UNIT II
BTECH_ECE-SEM 32
(c)I/O can be interfaced in MAX/MIN mode
(d)Supports Pipelining
3. What is the length of flag register and how many flags are there in
8086?
(a)16 bits and 9 (b) 8 bits and 9 (c) 16 bits and 8 (d) 16 bits
and 10
4. If there are 20 address lines in a microprocessor, what is the size
of memory that can be interfaced?
(a)64 KB (b) 1 MB(c) 10 MB(d) 2 MB
5. If SP = 4FFFAH, after execution of POP AX instruction SP becomes
(a)4FFFCH (b) 4FFF8H (c) 4FFECH (d) 4FFE8H
6. If Direction flag = 0, on executing MOVSB instruction the address
of the string will be
(a)Decrementing (b) Incrementing (c) Will not change (d)
None
7. Which segment is used to store interrupt and subroutine return
address?
(a)Code Segment (b) Data segment (c) Extra segment (d)Stack
segment
8. The method of accessing data in Stack and Queue is
(a)LIFO, LILO (b)FIFO, LIFO (c) LIFO, FIFO (d) LILO, FIFO
9. The LOCK prefix instruction, when executed locks ______
(a) the registers (b) the I/O devices(c) the bus (d) the Master
10. A high on HOLD pin indicates
(a)The master processor is holding the Bus
(b)Another master is requesting to take over the system bus
(c)The master is holding the slave bus
(d)All the above
11. If the result is too large positive number or a too small negative
number, which flag is set?
(a)Trap flag (b) Direction flag (c) Overflow flag (d) Auxiliary
Carry flag
12. The instruction IN AL,80H copies
(a)8 bit content from AL to 80H port address
(b)8 bit content from 80H port address to AL
(c)16 bit content from AL to 80H port address
(d)16 bit content from 80H port address to AL

13. Fetching next instruction while the current instruction is


executing is called ________ and this is done to increase the ________
of execution.
(a)Pipelining, Speed (b) Speeding up, Throughput
(c) Pipelining, Throughput (d) Queuing, Speed
14. The pins QS1, QS0 belongs to ___ mode and reflects the status
|MPMC UNIT II
BTECH_ECE-SEM 32
of _____________.
(a)MIN, Interrupt register (b) MAX, Interrupt register
(c) MAX, Instruction Queue (d) MIN, Instruction Queue
15. When the status bits ( S2 , S1, S0 ) are 1 1 0, it represents
(a)I/O Read (b) Memory Read (c) I/O Write (d) Memory Write

10. Assignments
S.No Question BL CO
Explain with examples the following 8086 instructions
1 2 CO2
a) AAA b) CBW c) IMUL d) INTO
Write an assembly language program for 8086 to sort the CO2
2 2
array of elements in ascending order.
Write an assembly language program for 8086 to find the CO2
3 2
largest element in an array.
Write an assembly language program for 8086 to convert BCD CO2
4 2
data to binary data
What is the function of the ASSUME, DB, EQU, EVEN assembler CO2
5 2
directives of 8086?
Explain the assembler directives ASSUME, EQU, DW, and EVEN CO2
6 2
with suitable examples.
Explain data transfer, arithmetic and logical group instructions CO2
7 2
of 8086 microprocessor

11. Part A- Question & Answers

S.N Question& Answers B CO


o L
1 How will carry and zero flags reflect the result of the
instruction CMP BX, CX?
The CMP instruction compares the content of BX and CX
If BX = CX, Carry Flag is reset, Zero flag is set CO
2
2
If BX > CX, Carry Flag is reset, Zero flag is reset
If BX < CX, Carry Flag is set, Zero flag is reset
Set = 1, Reset = 0
2 Write any four miscellaneous instructions in 16 bit 2 CO
2
microprocessor.
Four miscellaneous instructions in 16 bit microprocessor are
1. INT
2. LOCK
3. ESC

|MPMC UNIT II
BTECH_ECE-SEM 32
4. WAIT
3 What is the purpose of LEA instruction in 8086?
Load Effective Address (LEA) instruction determines the offset of the
variable or memory location named as the source and loads this
address in the specified 16-bit register. Flags are not affected by
LEA instruction.
Eg: LEA Cx, TOTAL CO
2
2
Or
Load Effective Address (LEA) instruction determines and loads the
offset of the variable or memory location in the specified 16-bit
register. Flags are not affected by LEA instruction.
Eg: LEA Cx, TOTAL
4 List any four program control instructions available in 8086.

Four program control instruction available in 8086 are


1. JNZ – Jump if no zero, JB – Jump if below
2. JMP – Jump unconditionally
3. CALL – Call to specified subroutine , RET – Return from specified CO
2
2
subroutine
4. JA/JNBE – Jump if above / Jump if not below or equal
5. JE/JZ – Jump if equal / Jump if zero
6. JO – Jump if Overflow flag set
7. JS – Jump if sign flag set
5 What are the 8086 instructions used for BCD arithmetic?
The 8086 instructions used for BCD arithmetic are
CO
2
1. DAA – Decimal Adjust Accumulator 2
DAS – Decimal Adjust after Subtraction

12.Part B- Questions

|MPMC UNIT II
BTECH_ECE-SEM 32
S.No Question BL CO
1 Write an assembly language program for 8086 to find the
2 CO2
largest element in an array.
2 Write an assembly language program for 8086 to convert
2 CO2
BCD data to binary data
3 What is the function of the ASSUME, DB, EQU, EVEN
2 CO2
assembler directives of 8086?
4 Explain the assembler directives ASSUME, EQU, DW, and
2 CO2
EVEN with suitable examples.
5 Explain data transfer, arithmetic and logical group
2 CO2
instructions of 8086 microprocessor

13.Supportive Online Certification Courses


1. Microprocessors and Microcontrollers By SANTANU CHATTOPADHYAY,
conducted by IIT Kharagpur – 12 weeks.
2. Microprocessors By Dr. Pramod Agarwal, conducted by IIT Roorkee – 12
weeks.

14. Real Time Applications


S.No Application CO
1. Calculators. CO1,CO2,CO3,C
O4
2. Accounting system. CO1,CO2,CO3,C
O4
3. Games machine. CO1,CO2,CO3,C
O4
4. Complex Industrial Controllers. CO1,CO2,CO3,C
O4
5. Traffic light Control. CO1,CO2,CO3,C
O4
6. Data acquisition systems. CO1,CO2,CO3,C
O4
7. Multi user, multi-function environments. CO1,CO2,CO3,C
O4
8. Military applications. CO1,CO2,CO3,C
O4
9. Calculators. CO1,CO2,CO3,C
O4
10. Accounting system. CO1,CO2,CO3,C
O4
11. Games machine. CO1,CO2,CO3,C
O4
12. Complex Industrial Controllers. CO1,CO2,CO3,C
O4
13. Traffic light Control. CO1,CO2,CO3,C
O4
14. Data acquisition systems. CO1,CO2,CO3,C
O4
|MPMC UNIT II
BTECH_ECE-SEM 32
15. Contents Beyond the Syllabus
1. Basic Examples for small projects
Basic examples will help in designing the project easily.

16.Prescribed Text Books & Reference Books


Text Book
1. “Microprocessor and Microcontrollers”, N. Senthil Kumar, M. Saravanan, S.
Jeevanathan, Oxford Publishers. 1 st Edition, 2010

2. “The X86 Microprocessors , Architecture, Programming and Inerfacing” , Lyla


B. Das, Pearson Publications, 2010
3. MSP430 microcontroller basics. John H. Davies, Newnes Publication, I st
Edition, 2008

References:

1. http://processors.wiki.ti.com/index.php/
MSP430_LaunchPad_Low_Power_Mode
2. http://processors.wiki.ti.com/index.php/MSP430_16-Bit_Ultra-
Low_Power_MCU_Training

17. Mini Project Suggestion


 Build a Car Parking Indicator
 Design and Apply an Electronic Differential System
 Build a MATLAB Based Inspection System with Image Processing
 Perform Image Encryption and Verification with Chaotic Maps
 Measure an Object’s Diameter in an Image by using MATLAB
 Use MATLAB to Automate Certificate Generation
 Create Light Animations with MATLAB and Arduino
 Build a Circuit Design Calculator
 Compress Images without Loss
 Perform Real-Time Face Detection with MATLAB

|MPMC UNIT II
BTECH_ECE-SEM 32

You might also like