[go: up one dir, main page]

0% found this document useful (0 votes)
99 views60 pages

Mic 314321 Notes

The document is a course outline for Microprocessor Programming (314321) by Prof. Nagesh A. Goden, detailing the syllabus for Semester IV in Computer Engineering. It covers topics such as the 8086 microprocessor architecture, assembly language programming, instruction sets, and memory segmentation. The document includes a structured index and various questions and answers related to the 8086 microprocessor's features, registers, and operational principles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views60 pages

Mic 314321 Notes

The document is a course outline for Microprocessor Programming (314321) by Prof. Nagesh A. Goden, detailing the syllabus for Semester IV in Computer Engineering. It covers topics such as the 8086 microprocessor architecture, assembly language programming, instruction sets, and memory segmentation. The document includes a structured index and various questions and answers related to the 8086 microprocessor's features, registers, and operational principles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

MICROPRCESSOR

PROGRAMMING
(314321)
(Semester IV- Computer Engineering)

By,
Prof. Nagesh A. Goden
(Lecturer, Computer Engineering Department)
Microprocessor Programming (314321)

INDEX

Chapter Name of Chapter Marks Page No

1 8086 -16 bit Microprocessor 14 2-13

The Art of Assembly Language


2 08 14-18
Programming

Instruction Set of 8086


3 18 19-33
Microprocessor

4 Assembly Language Programming 20 34-47

5 Procedure and Macro 10 48-59

A.G.Patil Polytechnic Institute, Solapur Page 1


Microprocessor Programming (314321)
Chapter 1 :8086 -16 bit Microprocessor

[Marks-14]

2_Marks Questions & Answers

1. List any four salient features of 8086 microprocessor.

Ans:
1) It is a 16 bit μp.
2) 8086 has a 20 bit address bus can access up to 220 memory locations (1MB).
3) It can support up to 64K I/O ports.
4) It provides 16-bit registers. AX,BX,CX,DX,CS,SS,DS,ES,BP,SP,SI,DI,IP & FLAG
REGISTER.
5) It has multiplexed address and data bus AD0-AD15 and A16 – A19.
6) 8086 is designed to operate in two modes, Minimum and Maximum.
7) It can prefetches up to 6 instruction bytes from memory and queues them in order to speed up
instruction execution.
8) Interrupts:-8086 has 256 vectored interrupts.
9) Provides separate instructions for string manipulation.
10) Operating clock frequencies 5MHz, 8MHz, 10MHz.

2. State the function of ALE and Ready pin of 8086.

Ans: ALE :This active high ,output signal used to indicate availability of valid address on address/data
lines and is connected to latch enable input of latches (8282 or 74LS373) .

READY: This input is used to insert wait state into the timing cycle of the 8086. If the ready pin is at
logic 1, it has no effect on the operation of the microprocessor. If it is logic 0, the 8086 enters the waits
state and remains the idle. This pin is used to interface the operating peripherals with the 8086.

3. Draw format of flag register of 8086.

Ans: Flag Register format of 8086

A.G.Patil Polytechnic Institute, Solapur Page 2


Microprocessor Programming (314321)
4. State the function of BHE and A0 pins of 8086.

Ans:BHE: BHE stands for Bus High Enable. It is available at pin 34 and used toindicate the transfer of
data using data bus D8-D15. This signal is low duringthe first clock cycle, thereafter it is active.

A0: A0 is analogous to BHE for the lower byte of the data bus, pinsD0-D7. A0 bitis Low during T1 state
when a byte is to be transferred on the lower portion ofthe bus in memory or I/O operations.

5. State the function of READY , M/IO and INTR pin of 8086.

Ans: Ready:It is used as acknowledgement from slower I/O device or memory.It is Active high signal,
when high; it indicates that the peripheral device isready to transfer data.

M/IO: This signal is used to differentiate between I/O & memory operations. When it is high, it indicates
memory operation and when low, it indicates I/O operation.

INTR: This is a level triggered interrupt request input, checked during last clockcycle of each instruction
to determine the availability of request. If anyinterrupt request is occurred, the processor enters the
interrupt acknowledgecycle.

6. Name the general purpose register of 8086, give brief description of each.

Ans: 1. AX (Accumulator) – Used to store the result for arithmetic / logical operations All I/O data
transfer using IN & OUT instructions use “A” register(AH / AL or AX).
2. BX – Base – used to hold the offset address or data in indirect addressing mode.
3. CX – acts as a counter for repeating or looping instructions.
4. DX – Used with AX to hold 32 bit values during multiplication and division. Used to hold
address of I/O port in indirect addressing mode.

7. State the names of segment registers in 8086 microprocessor.

Ans: CS – Code Segment – holds base address for all executable instructions in a program.
SS -Stack segment- holds the Base address of the stack.
DS – Data Segment – default base address for variables.
ES – Extra Segment – additional base address for memory variables in extra segment.

A.G.Patil Polytechnic Institute, Solapur Page 3


Microprocessor Programming (314321)
4 Marks Questions & Answers

1. Draw functional block diagram of 8086 microprocessor.


Ans:

BIU (Bus Interface Unit) :BIU takes care of all data and addresses transfers on the buses for the EU like
sending addresses, fetching instructions from the memory, reading data from the ports and the memory as
well as writing data to the ports and the memory. EU has no direction connection with System Buses so
this is possible with the BIU. EU and BIU are connected with the Internal Bus.
It has the following functional parts −Instruction queue , Segment register , Instruction pointer
EU (Execution Unit) :Execution unit gives instructions to BIU stating from where to fetch the data and
then decode and execute those instructions. Its function is to control operations on data using the
instruction decoder & ALU. EU has no direct connection with system buses as shown in the above figure,
it performs operations over data through BIU.
It has the following functional parts – ALU, Flag Register, General Purpose Register

A.G.Patil Polytechnic Institute, Solapur Page 4


Microprocessor Programming (314321)
2. Draw flag register of 8086 and explain any four flags.
Ans: Flag Register of 8086

Conditional /Status Flags:

C-Carry Flag : It is set when carry/borrow is generated out of MSB of result. (i.e D7 bit for 8-bit
operation, D15 bit for a 16 bit operation).

P-Parity Flag This flag is set to 1 if the lower byte of the result contains even number of 1‟s otherwise it
is reset.

AC-Auxiliary Carry Flag This is set if a carry is generated out of the lower nibble, (i.e. From D3 to D4
bit)to the higher nibble

Z-Zero Flag This flag is set if the result is zero after performing ALU operations. Otherwise it is reset.

S-Sign Flag This flag is set if the MSB of the result is equal to 1 after performing ALU operation ,
otherwise it is reset.

O-Overflow Flag This flag is set if an overflow occurs, i.e. if the result of a signed operation is large
enough to be accommodated in destination register.

Control Flags:

T-Trap Flag If this flag is set ,the processor enters the single step execution mode.

I-Interrupt Flag it is used to mask(disable) or unmask(enable)the INTR interrupt. When this flag is
set,8086 recognizes interrupt INTR. When it is reset INTR is masked.

D-Direction Flag It selects either increment or decrement mode for DI &/or SI register during string
instructions.

A.G.Patil Polytechnic Institute, Solapur Page 5


Microprocessor Programming (314321)
3. What is pipelining? How it improves the processing speed. State the advantages ofpipelining .
Ans: Pipelining : Process of fetching the next instruction while the current instruction is executing is
called pipelining which will reduce the execution time. The technique used to enable an instruction to
complete with each clock cycle. Normally, on a non – pipelined processor, nine clock cycles are required
for fetch, decode and execute cycles for the three instructions as shown in Fig (a).

This takes longer time when compared to pipelined processor. In this ,the fetch, decode and execute
operations are performed in parallel, so only five clock cycles are required to execute the same three
instructions as shown Fig(b).

In 8086, pipelining is implemented by providing 6 byte queue where as long as 6 one byte instructions can
be stored well in advance and then one by one instruction goes for decoding and executions. So, while
executing first instruction in a queue, processor decodes second instruction and fetches 3rd instruction
from the memory In this way, 8086 perform fetch, decode and execute operation in parallel i.e. in single
clock cycle as shown in above fig (b)

Advantages of pipelining:

 The execution unit always reads the next instruction byte from the queue in BIU. This is faster
than sending out an address to the memory and waiting for the next instruction byte to come.
 More efficient use of processor.
 Quicker time of execution of large number of instruction.
 In short pipelining eliminates the waiting time of EU and speeds up the processing. -The 8086 BIU
