0% found this document useful (0 votes)
7 views125 pages

Mic Merged

The document provides an overview of the 8086 16-bit microprocessor, detailing its architecture, features, and operational modes. It discusses the evolution of microprocessors, pin descriptions, and the functioning of the microprocessor, including the fetch-decode-execute cycle. Additionally, it outlines the characteristics of the execution unit and the bus interface unit, along with the significance of various flags in the flag register.

Uploaded by

prathmeshphalle4
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)
7 views125 pages

Mic Merged

The document provides an overview of the 8086 16-bit microprocessor, detailing its architecture, features, and operational modes. It discusses the evolution of microprocessors, pin descriptions, and the functioning of the microprocessor, including the fetch-decode-execute cycle. Additionally, it outlines the characteristics of the execution unit and the bus interface unit, along with the significance of various flags in the flag register.

Uploaded by

prathmeshphalle4
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/ 125

UNIT-I 8086 16 bit Microprocessor

UNIT-I 8086 16-bit Microprocessor

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
UNIT-I 8086 16 bit Microprocessor

Syllabus: Total Marks:14

1.1 8086 Microprocessor: salient features, pin description

1.2 Architecture of 8086: functional block diagram, register organization.

1.3 Concept of pipelining 1.4 Memory segmentation, physical memory address generation

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
UNIT-I 8086 16 bit Microprocessor

Introduction to Microprocessor:
● A processor is the logic circuitry that responds to and processes the basic instruc ons that
drives a computer.
● The term processor has generally replaced the term central processing unit (CPU).
● The processor in a personal computer or embedded in small devices is o en called a
microprocessor.
● The processor (CPU, for Central Processing Unit) is the computer's brain. It allows the
processing of numeric data, meaning information entered in binary form, and the execution
of instructions stored in memory.

Evolution of Microprocessor:
 A microprocessor is used as the CPU in a microcomputer. There are now many different
microprocessors available.
 Microprocessor is a program-controlled device, which fetches the instructions from
memory, decodes and executes the instructions.
 Most Microprocessor are single- chip devices. Microprocessor is a backbone of
computer system. which is called CPU Microprocessor speed depends on the processing
speed depends on DATA BUS WIDTH.
 A common way of categorizing microprocessors is by the no. of bits that their ALU can
Work with at a time.
 The address bus is unidirectional because the address information is always given by the
Microprocessor to address a memory location of an input / output devices.
 The data bus is Bi-directional because the same bus is used for transfer of data between
Micro Processor and memory or input / output devices in both the direction. It has
limitations on the size of data.
 Microprocessor contain ROM chip because it contains instructions to execute data.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
UNIT-I 8086 16 bit Microprocessor

1. 4-bit Microprocessor: The first microprocessor (Intel 4004) was invented in 1971. It was
a 4-bit calculation device with a speed of 108 kHz. It has 3200 PMOS transistors. It is a 4-
bit device used in calculator.
2. 8-Bit microprocessor: In 1972, Intel came out with the 8008 which is 8-bit. In 1974, Intel
announced the 8080 followed by 8085 is a 8-bit processor Because 8085 processor has 8
bit ALU. The 8080 is referred to as a “Second generation Microprocessor”.
Limitations of 8 Bit microprocessor:
- Low speed of execution
- Low memory addressing capability
- Limited number of general-purpose registers
- Less power full instruction set 4.
3. 16-bit Microprocessor: Similarly, 8086 processor has 16-bit ALU. This had a larger
instruction set then 8080. used NMOS transistors, so it operated much faster than the
8008.

Examples for 4/ 8 / 16 / 32-bit Microprocessors:

- 4-Bit processor – 4004/4040


- 8-bit Processor - 8085 / Z80 / 6800
- 16-bit Processor - 8086 / 68000 / Z8000
- 32-bit Processor - 80386 / 80486

What are 1st / 2nd / 3rd / 4th generation processor?

 The processor made of PMOS technology is called 1st generation processor, and it is
made up of 4 bits
 The processor made of NMOS technology is called 2nd generation processor, and it is
made up of 8 bits
 The processor made of CMOS technology is called 3rd generation processor, and it is
made up of 16 bits

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
UNIT-I 8086 16 bit Microprocessor

 The processor made of HCMOS technology is called 4th generation processor, and it is
made up of 32 bits (HCMOS : High-density n- type Complementary Metal Oxide Silicon
field effect transistor)

Block diagram of microprocessor:

How does a Microprocessor Work?

 The microprocessor follows a sequence: Fetch, Decode, and then Execute. Initially, the
instructions are stored in the memory in a sequential order.
 The microprocessor fetches those instructions from the memory, then decodes it and
executes those instructions till STOP instruction is reached.
 Later, it sends the result in binary to the output port. Between these processes, the register
stores the temporarily data and ALU performs the computing functions.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
UNIT-I 8086 16 bit Microprocessor

8086 Microprocessor: Pin descriptions


- 8086 Microprocessor is an enhanced version of 8085 Microprocessor that was designed
by Intel in 1976.
- It is a 16-bit Microprocessor having 20 address lines and 16 data lines that provides up
to 1MB storage.
- It consists of powerful instruction set, which provides operations like multiplication and
division easily.
- It supports two modes of operation, i.e. Maximum mode and Minimum mode.
Maximum mode is suitable for system having multiple processors and Minimum mode is
suitable for system having a single processor.

8086 Pin Diagram:

- 8086 was the first 16-bit microprocessor available in 40-pin DIP (Dual Inline Package)
chip. Here is the pin diagram of 8086 microprocessor.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
UNIT-I 8086 16 bit Microprocessor

Power supply and frequency signals:

- It uses 5V DC supply at VCC pin 40, and uses ground at VSS pin 1 and 20 for its operation

Clock signal:

- Clock signal is provided through Pin-19. It provides timing to the processor for
operations. Its frequency is different for different versions, i.e. 5MHz, 8MHz and 10MHz.

Address/data bus:

- AD0-AD15. These are 16 address/data bus. AD0-AD7 carries low order byte data and
AD8AD15 carries higher order byte data. During the first clock cycle, it carries 16-bit
address and after that it carries 16-bit data.

Address/status bus:

- A16-A19/S3-S6. These are the 4 address/status buses. During the first clock cycle, it
carries 4- bit address and later it carries status signals.

S7/BHE

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

Read($\overline{RD}$):

- It is available at pin 32 and is used to read signal for Read operation.

Ready:

- It is available at pin 22. It is an acknowledgement signal from I/O devices that data is
transferred. It is an active high signal. When it is high, it indicates that the device is
ready to transfer data. When it is low, it indicates wait state

RESET:

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
UNIT-I 8086 16 bit Microprocessor

- It is available at pin 21 and is used to restart the execution. It causes the processor to
immediately terminate its present activity. This signal is active high for the first 4 clock
cycles to RESET the microprocessor.

INTR:

- It is available at pin 18. It is an interrupt request signal, which is sampled during the last
clock cycle of each instruction to determine if the processor considered this as an
interrupt or not.

NMI:

- It stands for non-maskable interrupt and is available at pin 17. It is an edge triggered
input, which causes an interrupt request to the microprocessor.

$\overline{TEST}$:

- This signal is like wait state and is available at pin 23. When this signal is high, then the
processor has to wait for IDLE state, else the execution continues.

MN/$\overline{MX}$:

- It stands for Minimum/Maximum and is available at pin 33. It indicates what mode the
processor is to operate in; when it is high, it works in the minimum mode and vice-
aversa.

INTA:

- It is an interrupt acknowledgement signal and id available at pin 24. When the


microprocessor receives this signal, it acknowledges the interrupt.

ALE:

- It stands for address enable latch and is available at pin 25. A positive pulse is generated
each time the processor begins any operation. This signal indicates the availability of a
valid address on the address/data lines.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
UNIT-I 8086 16 bit Microprocessor

DEN:

- It stands for Data Enable and is available at pin 26. It is used to enable Transreceiver
8286. The transreceiver is a device used to separate data from the address/data bus.

DT/R:

- It stands for Data Transmit/Receive signal and is available at pin 27. It decides the
direction of data flow through the transreceiver. When it is high, data is transmitted out
and vice-a-versa.

M/IO:

- This signal is used to distinguish between memory and I/O operations. When it is high, it
indicates I/O operation and when it is low indicates the memory operation. It is
available at pin 28.

WR:

- It stands for write signal and is available at pin 29. It is used to write the data into the
memory or the output device depending on the status of M/IO signal.

HLDA:

- It stands for Hold Acknowledgement signal and is available at pin 30. This signal
acknowledges the HOLD signal.

HOLD:

- This signal indicates to the processor that external devices are requesting to access the
address/data buses. It is available at pin 31.

QS1 and QS0:

- These are queue status signals and are available at pin 24 and 25. These signals provide
the status of instruction queue. Their conditions are shown in the following table –

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
UNIT-I 8086 16 bit Microprocessor

S0, S1, S2:

- These are the status signals that provide the status of operation, which is used by the
Bus Controller 8288 to generate memory & I/O control signals. These are available at pin
26, 27, and 28. Following is the table showing their status –

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
UNIT-I 8086 16 bit Microprocessor

LOCK:

- When this signal is active, it indicates to the other processors not to ask the CPU to
leave the system bus. It is activated using the LOCK prefix on any instruction and is
available at pin 29.

RQ/GT1 and RQ/GT0:

- These are the Request/Grant signals used by the other processors requesting the CPU to
release the system bus. When the signal is received by CPU, then it sends
acknowledgment. RQ/GT0 has a higher priority than RQ/GT1

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
UNIT-I 8086 16 bit Microprocessor

Silent features of 8086.


1) It is a 16-bit Microprocessor.
2) 8086 has a 20-bit address bus that can access upto 220 memory locations (1 MB).
3) It has two blocks: BIU and EU.
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 i.e. AD0- AD15 and A16 – A19.
6) It works in a multiprocessor environment. Control signals are generated by an external
BUS Controller.
7) 8086 is designed to operate in two modes, Minimum and Maximum.
8) It can prefetches up to 6 instruction bytes from memory and queues them in order to
speed up instruction execution.
9) Interrupts: - 8086 has 256 vectored interrupts.
10) Provides separate instructions for string manipulation.
11) Operating frequency range is 6-10MHz.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
UNIT-I 8086 16 bit Microprocessor

Architecture of 8086.

- 8086 Microprocessor is divided into two functional units, i.e. EU (Execution Unit) and BIU (Bus
Interface Unit).

EU (Execution Unit)

- Execution unit gives instructions to BIU starting 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.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
UNIT-I 8086 16 bit Microprocessor

ALU

- It handles all arithmetic and logical operations, like +, −, ×, /, OR, AND, NOT opera ons.

Flag Register

- It is a 16-bit register that behaves like a flip-flop, i.e. it changes its status according to the result
stored in the accumulator.
- It has 9 flags and they are divided into 2 groups − Condi onal Flags and Control Flags.

Conditional Flags:

- It represents the result of the last arithmetic or logical instruction executed. Following is the list
of conditional flags
1. Carry flag − This flag indicates an overflow condi on for arithme c opera ons
2. Auxiliary flag − When an opera on is performed at ALU, it results in a carry/barrow from lower
nibble (i.e. D0 – D3) to upper nibble (i.e. D4 – D7), then this flag is set, i.e. carry given by D3 bit
to D4 is AF flag. The processor uses this flag to perform binary to BCD conversion.
3. Parity flag − This flag is used to indicate the parity of the result, i.e. when the lower order 8-bits
of the result contains even number of 1’s, then the Parity Flag is set. For odd number of 1’s, the
Parity Flag is reset.
4. Zero flag − This flag is set to 1 when the result of arithme c or logical opera on is zero else it is
set to 0
5. Sign flag − This flag holds the sign of the result, i.e. when the result of the opera on is nega ve,
then the sign flag is set to 1 else set to 0
6. Overflow flag − This flag represents the result when the system capacity is exceeded.

Control Flags:

- Control flags controls the operations of the execution unit. Following is the list of control flag.
1. Trap flag − It is used for single step control and allows the user to execute one instruc on at a
time for debugging. If it is set, then the program can be run in a single step mode.
2. Interrupt flag − It is an interrupt enable/disable flag, i.e. used to allow/prohibit the interrup on
of a program. It is set to 1 for interrupt enabled condition and set to 0 for interrupt disabled
condition

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
UNIT-I 8086 16 bit Microprocessor

