Mic Merged
Mic Merged
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
UNIT-I 8086 16 bit Microprocessor
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.
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)
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 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
- 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}$):
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:
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.
- 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
- 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.
- 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
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
- 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.
- 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 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
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
UNIT-I 8086 16 bit Microprocessor
Advantages of Pipelining
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
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
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
UNIT-II The Art of Assembly Language Programming
2.1 Program development steps: defining problem and constrains, writing algorithms, flowchart,
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
UNIT-II The Art of Assembly Language Programming
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.
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
UNIT-II The Art of Assembly Language Programming
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.
- 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
- The range of values : 0 – 65535 for unsigned numbers -32768 to 32767 for signed numbers
Syntax: Name_Of_Variable DW Initialisation_Value
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
UNIT-II The Art of Assembly Language Programming
- 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.
- 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.
- 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.
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
- 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
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
9. SEGMENT:
- It is used to indicate the start of a logical segment. It is the name given to the segment.
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
- 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.
The optional start_address indicates the location in the code segment where execution is to begin.
- 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
identifies the start of procedure named Addition and tells the assembler that the procedure is far.
- This directive used to tell the assembler to insert a block of source code from named file
into current source module.
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
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
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
- 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.
- 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
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
UNIT-III Instruction Set of 8086 Microprocessor
- 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.
- 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.
- 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
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
UNIT-III Instruction Set of 8086 Microprocessor
- 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
- 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.
- 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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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
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:
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.
- 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
- 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):
DEC (Decrement):
CMP (Comparison):
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
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:
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:
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:
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:
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
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
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.
- 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.
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.
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
UNIT-III Instruction Set of 8086 Microprocessor
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
UNIT-III Instruction Set of 8086 Microprocessor
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 19
UNIT-III Instruction Set of 8086 Microprocessor
MOV:
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 20
UNIT-III Instruction Set of 8086 Microprocessor
XCHG:
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 21
UNIT-III Instruction Set of 8086 Microprocessor
- 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:
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.
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 23
UNIT-III Instruction Set of 8086 Microprocessor
LOOP:
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
STD: It sets the direction flag to 1. If it is set, string bytes are accessed from higher memory address
CLD: It clears the direction flag to 0, If it is reset, the string bytes are accessed from lower memory
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 25
UNIT-III Instruction Set of 8086 Microprocessor
- The following are the various string manipulation instructions in the 8086 microprocessors.
REP:
- 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
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
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 28
UNIT-III Instruction Set of 8086 Microprocessor
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 29
UNIT-III Instruction Set of 8086 Microprocessor
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 30
UNIT-IV Assembly Language Programming
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
UNIT-IV Assembly Language Programming
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-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:
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
UNIT-IV Assembly Language Programming
.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
.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
.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
.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
.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
.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
.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
.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
.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
.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
.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
.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
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
.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
.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
.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
.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
.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
.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
.MODEL SMALL
.DATA
NUMBER DB 11H
ODD_NO DB 00H
EVEN_NO DB 00H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AL,NUMBER
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 20
UNIT-IV Assembly Language Programming
.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
.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
.MODEL SMALL
.DATA
NUMBER DB 11H
POSITIVE_NO DB 00H
NEGATIVE_NO DB 00H
.CODE
MOV AX, @DATA
MOV DS, AX
MOV AL, NUMBER
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
.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
.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
.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
.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
.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
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
.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
.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
.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
.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
.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
.MODEL SMALL
.DATA
STR1 DB 'VJTECH$'
STR2 DB 20 DUP('$')
MSG DB 'Copied String:$'
.CODE
MOV AX,@DATA
MOV DS,AX
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
.MODEL SMALL
.DATA
STR1 DB 'VJTECH$'
STR2 DB 'ACADEMY$'
MSG DB 'Concatenated String:$'
.CODE
MOV AX,@DATA
MOV DS,AX
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
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:
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:
- 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:
- 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
Example Example
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
UNIT-V PROCEDURES AND MACROS
PROC
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
CALL INSTRUCTION:
Operation:
SP SP - 2
Save IP on stack
IP address of 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:
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
UNIT-V PROCEDURES AND MACROS
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.
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
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
- 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 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.
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
- 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
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
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
UNIT-V PROCEDURES AND MACROS
Microprocessor by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
UNIT-V PROCEDURES AND MACROS
.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
.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
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
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