will not initiate a fetch unless and until there are two empty bytes in its queue. 8086 BIU normally
obtains two instruction bytes per fetch.

A.G.Patil Polytechnic Institute, Solapur Page 6


Microprocessor Programming (314321)
4. Define logical and effective address. Describe physical addressgeneration process in 8086. If
DS=345AH and SI=13DCH. Calculatephysical address.
Ans:

A logical address is the address at which an item (memory cell, storageelement) appears to reside from
the perspective of an executing applicationprogram. A logical address may be different from the physical
address dueto the operation of an address translator or mapping function.

Effective Address or Offset Address: The offset for a memory operand iscalled the operand's effective
address or EA. It is an unassigned 16 bitnumber that expresses the operand's distance in bytes from the
beginning ofthe segment in which it resides. In 8086 we have base registers and indexregisters.

Generation of 20 bit physical address in 8086:-


1. Segment registers carry 16 bit data, which is also known as base address.
2. BIU appends four 0 bits to LSB of the base address. This address becomes 20-bit address.
3. Any base/pointer or index register carries 16 bit offset.
4. Offset address is added into 20-bit base address which finally forms 20 bit physical address of memory
location.

Fig:Mechanism used to calculate physical address in 8086

Calculate the physical address for the given DS=345AH and SI=13DCH
Physical Address = DS(0000) + SI
= 345AH (0000) + 13DCH
= 345A0+13DC
= 3597CH

Physical Address = 3597CH

A.G.Patil Polytechnic Institute, Solapur Page 7


Microprocessor Programming (314321)
5. Explain concept of segmentation with diagram. list its advantages.
Ans:
Memory Segmentation: The memory in an 8086 microprocessor is organized as a segmented memory.
The physical memory is divided into 4 segments namely,- Data segment, Code Segment, Stack Segment
and Extra Segment.
Description:
 Data segment is used to hold data, Code segment for the executable program, Extra segment also
holds data specifically in strings and stack segment is used to store stack data.
 Each segment is 64Kbytes & addressed by one segment register. i.e CS,DS,ES or SS
 The 16 bit segment register holds the starting address of the segment
 The offset address to this segment address is specified as a 16-bit displacement (offset) between
0000 to FFFFH. Hence maximum size of any segment is 216=64K locations.
 Since the memory size of 8086 is 1Mbytes, total 16 segments are possible with each having
64Kbytes.
 The offset address values are from 0000H to FFFFH so the physical address range from 00000H to
FFFFFH.

Advantages of Segmentation:

 The size of address bus of 8086 is 20 and is able to address 1 Mbytes( ) of physical memory.
 The compete 1 Mbytes memory can be divided into 16 segments,each of 64 Kbytes size.
 It allows memory addressing capability to be 1 MB.
 It gives separate space for Data, Code, Stack and Additional Datasegment as Extra segment size.
 The addresses of the segment may be assigned as 0000H to F000Hrespectively.
 The offset values are from 00000H to FFFFFH
 Segmentation is used to increase the execution speed of computersystem so that processor can able
to fetch and execute the data frommemory easily and fast.

A.G.Patil Polytechnic Institute, Solapur Page 8


Microprocessor Programming (314321)
6. Describe register organization of 8086.
Ans:Register Organization of 8086:
All the registers of 8086 are 16-bit registers. The general purpose registers can be used as either 8-bit
registers or 16-bit registers.
The register set of 8086 can be categorized into 4 different groups. The register organization of 8086
is shown in the figure.

General Data Registers: The registers AX, BX, CX and DX are the general purpose 16-bit registers.

AX is used as 16-bit accumulator. The lower 8-bit is designated as AL and higher 8-bitis designated as AH.
AL can be used as an 8-bit accumulator for 8-bit operation.All data register can be used as either 16 bit or
8 bit.

BX is a 16 bit register, but BLindicates the lower 8-bit of BX and BH indicates the higher 8-bit of BX.

The register CX is used default counter in case of string and loop instructions.The register BX is used as
offset storage for forming physical address in case ofcertain addressing modes.

DX register is a general purpose register which may be used as an implicit operand ordestination in case of
a few instructions.

Segment Registers:

The 8086 architecture uses the concept of segmented memory. 8086 able to address toaddress a memory
capacity of 1 megabyte and it is byte organized. This 1 megabytememory is divided into 16 logical
segments. Each segment contains 64 kbytes ofmemory.

A.G.Patil Polytechnic Institute, Solapur Page 9


Microprocessor Programming (314321)

There are four segment register in 8086


• Code segment register (CS)
• Data segment register (DS)
• Extra segment register (ES)
• Stack segment register (SS)

Pointers and Index Registers:

The pointers contain offset within the particular segments.


The pointer register IP contains offset within the code segment.
The pointer register BP contains offset within the data segment.
The pointer register SP contains offset within the stack segment.

The index registers are used as general purpose registers as well as for offset storagein case of indexed,
base indexed and relative base indexed addressing modes.
The register SI is used to store the offset of source data in data segment.
The register DI is used to store the offset of destination in data or extra segment.
The index registers are particularly useful for string manipulation.

Flag Register:

A 16 flag register is used in 8086. It is divided into two parts .


(a) Condition code or status flags
(b) Machine control flags

A.G.Patil Polytechnic Institute, Solapur Page 10


Microprocessor Programming (314321)
7. Differentiate between minimum mode and maximum mode of 8086 microprocessor.
Ans:

A.G.Patil Polytechnic Institute, Solapur Page 11


Microprocessor Programming (314321)

8. Draw & explain Signal Description of 8086 Microprocessor.

Ans: Intel 8086 is a 16-bit HMOS microprocessor. It is available in 40 pin DIP chip. It uses a 5V DC
supply for its operation. The 8086 uses 20-line address bus. It has a 16-line data bus. The 20 lines of the
address bus operate in multiplexed mode. The 16-low order address bus lines have been multiplexed with
data and 4 high-order address bus lines have been multiplexed with status signals.

AD0-AD15 : Address/Data bus. These are low


order address bus. They are multiplexed with data.
When AD lines are used to transmit memory
address the symbol A is used instead of AD, for
example A0-A15. When data are transmitted over
AD lines the symbol D is used in place of AD, for
example D0-D7, D8-D15 or D0-D15.

A16-A19 : High order address bus. These are


multiplexed with status signals.

S2, S1, S0 : Status pins. These pins are active


during T4, T1 and T2 states and is returned to
passive state (1,1,1 during T3 or Tw (when ready
is inactive). These are used by the 8288 bus
controller for generating all the memory and I/O
operation) access control signals. Any change in
S2, S1, S0 during T4 indicates the beginning of a
bus cycle.

BHE’/S7 : Bus High Enable/Status. During T1 it is low. It is used to enable data onto the most significant
half of data bus, D8-D15. 8-bit device connected to upper half of the data bus use BHE (Active Low)
signal. It is multiplexed with status signal S7. S7 signal is available during T2, T3 and T4.

RD: This is used for read operation. It is an output signal. It is active when low.

READY : This is the acknowledgement from the memory or slow device that they have completed the
data transfer. The signal made available by the devices is synchronized by the 8284A clock generator to
provide ready input to the microprocessor. The signal is active high(1).

INTR : Interrupt Request. This is triggered input. This is sampled during the last clock cycles of each
instruction for determining the availability of the request. If any interrupt request is found pending, the

A.G.Patil Polytechnic Institute, Solapur Page 12


Microprocessor Programming (314321)
processor enters the interrupt acknowledge cycle. This can be internally masked after resulting the
interrupt enable flag. This signal is active high(1) and has been synchronized internally.

NMI : Non maskable interrupt. This is an edge triggered input which results in a type II interrupt. A
subroutine is then vectored through an interrupt vector lookup table which is located in the system
memory. NMI is non-maskable internally by software. A transition made from low(0) to high(1) initiates
the interrupt at the end of the current instruction. This input has been synchronized internally.
INTA : Interrupt acknowledge. It is active low(0) during T2, T3 and Tw of each interrupt acknowledge
cycle.
MN/MX’ : Minimum/Maximum. This pin signal indicates what mode the processor will operate in.
RQ’/GT1′, RQ’/GT0′ : Request/Grant. These pins are used by local bus masters used to forc the
microprocessor to release the local bus at the end of the microprocessor‟s current bus cycle. Each of the
pin is bi-directional. RQ‟/GT0′ have higher priority than RQ‟/GT1′.