3. Direction flag − It is used in string opera on. As the name suggests when it is set then string
bytes are accessed from the higher memory address to the lower memory address and vice-a-
versa

General purpose register

- There are 8 general purpose registers, i.e., AH, AL, BH, BL, CH, CL, DH, and DL. These registers
can be used individually to store 8-bit data and can be used in pairs to store 16 bit data.
- The valid register pairs are AH and AL, BH and BL, CH and CL, and DH and DL. It is referred to the
AX, BX, CX, and DX respectively
- AX register − It is also known as accumulator register. It is used to store operands for arithme c
operations.
- BX register − It is used as a base register. It is used to store the star ng base address of the
memory area within the data segment.
- CX register − It is referred to as counter. It is used in loop instruc on to store the loop counter
- DX register − This register is used to hold I/O port address for I/O instruc on.

Pointers and Index Registers:

- SP and BP are pointer registers which holds the 16 bit offset address within the particular
segment.
- SI and DI are the index registers.
- During the execution of string related instructions SI is used to store the offset of source data in
data segment and DI is used to store the offset of destination data in data or extra segment.
- Instruction pointer − It is a 16-bit register used to hold the address of the next instruction to be
executed.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
UNIT-I 8086 16 bit Microprocessor

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.
- Instruction queue − BIU contains the instruction queue. BIU gets upto 6 bytes of next
instructions and stores them in the instruction queue. When EU executes instructions and is
ready for its next instruction, then it simply reads the instruction from this instruction queue
resulting in increased execution speed. Fetching the next instruction while the current
instruction executes is called pipelining.
- Segment register − BIU has 4 segment buses, i.e. CS, DS, SS& ES. It holds the addresses of
instructions and data in memory, which are used by the processor to access memory locations.
It also contains 1 pointer register IP, which holds the address of the next instruction to executed
by the EU.
- CS − It stands for Code Segment. It is used for addressing a memory loca on in the code
segment of the memory, where the executable program is stored.
- DS − It stands for Data Segment. It consists of data used by the program andis accessed in the
data segment by an offset address or the content of other register that holds the offset address.
- SS − It stands for Stack Segment. It handles memory to store data and addresses during
execution.
- ES − It stands for Extra Segment. ES is addi onal data segment, which is used by the string to
hold the extra destination data.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
UNIT-I 8086 16 bit Microprocessor

Memory segmentation
- Memory segmentation: Segmentation is the process in which the main memory of a computer
is logically divided into the different segments and each segment has its own base address.
- It is basically used to increase the speed of the execution of the computer system
- Processor is able to fetch and execute the data from the memory easily and fast.
- Memory in 8086 is segmented in different segments and this memory management techniques
are called as segmentation.
- The complete physical memory is divided into the number of logical segments. Size of each
segment is 64Kbyte.
- These segments are addressed by one of the segment register that are DS,CS,SS and ES.
- These segment registers holds the starting address of a particular segments.
- The CPU of 8086 is able to address the 1Mbyte of physical memory
- The complete 1 Mbyte memory can be divided into the 16 segments each of 64 Kbyte in size.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
UNIT-I 8086 16 bit Microprocessor

Advantages of the Segmentation

- It provides a powerful memory management mechanism.


- Data related or stack related operations can be performed in different segments.
- Code related operation can be done in separate code segments.
- It allows to processes to easily share data.
- It allows to extend the address ability of the processor, i.e. segmentation allows the use of 16 bit
registers to give an addressing capability of 1 Megabytes. Without segmentation, it would
require 20-bit registers.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
UNIT-I 8086 16 bit Microprocessor

Concepts of pipelining in 8086:


- Pipelining is simply pre-fetching instruction and lining up them in queue.
- The process of fetching the next instruction when the present instruction is being executed is
called as pipelining.
- Pipelining has become possible due to the use of 6 byte queue.
- BIU (Bus Interfacing Unit) fills in the queue until the entire queue is full. BIU restarts filling in the
queue when at least two locations of queue are vacant.
- The Technique used to enable an instruction to complete with each clock cycle is called as
pipelining.
- In Pipelined processor, the fetch, decode and execute operation are performed in parallel.
- So, pipelining improve the execution speed of microprocessor.

Advantages of Pipelining

- Pipelining enables many instructions to be executed at the same time.


- It allows execution to be done in fewer cycles.
- Speed up the execution speed of the processor.
- More efficient use of processor.
- 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.
- Pipelining removes 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.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 19
UNIT-I 8086 16 bit Microprocessor

Interrupts in 8086
- An interrupt is a condition that halts the microprocessor temporarily to work on a different task
and then return to its previous task.
- Interrupt is an event or signal that request to attention of CPU. This halt allows peripheral
devices to access the microprocessor.
- Whenever an interrupt occurs the processor completes the execution of the current instruction
and starts the execution of an Interrupt Service Routine (ISR) or Interrupt Handler.
- ISR is a program that tells the processor what to do when the interrupt occurs. After the
execution of ISR, control returns back to the main routine where it was interrupted.
- The different types of interrupts present in 8086 microprocessor are given by
1) Hardware Interrupts
- Hardware interrupts are those interrupts which are caused by any peripheral device by sending
a signal through a specified pin to the microprocessor.
- There are two hardware interrupts in 8086 microprocessor.
- NMI (Non Mask able Interrupt) –
 It is a single pin non mask able hardware interrupt which cannot be disabled. It is the
highest priority interrupt in 8086 microprocessor.
 After its execution, this interrupt generates a TYPE 2 interrupt. IP is loaded from word
location 00008 H and CS is loaded from the word location 0000A H
- INTR (Interrupt Request) –
 It provides a single interrupt request and is activated by I/O port.
 This interrupt can be masked or delayed.
 It is a level triggered interrupt.
 It can receive any interrupt type, so the value of IP and CS will change on the interrupt
type received.
2) Software Interrupts
- These are instructions that are inserted within the program to generate interrupts.
- There are 256 software interrupts in 8086 microprocessor.
- Some important software interrupts are:
 TYPE 0 corresponds to division by zero(0).
 TYPE 1 is used for single step execution for debugging of program

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 20
UNIT-I 8086 16 bit Microprocessor

 TYPE 2 represents NMI and is used in power failure conditions.


 TYPE 3 represents a break-point interrupt.
 TYPE 4 is the overflow interrupt.

Difference between minimum and maximum mode:

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 21
UNIT-I 8086 16 bit Microprocessor

Flag Registers:

- It is a 16-bit register that behaves like a flip-flop, i.e. it changes its status according to the result
stored in the accumulator. It has 9 flags and they are divided into 2 groups − Condi onal Flags
and Control Flags.

Conditional Flags

It represents the result of the last arithmetic or logical instruction executed. Following is the list of
conditional flags –

- Carry flag − This flag indicates an overflow condi on for arithme c opera ons.
- Auxiliary flag − When an opera on is performed at ALU, it results in a carry/barrow from lower
nibble (i.e. D0 – D3) to upper nibble (i.e. D4 – D7), then this flag is set, i.e. carry given by D3 bit
to D4 is AF flag. The processor uses this flag to perform binary to BCD conversion.
- Parity flag − This flag is used to indicate the parity of the result, i.e. when the lower order 8-bits
of the result contains even number of 1’s, then the Parity Flag is set. For odd number of 1’s, the
Parity Flag is reset.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 22
UNIT-I 8086 16 bit Microprocessor

- Zero flag − This flag is set to 1 when the result of arithmetic or logical operation is zero else it is
set to 0.
- Sign flag − This flag holds the sign of the result, i.e. when the result of the opera on is nega ve,
then the sign flag is set to 1 else set to 0.
- Overflow flag − This flag represents the result when the system capacity is exceeded.

Control Flags

Control flags controls the operations of the execution unit. Following is the list of control flag

- Trap flag − It is used for single step control and allows the user to execute one instruc on at a
time for debugging. If it is set, then the program can be run in a single step mode.
- Interrupt flag − It is an interrupt enable/disable flag, i.e. used to allow/prohibit the interrup on
of a program. It is set to 1 for interrupt enabled condition and set to 0 for interrupt disabled
condition.
- Direction flag − It is used in string opera on. As the name suggests when it is set then string
bytes are accessed from the higher memory address to the lower memory address and vice-a-
versa.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 23
UNIT-II The Art of Assembly Language Programming

UNIT-II The Art of Assembly Language


Programming

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
UNIT-II The Art of Assembly Language Programming

Syllabus: Total Marks-08

2.1 Program development steps: defining problem and constrains, writing algorithms, flowchart,

initialization checklist, choosing instructions, converting algorithms to assembly language programs.

2.2 Assembly Language Programming Tools: Editor, Assembler, Linker Debugger.

2.3 Assembler Directives

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
UNIT-II The Art of Assembly Language Programming

Program development steps:

1. Defining the problem:


 Define very carefully about the problem that you want the program to solve.
 You need not to write down program but you must know what you would like to do.

2. Algorithm:
 The formula or sequence of operations or task need to perform by your program can
be specified as a step in general English is called as algorithm.
 It is step by step method or statement written in general English language of solving
problem.

3. Flowchart:
 The flowchart is graphically representation of the program operation or task
 Flowchart is easy to understand.
 We write algorithm and flowchart before writing the actual program.

4. Initialization Checklist:
 In program there are many variables, constants and various part of the system such
as segment registers, flag, stack, programmable ports which must be initialize
properly

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
UNIT-II The Art of Assembly Language Programming

5. Choosing Instructions:
 Next Step is to choose appropriate instruction that performs your problem’s
operations and task.

 You must know instruction set of microprocessors.

6. Converting Algorithms to Assembly Language Program:


 Once you have selected the instructions for the operations to be performed then
arrange these instructions in sequence as per algorithm.
 So that desired output must be obtaining after execution.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
UNIT-II The Art of Assembly Language Programming

Assembly Language Program Development Tools


 Editor
 Assembler
 Linker
 Debugger
1. Editor:
- An Editor is a program which helps you to construct your assembly language program in
right format.
- You can type your program using editor.
- Assembly language file extension is .asm file
- The DOS based editor such as EDIT, WordStar and Norton Editor is used.

2. Assembler:
- An assembler is a correct program that translate assembly language program to the
binary code for each instruction.
- File is generated called as object file with extension .obj
- We use TASM assembler.

3. Linker:
- A Linker is a program which combines if requested more one assembled module into
one executable program.
- It is a programming tool used to convert Object code (.OBJ) into executable (.EXE)
program.
- File is called as executable file with extension .exe
- We use TLINK Linker

4. Debugger:
- Debugger is a program that allows the execution of program in single step mode under
the control of the user.
- The process of locating and correcting errors using a debugger is known as debugging.
- We use Turbo Debugger (TD).

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
UNIT-II The Art of Assembly Language Programming

ASSEMBLER DIRECTIVES:

- Assembler directives are the commands to the assembler that direct the assembly process.
- They indicate how an operand is treated by the assembler and how assembler handles the
program.
- They also direct the assembler how program and data should arrange in the memory.
- ALPs are composed of two types of statements.
 The instructions which are translated to machine codes by assembler.
 The directives that direct the assembler during assembly process, for which no machine
code is generated.

1) DB – Define byte (8 bits):

- It is used to declare a byte type variable of 8 bit. A BYTE is made up of 8 bits. It also can be used
to declare an array of bytes.

- The range of values that can be stored in a byte is 0 to 255 for unsigned numbers and –128 to
+127 for signed numbers.
Syntax: Name_of_Variable DB Initialization_Value

Examples: NUM DB 80h

2. DW – The DW directive is used to declare a WORD type variable.:

- A WORD occupies 16 bits or (2 BYTE).

- The range of values : 0 – 65535 for unsigned numbers -32768 to 32767 for signed numbers
Syntax: Name_Of_Variable DW Initialisation_Value

Examples: NUM DW ‘78’


Word DW 1234h

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
UNIT-II The Art of Assembly Language Programming

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

- This is used to define a double word (32-bit) type variable.

- The range of values: 0 to 232-1 bits for unsigned numbers. -232-1to +232-1 for signed numbers.

- This can be used to define a single double word or multiple double word.

Syntax: Name_of_Variable DD Initialization_Value(,s)