LOCK’ : Its an active low pin. It indicates that other system bus masters have not been allowed to gain
control of the system bus while LOCK‟ is active low(0). The LOCK signal will be active until the
completion of the next instruction.
TEST’ : This examined by a „WAIT‟ instruction. If the TEST pin goes low(0), execution will continue,
else the processor remains in an idle state. The input is internally synchronized during each of the clock
cycle on leading edge of the clock.
CLK : Clock Input. The clock input provides the basic timing for processing operation and bus control
activity. Its an asymmetric square wave with a 33% duty cycle.
RESET : This pin requires the microprocessor to terminate its present activity immediately. The signal
must be active high(1) for at least four clock cycles.
Vcc : Power Supply( +5V D.C.)
GND : Ground

QS1,QS0 : Queue Status. These signals indicate the status of the internal 8086 instruction queue
according to the table shown below
DT/R : Data Transmit/Receive. This pin is required in minimum systems, that want to use an 8286 or
8287 data bus transceiver. The direction of data flow is controlled through the transceiver.

DEN : Data enable. This pin is provided as an output enable for the 8286/8287 in a minimum system
which uses transceiver. DEN is active low(0) during each memory and input-output access and for INTA
cycles.

HOLD/HOLDA : HOLD indicates that another master has been requesting a local bus .This is an active
high(1). The microprocessor receiving the HOLD request will issue HLDA (high) as an acknowledgement
in the middle of a T4 or T1 clock cycle.
ALE : Address Latch Enable. ALE is provided by the microprocessor to latch the address into the 8282 or
8283 address latch. It is an active high(1) pulse during T1 of any bus cycle. ALE signal is never floated, is
always integer.

A.G.Patil Polytechnic Institute, Solapur Page 13


Microprocessor Programming (314321)
Chapter 2 :The Art of Assembly Language Programming

[Marks-08]

2_Marks Questions & Answers

1. List the major steps in developing an Assembly language program.

Ans: 1. Defining the problem


2. Writing Algorithm
3. Draw Flowchart
4. Initialization checklist
5.Choosing instructions
6. Converting algorithms to assembly language program

2. What is the role of Assembler and linker?


Ans: Assembler: Assembler is a program that translates assembly language program to the correct binary
code. It also generates the file called as object file with extension .obj. It also displays syntax errors
in the program, if any.It can be also be used to produce list(.lst) and .crf files.

Linker: It is a programming tool used to convert Object code into executable program called .EXE
module. It combines, if requested, more than one separated assembled modules into oneexecutable
module such as two or more assembly programs or an assembly languagewith C program.

3. List assembly language programming tools.


Ans: 1. Editors
2. Assembler
3. Linker
4. Debugger.

4. State the role Debugger in assembly language programming.


Ans: Debugger: Debugger is the program that allows the extension of program in single step mode under
the control of the user. The process of locating & correcting errors using a debugger is known as
Debugger.
Some examples of debugger are DOS debug command Borland turbo debugger TD, Microsoft
debugger known as code view cv, etc

A.G.Patil Polytechnic Institute, Solapur Page 14


Microprocessor Programming (314321)

5. Define flow chart and algorithm.


Ans: Flowchart: The flowchart is a graphically representation of the program operation or task.

Algorithm: The formula or sequence of operations to be performed by the program, specified as


steps in general English, is called algorithm.

6. Draw the symbols used in a flowchart while developing ALP, Mention the use of each
symbol.
Ans:

A.G.Patil Polytechnic Institute, Solapur Page 15


Microprocessor Programming (314321)

4 Marks Questions & Answers

1. State the Assembler Directives used in 8086 Assembly Language programming .


Ans:
DB - Define byte (8 bits):
It is used to declare a byte type variable of 8 bit. It also can be used to declare an array of bytes.

DW (Define Word) :
This is used to define a word (16-bit) type variable.
The range of values : 0 – 65535 for unsigned numbers -32768 to 32767 for signed numbers
This can be used to define a single word or multiple words.

DD - (Define Double Word or Data Double Word):


This is used to define a double word (32-bit) type variable.
This can be used to define a single double word or multiple double word

DQ : Define Quad Word :


This is used to define a quad word (64-bit) type variable. This directive is used to tell the
assembler to declare a variable 4 words in length or to reserve 4 words of storage.

DUP: Duplicate memory location:-


This directive can be used to generate multiple bytes or words with known as well as un-initialized
values.

ASSUME:-
Assume directive is used to tell Assembler the name of the logical segment it should use for the
specified segment.When program is loaded the processor segment register should point to the
respective logical segments.
Example: - Assume CS: MSBTE_CODE, DS: MSBTE_DATA

SEGMENT:
Used to indicate the beginning of logical segment . Preceding the SEGMENT directive is the name
you want to give the segment.
Syntax: Segment_Name SEGMENT [Word/Public]

A.G.Patil Polytechnic Institute, Solapur Page 16


Microprocessor Programming (314321)

EQU :Equate to:-


The EQU directive is used to declare the micro symbols to which some constant value is assigned.
Micro assembler will replace every occurrence of the symbol in a program by its value.
Syntax: Symbol name EQU expression
Example: CORRECTION_FACTOR EQU 100

2. Explain Editors, Assemblers, Linkers and Debuggers.


Ans: Editor: An editor is a program which allows you to create a file containing the assembly language
statements for your program. For example: Turbo Editor

Assembler:Assembler is a program that translates assembly language program to the correct


binary code. It also generates the file called as object file with extension .obj. It also displays
syntax errors in the program, if any.It can be also be used to produce list(.lst) and .crf files.

Linker: It is a programming tool used to convert Object code into executable program called .EXE
module. It combines, if requested, more than one separated assembled modules into oneexecutable
module such as two or more assembly programs or an assembly languagewith C program.

Debugger: Debugger is the program that allows the extension of program in single step mode
under the control of the user. The process of locating & correcting errors using a debugger is
known as Debugger.

A.G.Patil Polytechnic Institute, Solapur Page 17


Microprocessor Programming (314321)

3. Explain assembly language program development steps.


Ans: 1. Defining the problem: The first step in writing program is to think very carefully about the
problem that the program must solve.

2. Algorithm: The formula or sequence of operations to be performed by the program can be


specified as a step in general English is called algorithm.

3. Flowchart: The flowchart is a graphically representation of the program operation or task.

4. Initialization checklist: Initialization task is to make the checklist of entire variables, constants,
all the registers, flags and programmable ports

5. Choosing instructions: Choose those instructions that make program smaller in size and more
importantly efficient in execution.

6. Converting algorithms to assembly language program: Every step in the algorithm is


converted into program statement using correct and efficient instructions or group of instructions.

A.G.Patil Polytechnic Institute, Solapur Page 18


Microprocessor Programming (314321)

Chapter 3: Instruction Set of 8086 Microprocessor

[Marks-16]

2_Marks Questions & Answers

7. List theMachine Language Instruction Format of 8086.

Ans: Machine Language Instruction Format of 8086 as given below

8. List Addressing modes of 8086.


Ans: 1. Immediate addressing mode
2.Register addressing mode
3.Direct addressing mode
4.Register Indirect addressing mode
5.Indexed addressing mode
6.Based Indexed addressing mode
7.Register relative addressing mode
8.Relative Based Indexed addressing mode

A.G.Patil Polytechnic Institute, Solapur Page 19


Microprocessor Programming (314321)

9. Write Classification of 8086 Instruction Set.


Ans:

Classification of instruction set of 8086


Data Transfer Instructions
Arithmetic Instructions
Bit Manipulation Instructions
String Instructions
Program Execution Transfer Instructions (Branch & Loop Instructions)
Processor Control Instructions
Iteration Control Instructions
Interrupt Instructions

10. What is role of XCHG instruction in assembly language program? Give example

Ans: Role of XCHG: This instruction exchanges the contents of a register with the contents of another
register or memory location.
Example:
XCHG AX, BX ; Exchange the word in AX with word in BX.

11. Draw machine language instruction format for Register-to-Register transfer.


Ans:

Fig. Machine language instruction format for Register-to-Register transfer

12. State the use of STC and CMC instruction of 8086.


Ans: STC – This instruction is used to Set Carry Flag. CF1
CMC – This instruction is used to Complement Carry Flag.
CF~ CF

13. List any four instructions from the bit manipulation instructions of 8086.
Ans: Bit Manipulation Instructions: These instructions are used to perform operations where data bits
are involved i.e. operations like logical, shift, etc.
Following is the list of instructions under this group −

A.G.Patil Polytechnic Institute, Solapur Page 20


Microprocessor Programming (314321)
Instructions to perform logical operation
NOT − Used to invert each bit of a byte or word.
AND − Used for adding each bit in a byte/word with the corresponding bit in another byte/word.
OR − Used to multiply each bit in a byte/word with the corresponding bit in another byte/word.
XOR − Used to perform Exclusive-OR operation over each bit in a byte/word with the
corresponding bit in another byte/word.

14. Write assembly language instruction of 8086 microprocessor to

(i) Add 100H to the contents of AX register.


(ii) Rotate the contents of AX towards left by 2 bits.
Ans: (i) Add 100H to the contents of AX register.
ADD AX,0100H
(ii) Rotate the contents of AX towards left by 2 bits.
MOV CL,02H
ROL AX, CL

15. Explain with suitable example the instruction given below:


i) DAA
(ii) AAM
Ans: (i) DAA is Decimal Adjust after BCD Addition)
This instruction is used to make sure the result of adding two packed BCD numbers is adjusted to
be a correctBCD number. The result of the addition must be in AL for DAA instruction to
work correctly.If the lower nibble in AL after addition is > 9 or Auxiliary Carry Flag is set, then
add 6 to lowernibble of AL.
If the upper nibble in AL is > 9 or Carry Flag is set, and then add 6 to upper nibble of
AL.
Example: - (Any Same Type of Example)
if AL=99 BCD and BL=99 BCD
Then ADD AL, BL
1001 1001 = AL= 99 BCD
+ 1001 1001 = BL = 99 BCD
---------------
0011 0010 = AL =32 H and CF=1, AF=1
---------------
After the execution of DAA instruction, the result is
0011 0010 =AL =32H AF =1
+ 0110 0110
-------------------------
1001 1000 =AL =98 in BCD& CF = 1

A.G.Patil Polytechnic Institute, Solapur Page 21


Microprocessor Programming (314321)

ii) AAM Instruction: (BCD Adjust After Multiply).


After the two unpacked BCD digits are multiplied, the AAM instruction is used toadjust the
product to two unpacked BCD digits in AX.
Examples: ( Any other Equivalent Example should be given correct)
MUL CL ; AL = 0000 0100 = Unpacked BCD 4
; CL = 0000 0110 = Unpacked BCD 6
; AX = 0000 0000 0001 1000 = 0018H ; AL x CL Result in AX.
AAM ; AX = 0000 0010 0000 0100 = 02 04H
Which is unpacked BCD for 24.

4 Marks Questions & Answers

1. Write classification of instruction set of 8086. Explain any one type out of them.
Ans:
Classification of instruction set of 8086
Data Transfer Instructions
Arithmetic Instructions
Bit Manipulation Instructions
String Instructions
Program Execution Transfer Instructions (Branch & Loop Instructions)
Processor Control Instructions
Iteration Control Instructions
Interrupt Instructions

1) Arithmetic Instructions:
These instructions are used to perform arithmetic operations like addition, subtraction,
multiplication, division, etc.
ADD:
The add instruction adds the contents of the source operand to the destination operand.

Eg. ADD AX, 0100H


ADD AX, BX
ADD AX, [SI]
ADD AX, [5000H]
ADD [5000H], 0100H
ADD 0100H

ADC: Add with Carry


This instruction performs the same operation as ADD instruction, but adds the carry flag to the
result.
Eg. ADC 0100H
ADC AX, BX
ADC AX, [SI]
ADC AX, [5000]
A.G.Patil Polytechnic Institute, Solapur Page 22
Microprocessor Programming (314321)
ADC [5000], 0100H

SUB: Subtract
The subtract instruction subtracts the source operand from the destination operand and the result is
left in the destination operand.
Eg. SUB AX, 0100H
SUB AX, BX
SUB AX, [5000H]
SUB [5000H], 0100H

SBB: Subtract with Borrow


The subtract with borrow instruction subtracts the source operand and the borrow flag (CF) which
may reflect the result of the previous calculations, from the destination operand
Eg. SBB AX, 0100H
SBB AX, BX
SBB AX, [5000H]
SBB [5000H], 0100H

INC: Increment
This instruction increases the contents of the specified Register or memory location by 1.
Immediate data cannot be operand of this instruction.
Eg. INC AX
INC [BX]
INC [5000H]

DEC: Decrement
The decrement instruction subtracts 1 from the contents of the specified register or memory
location.
Eg. DEC AX
DEC [5000H]

NEG: Negate
The negate instruction forms 2‟s complement of the specified destination in the instruction. The
destination can be a register or a memory location. This instruction can be implemented by
inverting each bit and adding 1 to it.
Eg. NEG AL
AL = 0011 0101 35H Replace number in AL with its 2‟s complement
AL = 1100 1011 = CBH

CMP: Compare
This instruction compares the source operand, which may be a register or an immediate data or a
memory location, with a destination operand that may be a register or a memory location
Eg. CMP BX, 0100H
CMP AX, 0100H
CMP [5000H], 0100H
CMP BX, [SI]
CMP BX, CX

MUL: Unsigned Multiplication Byte or Word


This instruction multiplies an unsigned byte or word by the contents of AL.
Eg.
MUL BH ; (AX) (AL) x (BH)

A.G.Patil Polytechnic Institute, Solapur Page 23


Microprocessor Programming (314321)
MUL CX ; (DX)(AX) (AX) x (CX)
MUL WORD PTR [SI] ; (DX)(AX) (AX) x ([SI])

IMUL: Signed Multiplication


This instruction multiplies a signed byte in source operand by a signed byte in AL or a signed
word in source operand by a signed word in AX.
Eg. IMUL BH
IMUL CX
IMUL [SI]

CBW: Convert Signed Byte to Word


This instruction copies the sign of a byte in AL to all the bits in AH. AH is then said to be sign
extension of AL.
Eg. CBW
AX= 0000 0000 1001 1000 Convert signed byte in AL signed word in AX.
Result in AX = 1111 1111 1001 1000

CWD: Convert Signed Word to Double Word


This instruction copies the sign of a byte in AL to all the bits in AH. AH is then said to be sign
extension of AL.
Eg. CWD
Convert signed word in AX to signed double word in DX : AX
DX= 1111 1111 1111 1111
Result in AX = 1111 0000 1100 0001

DIV: Unsigned division


This instruction is used to divide an unsigned word by a byte or to divide an unsigned double word
by a word.
Eg.
DIV CL ; Word in AX / byte in CL
; Quotient in AL, remainder in AH
DIV CX ; Double word in DX and AX / word
; in CX, and Quotient in AX,
; remainder in DX

2) Processor Control Instructions

These instructions are used to control the processor action by setting/resetting the flag values.

STC:
It sets the carry flag to 1.
CLC:
It clears the carry flag to 0.
CMC:
It complements the carry flag.
STD:
It sets the direction flag to 1. If it is set, string bytes are accessed from higher memory address to
lower memory address.
CLD:
It clears the direction flag to 0. If it is reset, the string bytes are accessed from lower memory
address to higher memory address.

A.G.Patil Polytechnic Institute, Solapur Page 24


Microprocessor Programming (314321)

2. Describe any 6 addressing modes of 8086 with one example each.


Ans: 1. Immediate addressing mode:
An instruction in which 8-bit or 16-bit operand (data) is specified in the instruction, then the
addressing mode of such instruction is known as Immediate addressing mode.
Example:
MOV AX,67D3H
2. Register addressing mode
An instruction in which an operand (data) is specified in general purpose registers, then the
addressing mode is known as register addressing mode.
Example:
MOV AX,CX
3. Direct addressing mode
An instruction in which 16 bit effective address of an operand is specified in the instruction, then
the addressing mode of such instruction is known as direct addressing mode.
Example:
MOV CL,[2000H]
4. Register Indirect addressing mode
An instruction in which address of an operand is specified in pointer registeror in index register or
in BX, then the addressing mode is known as registerindirect addressing mode.
Example:
MOV AX, [BX]
5. Indexed addressing mode
An instruction in which the offset address of an operand is stored in indexregisters (SI or DI) then
the addressing mode of such instruction is known asindexed addressing mode.
DS is the default segment for SI and DI.
For string instructions DS and ES are the default segments for SI and DI resp.this is a special case
of register indirect addressing mode.
Example:
MOV AX,[SI]
6. Based Indexed addressing mode:
An instruction in which the address of an operand is obtained by adding the content of base
register (BX or BP) to the content of an index register (SI or DI) The default segment register may
be DS or ES
Example:
MOV AX, [BX][SI]