Examples: NUMBER DD 1,2,3,4,9 ; Allocate 20 memory locations.


NUM DD? ; Allocate 4 memory locations

4. DQ - (Define Quad Word):

- This is used to define a quad word (64-bit) type variable.

- The range of values: 0 to 264-1 bits for unsigned numbers. -264-1to +264-1 for signed numbers.

- This can be used to define a single quad word or multiple quad word.

Syntax: Name_of_Variable DQ Initialization_Value(,s)

Examples: NUMBER DQ 1,2,3,4,9 ; Allocate 40 memory locations.


NUM DQ? ; Allocate 8 memory locations

5. 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

Examples: SUM EQU 100


NUM EQU 50

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
UNIT-II The Art of Assembly Language Programming

6. ORG:

- The directive ORG assigns the location counter with value specified in the directive. It helps in
placing the machine code in the specified location while translating instructions into machine
codes by the assembler. $ is used to indicate current value of location counter

Syntax: ORG [$+] Numeric_value

Examples: ORG 3000H ; set location counter to 3000H

7. DUP: Duplicate memory location:

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

8. ASSUME:

- Assume directive is used to tell Assembler the name of the logical segment it should use for the
specified segment.

Syntax: ASSUME

Seg_Reg: Seg_Name,…………….,Seg_Reg: Seg_Name

where,
ASSUME is assembler directive
Seg_Reg is any o the segment register i.e CS,DS,SS,ES
Seg_Name is the user defined segment name

Examples: ASSUME CS: VJTECH_CODE, DS: VJTECH_DATA, SS:VJTECH_STACK

9. SEGMENT:

- It is used to indicate the start of a logical segment. It is the name given to the segment.

- The directive segment follows the name of Segment.

Syntax: Segment_Name SEGMENT [Word/Public]

Examples: CODE SEGMENT WORD the code segment is used to indicate to the
assembler the start of logical segment.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
UNIT-II The Art of Assembly Language Programming

10. END: End of the program

- The directive END is used to inform assembler the end of the program. END directive is placed
after the last statement of a program to tell the assembler that this is the end of the program
module. The assembler will ignore any statement after an END directive.

Syntax: END [Start_Address]

The optional start_address indicates the location in the code segment where execution is to begin.

11. PROC: (PROCEDURE)

- It is used to identify the start of a procedure. It follows a name we give the procedure.
- After the procedure the term NEAR and FAR is used to specify the procedure

Example: Addition PROC FAR

identifies the start of procedure named Addition and tells the assembler that the procedure is far.

12. INCLUDE-Include source code from file

- This directive used to tell the assembler to insert a block of source code from named file
into current source module.

Syntax: INCLUDE <file path specification with file name>


Example: INCLUDE C:\Tasm\abc.lib

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
UNIT-III Instruction Set of 8086 Microprocessor

UNIT-III Instruction Set of 8086


Microprocessor

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
UNIT-III Instruction Set of 8086 Microprocessor

Syllabus: Total Marks-20

3.1 Machine Language Instruction Format.

3.2 Addressing modes.

3.3 Instruction set, group of instructions: Arithmetic instructions, logical instructions, data transfer
instructions, bit manipulation instructions, string operation instructions, program control transfer or
branching instructions, process control instructions.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
UNIT-III Instruction Set of 8086 Microprocessor

Addressing Modes of 8086:


- Addressing mode is used to have the access of the data

- They give various way to access data or operand in memory, registers or I/O
- Addressing modes are a technique of describing how an instruction will operate data.
- Addressing modes store the operands (source or destination) in any internal register or memory
location. Addressing modes refer to the various approaches to addressing the operands.

Types Of Addressing Modes Of 8086:

There are mainly 8 addressing modes of an 8086 microprocessor.


1. Immediate Addressing Mode
2. Direct Addressing Mode
3. Register Addressing Mode
4. Register Indirect Addressing Mode
5. Based Addressing Mode
6. Indexed Addressing Mode
7. Based Indexed Addressing Mode
8. Based with displacement Addressing Mode

1. Immediate Addressing Mode

- The operands are specified within the instructions in this type of mode.
- The first operand is never an immediate value. Instead, it contains a destination field that specifies
the length of the data and can be either a register or a memory address.
- The second operand contains the actual data, a series of subsequent bytes.
- Some examples of Immediate Addressing Modes are given below

MOV AL, 28H

- It moves the 8-bit data 28H into AL.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
UNIT-III Instruction Set of 8086 Microprocessor

2. Direct Addressing Mode

- Direct Addressing Mode the effective address of memory location where operand is present is
written directly in the instruction.
- In the direct addressing mode, a 16-bit memory address (offset) is directly specified in the
instruction as a part of it.
- All the addressing techniques, direct addressing is the most straightforward.

MOV AX, [5000H]

- The square brackets around the 5000H imply the contents of the memory location. When it is
executed, this instruction will copy the contents of the memory location into the AX register.

3. Register Addressing Mode

- The data is referred to using the specific register in which it is stored. Both operands in this
particular addressing mode are registers.
- Register Addressing Mode the data is stored in a register and it is referred using the particular
register. Some examples of Register addressing modes are given below.

MOV AX, BX

- It implies that the content of the 16-bit BX register is copied into the 16-bit AX register.

MOV CL, DL

- Moves 8-bit contents of DL into CL.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
UNIT-III Instruction Set of 8086 Microprocessor

4. Register Indirect Addressing Mode

- In this Addressing Mode, the address of the memory location which contain data or operand is
determined in an indirect way, using the offset registers.
- The offset address of data is in either BX or SI or DI register (Base register, source index or
Destination index).
- The default segment is either DS or ES.
- Example: MOV AX, [BX];
- Suppose the register BX contains 4895H, then the contents, 4895H are moved to AX

MOV CX, {BX}

- Here data is present in a memory location in DS whose offset address is in BX

5. Based Addressing Mode

- In this addressing mode, the offset address of the operand is given by the sum of contents of the
BX/BP registers and 8-bit/16-bit displacement.

MOV DX, [BX+04]

- Given instruction indicates that there is a value stored in the BX register and this is added with 4 bits
displacement value and will store in the AX register.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
UNIT-III Instruction Set of 8086 Microprocessor

6. Indexed Addressing Mode

- The SI or DI register stores the index value for memory data, and the instruction will specify either a
signed 8-bit or an unsigned 16-bit displacement.
- This mode is a special case of Register Indirect Addressing Mode.
- Here offset of the operand is stored in one of the index registers.
- DS is the default segment for SI and DI in string instruction DS and ES default segment for register SI
and DI.

MOV CX, [SI + 16]

- Given instruction indicates that there is a value stored in the SI (Source Index) register and this is
added with 16 bits displacement value and will store in the CX register.

7. Base Indexed Addressing Mode

- The effective address of data is formed by adding content of base register any one of BX or BP to the
content of index register any one of SI or DI.
- In the based index addressing of 8086, a base register (BX or BP), an index register (SI or DI), and a
displacement are added to determine the effective address.

MOV AX, [BX + SI]

- Given instruction indicates that there is a value stored in the SI (Source Index) register and this is
added to the value of the BX register and will store in the AX register.

8. Based with displacement Addressing Mode

- In this addressing mode, the operand offset is calculated by adding the content of the base register.
The index records the content and an 8- or 16-bit offset.

MOV AX, [BX+SI+08],

- Given instruction indicates that there is a value stored in the SI (Source Index) register and this is
added to the value of the BX register along with the 8-bit displacement value and will store in the AX
register.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
UNIT-III Instruction Set of 8086 Microprocessor

Instruction Set of 8086:

1. Arithmetic Instructions
2. Logical Instructions
3. Data Transfer Instructions.
4. Bit Manipulation Instructions
5. String Operation Instructions
6. Branching Instructions/Program Control Transfer Instructions
7. Process Control Instructions

 Operand types:

- Register: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
- Segment Register: DS, ES, SS, and only as second operand: CS.
- Memory: [BX], [BX+SI+7], variable, etc
- Immediate: 5, -24, 3Fh, 10001101b, etc.

 Notes:

- When two operands are required for an instruction, they are separated by comma
- When there are two operands, both operands must have the same size
- Some instructions allow several operand combinations
- These marks are used to show the state of the flags:

 1 - instruction sets this flag to 1


 0 - instruction sets this flag to 0.
 r - flag value depends on result of the instruction.
 ? - flag value is undefined (maybe 1 or 0).

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
UNIT-III Instruction Set of 8086 Microprocessor

 Arithmetic Instructions:

- The 8086 provides many arithmetic operations: addition, subtraction, negation, increment,
decrement, multiplication and comparing two values.

ADD (Addition)/ADC (Addition with Carry):

- The add instruction adds the contents of the source operand to the destination operand.
- Syntax: ADD operand1, operand2
ADC operand1, operand2
- Operation:
Destination = Destination + Source //ADD
Destination = Destination + Source + CF //ADC
- Example:
ADD AX, BX // Add contents of AX and BX and result in AX
ADC AX, BX // Add contents of AX and BX plus carry status and result in AX

SUB (Subtract) / SBB (Subtract with Borrow):

- The subtract instruction subtracts the source operand from the destination operand and the
result stored in the destination operand.
- Syntax: SUB operand1, operand2
SBB operand1, operand2
- Operation:
Destination = Destination - Source //SUB
Destination = Destination - Source - CF //SBB
- Example:
SUB AX, BX // SUB contents of AX and BX and result in AX
SBB AX, BX // SUB contents of AX and BX plus carry status and result in AX

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
UNIT-III Instruction Set of 8086 Microprocessor

INC (Increment):

- The instruction adds 1 to the indicated destination


- This instruction increases the contents of the specified Register or memory location.
- Syntax: INC operand
- Operation:
Destination = Destination + 1
- Example:
INC AX // Increase value of AX by 1

DEC (Decrement):

- The instruction subtracts 1 to the indicated destination


- This instruction decreases the contents of the specified Register or memory location.
- Syntax: DEC operand
- Operation:
Destination = Destination - 1
- Example:
DEC AX // Decrease value of AX by 1

CMP (Comparison):

- This instruction is used to compare source and destination


- Syntax: CMP operand1, operand2
- Operation:
If destination > source then ZF=0
If destination < source then ZF=0
If destination == source then ZF=1
- Example:

MOV AL, 5
MOV BL, 5
CMP AL, BL; //ZF=1 means equal

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
UNIT-III Instruction Set of 8086 Microprocessor

DAA

- It is a decimal adjust after addition.


- This instruction is used after ADD/ADC
- The ADD/ADC instruction adds the two BCD numbers in hexadecimal format and DAA
instruction converts this hexadecimal result to BCD result.
- Syntax: DAA

DAS

- It is a decimal adjust after subtraction.


- This instruction is used after SUB/SBB
- The SUB/SBB instruction subtracts the two BCD numbers in hexadecimal format and DAS
instruction converts this hexadecimal result to BCD result.
- Syntax: DAS

NEG

- 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
- Syntax: NEG Operand1
- Example:

MOV AL, 5 // AL = 05h


NEG AL // AL = 0FBh (-5)
NEG AL // AL = 05h (5)

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
UNIT-III Instruction Set of 8086 Microprocessor

MUL

- This instruction is used to multiply an unsigned byte from source with an unsigned byte in AL
register.
- Example:

MOV AL, 200


MOV BL, 4
MUL BL //AX = 0320h (800)

IMUL

- This instruction is used to multiply a signed byte from source with an unsigned byte in AL
register.
- Example:

MOV AL, -2
MOV BL, -4
IMUL BL // AX = 8

DIV

- This instruction is used to divide an unsigned word by a byte or to divide an unsigned double
word (32 bits) by a word.
- When a word is divided by a byte, the word must be in the AX register.
- The divisor can be in a register or a memory location. After the division, AL will contain the 8-bit
quotient, and AH will contain the 8-bit remainder.
- When a double word is divided by a word, the most significant word of the double word must be
in DX, and the least significant word of the double word must be in AX.
- After the division, AX will contain the 16-bit quotient and DX will contain the 16-bit remainder.
- Example:

MOV AX, 203


MOV BL, 4
DIV BL // AL = 50 (32h), AH = 3

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
UNIT-III Instruction Set of 8086 Microprocessor

IDIV