A.G.Patil Polytechnic Institute, Solapur Page 25


Microprocessor Programming (314321)
7. Register relative addressing mode:
An instruction in which the address of the operand is obtained by adding the displacement (8-bit or
16 bit) with the contents of base registers or index registers (BX, BP, SI, DI). The defaultsegment
register is DS or ES.
Example:
MOV AX, 50H[BX]
8. Relative Based Indexed addressing mode
An instruction in which the address of the operand is obtained by adding thedisplacement (8 bit or
16 bit) with the base registers (BX or BP) and indexregisters (SI or DI) to the default segment.
Example:
MOV AX, 50H [BX][SI]

3. Explain logical instructions of 8086.(Any Four)

Ans: Logical instructions.

1) AND- Logical AND


Syntax : AND destination, source
Operation
Destination ←destination AND source
Flags Affected :CF=0,OF=0,PF,SF,ZF
This instruction AND‟s each bit in a source byte or word with thesame number bit in a destination
byte or word. The result is put indestination.
Example: AND AX, BX
• AND AL,BL
• AL 1111 1100
• BL 0000 0011
---------------------
AL0000 0000 (AND AL,BL)
2) OR –Logical OR
Syntax :OR destination, source
Operation
Destination OR source
Flags Affected :CF=0,OF=0,PF,SF,ZF
This instruction OR‟s each bit in a source byte or word with the corresponding bit in a destination
byte or word. The result is put in a specified destination.
Example :
• OR AL,BL
A.G.Patil Polytechnic Institute, Solapur Page 26
Microprocessor Programming (314321)
• AL 1111 1100
• BL 0000 0011
---------------------
AL1111 1111

3)NOT – Logical Invert


Syntax : NOT destination
Operation: Destination NOT destination
Flags Affected :None
The NOT instruction inverts each bit of the byte or words at the
specified destination.
Example
NOT BL
BL = 0000 0011
NOT BL gives 1111 1100

4) XOR – Logical Exclusive OR


Syntax :XOR destination, source
Operation :Destination Destination XOR source
Flags Affected :CF=0,OF=0,PF,SF,ZF
This instruction exclusive, OR‟s each bit in a source byte or wordwith the same number bit in a
destination byte or word.
Example(optional)
XOR AL,BL
• AL 1111 1100
• BL 0000 0011
---------------------
AL1111 1111 (XOR AL,BL)

5)TEST
Syntax : TEST Destination, Source
This instruction AND‟s the contents of a source byte or word with thecontents of specified
destination byte or word and flags are updated, flags are updated as result ,but neither operands are
changed.
Operation performed:

A.G.Patil Polytechnic Institute, Solapur Page 27


Microprocessor Programming (314321)
Flags set for result of (destination AND source)
Example: (Any 1)
TEST AL, BL ; AND byte in BL with byte in AL, no result, Update PF,
SF, ZF.
e.gMOV AL, 00000101
TEST AL, 1 ; ZF = 0.
TEST AL, 10b ; ZF = 1

4. Describe any four string instructions of 8086 assembly language.

Ans: 1) REP:
REP is a prefix which is written before one of the string instructions. It will cause During length
counter CX to be decremented and the string instruction to be repeated until CX becomes 0.
Two more prefix.
REPE/REPZ: Repeat if Equal /Repeat if Zero.
It will cause string instructions to be repeated as long as the compared bytesor words Are equal
and CX≠0.
REPNE/REPNZ: Repeat if not equal/Repeat if not zero.
It repeats the strings instructions as long as compared bytes or words are notequalAnd CX≠0.
Example: REP MOVSB

2)MOVS/ MOVSB/ MOVSW - Move String byte or word.


Syntax:
MOVS destination, source
MOVSB destination, source
MOVSW destination, source
Operation: ES:[DI]<----- DS:[SI]
It copies a byte or word a location in data segment to a location in extrasegment. The offset of
source is pointed by SI and offset of destination ispointed by DI.CX register contain counter and
direction flag (DE) will be setor reset to auto increment or auto decrement pointers after one move.
Example
LEA SI, Source
LEA DI, destination
CLD
MOV CX, 04H

A.G.Patil Polytechnic Institute, Solapur Page 28


Microprocessor Programming (314321)
REP MOVSB

3] CMPS /CMPSB/CMPSW: Compare string byte or Words.


Syntax:
CMPS destination, source
CMPSB destination, source
CMPSW destination, source
Operation: Flags affected < ----- DS:[SI]- ES:[DI]
It compares a byte or word in one string with a byte or word in another string. SI Holds the offset
of source and DI holds offset of destination strings. CS contains counter and DF=0 or 1 to auto
increment or auto decrement pointer after comparing one byte/word.
Example
LEA SI, Source
LEA DI, destination
CLD
MOV CX, 100
REPE CMPSB

4] SCAS/SCASB/SCASW: Scan a string byte or word.


Syntax:
SCAS/SCASB/SCASW
Operation: Flags affected < ----- AL/AX-ES: [DI]
It compares a byte or word in AL/AX with a byte /word pointed by ES: DI. The string to be
scanned must be in the extra segment and pointed by DI. CX contains counter and DF may be 0 or
1.When the match is found in the string execution stops and ZF=1 otherwise ZF=0.
Example
LEA DI, destination
MOV Al, 0DH
MOV CX, 80H
CLD
REPNE SCASB

5] LODS/LODSB/LODSW:
Load String byte into AL or Load String word into AX.
Syntax:
LODS/LODSB/LODSW
A.G.Patil Polytechnic Institute, Solapur Page 29
Microprocessor Programming (314321)
Operation: AL/AX < ----- DS: [SI]
IT copies a byte or word from string pointed by SI in data segment into ALor AX.CXmay contain
the counter and DF may be either 0 or 1
Example
LEA SI, destination
CLD
LODSB

6] STOS/STOSB/STOSW (Store Byte or Word in AL/AX)


Syntax STOS/STOSB/STOSW
Operation: ES:[DI] < ----- AL/AX
It copies a byte or word from AL or AX to a memory location pointed by DI in extra segment CX
may contain the counter and DF may either set or reset

5. Select assembly language for each of the following


i) rotate register BL right 4 times
ii) multiply AL by 04H
iii) Signed division of AX by BL
iv) Move 2000h in BX register
v) increment the counter of AX by 1
vi) compare AX with BX
Ans:i) MOV CL, 04H
RCL AX, CL1
Or
MOV CL, 04H
ROL AX, CL
Or
MOV CL, 04H
RCR AX, CL1

Or
MOV CL, 04H
ROR AX, CL

A.G.Patil Polytechnic Institute, Solapur Page 30


Microprocessor Programming (314321)
ii) MOV BL,04h
MUL BL

iii) IDIV BL

iv) MOV BX,2000h

v) INC AX

vi) CMP AX,BX

6. Explain four rotate instructions with their syntax, operation and example.
Ans:
1.ROL – Rotate bits of byte or word left, MSB to LSB and to CF
Syntax: ROL destination, count
Eg:
ROL BL, 2 ; Rotate all bits in BL left by 1 bit ,copy MSB to LSB and to CF
IF BL = 11110000
After Execution 11000011, CF= 1
2. ROR – Rotate bits of byte or word right, LSB to MSB and to CF
Syntax: ROR destination, count
Eg:
ROR BL, 2 ; Rotate all bits in BL right by 1 bit ,copy LSB to MSB and to CF
IF BL = 11110000
After Execution 00111100, CF= 0
3.RCL – Rotate bits of byte or word left, MSB to CF and CF to LSB.
Syntax: RCL destination, count
Eg:
RCL BL, 2 ; Rotate all bits in BL left by 1 bit ,copy MSB to CF and CF to LSB
IF BL = 11110000, CF=0
After Execution 11000001 , CF= 1

4. RCR – Rotate bits of byte or word right, LSB to CF and CF to MSB.


Syntax: RCR destination, count
Eg:
RCR BL, 1 ; Rotate all bits in BL right by 1 bit ,copy LSB to CF and CF to MSB.
IF BL = 11110000, CF= 0 After Execution 00111100 , CF= 0

A.G.Patil Polytechnic Institute, Solapur Page 31


Microprocessor Programming (314321)

7. Identify addressing modes of the following instructions:


(i) ADD CX, DX
(ii) MOV BX,1378H
(iii) MOV CX, [BP][SI]
(iv) MOV [4321H], CL
Ans:

(i) ADD CX, DX : Register Addressing Mode


(ii) MOV BX,1378H : Immediate Addressing Mode
(iii) MOV CX, [BP][SI] : Based Indexed addressing mode
(iv) MOV [4321H], CL :Direct Addressing Mode

8. Explain the following instructions with suitable examples:


(i) ADC
(ii) XCHG
(iii) MUL
(iv) AND
Ans:
(i) ADC Destination, Source
1) This instruction is used to add the contents of source to the destination and carry flag.
2) The result is stored in the destination.
3) The source operand can be a immediate, a register or a memory location addressed by any of the
24 addressing modes.
4) The destination can be a register or a memory location, but not an immediate data.
5) Both operands cannot be immediate data or memory location.
6) The source and the destination must be of the same data type i.e., ADD instruction adds a byte
to byte or a word to word. It adds the two operands with CF. It effects AF, CF, OF, PF, SF, ZF
flags.
E.g.:
ADC AL, 74H
ADC DX, AX
ADC AX, [BX]

( ii ) XCHG Destination, Source

A.G.Patil Polytechnic Institute, Solapur Page 32


Microprocessor Programming (314321)
This instruction exchanges Source with Destination. .It cannot exchange two memory locations
directly. The source and destination can be any of the general purpose register or memory location,
but not two locations simultaneously.
No segment registers can be used.
E.g.:
XCHG DX, AX
XCHG BL, CH
XCHG AL,[9800]

(iii) MUL (Unsigned multiplication)


Syntax :-- MUL source
1. This instruction multiplies an unsigned byte from source with an unsigned byte inAL register
or Unsigned word from source with an unsigned word in AX register.
2. The source can be a register or memory location but cannot be an immediate data.
Operation Performed :--
a. If source is byte then AX  AL * unsigned 8 bit source
b. If source is word then DX, AX  AX * unsigned 16 bit source
Examples:--
1. MUL BL ; Multiply AL by BL & the result in AX
2. MUL CX ; Multiply AX by CX & the result in DX,AX
3. MUL Byte PTR [SI] ; AX  AL * [SI]

( iv )AND (Logical AND)


This instruction logically ANDs each bit of the source byte or word with the corresponding bit in
the destination and stores result in the destination.
Syntax: AND destination, source
Examples:
AND BH, CL ; AND byte in CL with Byte in BH, result in BH.
AND BX,00FFH ; AND word in BX with immediate data 00ffH
AND [5000H], DX; AND word in DX with a word in memory with offset 5000 in DS.

A.G.Patil Polytechnic Institute, Solapur Page 33


Microprocessor Programming (314321)

Chapter 4: Assembly Language Programming

[Marks-20]

2_Marks Questions & Answers

16. Draw flowchart for multiplication of two 16 bit numbers.

Ans:

17. Draw model of assembly language programming


Ans:
Data_Seg SEGMENT
:
Data declaration
:
Data_Seg ENDS
Code_Seg SEGMENT
ASSUME CS:Code_Seg, DS:Data_Seg
Start: MOV AX, Data_Seg
MOV DS, AX
:
Program code
:
Code_Seg ENDS
END Start

A.G.Patil Polytechnic Institute, Solapur Page 34


Microprocessor Programming (314321)

18. Write ALP for addition of two 8bit numbers. Assume suitable data.
Ans:
DATA SEGEMT
NUM 1 DB 10H
NUM2 DB 20H
RESULT DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:MOV AX, DATA
MOV DS,AX
MOV AL, NUM1
MOV BL, NUM2
ADD AL,BL
MOV RESULT, AL
INT 21H
CODE ENDS
END START

A.G.Patil Polytechnic Institute, Solapur Page 35


Microprocessor Programming (314321)

4 Marks Questions & Answers

[Note: Any other logic/ program may be considered]

9. Write an ALP to add two 16-bit numbers.


Ans:
DATA SEGMENT
NUMBER1 DW 6753H
NUMBER2 DW 5856H
SUM DW 0
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START: MOV AX, DATA
MOV DS, AX
MOV AX, NUMBER1
MOV BX, NUMBER2
ADD AX, BX
MOV SUM, AX
MOV AH, 4CH
INT 21H
CODE ENDS
END START

10. Write an ALP to find length of string.


Ans: Data Segment
STRG DB 'GOOD MORNING$'
LEN DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS : DATA
START: MOV DX, DATA
MOV DS,DX
LEA SI, STRG
MOV CL,00H
MOV AL,'$'
NEXT: CMP AL,[SI]
JZ EXIT
ADD CL,01H
INC SI
JMP
NEXT EXIT: MOV LEN,CL
MOV AH,4CH
INT 21H
CODE ENDS
END START

A.G.Patil Polytechnic Institute, Solapur Page 36


Microprocessor Programming (314321)

11. Write an ALP to count no.of 0’s in 16 bit number.


Ans: DATA SEGMENT
N DB 1237H
Z DB 0
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA, CS:CODE
START: MOV DX,DATA
MOV DS,DX
MOV AX, N
MOV CL,08
NEXT: ROL AX,01
JC ONE
INC Z
ONE: LOOP NEXT
INT 21H
CODE ENDS
END START

12. Write an ALP to find largest number in array of elements 10H, 24H, 02H, 05H, 17H.
Ans: DATA SEGMENT
ARRAY DB 10H,24H,02H,05H,17H
LARGEST DB 00H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV DX,DATA
MOV DS,DX
MOV CX,04H
MOV SI ,OFFSET
ARRAY MOV AL,[SI]
UP: INC SI
CMP AL,[SI]
JNC NEXT
MOV AL,[SI]
NEXT: DEC CX
JNZ UP
MOV LARGEST,AL
MOV AX,4C00H
INT 21H
CODE ENDS
END START

A.G.Patil Polytechnic Institute, Solapur Page 37


Microprocessor Programming (314321)

13. Write an ALP to reverse a string. Also draw flowchart for same.
Ans: Program:
DATA SEGMENT
STRB DB 'GOOD MORNING$'
REV DB 0FH DUP(?)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV DX,DATA
MOV DS,DX
LEA SI,STRB
MOV CL,0FH
LEA DI,REV
ADD DI,0FH
UP:MOV AL,[SI]
MOV [DI],AL
INC SI
DEC DI
LOOP UP
INT 21H
CODE ENDS
END START

Flowchart:

A.G.Patil Polytechnic Institute, Solapur Page 38


Microprocessor Programming (314321)

14. Write an assembly language program to find largest number from array of 10 numbers.
Ans:
DATA SEGMENT
ARRAY DB 15H,45H,08H,78H,56H,02H,04H,12H,23H,09H
LARGEST DB 00H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:MOV DX,DATA
MOV DS,DX
MOV CX,09H
MOV SI ,OFFSET ARRAY
MOV AL, [SI]
UP:INC SI
CMP AL,[SI]
JNC NEXT
MOV AL,[SI]
NEXT: DEC CX
JNZ UP
MOV LARGEST,AL ; AL=78h
MOV AX,4C00H
INT 21H
CODE ENDS
END START

15. Write an assembly language program to perform addition of two 16-bit numbers.
Ans:
DATA SEGMENT
N1 DW 2804H
N2 DW 4213H
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
MOV AX, N1
MOV BX, N2
ADD AL,BL
MOV CL,AL
MOV AL,AH
ADD AL,BH
MOV CH,AL
MOV AH,4CH
INT 21H
CODE ENDS
END START

A.G.Patil Polytechnic Institute, Solapur Page 39


Microprocessor Programming (314321)

16. Write an assembly language program to sort an array of 10 numbers in ascending order.
Ans:
DATA SEGMENT
ARRAY DB 15h,05h,08h,78h,56h, 60h, 54h, 35h, 24h, 67h
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS:DATA
START:MOV DX, DATA
MOV DS, DX
MOV BL,0AH
step1: MOV SI,OFFSET ARRAY
MOV CL,09H
step: MOV AL,[SI]
CMP AL,[SI+1]
JC Down
XCHG AL,[SI+1]
XCHG AL,[SI]
Down : ADD SI,1
LOOP step
DEC BL
JNZ step1
CODE ENDS
END START