- This instruction is used to divide a signed word by a signed byte, or to divide a signed
double word by a signed word.
- When dividing a signed word by a signed byte, the word must be in the AX register. The divisor
can be in an 8-bit register or a memory location. After the division, AL will contain the signed
quotient, and AH will contain the signed remainder
- Example:

MOV AX, -203


MOV BL, 4
IDIV BL // AL = -50 (0CEh), AH = -3 (0FDh)

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
UNIT-III Instruction Set of 8086 Microprocessor

 Logical Instructions:

- These instructions perform the logical operations like AND, OR, NOT and XOR

Logical AND:

- This instruction AND’s bit by bit the source operand with destination operand and result is
stored in destination specified in the instruction.
- Syntax: AND destination, source
- Operation:
Destination  Destination AND Source
These rules apply:
1 AND 1 = 1
1 AND 0 = 0
0 AND 1 = 0
0 AND 0 = 0
- Example:
AND AX, 0008H
If the content of AX is 3A0F
AX => 0011 1010 0000 1111
0008 => 0000 0000 0000 1000
===================================
AX => 0000 0000 0000 1000

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
UNIT-III Instruction Set of 8086 Microprocessor

Logical OR:

- This instruction OR’s bit by bit the source operand with destination operand and result is stored
in destination specified in the instruction.
- Syntax: OR destination, source
- Operation:
Destination  Destination OR Source
These rules apply:
1 OR 1 = 1
1 OR 0 = 1
0 OR 1 = 1
0 OR 0 = 0
- Example:
OR AX, 0008H
If the content of AX is 3A0F
AX => 0011 1010 0000 1111
0008 => 0000 0000 0000 1000
===================================
AX => 0011 1010 0000 1111
Logical XOR:

- This instruction XOR’s bit by bit the source operand with destination operand and result is
stored in destination specified in the instruction.
- Syntax: XOR destination, source
- Operation:
Destination  Destination XOR Source
These rules apply:
1 XOR 1 = 0
1 XOR 0 = 1
0 XOR 1 = 1
0 XOR 0 = 0
- Example:

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
UNIT-III Instruction Set of 8086 Microprocessor

XOR AX, 0008H


If the content of AX is 3A0F
AX => 0011 1010 0000 1111
0008 => 0000 0000 0000 1000
===================================
AX => 0011 1010 0000 0111

Logical NOT:

- This instruction inverts each of the byte or word at the specified destination i.e.1’s complement.
- Syntax: NOT destination
- Operation:
Destination  NOT Destination
These rules apply:
if bit is 1 turn it to 0.
if bit is 0 turn it to 1.
- Example:
MOV AL, 00011011b
NOT AL // AL = 11100100b
Logical TEST:

- Logical AND between all bits of two operands for flags only. These flags are affected: ZF, SF, PF.
Result is not stored anywhere.
- Syntax: TEST destination, source
- Operation:
Flags set for result of (AND destination, source)
These rules apply:
1 AND 1 = 1
1 AND 0 = 0
0 AND 1 = 0
0 AND 0 = 0
- Example:

TEST AL, BH // AND BH with AL. No result stored; Update PF, SF, ZF.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
UNIT-III Instruction Set of 8086 Microprocessor

 Shift and Rotate Instructions:

SHL/SAL:

- SHL and SAL are two mnemonics for the same instruction. This instruction shifts each bit in the
specified destination some number of bit positions to the left.
- 0 is put in the LSB position. The MSB will be shifted into CF. In the case of multi-bit shift, CF will
contain the bit most recently shifted out from the MSB.
- Bits shifted into CF previously will be lost.

- Syntax: SHL destination, count


- Example:
MOV AL, 11100000b
SHL AL, 1 // AL = 11000000b, CF=1.
SHR:

- This instruction shifts each bit in the specified destination some number of bit positions to the
right.
- 0 is put in the MSB position. The bit shifted out of the LSB position goes to CF. In the case of
multi-bit shifts, CF will contain the bit most recently shifted out from the LSB.
- Bits shifted into CF previously will be lost.

- Syntax: SHR destination, count


- Example:
MOV AL, 00000111b
SHR AL, 1 //AL = 00000011b, CF=1.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
UNIT-III Instruction Set of 8086 Microprocessor

SAR:

- This instruction shifts each bit in the specified destination some number of bit positions to the
right.
- A copy of the old MSB is put in the MSB position.
- The sign bit is copied into the MSB.
- The LSB will be shifted into CF. In the case of multiple-bit shift, CF will contain the bit most
recently shifted out from the LSB. Bits shifted into CF previously will be lost.
- Bits shifted into CF previously will be lost.

- Syntax: SAR destination, count


- Example:
MOV BL, 4Ch // BL = 01001100b
SAR BL, 1 // BL = 00100110b, CF=0.

MOV AL, 0E0h // AL = 11100000b


SAR AL, 1 //AL = 11110000b, CF=0.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
UNIT-III Instruction Set of 8086 Microprocessor

ROL (Rotate left without carry):

- This instruction stands for Rotate Left without carry flag.


- The contents of the operand are rotated left bit-wise by some number of positions depending
on the count value.
- During this rotation, the most significant bit (MSB) is moved into the carry flag (CF) as well as
into the least significant bit (LSB) position.

- Syntax: ROL Destination, Count


- Example:

MOV AL, 1Ch // AL = 00011100b


ROL AL, 1 // AL = 00111000b, CF=0.

ROR (Rotate right without carry):

- This instruction stands for Rotate Right without carry flag.


- The contents of the operand are rotated right bit-wise by some number of positions depending
on the count value.
- During this rotation, instruction rotates the bits right, the least significant bit (LSB) is moved into
the carry flag (CF) as well as into the most significant bit (MSB) position.

- Syntax: ROR Destination, Count


- Example:
MOV AL, 1Ch // AL = 00011100b
ROR AL, 1 // AL = 00001110b, CF=0.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
UNIT-III Instruction Set of 8086 Microprocessor

RCL (Rotate left with carry):

- This instruction stands Rotate Left Through Carry.


- The contents of the operand will be rotated bit-wise left with the carry flag involved in the
rotation.
- This operation is circular because the MSB of the operand is rotated into the carry flag and the
bit in the carry flag is rotated around into LSB of the operand.

- Syntax: RCL Destination, Count


- Example:
STC // set carry (CF=1).
MOV AL, 1Ch // AL = 00011100b
RCL AL, 1 // AL = 00111001b, CF=0.

RCR (Rotate right with carry):

- This instruction stands Rotate Right Through Carry.


- The contents of the operand will be rotated bit-wise right with the carry flag involved in the
rotation.
- This operation is circular because the LSB of the operand is rotated into the carry flag and the bit
in the carry flag is rotate around into MSB of the operand.

- Syntax: RCR Destination, Count


- Example:
STC // set carry (CF=1).
MOV AL, 1Ch //AL = 00011100b
RCR AL, 1 // AL = 10001110b, CF=0.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 19
UNIT-III Instruction Set of 8086 Microprocessor

 Data Transfer Instructions:

- These instructions are used to transfer data from source to destination.


- The operand can be a constant, memory location, register or I/O port address.

MOV:

- This instruction copies a word or a byte of data from source to a destination.


- The destination can be a register or a memory location.
- The source can be a register, a memory location, or an immediate number.
- Syntax: MOV destination, source
- Example:
MOV CX, 037A H
MOV AL, BL
MOV BX, [0301 H]
PUSH:

- It pushes the operand into top of stack.


- Syntax: PUSH operand
- Example:
MOV AX, 1234h
PUSH AX
POP:

- It pops the operand from top of stack.


- Syntax: POP operand
- Example:
MOV AX, 1234h
PUSH AX
POP DX // DX = 1234h

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 20
UNIT-III Instruction Set of 8086 Microprocessor

XCHG:

- This instruction exchanges the contents of source with destination.


- The source and destination must both be of the same type
- Syntax: XCHG destination, source
- Example:
MOV AL, 5
MOV AH, 2
XCHG AL, AH //AL = 2, AH = 5
XCHG AL, AH // AL = 5, AH = 2
IN:

- The IN instruction copies data from a port to the AL or AX register.


- If an 8-bit port is read, the data will go to AL. If a 16-bit port is read, the data will go to AX.
- Second operand is a port number.
- Syntax: IN Accumulator, Port Address
- Example:
IN AL, OC8H //Input a byte from port OC8H to AL
IN AX, 34H //Input a word from port 34H to AX
OUT:

- The OUT instruction copies data from AL or AX register to.


- First operand is a port number.
- Syntax: OUT Port Address, Accumulator
- Example:
OUT 3BH, AL //Copy the content of AL to port 3BH
OUT 2CH, AX //Copy the content of AX to port 2CH

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 21
UNIT-III Instruction Set of 8086 Microprocessor

 Branching Instructions/Program Control Transfer Instructions:

- Branch Instructions transfers the flow of execution of the program to a new Address specified in
the instruction directly or indirectly.
- The Machine control instructions control the bus usage and execution

CALL:

- This instruction is used to call a subroutine or function or procedure.


- The address of next instruction after CALL is saved onto stack
- Syntax: CALL destination
- Example:
CALL p1
ADD AX, 1
p1 PROC //procedure declaration.
MOV AX, 1234h
RET //return to caller.
p1 ENDP
RET:

- It returns the control from procedure to calling program.


- Every CALL instruction should have a RET.
- Syntax: RET
- Example:
CALL p1
ADD AX, 1
p1 PROC //procedure declaration.
MOV AX, 1234h
RET //return to caller.
p1 ENDP

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 22
UNIT-III Instruction Set of 8086 Microprocessor

JMP:

- This instruction is used for unconditional jump from one place to another.

- Every CALL instruction should have a RET.


- Syntax: JMP LabelName
- Example:
MOV AL, 5
JMP label1
MOV BL, 5
label1:
MOV CL, 5
Jxx Des (Conditional Jump): All the conditional jumps follow some conditional statements or any
instruction that affects the flag.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 23
UNIT-III Instruction Set of 8086 Microprocessor

LOOP:

- This instruction is used to repeat a series of instructions some number of times.


- The number of times the instruction sequence is to be repeated is loaded into CX.
- Each time the LOOP instruction executes, CX is automatically decremented by 1.
- If CX is not 0, execution will jump to a destination specified by a label in the instruction.
- If CX = 0, execution will simply go on to the next instruction after LOOP.
- Syntax: LOOP LabelName

 Machine Control Instructions:

HLT (Halt):

- The instruction HLT causes the processor to enter the halt state.
- The CPU stops fetching and executing the instruction
- Syntax: HLT
NOP (No Operation):

- This instruction is used to add wait state of three clock cycles and during these three
clock cycles CPU does not perform any operation.
- Syntax: NOP
WAIT:

- When this instruction is executed, the 8086 enters an idle condition in which it is doing no
processing.
- The 8086 will stay in this idle state until the 8086-test input pin is made low or until an interrupt
signal is received on the INTR or the NMI interrupt input pins.
- Syntax: WAIT
LOCK:
- This instruction prevents other processors to take the control of shared resources.
- Syntax: LOCK

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 24
UNIT-III Instruction Set of 8086 Microprocessor

 Process Control Instructions:

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.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 25
UNIT-III Instruction Set of 8086 Microprocessor

 String Operation Instructions:

- The following are the various string manipulation instructions in the 8086 microprocessors.

REP:

- REP Stands for 'Repeat'.


- This instruction repeats the given instructions till CX does not becomes zero, i.e. CX ! =0
- Repeat following MOVSB, MOVSW, LODSB, LODSW, STOSB, STOSW instructions CX times.
- REP is a prefix, which is written before one of the string instructions. It will cause the CX register
to be decremented and the string instruction to be repeated until CX = 0.
- The instruction REP MOVSB, for example, will continue to copy string bytes until the number of
bytes loaded into CX has been copied.
REPE and REPZ:

- REPE and REPZ are two mnemonics for the same purpose.
- REPE stand for repeat if equal.
- REPZ stand for repeat if zero.
- They are used with the Compare String instruction or Scan String instruction.
- They will cause the string instruction to be repeated as long as the compared bytes or words are
equal (ZF = 1) and CX is not yet counted down to zero.
- Repeat following CMPSB, CMPSW, SCASB, SCASW instructions while ZF = 1 (result is Equal),
maximum CX times.
REPNE and REPNZ:

- REPNE and REPNZ are two mnemonics for the same purpose.
- REPNE stand for repeat if not equal.
- REPNZ stand for repeat if not zero.
- They are used with the Compare String instruction or Scan String instruction.
- They will cause the string instruction to be repeated as long as the compared bytes or words are
not equal (ZF = 1) and CX is not yet counted down to zero.
- Repeat following CMPSB, CMPSW, SCASB, SCASW instructions while ZF = 0 (result is Not Equal),
maximum CX times.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 26
UNIT-III Instruction Set of 8086 Microprocessor

MOVS – Move String


MOVSB - Move String Byte
MOVSW – Move String Word:
- These instructions are used to copy source string to destination string.
- The source must be placed in data segment and destination must be in extra segment.
- This instruction copies a byte or word from a location in the data segment to a location in the extra
segment.
- The offset of the source byte or word in the data segment must be in the SI register.
- The offset of the destination in the extra segment must be contained in the DI register.
- For multiple byte or multiple word moves the number of elements to be moved is put in the CX register so
that it can function as a counter.
- After the byte or word is moved SI and DI are automatically adjusted to point to the next source and the
next destination.
- If the direction flag is 0, then SI and DI will be incremented by 1 after a byte move and they will be
incremented by 2 after a word move.
- If the DF is a 1, then SI and DI will be decremented by 1 after a byte move and they will be decremented
by 2 after a word move.
- Example:

MOV SI, OFFSET SOURCE //Load offset of start of source string in DS into SI
MOV DI, OFFSET DESTINATION //Load offset of start of destination string in ES into DI
CLD //Clear DF to auto increment SI and DI after move
MOV CX, 04H //Load length of string into CX as counter
REP MOVSB //Move string byte until CX = 0

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 27
UNIT-III Instruction Set of 8086 Microprocessor

CMPS – Compare String


CMPSB - Compare String Byte
CMPSW – Compare String Word:
- The CMPS instruction can be used to compare a byte in one string with a byte in another string
or to compare a word in one string with a word in another string.
- SI is used to hold the offset of a byte or word in the source string and DI is used to hold the
offset of a byte or a word in the other string.
- The comparison is done by subtracting the byte or word pointed to by DI from the byte or word
pointed to by SI.
- The instruction CMPS compares a byte or word in the source string with a byte or word in the
destination string
- The AF, CF, OF, PF, SF, and ZF flags are affected by the comparison
- Example:
MOV SI, OFFSET FIRST //Point SI to source string
MOV DI, OFFSET SECOND //Point DI to destination string
CLD //DF cleared, SI and DI will auto-increment after compare
MOV CX, 100 //Put number of string elements in CX
REPE CMPSB //Repeat the comparison of string bytes until end of string or
until compared bytes are not equal

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 28
UNIT-III Instruction Set of 8086 Microprocessor

SCAS – Scan String


SCASB - Scan String Byte
SCASW – Scan String Word:
- The instruction SCAS scans a string byte or word with byte in AL and Word in AX
- The destination string is pointed by DI in ES.
- The string to be ‘scanned must be in the extra segment and DI must contain the offset of the
byte or the word to be compared.
- After the comparison DI will be automatically incremented or decremented according to
direction flag, to point to the next element in the two strings
- Example:
MOV DI, OFFSET STRING
MOV AL, 0DH //Byte to be scanned for into AL
MOV CX, 80 //CX used as element counter
CLD //Clear DF, so that DI auto increments
REPNE SCAS STRING //Compare byte in string with byte in AL

LODS – Load String


LODSB - Load String Byte
LODSW – Load String Word:
- This instruction copies a byte from a string location pointed to by SI to AL, or a word from a
string location pointed to by SI to AX.
- LODS does not affect any flags. LODSB copies byte and LODSW copies a word.
- Example:

CLD //Clear direction flag so that SI is auto-incremented

MOV SI, OFFSET SOURCE //Point SI to start of string

LODS SOURCE //Copy a byte or a word from string to AL or AX

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 29
UNIT-III Instruction Set of 8086 Microprocessor

STOS – Store String


STOSB - Store String Byte
STOSW – Store String Word:
- The STOS instruction copies a byte from AL or a word from AX to a memory location in the extra
segment.
- DI is used to hold the offset of the memory location in the extra segment.
- After the copy, DI is automatically incremented or decremented to point to the next string
element in memory.
- If the direction flag, DF, is cleared, then DI will automatically be incremented by one for a byte
string or incremented by two for a word String Instruction in 8086.
- If the direction flag is set, DI will be automatically decremented by one for a byte string or
decremented by two for a word string.
- Example:
MOV DI, OFFSET TARGET
STOS TARGET

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 30
UNIT-IV Assembly Language Programming

UNIT-IV Assembly Language Programming

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
UNIT-IV Assembly Language Programming

Syllabus: Total Marks-20

3.1 Machine Language Instruction Format.

3.2 Addressing modes.

3.3 Instruction set, group of instructions: Arithmetic instructions, logical instructions, data transfer
instructions, bit manipulation instructions, string operation instructions, program control transfer or
branching instructions, process control instructions.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
UNIT-IV Assembly Language Programming

Model of Assembly Language Programming:

###Model-1###
1) Using segment, assume, ends directives

mydata SEGMENT
-
-
Program data declaration (Data Segment of the program)
-
-
mydata ENDS

mycode SEGMENT
ASSUME CS:mycode, DS:mydata
MOV AX,mydata ;Data segment initialization
MOV DS,AX
-
-
Program Codes (Code Segment of the program)
-
-
mycode ENDS
END

###Model-2###
1) Using .DATA and. CODE directives

.MODEL SMALL
.STACK 100
.DATA
-
-
Program data declaration (Data Segment of the program)
-
-
.CODE
MOV AX,@DATA ;Data segment initialization
MOV DS,AX
-
-
Program Codes (Code Segment of the program)
-
-
ENDS
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
UNIT-IV Assembly Language Programming

Use of Assembler:

How to execute Assembly Program:

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
UNIT-IV Assembly Language Programming

Programming using Assembler:


 Write an Assembly language program to display “Hello World” message.

.MODEL SMALL
.DATA
MSG DB 'Hello World$'
.CODE
MOV AX,@DATA
MOV DS,AX
LEA DX,MSG
MOV AH,9H
INT 21h
MOV AH,4CH
INT 21h
END

 Write an Assembly language program to perform addition of two 8-bit numbers.

.MODEL SMALL
.DATA
num1 DB 80H
num2 DB 08H
sum DB ?
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AL,num1
MOV BL,num2
ADD AL,BL
MOV sum,AL
MOV AH,4CH
INT 21h
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
UNIT-IV Assembly Language Programming

 Write an Assembly language program to perform addition of two 16-bit numbers.

.MODEL SMALL
.DATA
num1 DW 8A64H
num2 DW 5F98H
sum DW ?
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AX,num1
MOV BX,num2
ADD AX,BX
MOV sum,AX
MOV AH,4CH
INT 21h
END

 Write an Assembly language program to perform addition of two 16-bit numbers where
result may be more than 16 bits.

.MODEL SMALL
.DATA
num1 DW FFFFH
num2 DW FFFFH
res_lsb DW 0000H
res_msb DW 0000H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AX,num1
MOV BX,num2
ADD AX,BX
JNC EXIT ;if result < 16 bit jump to exit
INC res_msb
EXIT:
MOV res_lsb,AX
MOV AH,4CH
INT 21h
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
UNIT-IV Assembly Language Programming

 Write an Assembly language program to perform subtraction of two 8-bit numbers.

.MODEL SMALL
.DATA
num1 DB 80H
num2 DB 08H
res DB ?
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AL,num1
MOV BL,num2
SUB AL,BL
MOV res,AL
MOV AH,4CH
INT 21h
END

 Write an Assembly language program to perform subtraction of two 16-bit numbers.

.MODEL SMALL
.DATA
num1 DW 8A64H
num2 DW 5F98H
res DW ?
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AX,num1
MOV BX,num2
SUB AX,BX
MOV res,AX
MOV AH,4CH
INT 21h
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
UNIT-IV Assembly Language Programming

 Write an ALP to find sum of 8-bit array elements.

.MODEL SMALL
.DATA
array DB 03H,07H,06H,04H,01H
res_lsb DB ?
res_msb DB ?
.CODE
MOV AX,@DATA
MOV DS,AX
MOV CX,05H
MOV SI, OFFSET array
UP:MOV AL,[SI]
ADD res_lsb,AL
JNC NEXT
INC res_msb
NEXT:
INC SI
LOOP UP;

MOV AH,4CH
INT 21h
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
UNIT-IV Assembly Language Programming

 Write an ALP to find sum of 16-bit array elements.

.MODEL SMALL
.DATA
array DW 0003H,0070H,0060H,004H,001H
res_lsb DW ?
res_msb DW ?
.CODE
MOV AX,@DATA
MOV DS,AX
MOV CX,05H
MOV SI, OFFSET array
UP:MOV AX,[SI]
ADD res_lsb,AX
JNC NEXT
INC res_msb
NEXT:
ADD SI,2
LOOP UP;

MOV AH,4CH
INT 21h
END

 Write an ALP for multiplication of two 8-bit numbers

.MODEL SMALL
.DATA
num1 DB 10H
num2 DB 07H
res DW 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AL,num1
MOV BL,num2
MUL BL
MOV res,AX
MOV AH,4CH
INT 21h
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
UNIT-IV Assembly Language Programming

 Write an ALP for multiplication of two 16-bit numbers

.MODEL SMALL
.DATA
num1 DW 1234H
num2 DW 0132H
res_lsb DW 00H
res_msb DW 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AX,num1
MOV BX,num2
MUL BX
MOV res_lsb,AX
MOV res_msb,DX
MOV AH,4CH
INT 21h
END

 Write an ALP to divide 16 bit by 8 bit numbers

.MODEL SMALL
.DATA
num1 DW 1254H
num2 DB 12H
Quotient DB 00H
Remainder DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AX,num1
MOV BL,num2
DIV BL
MOV Quotient,AL
MOV Remainder,AH
MOV AH,4CH
INT 21h
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
UNIT-IV Assembly Language Programming

 Write an ALP to divide 8 bit by 8 bit numbers

.MODEL SMALL
.DATA
num1 DB 54H
num2 DB 12H
Quotient DB 00H
Remainder DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AL,num1
MOV BL,num2
DIV BL
MOV Quotient,AL
MOV Remainder,AH
MOV AH,4CH
INT 21h
END

 Write an ALP to divide 16 bit by 16 bit numbers

.MODEL SMALL
.DATA
num1 DW 1254H
num2 DW 0012H
Quotient DW 0000H
Remainder DW 0000H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AX,num1
MOV BX,num2
DIV BX
MOV Quotient,AX
MOV Remainder,DX
MOV AH,4CH
INT 21h
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
UNIT-IV Assembly Language Programming

Arithmetic Operations on BCD Numbers

 Write an ALP for addition of two 8 bit BCD numbers. Consider Carry

.MODEL SMALL
.DATA
num1 DB 84H
num2 DB 28H
res_lsb DB 00H
res_msb DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AL,num1
MOV BL,num2
ADD AL,BL
DAA ; Adjust result to BCD
JNC EXIT
INC res_msb
EXIT:
MOV res_lsb,AL
MOV AH,4CH
INT 21h
END

The program is similar to addition of 8 bit hex numbers. Only DAA instruction needs to be used after
ADD to adjust result to BCD.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
UNIT-IV Assembly Language Programming

 Write ALP for addition of two 16 bit BCD numbers. Consider Carry

.MODEL SMALL
.DATA
NUM1 DW 9999H
NUM2 DW 9999H
RES_LSB DW 0000H
RES_MSB DW 0000H
.CODE
MOV AX, @DATA
MOV DS, AX
MOV AX, NUM1
MOV BX, NUM2
ADD AL, BL ; Lower byte addition and result in AL
DAA ; Adjust result to BCD
MOV CL, AL
MOV AL, AH
ADC AL, BH ;Higher byte addition and result in AL because
DAA works on AL only
DAA ; Adjust result to BCD
MOV CH, AL
JNC EXIT
INC RES_MSB
EXIT:
MOV RES_LSB, CX
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
UNIT-IV Assembly Language Programming

 Write an ALP for subtraction of two 8 bit BCD numbers.