17. Write an assembly language program to multiply two 16-bit unsigned numbers.
Ans:
DATA SEGMENT
N1 DW 2401H
N2 DW 1324H
C DD ?
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV AX,N1
MOV BX,N2
MUL BX
MOV WORD PTR C,AX
MOV WORD PTR C+2,DX
INT 21H
CODE ENDS
END START

A.G.Patil Polytechnic Institute, Solapur Page 40


Microprocessor Programming (314321)

18. Write an assembly language program to find length of a string.


Ans:
DATA SEGMENT
STRB DB 'GOOD MORNING$'
LEN DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:MOV DX,DATA
MOV DS,DX
LEA SI,STRB
MOV CL,00H
MOV AL,'$'
NEXT: CMP AL,[SI]
JZ EXIT
ADD CL,01H
INC SI
JMP NEXT
EXIT: MOV LEN,CL ;Len =0CH for Taken String.
MOV AH,4CH
INT 21H
CODE ENDS
END START

19. Write an assembly language program to multiply two 8-bit unsigned numbers.
Ans:
DATA SEGMENT
NUM1 DB 05H
NUM2 DB 02H
RESULT DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:MOV DX,DATA
MOV DS,DX
MOV AL,NUM1
MOV AH,NUM2
MUL NUM2
MOV RESULT,AX
MOV AX,4C00H
INT 21H
CODE ENDS
END START

A.G.Patil Polytechnic Institute, Solapur Page 41


Microprocessor Programming (314321)

20. Write an assembly language program to add two 8-bit BCD numbers.
Ans:
DATA SEGMENT
OP1 EQU 92H
OP2 EQU 52H
RESULT DB 02 DUP(00)
DATA ENDS
ASSUME CS: CODE , DS:DATA
CODE SEGMENT
START: MOV AX,DATA
MOV DS,AX
MOV BL,OP1
XOR AL,AL
MOV AL,OP2
ADD AL,BL
DAA
MOV RESULT ,AL
JNC MSBO
INC [RESULT+1]
MSBO: MOV AH,4CH
INT 21H
CODE ENDS
END START

21. Write an assembly language program to find reverse order of a given string. Also, write algorithm
and draw flowchart.
Ans:
Algorithm:
1. Initialize the Data segment and Code Segment registers.
2. SI=start of string to be reversed.
3. Assign CX=string length.
4. DI=reverse string address pointer.
5. Assign DI=string length.
6. Read the first character pointed by SI.
7. Store at the last character position pointed by DI.
8. Decrement DI to point to the next character, if DI !=0, go to step 6.
9. Stop.
Flowchart:

A.G.Patil Polytechnic Institute, Solapur Page 42


Microprocessor Programming (314321)

Program:

DATA SEGMENT
STRB DB 'GOOD MORNING$'
REV DB 0FH DUP(?)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:MOV DX,DATA
MOV DS,DX
LEA SI,STRB
MOV CL,0FH
LEA DI,REV
ADD DI,0FH
UP:MOV AL,[SI]
MOV [DI],AL
INC SI
DEC DI
LOOP UP
MOV AH,4CH
INT 21H
CODE ENDS
END START

22. Write ALP to divide two 16 bit numbers.


Ans:
DATA SEGMENT
A DW 4444H
B DW 0002H
C DW ?
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA, CS:CODE
START:MOV AX,DATA
MOV DS,AX
MOV AX,A
MOV BX,B
DIV BX
MOV C,AX
INT 21H
CODE ENDS
END START

A.G.Patil Polytechnic Institute, Solapur Page 43


Microprocessor Programming (314321)

23. Write ALP to Subtract two 16 bit numbers


Ans:
DATA SEGMENT
NUM1 DW 3210H
NUM2 DW 1200H
R DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START: MOV AX, DATA
MOV DS, AX
MOV AX, NUM1
MOV BX, NUM2
SUB AX, BX
MOV R, AX
MOV AH, 4CH
INT 21H
CODE ENDS
END START

24. Write ALP and draw flow chart to perform Block Transfer without using String Instruction
Ans:

A.G.Patil Polytechnic Institute, Solapur Page 44


Microprocessor Programming (314321)

25. Write an ALP to count the number of positive and negative numbers in array.
Ans:
;Count Positive No. And Negative No.S In Given ;Array Of 16 Bit No.
;Assume array of 6 no.s

DATA SEGMENT
ARRAY DW F423H,6523H,B658H,7612H, 2300H,1559H
COUNT DW 06H
POS_COUNT DB ?
NEG_COUNT DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV DX,0000H
MOV CX,COUNT
MOV SI, OFFSET ARRAY
NEXT: MOV AX,[SI]
ROR AX,01H
JC NEGATIVE
INC DL
JMP COUNT_IT
NEGATIVE: INC DH
COUNT_IT: INC SI
INC SI
LOOP NEXT
MOV NEG_COUNT,DL
MOV POS_COUNT,DH
INT 21H
CODE ENDS
END START

26. Write ALP to count ODD and EVEN numbers in an array.


Ans:
;Count ODD and EVEN No.S In Given ;Array Of 16 Bit No.
;Assume array of 10 no.s

DATA SEGMENT
ARRAY1 DW F423H, 6523H, B658H, 7612H, 9875H,
2300H, 1559H, 1000H, 4357H, 2981H
COUNT DW 0AH
ODD_COUNT DB ?
EVENCNT DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV DX,0000H

A.G.Patil Polytechnic Institute, Solapur Page 45


Microprocessor Programming (314321)
MOV CX,COUNT
MOV SI, OFFSET ARRAY1
NEXT: MOV AX,[SI]
ROR AX,01H
JC ODD_1
INC DL
JMP COUNT_IT
ODD_1 : INC DH
COUNT_IT: INC SI
INC SI
LOOP NEXT
MOV ODD_COUNT,DH
MOV EVENCNT,DL
MOV AH,4CH
INT 21H
CODE ENDS
END START

27. Write ALP to perform block transfer operation of 10 numbers.


Ans:
;Assume block of TEN 16 bit no.s
;Data Block Transfer Using String Instruction

DATA SEGMENT
BLOCK1 DW 1001H,4003H,6005H,2307H,4569H, 6123H,
1865H, 2345H,4000H,8888H
DATA ENDS
EXTRA SEGMENT
BLOCK2 DW ?
EXTRA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA,ES:EXTRA
START: MOV AX,DATA
MOV DS,AX
MOV AX,EXTRA
MOV ES,AX
MOV CX,000AH
LEA SI,BLOCK1
LEA DI,ES:BLOCK2
CLD
REPNZ MOVSW
MOV AX,4C00H
INT 21H
CODE ENDS
END START

A.G.Patil Polytechnic Institute, Solapur Page 46


Microprocessor Programming (314321)

28. Write an ALP to arrange numbers in array in descending order.


Ans:
DATA SEGMENT
ARRAY DB 15H,05H,08H,78H,56H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:MOV DX,DATA
MOV DS,DX
MOV BL,05H
STEP1: MOV SI,OFFSET ARRAY
MOV CL,04H
STEP: MOV AL,[SI]
CMP AL,[SI+1]
JNC DOWN
XCHG AL,[SI+1]
XCHG AL,[SI]
DOWN:ADD SI,1
LOOP STEP
DEC BL
JNZ STEP1
MOV AH,4CH
INT 21H
CODE ENDS
END START

A.G.Patil Polytechnic Institute, Solapur Page 47


Microprocessor Programming (314321)

Chapter 5: Procedure and Macro

[Marks-12]

2_Marks Questions & Answers

19. Define Macro.Give syntax.

Ans:
Macro: Small sequence of the codes of the same pattern are repeated frequently at different places
which perform the same operation on the different data of same data type, such repeated code can
be written separately called as Macro.

Syntax:
Macro_nameMACRO[arg1,arg2,…..argN)
…..
End

20. Define Procedure.Give syntax.


Ans:

Procedure: A procedure is group of instructions that usually performs one task. It is a reusable
section of a software program which is stored in memory once but can be used as often as
necessary.
A procedure can be of two types. 1)Near Procedure 2) Far Procedure

A.G.Patil Polytechnic Institute, Solapur Page 48