.MODEL SMALL
.DATA
num1 DB 85H
num2 DB 57H
res_bcd DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AL,num1
MOV BL,num2
SUB AL,BL
DAS ; Adjust result to BCD
MOV res_bcd,AL
MOV AH,4CH
INT 21h
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
UNIT-IV Assembly Language Programming

 Write ALP for subtraction of two 16 bit BCD numbers.

.MODEL SMALL
.DATA
NUM1 DW 9000H
NUM2 DW 0999H
RES_BCD DW 0000H
.CODE
MOV AX, @DATA
MOV DS, AX
MOV AX, NUM1
MOV BX, NUM2
SUB AL, BL ; Lower byte subtraction and result in AL
DAS ; Adjust result to BCD
MOV CL, AL
MOV AL, AH
SBB AL, BH ;Higher byte subtraction and result in AL
DAS ; Adjust result to BCD
MOV CH, AL
MOV RES_BCD, CX
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
UNIT-IV Assembly Language Programming

FIND SMALLEST AND LARGEST NUMBER IN ARRAY PROGRAMS

 Write ALP to find smallest number (8 bit) from array

.MODEL SMALL
.DATA
ARRAY DB 12H,16H,11H,08H,23H
SMALLEST DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV CX,04H
MOV SI,OFFSET ARRAY
MOV AL,[SI] ;AL=08H SI=23H CX=00H
UP: INC SI
CMP AL,[SI] ;IF 1ST NO < 2ND NO THEN GO TO NEXT
JC NEXT
MOV AL,[SI]
NEXT:
LOOP UP
MOV SMALLEST,AL
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
UNIT-IV Assembly Language Programming

 Write ALP to find smallest number(16 bit) from array

.MODEL SMALL
.DATA
ARRAY DW 0012H,0016H,0011H,0008H,0023H
SMALLEST DW 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV CX,04H
MOV SI,OFFSET ARRAY
MOV AX,[SI] ;AL=0008H SI=0023H CX=0000H
UP: INC SI
INC SI
CMP AX,[SI] ;IF 1ST NO < 2ND NO THEN GO TO
NEXT
JC NEXT
MOV AX,[SI]
NEXT:
LOOP UP
MOV SMALLEST,AX
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
UNIT-IV Assembly Language Programming

 Write ALP to find largest number (8 bit) from array

.MODEL SMALL
.DATA
ARRAY DB 12H,16H,11H,08H,23H
LARGEST DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV CX,04H
MOV SI,OFFSET ARRAY
MOV AL,[SI] ;AL=23H SI=23H CX=00H
UP: INC SI
CMP AL,[SI] ;IF 1ST NO > 2ND NO THEN GO TO NEXT
JNC NEXT
MOV AL,[SI]
NEXT:
LOOP UP
MOV LARGEST,AL
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
UNIT-IV Assembly Language Programming

 Write ALP to find largest number (16 bit) from array

.MODEL SMALL
.DATA
ARRAY DW 0012H,0016H,0011H,0008H,0023H
LARGEST DW 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV CX,04H
MOV SI,OFFSET ARRAY
MOV AX,[SI] ;AX=0023H SI=0023H CX=00H
UP: INC SI
INC SI
CMP AX,[SI] ;IF 1ST NO > 2ND NO THEN GO TO NEXT
JNC NEXT
MOV AX,[SI]
NEXT:
LOOP UP
MOV LARGEST,AX
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 19
UNIT-IV Assembly Language Programming

ODD AND EVEN PROGRAMS

 Write ALP to check number is Even or Odd

.MODEL SMALL
.DATA
NUMBER DB 11H
ODD_NO DB 00H
EVEN_NO DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AL,NUMBER

ROR AL,1 ;ROTATE AT RIGHT


JNC DOWN
ROL AL,1 ;RESTORE NUMBER
MOV ODD_NO,AL
JMP EXIT
DOWN:
ROL AL,1 ;RESTORE NUMBER
MOV EVEN_NO,AL
EXIT:
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 20
UNIT-IV Assembly Language Programming

 Write ALP to count odd numbers in the array

.MODEL SMALL
.DATA
ARRAY DW 0011H,0013H,0012H,0010H,0024H
ODD_COUNT DW 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV SI,OFFSET ARRAY
MOV CX,05H
NEXT:MOV AX,[SI]
ROR AX,1 ;AX=0024H , ODD_COUNT=2 , CX=0
JNC DOWN
INC ODD_COUNT
DOWN:
ADD SI,2
LOOP NEXT
MOV AH,4CH
INT 21H
END

NOTE: Here we have done the program for 16 bit numbers array. For 8 bit numbers, only change the
directives from DW to DB. Use AL instead of AX and change instruction ADD SI, 2 to INC SI

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 21
UNIT-IV Assembly Language Programming

 Write ALP to count EVEN numbers in the array

.MODEL SMALL
.DATA
ARRAY DW 0011H,0013H,0012H,0010H,0024H
EVEN_COUNT DW 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV SI,OFFSET ARRAY
MOV CX,05H
NEXT:MOV AX,[SI]
ROR AX,1
JC DOWN
INC EVEN_COUNT
DOWN:
ADD SI,2
LOOP NEXT
MOV AH,4CH
INT 21H
END

NOTE: Here we have done the program for 16 bit numbers array. For 8 bit numbers, only change the
directives from DW to DB. Use AL instead of AX and change instruction ADD SI, 2 to INC SI

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 22
UNIT-IV Assembly Language Programming

 Write ALP program to find count of even and odd numbers in given array

.MODEL SMALL
.DATA
ARRAY DB 11H,12H,13H,14H,15H
ODD_COUNT DB 00H
EVEN_COUNT DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV SI,OFFSET ARRAY
MOV CX,05H
UP:MOV AL,[SI]
ROR AL,1
JNC DOWN
INC ODD_COUNT
JMP NEXT
DOWN:
INC EVEN_COUNT
NEXT:INC SI
LOOP UP
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 23
UNIT-IV Assembly Language Programming

POSITIVE AND NEGATIVE NUMBER PROGRAMS

 Write ALP to check whether number is Positive or Negative

.MODEL SMALL
.DATA
NUMBER DB 11H
POSITIVE_NO DB 00H
NEGATIVE_NO DB 00H
.CODE
MOV AX, @DATA
MOV DS, AX
MOV AL, NUMBER

ROL AL,1 ;ROTATE AT LEFT


JNC DOWN ;CHECK POSITIVE OR NEGATIVE
ROR AL,1 ;RESTORE NUMBER
MOV NEGATIVE_NO,AL
JMP EXIT
DOWN:
ROR AL,1 ;RESTORE NUMBER
MOV POSITIVE_NO,AL
EXIT:
MOV AH,4CH
INT 21H
END

NOTE: The MSB bit indicates sign of number i.e. D7 or D15. If it is 1, the number is Negative else the
number is positive.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 24
UNIT-IV Assembly Language Programming

 Write an ALP for 8086 to count positive numbers in array.

.MODEL SMALL
.DATA
ARRAY DW 0011H,1124H,2378H,2312H,0123H
POSITIVE_COUNT DW 00H
.CODE
MOV AX, @DATA ;Initialize data segment
MOV DS, AX
MOV CX, 05H ;Initialize counter
MOV SI,OFFSET ARRAY ;Initialize Memory Pointer
NEXT:
MOV AX,[SI] ;Read number from array
ROL AX,1 ;Rotate at left
JC DOWN ;If number is positive
INC POSITIVE_COUNT ;Increment positive counter
DOWN:
ADD SI,2 ;Increment memory pointer as array is word data type
LOOP NEXT ;Decrement Counter and if it is not 0 then go to NEXT
MOV AH,4CH
INT 21H
END

Note: 16 bit numbers in array. For 8 bit numbers, only change the directives from DW to DB. Use AL
instead of AX and change instruction ADD SI, 2 to INC SI

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 25
UNIT-IV Assembly Language Programming

 Write an ALP for 8086 to count negative numbers in array.

.MODEL SMALL
.DATA
ARRAY DW 0011H,1124H,2378H,2312H,0123H
NEGATIVE_COUNT DW 00H
.CODE
MOV AX, @DATA ;Initialize data segment
MOV DS, AX
MOV CX, 05H ;Initialize counter
MOV SI,OFFSET ARRAY ;Initialize Memory Pointer
NEXT:
MOV AX,[SI] ;Read number from array
ROL AX,1 ;Rotate at left
JNC DOWN ;If number is Negative
INC NEGATIVE_COUNT ;Increment negative counter
DOWN:
ADD SI,2 ;Increment memory pointer as array is word data type
LOOP NEXT ;Decrement Counter and if it is not 0 then go to NEXT
MOV AH,4CH
INT 21H
END

Note: 16 bit numbers in array. For 8 bit numbers, only change the directives from DW to DB. Use AL
instead of AX and change instruction ADD SI, 2 to INC SI

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 26
UNIT-IV Assembly Language Programming

 Write an ALP for 8086 to count positive and negative numbers in array.

.MODEL SMALL
.DATA
ARRAY DW 0011H,1124H,2378H,2312H,0123H
POSITIVE_COUNT DW 00H
NEGATIVE_COUNT DW 00H
.CODE
MOV AX, @DATA ;Initialize data segment
MOV DS, AX
MOV CX, 05H ;Initialize counter
MOV SI,OFFSET ARRAY ;Initialize Memory Pointer
NEXT:
MOV AX,[SI] ;Read number from array
ROL AX,1 ;Rotate at left
JNC DOWN
INC NEGATIVE_COUNT ;Increment negative counter
JMP DOWN1
DOWN:
INC POSITIVE_COUNT ;Increment positive counter
DOWN1:
ADD SI,2 ;Increment memory pointer as array is word data type
LOOP NEXT ;Decrement Counter and if it is not 0 then go to NEXT
MOV AH,4CH
INT 21H
END

Note: This program for 16-bit numbers array. For 8-bit numbers, only change the directives from DW to
DB. Use AL instead of AX and change instruction ADD SI, 2 to INC SI

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 27
UNIT-IV Assembly Language Programming

COUNT NUMNER OF ZEROS AND ONES PROGRAMS


- The number of zeroes and ones can be count by rotating the number towards right or left 8
times for 8-bit number and 16 times for 16-bit number.
- Any of instruction ROR, ROL, RCR, RCL can be used.
- When we rotate the number left or right, corresponding bit i.e. D0 or D7 initially goes into carry
flag. Then we can check carry flag by JC or JNC to count number of ones or zeroes.

 Write an ALP to count number of ONES in 16 bit Number

.MODEL SMALL
.DATA
NUMBER DW 0007H
ONE_COUNT DB 00H
.CODE
MOV AX,@ DATA ;initialize data segment
MOV DS,AX
MOV CX,16 ;initialize rotation counter by 16
MOV AX,NUMBER ;load number in AX
NEXT:
ROR AX,1 ;rotate number by 1 bit right
JNC DOWN ;if bit not equal to 1 then go to down
INC ONE_COUNT ;else increment counter ones by one
DOWN:
LOOP NEXT ;decrement counter by 1 & if its not 0 then go to NEXT
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 28
UNIT-IV Assembly Language Programming

 Write an ALP to count number of ZEROES in 16 bit Number

.MODEL SMALL
.DATA
NUMBER DW 0007H
ZERO_COUNT DB 00H
.CODE
MOV AX,@ DATA ;initialize data segment
MOV DS,AX
MOV CX,16 ;initialize rotation counter by 16
MOV AX,NUMBER ;load number in AX
NEXT:
ROR AX,1 ;rotate number by 1 bit right
JC DOWN ;if bit not equal to 1 then go to down
INC ZERO_COUNT ;else increment counter ones by one
DOWN:
LOOP NEXT ;decrement counter by 1 and if not 0 then go to NEXT
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 29
UNIT-IV Assembly Language Programming

 Write an ALP to count number of ZEROES & ONES in 16-bit Number

.MODEL SMALL
.DATA
NUMBER DW 0007H
ONE_COUNT DB 00H
ZERO_COUNT DB 00H
.CODE
MOV AX,@ DATA ;initialize data segment
MOV DS,AX
MOV CX,16 ;initialize rotation counter by 16
MOV AX,NUMBER ;load number in AX
NEXT:
ROR AX,1 ;rotate number by 1 bit right
JC DOWN ;if bit not equal to 1 then go to down
INC ZERO_COUNT ;increment ZERO counter ones by one
JMP DOWN1
DOWN:
INC ONE_COUNT ;increment ONE counter ones by one
DOWN1:
LOOP NEXT ;decrement counter by 1 & if its not 0 then go to NEXT
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 30
UNIT-IV Assembly Language Programming