Microprocessor Programming (314321)

21. Write any two differences between NEAR and FAR procedure.
Ans:

A.G.Patil Polytechnic Institute, Solapur Page 49


Microprocessor Programming (314321)

4 Marks Questions & Answers

[Note: Any other logic/ program may be considered]

1. Give the difference between intersegment and intrasegment CALL


Ans:

2. Write an assembly language program to solve p= x2+y2 using Macro. (x and y are 8 bit
numbers.
Ans:
PROG MACRO a,b
MOV al,a
MUL al
MOV bl,al
MOV al,b
MUL al
ADD al,bl
ENDM
DATA SEGMENT
x DB 02H
y DB 03H
p DB DUP()
DATA ENDS
CODE SEGMENT
A.G.Patil Polytechnic Institute, Solapur Page 50
Microprocessor Programming (314321)
ASSUME CS:CODE,DS:DATA
START:MOV ax,data
MOV ds,ax
PROG x, y
MOV p,al
MOV ah,4Ch
Int 21H
CODE ENDS
END START

3. Write an ALP for addition of series of 8-bit number using procedure.


Ans:
DATA SEGMENT
NUM1 DB 10H,20H,30H,40H,50H
RESULT DB 0H
CARRY DB 0H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV DX,DATA
MOV DS, DX
MOV CL,05H
MOV SI, OFFSET NUM1
UP: CALL SUM
INC SI
LOOP UP
MOV AH,4CH
INT 21H

SUM PROC; Procedure to add two 8 bit numbers


MOV AL,[SI]
ADD RESULT, AL
JNC NEXT
INC CARRY
NEXT: RET
SUM ENDP
CODE ENDS
END START

A.G.Patil Polytechnic Institute, Solapur Page 51


Microprocessor Programming (314321)

4. Describe re-entrant and recursive procedure with schematic diagram.


Ans:

In some situation it may happen that Procedure 1is called from main program Procrdure2 is called
from procedure1And procrdure1 is again called from procdure2. In this situation program
execution flow reenters in the procedure1. These types of procedures are called re enterant
procedures. The RET instruction at the end of procrdure1 returns to procedure2.

The RETinstruction at the end of procedure2 will return the execution to procedure1.Procedure1
will again executed from where it had stopped at the time of calling procrdure2 and the RET
instruction at the end of this will return the program execution to main program. The flow of
program execution for re-entrant procedure is as shown in FIG

Reentrant Procedure:
A reentrant procedure is one in which a single copy of the program code can be shared by multiple
users during the same period of time. Re-entrance has two key aspects: The program code cannot
modify itself and the local data for each user must be stored separately.

Recursive procedures:
An active procedure that is invoked from within itself or from within another active procedure is a
recursive procedure. Such an invocation is called recursion. A procedure that is invoked
recursively must have the RECURSIVE attribute specified in the PROCEDURE statement.

A.G.Patil Polytechnic Institute, Solapur Page 52


Microprocessor Programming (314321)

5. Write ALP using procedure to solve equation such as Z= (A+B)*(C+D)


Ans:

; Procedure For Addition

SUM PROC NEAR


ADD AL,BL
RET
SUM ENDP

DATA SEGMENT
NUM1 DB 10H
NUM2 DB 20H
NUM3 DB 30H
NUM4 DB 40H
RESULT DB?
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE,DS:DATA
START:MOV AX,DATA
MOV DS,AX
MOV AL,NUM1
MOV BL,NUM2
CALL SUM
MOV CL,AL
MOV AL, NUM3
MOV BL,NUM4
CALL SUM
MUL CL
MOV RESULT,AX
MOV AH,4CH
INT 21H
CODE ENDS
END START

A.G.Patil Polytechnic Institute, Solapur Page 53


Microprocessor Programming (314321)

6. Write ALP using macro to perform multiplication of two 8 Bit Unsigned numbers.
Ans:
; Macro For Multiplication

PRODUCT MACRO FIRST,SECOND


MOV AL,FIRST
MOV BL,SECOND
MUL BL
PRODUCT ENDM
DATA SEGMENT
NO1 DB 05H
NO2 DB 04H
MULTIPLE DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE,DS:DATA
START:MOV AX,DATA
MOV DS,AX
PRODUCT NO1,NO2
MOV MULTIPLE, AX
MOV AH,4CH
INT 21H
CODE ENDS
END START

A.G.Patil Polytechnic Institute, Solapur Page 54


Microprocessor Programming (314321)

7. Explain MACRO with suitable example. List four advantages of it.


Ans:
Macro Small sequence of the codes of the same pattern are repeated frequently at different places
which perform the same operation on the different data of same data type,
such repeated code can be written separately called as Macro.
When assembler encounters a Macro name later in the source code, the block of code associated
with the Macro name is substituted or expanded at the point of call, known as macro expansion.
Macro called as open subroutine.

Syntax:
Macro_nameMACRO[arg1,arg2,…..argN)
….. Endm

Example:
MyMacro MACRO p1, p2, p3 ; macro definition with arguments
MOV AX, p1
MOV BX, p2
MOV CX, p3
ENDM ;indicates end of macro.
DATA SEGMENT
DATA ENDS
CODE SEGMENT ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
MYMACRO 1, 2, 3 ; macro call
MYMACRO 4, 5, DX
MOV AH,4CH
INT 21H
CODE ENDS
END START

Advantages of Macro:
1. Simplify and reduce the amount of repetitive coding.
2. Reduces errors caused by repetitive coding.
3. Makes program more readable.
4. Execution time is less as compare to procedure as no extra instructions are required.

(OR Any Same Type of Example can be considered)

A.G.Patil Polytechnic Institute, Solapur Page 55


Microprocessor Programming (314321)

8. Differentiate between procedure and Macro.(any 4 points)


Ans:
MACRO PROCEDURE

A.G.Patil Polytechnic Institute, Solapur Page 56


Microprocessor Programming (314321)

9. Write an assembly language program for sum of series of 05 numbers using procedure.
Ans:

DATA SEGMENT
NUM1 DB 10H,02H,30H,04H,05H
RESULT DB 1 DUP(0)
CARRY DB 0H
DATA ENDS
CODE SEGMENT
START:ASSUME CS:CODE,DS:DATA
MOV DX,DATA
MOV DS,DX
MOV CL,05H
CALL SERIES_ADD ;Procedure Call
MOV AX,4C00H
INT 21H

SERIES_ADD PROC
MOV SI, OFFSET NUM1
UP:MOV AL,[SI]
ADD RESULT,AL
JNC NEXT
INC CARRY
NEXT:INC SI
LOOP UP
RET
SERIES_ADD ENDP
CODE ENDS
END START

10. Explain with suitable example how parameters are passed on the stack. Also, list out different
parameter passing ways in procedure.
Ans:

PARAMETER PASSING ON THE STACK:

To pass a large number of parameters to the called procedure, the parameters can be placed on the
stack for the calling procedure. Here, it is useful to use the stack base pointer i.e BP register to make a
frame boundary for easy access to the parameters. The stack can also be used to pass parameters back
from the called procedure to the calling procedure. The procedure during its execution pops back the
appropriate parameters as and when required.
In the example given below, The variable NUM is used in the called procedure using BP register,
which points to the corresponding location in the stack.

A.G.Patil Polytechnic Institute, Solapur Page 57


Microprocessor Programming (314321)

Different Parameter Passing Ways in Procedures:


1. Passing Parameters through the Registers
2. Passing Parameters in an Argument List

11. Explain CALL and RET instruction


Ans:
The CALL instruction is used to transfer execution to a subprogram or procedure by storing return
address on stack There are two types of calls-NEAR (Inter-Segment) and FAR(Intra-segment call).
Near call refers to a procedure call which is in thesame code segment as the call instruction and far
call refers to a procedure call which is in different code segment from that of the call instruction.

Syntax: CALL procedure_name


(direct/indirect)

RET: (Return from procedure)


The instruction RET is used to transfer program control from the procedure back to the calling
program.i.e main program or procedural CALL. The RET instruction are of two types
NEAR RET(intersegment return)
FAR RET(intra-segment return)
Syntax:- RET

A.G.Patil Polytechnic Institute, Solapur Page 58


Microprocessor Programming (314321)

12. Differentiate between NEAR and FAR CALLS.


Ans:

A.G.Patil Polytechnic Institute, Solapur Page 59

You might also like