BLOCK TRANSFER PROGRAM

 Write ALP to perform block transfer operation of 5 NOS. Assume block of FIVE 16 bit NOS

.MODEL SMALL
.DATA
ARRAY1 DW 1011H,0403H,0505H,2007H,0069H
ARRAY2 DW 5 DUP(0) ; Empty Array
.CODE
MOV AX, @DATA
MOV DS, AX
MOV CX, 05H ; Initialize counter
MOV SI, OFFSET ARRAY1 ; Initialize Memory ptr for Source Block
MOV DI, OFFSET ARRAY2 ;Initialize Memory ptr for destination Block
UP: MOV AX, [SI] ; Read number from source array
MOV [DI], AX ; Write number to destination array
ADD SI, 2
ADD DI, 2
LOOP UP
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 31
UNIT-IV Assembly Language Programming

ASCENDING AND DESCENDING ORDER

Write an ALP to arrange numbers(8-bit) in array in descending order.

.MODEL SMALL
.DATA
ARRAY DB 56H,45H,12H,08H,05H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV BX,04H ;Pass counter
UP1:MOV SI,OFFSET
MOV CX,04H ;compare counter
UP:MOV AL,[SI]
CMP AL,[SI+1] ;If first no>second no then go to label Down
JNC DOWN
XCHG AL,[SI+1] ;INTERCHANGE NUMBERS
XCHG AL,[SI]
DOWN:
INC SI
LOOP UP ;Decrement counter i.e CX=CX-1
DEC BX ;Decrement pass counter
JNZ UP1 ;If not zero then go to label UP1
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 32
UNIT-IV Assembly Language Programming

Write an ALP to arrange numbers (16-bit) in array in descending order.

.MODEL SMALL
.DATA
ARRAY DW 0005H,0045H,0012H,0008H,0056H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV BX,04H ;Pass counter
UP1:MOV SI,OFFSET ARRAY
MOV CX,04H ;compare counter
UP:MOV AX,[SI]
CMP AX,[SI+2] ;If first no>second no then go to label Down
JNC DOWN
XCHG AX,[SI+2] ;INTERCHANGE NUMBERS
XCHG AX,[SI]
DOWN:
ADD SI,2
LOOP UP ;Decrement counter i.e CX=CX-1
DEC BX ;Decrement pass counter
JNZ UP1 ;If not zero then go to label UP1
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 33
UNIT-IV Assembly Language Programming

Write an ALP to arrange numbers (8-bit) in array in ascending order.

.MODEL SMALL
.DATA
ARRAY DB 05H,08H,12H,45H,56H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV BX,04H ;Pass counter
UP1:MOV SI,OFFSET ARRAY
MOV CX,04H ;compare counter
UP:MOV AL,[SI]
CMP AL,[SI+1] ;If first no<second no then go to label Down
JC DOWN
XCHG AL,[SI+1] ;INTERCHANGE NUMBERS
XCHG AL,[SI]
DOWN:
INC SI
LOOP UP ;Decrement counter i.e CX=CX-1
DEC BX ;Decrement pass counter
JNZ UP1 ;If not zero then go to label UP1
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 34
UNIT-IV Assembly Language Programming

Write an ALP to arrange numbers (16-bit) in array in ascending order.

.MODEL SMALL
.DATA
ARRAY DW 0005H,0008H,0012H,0045H,0056H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV BX,04H ;Pass counter
UP1:MOV SI,OFFSET ARRAY
MOV CX,04H ;compare counter
UP:MOV AX,[SI]
CMP AX,[SI+2] ;If first no<second no then go to label Down
JC DOWN
XCHG AX,[SI+2] ;INTERCHANGE NUMBERS
XCHG AX,[SI]
DOWN:
ADD SI,2
LOOP UP ;Decrement counter i.e CX=CX-1
DEC BX ;Decrement pass counter
JNZ UP1 ;If not zero then go to label UP1
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 35
UNIT-IV Assembly Language Programming

STRING OPERATIONS
Write an ALP to find length of string

.MODEL SMALL
.DATA
MSG DB 'COMPUTER$'
COUNT DB 0H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV SI,OFFSET MSG
NEXT:
MOV AL,[SI]
CMP AL,'$'
JE EXIT
INC COUNT
INC SI
JMP NEXT
EXIT:
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 36
UNIT-IV Assembly Language Programming

Write an ALP to display reverse of given string

.MODEL SMALL
.DATA
MSG1 DB 'VJTECH$'
MSG2 DB 20 DUP('$')
STR2 DB 'Reverse String:$'
COUNT DB 0H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV SI,OFFSET MSG1
NEXT:
MOV AL,[SI]
CMP AL,'$'
JE EXIT
INC SI
INC COUNT
JMP NEXT
EXIT:
MOV DI,OFFSET MSG2
UP: DEC SI
MOV AL,[SI]
MOV [DI],AL
INC DI
DEC COUNT
JNZ UP

MOV AH,09H
LEA DX,STR2
INT 21H
MOV AH,09H
LEA DX,MSG2
INT 21H

MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 37
UNIT-IV Assembly Language Programming

Write an ALP to copy one string to another

.MODEL SMALL
.DATA
STR1 DB 'VJTECH$'
STR2 DB 20 DUP('$')
MSG DB 'Copied String:$'
.CODE
MOV AX,@DATA
MOV DS,AX

MOV SI,OFFSET STR1


MOV DI,OFFSET STR2

NEXT:
MOV AL,[SI]
CMP AL,'$'
JE EXIT
MOV [DI],AL
INC SI
INC DI
JMP NEXT
EXIT:
MOV AH,09H
LEA DX,MSG
INT 21H
MOV AH,09H
LEA DX,STR2
INT 21H
MOV AH,4CH
INT 21
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 38
UNIT-IV Assembly Language Programming

Write an ALP to Concatenation of two strings

.MODEL SMALL
.DATA
STR1 DB 'VJTECH$'
STR2 DB 'ACADEMY$'
MSG DB 'Concatenated String:$'
.CODE
MOV AX,@DATA
MOV DS,AX

MOV SI,OFFSET STR1


NEXT:
MOV AL,[SI]
CMP AL,'$'
JE EXIT
INC SI
JMP NEXT
EXIT:
MOV DI,OFFSET STR2

UP: MOV AL,[DI]


CMP AL,'$'
JE EXIT1
MOV [SI],AL
INC SI
INC DI
JMP UP

EXIT1:
MOV AL,'$'
MOV [SI],AL

MOV AH,09H
LEA DX,MSG
INT 21H

MOV AH,09H
LEA DX,STR1
INT 21H

MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 39
UNIT-V PROCEDURES AND MACROS

UNIT-V PROCEDURES AND MACROS

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
UNIT-V PROCEDURES AND MACROS

Procedure:
 Procedure is a series of instructions that is to be executed several times in a program, and called
whenever required.
 Program control is transferred to the procedure, when CALL instruction is executed at run time.
 Memory required is less, as the program control is transferred to procedure.
 Stack is required at Procedure CALL.
 Extra overhead time is required for linkage between the calling program and called procedure.
 Parameters passed in registers, memory locations or stack.
 RET is required at the end of the Procedure.
 Procedure is called using:
CALL <Procedure_name>
 Directives used: PROC, ENDP, FAR, NEAR
 General Form:

ProcedureName PROC [near/far]


---
statements
---
ProcedureName ENDP

 Example
Addition PROC near
------
Addition ENDP

Advantages of Procedure:

1) Modular programming.
2) Reduced work load and development time.
3) Debugging of program is easier.
4) Reduction of line of code.
5) Reusability of code.
6) Library of procedure can be implemented.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
UNIT-V PROCEDURES AND MACROS

RE-ENTRANT PROCEDURE:

- The re-entrant procedure is a very special kind of procedure.


- In such kind of procedure, procedure 1 is called the mainline program, then procedure 2 is called
from procedure 1 and then again procedure 1 is called from procedure 2.
- This can be well understood from the following diagram

- In some situation it may happen that procedure1 is called from main program, procedure2 is
called from procedure1 and procedure1 is again called from procedure2.
- In this situation program execution flow reenters in the procedure1. These types of procedures
are called reentrant procedures.
- The RET instruction at the end of procrdure1 returns to procedure2. The RET instruction at the
end of procedure2 will return the execution to procedure1.
- Procedure1 will again be 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.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
UNIT-V PROCEDURES AND MACROS

RECURSIVE PROCEDURES:

- A recursive procedure is a procedure which calls itself.


- This results in the procedure call to be generated from within the procedures again and again.
- This can be understood as follows:

- The recursive procedures keep on executing until the termination condition is reached.
- The recursive procedures are very effective to use and to implement but they take a large
amount of stack space and the linking of the procedure within the procedure takes more time as
well as puts extra load on the processor.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
UNIT-V PROCEDURES AND MACROS

Differentiate between Recursive and Re-entrant Procedure:

Re-entrant procedure Recursive procedure


A procedure is said to be reentrant, if it can be
A recursive procedure is a procedure which calls
interrupted, used and re-entered without losing or
itself
writing over anything.

In recursive procedure the program sets aside a


In Re-entrant Procedure must first push all the flags few locations in stack for the storage of the
and registers used in the procedure. parameters which are passed each time the
computation is done and the value is returned

In recursive procedure Each value returned is then


To be a re-entrant, It should also use only registers or
obtained by popping back from the stack at every
stack to pass parameters.
RET instruction when executed at the end of the
procedure.

Example Example

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
UNIT-V PROCEDURES AND MACROS

DIRECTIVES FOR PROCEDURE:

PROC

- The PROC directive is used to identify the start of a procedure.


- The term NEAR or FAR is used to specify the type of the procedure.
- If the term is not specified, then assembler assumes NEAR as a type Specifier.
- General Form:
Procedure_name PROC [NEAR/FAR]
---

Example:
Addition PROC near
------
Addition ENDP

ENDP

- The directive is used along with the name of the procedure to indicate the end of a procedure to the
assembler.

General Form:
Procedure_name ENDP
Example:
ADDITION ENDP ; End of ADDITION Procedure

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
UNIT-V PROCEDURES AND MACROS

PROCEDURE CALL and RET INSTRUCTIONS:

CALL INSTRUCTION:

- It is used to transfer program control to the sub-program or subroutine.


- The CALL can be NEAR, where the procedure is in the same segment for each.
- In FAR CALL, procedure is in a different segment.

Syntax: CALL procedure name

Operation:

1) For NEAR CALL

SP  SP - 2
Save IP on stack
IP  address of procedure

2) For FAR CALL


SP  SP-2
Save CS on stack
CS New segment base containing procedure
SP SP-2
Save IP on stack
IP Starting address of called procedure

RET INSTRUCTION:

- It is used to transfer program execution control from a procedure to the next instruction
immediate after the CALL instruction in the calling program.

Syntax: RET

Operation:

1) For Near Return


IP  Content from top of stack
SP  SP + 2

2) For Far Return


IP Contents from top of stack
SP SP+2
CS  Contents of top of stack
SP SP+2

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
UNIT-V PROCEDURES AND MACROS

DIFFERENCE BETWEEN NEAR AND FAR CALL

NEAR FAR
A near call is a call to procedure which is in A far call is a call to procedure which is in different
same code segment segment
Also called as Intra_Segment call. Also called as Inter_Segment call.
A FAR call replaces the old CS:IP pairs with new CS:IP
A NEAR call replaces the old IP with new IP.
pairs.
Value of old IP is pushed on the stack. Value of old CS:IP pairs are pushed on to the stack

The contents of CS are not stored. The contents of CS are also stored along with offset.

In Far call contents of SP are decremented by 2 and


In near call contents of SP is decremented by 2
value of CS is loaded. Then SP is again decremented by 2
and contents of offset address IP is stored
and IP is loaded.

Operation performed:
Operation performed: FAR PROC
NEAR PROC SP = SP -2
SP = SP – 2 Save CS on stack Save IP on stack
Save IP on stack CS = new segment base address of the called procedure
IP = Address of procedure SP =SP-2
Save IP on the stack and
IP = New offset Address of the called procedure

Less stack locations are required. More stack locations are required.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
UNIT-V PROCEDURES AND MACROS

PARAMETER PASSING IN PROCEDURE

Passing Parameter through Registers

- Procedures may require input data for their execution.


- The data or constants may be passed to the procedure by the main program or some procedures
through the register as given in the following example.
- Here, registers AX and BX are passed to the procedure, where their contents are added.
- When the procedure is called using CALL instruction, the instructions in the procedure are executed.
- The registers AX and BX are added.
- Thus, the data from the main program are used by the procedure using registers.

CODE SEGMENT
MOV AX, 2323H
MOV BX, 6262H
:
CALL PROC1
:
PROC1 PROC
:
ADD AX,BX
:
RET
PROC1 ENDP
END

- The disadvantage of using registers to pass parameters is that the number of registers limits the
number of parameters you can pass.
- E.g. An array of 100 elements can’t be passed to a procedure using registers.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
UNIT-V PROCEDURES AND MACROS

2. Passing Parameters 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.

PUSH MULTIPLICAND
PUSH MULTIPLIER
CALL MULTI

- The procedure can now access these variables on the stack.


- Stack pointer (SP - register) points to the topmost element on the stack.
- After calling a procedure, sp will point to return address.
- Thus, the, the last parameter pushed i.e MULTIPLIER is at SP+2.
- To access this parameter the address [SP+2] can be used.
- The second parameter MULTIPLICAND can be accessed using the address [SP+4].
Stack
Return Address SP
MULTTIPLIER sp+2
MULTIPLICAND SP+4

- The only problem with this approach is that the SP register's value will change if the stack is used
within the procedure.

- The value can be stored in the BP (base pointer) register. Thus references can be made relative to the
BP register, i.e. [BP+2] and [BP+4].

- The second procedure may destroy the value BP register and the first procedure would not be able to
regain its value. So its better to save the BP register at the top of the procedure code and restore it at
the bottom.

- After push BP:-

Stack
BP SP
Return Address sp+2
MULTTIPLIER SP+4
MULTIPLICAND SP+6

- The stack can also be used to pass parameters back from the called procedure to the calling procedure.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
UNIT-V PROCEDURES AND MACROS

- The procedure during its execution pops back the appropriate parameters as and when required.

.MODEL SMALL
.DATA
MULTIPLICAND DW 1234H
MULTIPLIER DW 4232H
.CODE
MOV AX, @data
MOV DS, AX
:
:
PUSH MULTIPLICAND
PUSH MULTIPLIER
CALL MULTI
:
:
MULTI PROC NEAR
PUSH BP
MOV BP, SP ; Copies offset of SP into BP
MOV AX, [BP + 6] ; MULTIPLICAND is available at [BP + 6] and is passed to AX
MOV BX, [BP + 4] ; MULTIPLIER value is passed
MUL BX
POP BP
RET ; Increments SP by 4 to return address
MULTI ENDP ; End procedure
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
UNIT-V PROCEDURES AND MACROS

Passing parameters using memory:

- In the cases where few parameters have to be passed to and from a procedure, registers are
convenient.
- But, in cases when we need to pass a large number of parameters to procedure, we use memory.
- This memory may be a dedicated section of general memory or a part of it.

Example:

.MODEL SMALL
.DATA
NUM1 DB 10H ;Memory location of num1
NUM2 DB 12H ;Memory location of num2
RES DB ?
.CODE
MOV AX,@DATA
MOV DS,AX
CALL ADDITION
MOV AH,4CH
INT 21H
ADDITION PROC
MOV AL,NUM1
MOV BL,NUM2
ADD AL,BL
MOV RES,AL
RET
ADDITION ENDP
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
UNIT-V PROCEDURES AND MACROS

ASSEMBLY LANGUAGE PROGRAMMING USING PROCEDURES

Write an assembly language program to add two numbers using procedure.

Model small
.Data
NUM1 DW 1234H
NUM2 DW 5481H
RES DW ?
.Code
MOV AX,@DATA
MOV DS,AX
CALL ADDITION ; Call procedure Addition
MOV AH,4CH
INT 21H
ADDITION PROC
MOV AX,NUM1
MOV BX,NUM2
ADD AX,BX
MOV RES,AX
RET
ENDP
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
UNIT-V PROCEDURES AND MACROS

Write an assembly language program to multiply two 8 bits numbers using NEAR
procedure.

.MODEL SMALL
.DATA
NUM1 DB 53H
NUM2 DB 33H
RES DW ?
.CODE
MOV AX,@DATA
MOV DS,AX
CALL MULT ; Call to procedure Multiplication
MOV AH,4CH
INT 21H
MULT PROC
MOV AL,NUM1
MUL NUM2
MOV RES,AX
RET
ENDP
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
UNIT-V PROCEDURES AND MACROS

Write an assembly language program to find smallest number from array using
procedure.

.MODEL SMALL
.DATA
ARRAY DW 0012H,0016H,0011H,0008H,0023H
SMALLEST DW 00H
.CODE
MOV AX,@DATA
MOV DS,AX
CALL FIND_SMALL
MOV AH,4CH
INT 21H
FIND_SMALL PROC
MOV CX,04H
MOV SI,OFFSET ARRAY
MOV AX,[SI]
UP:INC SI
INC SI
CMP AX,[SI] ;IF 1ST NO < 2ND NO THEN GO TO NEXT
JC NEXT
MOV AX,[SI]
NEXT:
LOOP UP
MOV SMALLEST,AX
RET
ENDP
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
UNIT-V PROCEDURES AND MACROS

Write an assembly language program to find largest number from array using
procedure.

.MODEL SMALL
.DATA
ARRAY DW 0012H,0016H,0011H,0008H,0023H
LARGEST DW 00H
.CODE
MOV AX,@DATA
MOV DS,AX
CALL FIND_LARGE
MOV AH,4CH
INT 21H
FIND_LARGE PROC
MOV CX,04H
MOV SI,OFFSET ARRAY
MOV AX,[SI]
UP:INC SI
INC SI
CMP AX,[SI] ;IF 1ST NO > 2ND NO THEN GO TO NEXT
JNC NEXT
MOV AX,[SI]
NEXT:
LOOP UP
MOV LARGEST,AX
RET
FIND_LARGE ENDP
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
UNIT-V PROCEDURES AND MACROS

Write an ALP to arrange numbers in array in ascending order using procedure.


.MODEL SMALL
.DATA
ARRAY DW 0005H,0045H,0012H,0008H,0056H
.CODE
MOV AX,@DATA
MOV DS,AX
CALL ASCENDING
MOV AH,4CH
INT 21H
ASCENDING PROC
MOV BX,04H
UP1:MOV SI,OFFSET ARRAY
MOV CX,04H
UP:MOV AX,[SI]
CMP AX,[SI+2]
JC DOWN
XCHG AX,[SI+2]
XCHG AX,[SI]
DOWN:
ADD SI,2
LOOP UP
DEC BX
JNZ UP1
RET
ASCENDING ENDP
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
UNIT-V PROCEDURES AND MACROS

Write an ALP to arrange 16 bit numbers in array in descending order using


procedure.
.MODEL SMALL
.DATA
ARRAY DW 0005H,0045H,0012H,0008H,0056H
.CODE
MOV AX,@DATA
MOV DS,AX
CALL DESCENDING
MOV AH,4CH
INT 21H
DESCENDING PROC
MOV BX,04H
UP1:MOV SI,OFFSET ARRAY
MOV CX,04H
UP:MOV AX,[SI]
CMP AX,[SI+2]
JNC DOWN
XCHG AX,[SI+2]
XCHG AX,[SI]
DOWN:
ADD SI,2
LOOP UP
DEC BX
JNZ UP1
RET
DESCENDING ENDP
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
UNIT-V PROCEDURES AND MACROS

Write an assembly language program for sum of series of 05 numbers using


procedure.

.MODEL SMALL
.DATA
array DW 0003H,0070H,0060H,004H,001H
res_lsb DW ?
res_msb DW ?
.CODE
MOV AX,@DATA
MOV DS,AX
CALL FIND_SUM
MOV AH,4CH
INT 21h
FIND_SUM PROC
MOV CX,05H
MOV SI, OFFSET array
UP:MOV AX,[SI]
ADD res_lsb,AX
JNC NEXT
INC res_msb
NEXT:
ADD SI,2
LOOP UP
RET
FIND_SUM ENDP
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 19
UNIT-V PROCEDURES AND MACROS

Write ALP for addition of two BCD numbers using procedure.

.MODEL SMALL
.DATA
num1 DB 84H
num2 DB 28H
res_lsb DB 00H
res_msb DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
CALL ADD_BCD
MOV AH,4CH
INT 21h
ADD_BCD PROC
MOV AL,num1
MOV BL,num2
ADD AL,BL
DAA ; Adjust result to BCD
JNC EXIT
INC res_msb
EXIT:
MOV res_lsb,AL
RET
ADD_BCD ENDP
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 20
UNIT-V PROCEDURES AND MACROS

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 is also called as open subroutine.
- Syntax:
Macro_name MACRO[arg1,arg2,…..argN)
…..
Endm

Advantages of Macro:
 The speed of the execution of the program is increased.
 It saves a lot of time that is spent by the compiler for invoking / calling the functions.
 It reduces the length of the program.
 Reduces amount of repetitive coding
 Reduces errors caused by repetitive coding.

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 21
UNIT-V PROCEDURES AND MACROS

DIRECTIVES USED FOR DEFINING MACRO


1) Macro definition or (Macro directive):
- The directive MACRO informs the assembler the beginning of MACRO.
- It consists of name of macro followed by keyword MACRO and MACRO arguments if any.
- Syntax:
Macro_name MACRO[arg1,arg2,…..argN)
…..
Endm
2) ENDM Directive :END OF MACRO
- The directive ENDM informs the assembler the end of macro.
- Syntax: ENDM
3) LOCAL
- Macros are expanded directly in code, therefore if there are labels inside the macro definition
you may get "Duplicate declaration" error when macro is used for twice or more.
- To avoid such problem, use LOCAL directive followed by names of variables, labels or procedure
names.
- Syntax: LOCAL <label>

4) INCLUDE-Include source code from file


- This directive used to tell the assembler to insert a block of source code from named file into
current source module.
- Syntax: INCLUDE <file path specification with file name>
- Example: INCLUDE C:\Tasm\Macro.lib

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 22
UNIT-V PROCEDURES AND MACROS

Write ALP using macro to perform multiplication of two 8 Bit Unsigned numbers.
.MODEL SMALL
PRODUCT MACRO a,b,c
MOV AL,a
MOV BL,b
MUL BL
MOV c,AX
ENDM
.DATA
NO1 DB 06H
NO2 DB 03H
RESULT DW ?
.CODE
MOV AX,@DATA
MOV DS,AX
PRODUCT NO1,NO2,RESULT
MOV AH,4CH
INT 21H
END

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 23
UNIT-V PROCEDURES AND MACROS

Compare Procedure and Macros

Procedure Macro
Macro is a small sequence of code of the same
Procedure is a series of instructions is to be executed
pattern, repeated frequently at different places,
several times in a program, and called whenever
which perform the same operation on different data
required.
of the same data type.
Program control is transferred to the procedure, The MACRO code is inserted into the program,
when CALL instruction is executed at run time wherever MACRO is called, by the assembler
Memory required is less, as the program control is Memory required is more, as the code is inserted at
transferred to procedure. each MACRO call
Procedures are used for large group of instructions to Macro are used for small group of instructions to be
be repeated repeated
Object code is generated every time the macro is
Object code is generated only once in memory.
called.
CALL & RET instructions are used to call procedure and
Macro can be called just by writing its name.
return from procedure.
Length of the object file is less Object file becomes lengthy.
Directives PROC & ENDP are used for defining MACRO and ENDM are used for defining MACRO
procedure. Directives
More time is required for its execution for linkage
Less time is required for its execution
between the calling program and called procedure
Syntax: Syntax:
Procedure_name PROC MACRO-name MACRO [arg1,arg2 ,… argN]
---- ----
---- -----
Procedure_name ENDP ENDM
For Example: For Example:
Addition PROC near Addition MACRO msg
------ ------
Addition ENDP ENDM

Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 24

You might also like