Sunteți pe pagina 1din 107

TABLE OF CONTENTS

Chapter 1: 8086 - 16 Bit Microprocessor ............................................................................... 6

1.1 Features of 8086 microprocessor. ................................................................................ 6


1.2 Internal architecture of 8086........................................................................................ 6
1.3 Register organization of 8086...................................................................................... 7
1.4 Special uses of general purpose registers. ................................................................... 8
1.5 Flag register of 8086 .................................................................................................... 8
1.6 How queuing speeds up the processing of 8086 operations. ..................................... 11
1.7 Steps in physical address generation in 8086 MICROPROCESSORS. .................... 13
1.8 Memory segmentation. .............................................................................................. 14
1.9 Concept of pipelining in 8086 ................................................................................... 15
1.10 Pin diagram of 8086. .............................................................................................. 17
1.11 Functions for differenet pins of 8086..................................................................... 17

Chapter 2: The Art of Assembly language programming .................................................. 26

2.1 Program development steps: ...................................................................................... 26

Defining the problem: ...................................................................................................... 26


Algorithm: ........................................................................................................................ 26
Flowchart:......................................................................................................................... 26
Initialization checklist: ..................................................................................................... 26
Choosing instructions: ...................................................................................................... 27
Converting algorithms to assembly language programs: ................................................. 27

2.2 Assembly language program development tools: ...................................................... 27

Editor: ............................................................................................................................... 27
Assembler: ........................................................................................................................ 27
Linker: .............................................................................................................................. 27
Debugger: ......................................................................................................................... 28

2.3 Assembler directives.................................................................................................. 28

DB: define byte ................................................................................................................ 28


DW: define word .............................................................................................................. 28
DQ: DEFINE Quad word ................................................................................................. 29
DD: define double word ................................................................................................... 29

1|Ubaid Saudagar
DT: define ten bytes ......................................................................................................... 29
ASSUME: assume logical segment name ........................................................................ 29
SEGMENT: ...................................................................................................................... 29
ENDS: end of segment ..................................................................................................... 30
END: end of program ....................................................................................................... 30
Org (originate) directive ................................................................................................... 30
EQU (equate) directive..................................................................................................... 30
Proc and endp directive .................................................................................................... 30
Endp ................................................................................................................................. 31
Even directive ................................................................................................................... 31

Chapter 3: Instruction set of 8086 Microprocessor ............................................................ 32

3.1 Addressing modes: .................................................................................................... 32

1.1.1 Immediate addressing mode ............................................................................... 32


1.1.2 Direct addressing mode ...................................................................................... 32
3.1.3 Register addressing mode........................................................................................ 32
3.1.4 Register indirect addressing mode .......................................................................... 33
3.1.5 Register relative addressing mode........................................................................... 33
3.1.6 Based indexed addressing mode ........................................................................ 33
3.1.7 Relative based indexed addressing mode ................................................................ 34
3.1.8 mplicit addressing mode.......................................................................................... 34

3.2 8086 Instructions ............................................................................................................ 34

3.2.1 Data transfer Instructions ........................................................................................ 34


3.2.2 Arithmetic Instructions ............................................................................................ 38
3.2.3 Logical Instructions ................................................................................................. 42
3.2.4 String instructions ................................................................................................... 45
3.2.5 Flag controlled instructions ..................................................................................... 46
3.2.6 Branch Instructions ................................................................................................. 47

Chapter 4: Assembly Language programming ................................................................... 50

4.1 Model of 8086 Assembly language Program ................................................................. 50

4.1.1 Instruction format: ................................................................................................... 50


4.1.2 Assembler directives: .............................................................................................. 50

2|Ubaid Saudagar
4.2 Programming using Assembler: ..................................................................................... 50

1. Write an assembly language program to add two 8 bit numbers ........................... 50


2. Write an assembly language program to add two 16 bit numbers ......................... 51
3. Write an assembly language program to subtract two 8 bit numbers .................... 52
4. Write an assembly language program to subtract two 16 bit numbers .................. 53
5. Write an assembly language program to find the sum of series of n hex numbers.
…............................................................................................................................ 54
6. Write an assembly language program to find the sum of series of BCD numbers.
…............................................................................................................................ 55
7. Write an assembly language program to multiply two 8 bit unsigned numbers .... 57
8. Write an assembly language program to multiply two 16 bit unsigned numbers .. 58
9. Write an assembly language program to multiply two 8 bit signed numbers ........ 59
10. Write an assembly language program to multiply two 16 bit signed numbers ...... 60
11. Write an assembly language program to divide two unsigned numbers
(8bits/8bits)…................................................................................................................... 61
12. Write an assembly language program to divide two unsigned numbers (16bits/8bits)
…………................................................................................................................ 62
13. Write an assembly language program to divide two unsigned numbers
(16bits/16bits)................................................................................................................... 63
14. Write an assembly language program to divide two unsigned numbers
(32bits/16bits)................................................................................................................... 64
15. Write an assembly language program to find the smallest number from a given array
of n numbers ..................................................................................................................... 65
16. Write an assembly language program to find the largest number from array of n
numbers. ........................................................................................................................... 66
17. Write an assembly language program to arrange n numbers in an array in ascending
order …............................................................................................................................ 67
18. Write an assembly language programming to arrange an array of n numbers in
descending order .............................................................................................................. 69
19. Write a program in assembly language to perform block transfer of data without
using string instructions ................................................................................................... 70
20. Write a program in assembly language to perform block transfer of data using string
instructions ....................................................................................................................... 72

3|Ubaid Saudagar
21. Write an assembly language program to compare two strings without using string
instructions and display the result accordingly ................................................................ 73
22. Write an assembly language program to compare two strings using string
instructions and display the result accordingly. ............................................................... 74
23. Write a program in assembly language to calculate the length of the string ......... 75
24. Write an assembly language program to display string in reverse order ............... 76
25. Write an assembly language program to concatenate two strings (two words) ..... 78
26. Write an assembly language program to add two BCD numbers .......................... 80
27. Write an assembly language program to subtract two BCD numbers ................... 81
28. Write an assembly language program to multiply two BCD numbers .................. 82
29. Write an assembly language program to divide two BCD numbers ...................... 83
30. Write an assembly language program to convert a decimal number to hexadecimal
number. ............................................................................................................................. 84
31. Write an assembly language program to find whether given number is odd or even
…............................................................................................................................ 85
32. Write an assembly language program to find odd and even numbers in an array . 86
33. Write an assembly language program to find whether given number is Positive or
negative ............................................................................................................................ 88
34. Write an assembly language program to find positive and negative numbers in an
array 90
35. Write an assembly language program to find the number of ones and zeroes in a
number .............................................................................................................................. 92

Chapter 5: Procedure and Macro ......................................................................................... 94

5.1 About Procedure:............................................................................................................ 94

Definition: ........................................................................................................................ 94
Types of procedures: ........................................................................................................ 94
Directives used: ................................................................................................................ 94
CALL and RET instructions: ........................................................................................... 95
Re-entrant Procedure: ....................................................................................................... 96
Recursive Procedure:........................................................................................................ 97
Programs using Procedures: ............................................................................................. 97

5.2 About Macros: .............................................................................................................. 100

4|Ubaid Saudagar
Definition: ...................................................................................................................... 101
Difference between Procesure and Macro ..................................................................... 101
Programs using macros: ................................................................................................. 101

5|Ubaid Saudagar
CHAPTER 1: 8086 - 16 BIT MICROPROCESSOR

1.1 FEATURES OF 8086 MICROPROCESSOR.


✓ It requires +5V power supply
✓ It is a 40 pin IC
✓ Available in three clock rates: 5,8 and 10 MHz
✓ It is a 16 bit microprocessor
✓ Size of data bus is 16 bits
✓ Size of address bus is 20 bits
✓ Direct memory access upto 1Mb = 220
✓ Note: Size of the memory always depends on the number of address lines.
✓ Two separate units: Bus Interface Unit and Execution Unit
✓ External Co processor (8087) supported
✓ 6-byte instruction queue present to help speed up instruction execution
✓ Has multiplexed address and data bus
✓ It consists two memory banks viz. odd memory bank and even memory bank

1.2 INTERNAL ARCHITECTURE OF 8086.

6|Ubaid Saudagar
1.3 REGISTER ORGANIZATION OF 8086

General purpose registers:


There are in all 4 general purpose registers viz. AX, BX, CX, DX each of 16 bits i.e. 2 bytes
i.e. 1 word. These 4 registers are actually are pairs of 8 registers viz. AH, AL, BH, BL, CH,
CL, DH, DL each of 8 bits i.e. 1 byte. The letter X specify complete 16 bit register. The letters
L and H specify lower and higher bytes of a register.

Segment registers:
There are 4 segment registers in the Bus interface unit, size of each segment register is 16 bits.
Code Segment Register (CS register)
Data Segment Register (DS register)
Extra Segment Register (ES register)
Stack Segment Register (SS register)

Segment registers give the base address (segment address) to select any memory segment.
Operand register or Temporary register:
It is a 16 bit register which can store 16 bit data. This register cannot be used by the
programmer/user. It is used by the µp to store intermediate data or results therefore it is called
as temporary register.
Pointers and Index Registers:
The 16 bit register from where the µp will take 16 bit effective address is called memory
pointer.
There are three pointer registers as follows:
- Instruction pointer (IP)

7|Ubaid Saudagar
- Stack Pointer (SP)
- Base Pointer (BP)
Also there are 2 index registers which have multiple purposes such as:
- Can be used for pointer addressing of data
- Used as source in some string processing instructions
- Used to store Offset address
Note: The index registers are also a type of memory pointers but having multiple purpose
There are two index registers as follows:
- Source Index register (SI) - Destination Index Register (DI)

1.4 SPECIAL USES OF GENERAL PURPOSE REGISTERS.


1. AL/AX (Accumulator): It is intermediate register between the ALU and the memory.
Mostly, but not always the results of arithmetic and logical operations are stored in the
accumulator and then from there it is transferred to wherever required.
It is the most important general purpose register of the µp.
Eg: When the instruction DIV BL is executed what the µp does is that it divide content
of AX register with the content of BL register by default and after dividing the quotient
is by default stored in the AL register and remainder is stored in the AH register.
2. BX (Base Register): To select any memory location particularly in the data segment
we require two addresses : base address (stored in the segment register) and offset
address which can be stored in the base register in certain addressing modes.
3. CL/CX (Counter Register): It is used as a default counter register during loop, shift,
rotate, repeat instructions. It can be used as 8 bit counter as well as 16 bit counter.
4. DX (Data register): During multiplication and division operations when the result
goes beyond 16 bits i.e. 32bits, then the upper 16 bits are stored in the DX register.

1.5 FLAG REGISTER OF 8086

8|Ubaid Saudagar
Carry Flag:
During an addition operation if a carry is generated from the MSB or during a subtraction
operation a borrow is generated carry flag sets indicating a carry or borrow respectively.
Therefore when
CF = 1; carry is generated
CF = 0; carry not generated

Auxiliary carry flag:


During an addition operation if a carry is generated from the lower nibble to the higher
nibble, then AC flag sets.

Parity flag:
When an addition, subtraction, multiplication, division or any logical operation takes place
then depending on the result the parity flag gets affected.
µp checks the 8 LSB’s of the result:
If the number of one’s in the 8 LSB’s are 0/2/4/6/8 then the parity flag sets i.e. PF = 1
indicating even parity
If the number of one’s in the 8 LSB’s are 1/3/5/7 then the parity flag resets i.e. PF = 0
indicating odd parity

Zero flag:
Whenever any arithmetic or logical operation takes place and the result is 0 then zero flag
sets to indicate that the result is 0 i.e. ZF = 1.
Eg: Let A = 80H
B = 80H
SUB B instruction is executed. The result obtained is 00H in A register. When µp checks
this it has to reflect it somewhere that the result is zero, so it reflects in bit 7 which is
reserved for zero flag by setting its value to 1 i.e. ZF = 1

Sign flag:
Whenever the result of any arithmetic operation is negative the sign flag sets i.e. SF = 1
else remain in the reset state i.e. SF = 0.

Overflow flag (Bit 12):


Whenever there is an overflow from positive range to negative range or vice versa,
Overflow flag sets to 1 i.e. O = 1.

9|Ubaid Saudagar
E.g. mov al,40h
mov bl, 40h
add al, bl
Positive range is from 00H – 7FH = 0000 0000 B – 0111 1111 B
In the above range MSB bit is 0 throughout, which is the sign bit (0 indicates number is
positive)
Negative range is from 80H – FFH = 1000 0000 B – 1111 1111 B
In the above range MSB bit is 1 throughout, which is the sign bit (1 indicates number is
positive)
In the above example, before Addition AL = 40H and after execution of Addition
Instruction, AL = 80H
Since MSB bit of AL is 1 which indicates that number is negative and before execution the
MSB bit was 0, hence we say there is overflow from positive range to negative range.

Interrupt flag (Bit 10):


Interrupt flag is used to control the maskable interrupts of the µp 8086 which arrive at the
INTR pin. Let’s see how it controls the interrupts.

Shown above is the internal circuitry to control the maskable interrupts. When Bit 10 of
Flag register is 0 i.e. IF = 0, input to the AND gate is 0 therefore o/p of AND gate goes
low. Therefore, any input to the AND gate won’t be recognized hence any interrupt
occurring on the INTR pin won’t be acknowledged as it is not recognized.
When IF = 1 and interrupt on the INTR pin occurs, it is recognized and given to the internal
INTR circuitry through AND gate and then acknowledged.

Trap Flag (Bit 9):


This flag is used to detect any error in the program (debugging) by executing the program
in single stepping mode.
- If logic 0 is stored in TF, then the µp will execute all the instructions of a program in one
operation (free run operation)
Eg: When we execute the program and press key F9, the entire program gets executed in
one stroke. The imp. Point to understand is that when we press the key F9, value 0 gets
placed bit 9 of the flag register therefore Trap flag resets i.e. TF = 0.

10 | U b a i d S a u d a g a r
- If logic 1 is stored in TF, then the µp will execute one instruction of the program at a
time, after executing each instruction µp will execute INT1 (software interrupt), so µp will
branch from main program to subroutine.
The subroutine has a program which displays the result in different registers of µp on the
screen, so after each instruction the programmer can verify the result.
Eg: We use F8 function key to execute the program in single stepping mode. When we
press key F8 value 1 gets placed into bit 9 of flag register, hence trap flag sets i.e. TF = 1.

Direction Flag (Bit 11):

In the above example we see two memory blocks. The offset address of first memory block
is placed in the SI register and offset address of the second memory block is placed in the
DI register.
Now we need to manually increment SI and DI after each byte transfer from data segment
to extra segment.
To avoid all this what we do is after moving one byte from Data segment to extra segment
we clear the direction flag (CLD instruction is used to clear the direction flag) which puts
SI and DI in auto increment mode. In other words, whenever DF = 0, SI and DI increment
automatically.
If we are copying the data from the last location, we set the direction flag (STD instruction
is used to set the direction flag) which puts the SI and DI in auto decrement mode. In other
words, whenever DF = 1, SI and DI decrement automatically.

1.6 HOW QUEUING SPEEDS UP THE PROCESSING OF 8086


OPERATIONS.

This queue is based on FIFO (first in first out) basis which means that the byte which comes
first also goes out first from this register.

11 | U b a i d S a u d a g a r
µp 8086 has created 2 logical units within its architecture one is the bus interface unit (BIU)
and the other is the execution unit (EU).
While the EU is decoding an instruction or executing an instruction which does not require the
use of buses, the BIU fetches upto six bytes from the memory.
The BIU stores these prefetch bytes in FIFO register called as queue register.
When the EU is ready for the next instruction it simply takes (fetches) the next byte from the
instruction queue in the BIU.
This is much faster than sending out address to the system memory and waiting for memory to
send back the next instruction bytes.
This is a simple example of a car manufacturer which defines a person to make a car. Now
that person starts making the engine, then on its completion, he starts preparing the outer body
and continues.
We need to understand is that the person is completing one task and then starting another, hence
to manufacture one car will take a lot of time.
The manufacturer came up with an idea, that why not increase my staff. Instead of one staff I’ll
make it 5.
First : Engine manufacture
Second : Body manufacture
Third : Seat manufacture
Fourth : Painting
Fifth : Tyre manufacture
Now what happens that the work got divided and each person is busy. After completion of first
module it goes to second and so on till the fifth.
So every module is continuously busy, and the work got divided.

12 | U b a i d S a u d a g a r
1 person can do the work in 1 sec, now the work got divided, but first time all the modules are
in wait state and waiting for the previous one to complete i.e. M2 is waiting for M1 to complete.
So time taken is same i.e. 1s. But after the entire cycle is complete once, now the time taken is
only 0.2 sec.

1.7 STEPS IN PHYSICAL ADDRESS GENERATION IN 8086


MICROPROCESSORS.

Since address bus is of 20 bits = Physical address is of 20 bits


The calculation is as follows:
Physical address (PA) = BA + EA
But addition is a little different, let’s understand with an example:
Let BA = 1000H = 0001 0000 0000 0000 = 16 bit
EA = 3467H = 0011 0100 0110 0111 = 16 bit
Now base address is always appended with 0H i.e. 0000B at the end internally by the
µp.
Therefore BA now becomes = 0001 0000 0000 0000 0000 = 10000H

13 | U b a i d S a u d a g a r
Now,
PA = 0001 0000 0000 0000 0000 = 10000H
+ 0011 0100 0110 0111 = 3467H
---------------------------------------------
0001 0011 0100 0110 0111 = 13467H
---------------------------------------------
Now,
CS = BA = 2340H
IP = EA = 76A9H
PA = BA + EA
= 23400H = 0010 0011 0100 0000 0000
+ 76A9H = 0111 0110 1010 1001
= 2AAA9H = 0010 1010 1010 1010 1001

1.8 MEMORY SEGMENTATION.


The µp 8086 has 20 address lines therefore the number of memory locations it can
access is 220 = 1048576 = 1 Mb.
Address varies from 0000 0000 0000 0000 0000
1111 1111 1111 1111 1111
Hence our main aim is to access a memory location and from there access data.
The main point to understand is that the size of the registers is 16 bit and address is of
20 bits.
Therefore 8086 uses the memory segmentation scheme to access 1 Mb of memory or
1M locations.
The memory is divided into segments or blocks. The length of the segment is 64 Kb.
Two registers are used, one to access a segment and one to move within the segment
(select any location within the segment).
Consider the memory given on next slide:
Two steps to access any location within a memory:
➢ Select the segment with the help of base address or segment address (starting address
(indicating start of any segment). It means that the segment register contains the base
address with the help of which we are able to select any segment.

14 | U b a i d S a u d a g a r
➢ Now to move within the segment or in other words to select any location within the
segment we use offset address or effective address which we get from any pointer or
index register or any general purpose register.

Now these segments are logical segments, it means that there is no physical division within the
memory.
The segments can either be Code Segment, Data Segment, Extra Segment, Stack segment.
Code segment: When a segment is assigned as code segment, it is used to store the codes or
instructions
Data Segment: When a segment is assigned as data segment, it is used to store data used in the
programs
Extra Segment: It is also another segment used to store data
Stack Segment: It is used to store stack of data and address of main program during subroutine
call.

1.9 CONCEPT OF PIPELINING IN 8086


When one instruction is getting decoded and executed and simultaneously next instruction is
being fetched from the memory is called as pipelining process.
In 8086 with the help of queue register 6 instruction bytes are prefetched in the queue register,
whenever the EU requires bytes for decoding and execution purpose, it just has to fetch from
the queue register instead of going to the main memory and waist its time.
By the time it is decoding and executing the byte fetched from the queue, µp does not allow the
buses to remain idle, the external bus goes to the memory and fetches the next bytes and places
in the queue.
This is parallel processing and like a water pipe where the water continues to remain in motion.
Similar way the instruction bytes are in continuous motion i.e. from the memory to the BIU

15 | U b a i d S a u d a g a r
then into the queue then to the execution unit for execution. In other words, the motion of bytes
does not stop anywhere ideally unless and until a branch instruction arises in the queue.

Note: The µp does not perform the next fetch operation till at least two bytes of the instruction
queue are emptied.
This is a simple example of a car manufacturer which defines a person to make a car. Now
that person starts making the engine, then on its completion, he starts preparing the outer body
and continues.
We need to understand is that the person is completing one task and then starting another, hence
to manufacture one car will take a lot of time.

1 person can do the work in 1 sec, now the work got divided, but first time all the modules are
in wait state and waiting for the previous one to complete i.e. M2 is waiting for M1 to complete.
So time taken is same i.e. 1s. But after the entire cycle is complete once, now the time taken is
only 0.2 sec.

16 | U b a i d S a u d a g a r
1.10 PIN DIAGRAM OF 8086.

1.11 FUNCTIONS FOR DIFFERENET PINS OF 8086

𝑨𝑫𝟎 − 𝑨𝑫𝟏𝟓 - (Pin 16 - 2) - Multiplexed Address Data Bus


From pin number 16 – 2 are used by the multiplexed address data bus.
Multiplexing address and data lines means that the same lines are used for address and data.
Therefore, the same lines are used to carry address information as well as data.
During T1 state of every machine cycle ALE signal goes high and enables the latches, during
this T1 state address which is generated by the µp is available on this multiplexed bus.
After T1 state gets over ALE signal goes low which disables the latches and DEN signal also
goes low which enables the Transrecievers. Now data is available on the multiplexed bus.

𝑨𝟏𝟔 /𝑺𝟑 - 𝑨𝟏𝟗 /𝑺𝟔 (Pin 38 – 35) – Multiplexed address status bus
These lines work as Address Bus (A16 – A19) during T1 state of every machine cycle i.e. when
ALE signal goes high.
T2 onwards these work as status signals S3 to S6
S3 and S4 gives the status of the memory segment currently accessed.
S5 gives the status of Interrupt Flag.
S6 remains low in general.

17 | U b a i d S a u d a g a r
INTR (Pin 18):

This is input pin to the microprocessor. It is an interrupt pin and the input is level triggered,
which means that when logic high arrives on this pin it gets activated.
An interrupting device such as keyboard, printer etc. is not directly connected to this pin but
through a PIC (IC 8259).
Interrupts arriving on this pin can be masked with the help of IF flag. When the instruction CLI
is executed, IF flag gets cleared and interrupts arriving on this pin are masked (they don’t get
any response).

NMI (Pin 17):


This is an input pin to the µp. It is a positive edge triggered input. It is a non-maskable interrupt.
For any interrupt to be recognized the i/p at this pin should be high for at least 2 clock cycles.
Whenever there is a power failure this pin comes into use.

INTA (Pin 24):


This is an acknowledge signal going out from the µp to the PIC (IC 8259). Whenever the µp is
interrupted on the INTR pin with a high signal, if the µp accepts the interrupt which means that

18 | U b a i d S a u d a g a r
the IF is set, it sends an acknowledgement signal to the PIC from where the interrupt came,
indicating that it has accepted the interrupt.

RD (Pin 32) Read:


This is an active low pin
It is an output pin. Whenever the µp performs memory read or i/o read operation, this pin goes
low to indicate read operation. This pin is always used along with M/IO to indicate memory
read or I/O read.

READY (Pin 22):


This is an input acknowledgement signal from the slow peripheral devices to the
microprocessor to indicate that data transfer is complete.
Eg. Printer connected to microprocessor.
The main use of this pin is to insert wait states into the timing of the microprocessor.
If the READY pin is at logic 0 level, the µp enters wait states into its timing cycle and remains
idle.
The need for this is because the speed of the µp is very fast, it operates in MHz and speed of
peripheral device like printer is slow as compared to that of the µp. Hence µp sends data at a
faster rate continuously to the peripheral, which may cause loss of data, hence the peripheral
sends a low signal on the READY input to send the data at a slower rate by adding wait states
to its timing cycle.
Once one slot of data is received by the peripheral then it sends an acknowledgement high
signal on its input to indicate that data transfer is complete and send the next slot of data.

CLK (Pin 19) – Clock:


Clock pin provides the basic timing signal to the microprocessor.

19 | U b a i d S a u d a g a r
Vcc (Pin 40) ; GND (Pin 1 and 20):
Vcc i.e. +5V is applied at pin 40 and is used as power supply
There are two layers of 8086, hence two separate GND pins for two layers of 8086 chip are
used. This helps in improve noise rejection.

RESET (Pin 21):


It causes the processor to immediately terminate its present activity.
The 8284-clock generator provides this signal
This signal must be active high for at least 4 clock cycles
It clears all the flag register, the instruction queue, DS,SS, ES and IP register and sets the bits
of the CS register
Hence whenever the processor boots the reset vector address of 8086 is FFFF0H (as CS =
FFFFH and IP = 0000H)
Note: The reset vector is the default location a central processing unit will go to find the first
instruction it will execute after a reset. That is to say, the reset vector is a pointer or address
where the CPU should always begin as soon as it is able to execute instructions.

ALE (Pin 25):


ALE stands for Address Latch Enable.
As we studied in the previous slides about latch. Latch has an input pin STB which is connected
to the o/p pin of the µp i.e. ALE
As the name suggests, it is used to enable the latch by generating a high signal during T1 state
of every machine cycle.

20 | U b a i d S a u d a g a r
As soon as ALE pin goes high, latches gets enabled, address which is available on the address
bus goes to the input of the latches where it is latched (it holds the address at its o/p).
Now ALE goes low after T1 state gets over, hence disabling the latches (as STB also goes low).
Now data is available on the multiplexed A/D bus. Since latches are disabled hence data which
arrives at latches get no response.

TEST (Pin 23):


It is an active low input line dedicated for 8087 math co – processor (numeric processor).
When this pin is high in the maximum mode, it indicates to the microprocessor that it is busy.
Whenever those instructions arrive in the program which are dedicated to the co – processor
then they are sent to the co – processor for execution.
While the co – processor is busy executing those instructions it sends a high signal on the TEST
pin of the µp indicating that it is busy executing those instructions, till then the µp remains in
idle state.

MN/MX (Pin 33):


This is an input pin to the µp which will indicate the processor will work in which mode.
If input to this pin is high i.e. if the pin is connected to Vcc (+5V) then µp will enter in minimum
mode
If input to this pin is low i.e. if the pin is connected to GND (0V) then µp will enter in maximum
mode

BHE/𝑺𝟕 (Pin 34):


𝑆7 is used for future development.
BHE is active low o/p signal and stands for Bus High Enable.
This o/p pin is used to activate the odd memory bank, when the o/p of this pin is low it activates
the odd memory bank and data is available on the higher order data bus (D8 – D15)
If this pin goes high then odd memory bank gets disabled and any type of data transfer from
the higher order data bus is disabled.

WR – Write (Pin 29):


This is an o/p pin is used in minimum mode, i.e. MN/MX = 1. It is connected to the memory
or I/O device. When this pin goes low it indicates write operation.

21 | U b a i d S a u d a g a r
It is used along with M/IO pin to indicate whether memory write or io write operation.

M/IO (Pin 28):


This is an o/p pin used in minimum mode, to distinguish between a memory operation and io
operation. When this pin is low it indicates io operation and is used with RD and WR.
For Eg. M/IO is high, and RD is also low whereas WR is high, then memory read operation
takes place. For Eg. M/IO is high, and WR is also low whereas RD is high, then memory write
operation takes place. In similar manner IO read and IO write operation takes place, only M/IO
is low.
HOLD (Pin 31):
It is input pin used in the minimum mode. The i/p comes from the DMA controller which is
acting another bus master (as it also has data and address lines) requesting the µp to release the
system bus. In response 8086 completes the current machine cycle and releases the system bus.
In other words it tristates its external system bus.

HLDA (Pin 30):


It is acknowledged signal generated by the µp to the DMA controller after releasing the system
bus, indicating that it has released the system bus. After which DMA operation takes place.

DEN (Pin 26) – Data Enable:


This pin is used to activate the octal bus Transreciever IC 8286. It is connected to the OE pin
of the Transreciever which consist of 8 bidirectional buffers. Actually, when DEN goes low
OE goes low and all the internal buffers get activated.

22 | U b a i d S a u d a g a r
DT/R (Pin 27) – Data transmit/ Receive:

This pin is used to decide the direction of data flow i.e. either data will flow from the µp to
memory or io device or data will flow from the memory or io device to the µp. When this pin
goes high i.e. when DT/R = 1, which indicates data transmit from µp to memory or io device.
In other words, whenever write operation takes place, DT/R goes high, hence flow of data is
from the µp. When this pin goes low i.e. when DT/R = 0, which indicates receive i.e. µp
receives data from memory or io device. In other words, whenever read operation takes place,
DT/R goes low, hence flow of data is towards the µp.

𝑺𝟐 , 𝑺𝟏 , 𝑺𝟎 (Pin 28 – 26) – Status lines:


These are the active low status lines used in maximum mode which reflect the type of operation
being carried out by the processor. It indicates which machine cycle is currently in operation.
Refer table below:

23 | U b a i d S a u d a g a r
LOCK (Pin 29):
This pin is used in the maximum mode. When a programmer is executing some important
instructions during which no interrupt can be tolerated (such as when we restart our computer
and there are some updates which the windows is taking at that time if any other command is
given to the PC it will not react to it) at that time what the programmer does it writes a LOCK
prefix alongside the instruction, due to which this pin goes low indicating to the external bus
masters such as DMA that it cannot take control over the system bus.
When the next instruction arrives without the LOCK prefix then this pin goes high and allows
other bus masters to access gain over the system bus.

𝑸𝑺𝟏 , 𝑸𝑺𝟎 (Pin 24 – 25):


QS stands for queue status. It means that outside the chip we come to know what is the status
of the queue register inside the microprocessor. Let’s understand from the table below:

𝑹𝑸𝟎 /𝑮𝑻𝟎 , 𝑹𝑸𝟏 /𝑮𝑻𝟏 :


This signal works in the maximum mode when more than one processor is connected in the
system. RQ stands for bus request. GT stands for bus grant.

24 | U b a i d S a u d a g a r
0 has the higher priority than 1. It means that when two processors simultaneously asks the
8086 to release the system bus, the processor which is connected at pin 31 will get bus grant
first then the one which is connected at pin 30. The external processor will send a low signal as
a request to the µp to release the system bus, the µp will complete its current machine cycle
and release the bus i.e. it will grant the bus to the bus master by sending a low signal.
After using the system bus again, the external bus master will send a low signal thereby
releasing back the system bus to the µp.

25 | U b a i d S a u d a g a r
CHAPTER 2: THE ART OF ASSEMBLY LANGUAGE
PROGRAMMING

2.1 PROGRAM DEVELOPMENT STEPS:

DEFINING THE PROBLEM:

The first step in writing program is to think very carefully about the problem that you want the
program to solve.

ALGORITHM:

The sequence which your program should follow can be specified as a series of steps or in other
words sequence of operation.

FLOWCHART:

The flowchart is a graphical representation of the program operation or task. Each specific
operation is specified by different symbols.

INITIALIZATION CHECKLIST:

There may be many variables and segment registers which need to be defined in the program,
but before defining they need to be initialized.

26 | U b a i d S a u d a g a r
The best way to approach the initialization task is to make the checklist of entire variables and
registers which are going to be used in the program.

CHOOSING INSTRUCTIONS:

Proper instructions which are required in the program are chosen. Which means that entire
instruction set must be known.

CONVERTING ALGORITHMS TO ASSEMBLY LANGUAGE PROGRAMS:

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 obtained after
execution.
First we declare the data in the data segment, then we write the actual program in the code
segment.

2.2 ASSEMBLY LANGUAGE PROGRAM DEVELOPMENT TOOLS:

EDITOR:
It is used to type the assembly language statements for the program and save in a file.
The file containing the text of assembly language statements is called as source file and has
the extension .asm

ASSEMBLER:
Assembler is a software which reads the text from the source file, translates into respective
binary codes and saves into two files, one with the extension .obj called as object file and one
with the extension .lst called as list file.
The object file contains binary codes and addresses of the instructions.
The list file contains assembly language statements, binary codes, and offset address for each
instruction.
Assembler indicates syntax errors if any in the source file.

LINKER:
Large assembly language programs are usually divided into many small modules.

27 | U b a i d S a u d a g a r
The code for each module are separately developed, tested and finally linked into a single
large executable program. The linker is used to join many object files into a single large
object file. Linker produces files with the extension .exe which can be directly loaded in the
memory and executed.

DEBUGGER:

It enables the program to be loaded into the system memory, execute it and then debug it. It
also enables to insert breakpoints at any desired locations, change the content of register,
memory location and rerun the program.

2.3 ASSEMBLER DIRECTIVES

Assembly language contains two types of statements


- Instructions
- Directives
Instructions are translated into machine code by the assembler which the processor understands
by decoding them. In other words, instructions, instruct the processor to do a particular task.
Directives are instructions to the assembler which help the assembler in the assembly process
and these directives are not translated into machine code.
Or
Statements that direct the assembler to do some special task

DB: DEFINE BYTE

The DB directive is used to reserve a byte or bytes of memory location in the available memory.
Eg: array DB 20h ; {assembler reserves 1 byte of memory for the variable named sum and
initializes its value to 20}

DW: DEFINE WORD

The DW directive is used to reserve a word or words of memory location in the available
memory.
Eg: value DW 1234h ;{this makes the assembler reserve a word in memory}

28 | U b a i d S a u d a g a r
DQ: DEFINE QUAD WORD

This directive is used to direct the assembler to reserve four words (8 bytes) of memory for the
specified variable.
Eg: value DQ 1234567812345678h ; {this makes the assembler reserve 4 words in memory}

DD: DEFINE DOUBLE WORD

This directive is used to direct the assembler to reserve two words (4 bytes) of memory for the
specified variable.
Eg: value DD 12345678h ; {this makes the assembler reserve 2 words in memory}

DT: DEFINE TEN BYTES

This directive directs the assembler to define the specified variable requiring 10 bytes for its
storage and initialize 10 bytes with the specified values.
Eg: value DT 123456781234567812h;{this makes the assembler reserve ten bytes in memory}

ASSUME: ASSUME LOGICAL SEGMENT NAME

The ASSUME directive is used to inform the assembler the names of the logical segments to
be assumed for different segments used in the program.
Eg: the statement ASSUME CS : CODE directs the assembler that the machine code(executable
program) are available in a segment named CODE and hence CS register is to be loaded with
the segment address for the label CODE, while loading.
Eg: the statement ASSUME DS : DATA indicates to the assembler that the data items related
to the program are available in the logical segment named DATA, and DS register is to be
initialized by the segment address for data segment while loading

SEGMENT:

The segment directive is used to indicate the start of a logical segment.


Eg: CODE SEGMENT statement indicates to the assembler the start of a logical segment
called CODE
Eg: DATA SEGMENT statement indicates to the assembler the start of a logical segment
called DATA

29 | U b a i d S a u d a g a r
ENDS: END OF SEGMENT

This directive marks the end of a logical segment. The logical segments are assigned with the
names using ASSUME directive. The names appear with the ENDS directive as prefix to mark
the end of that segment.
Eg: DATA SEGMENT
DATA ENDS

END: END OF PROGRAM

The END directive marks the end of an assembly language program. When the assembler
comes across the END directive, it ignores the source lines available later.

ORG (ORIGINATE) DIRECTIVE

This assembler directive is used to assign starting memory location of program.


If org is not given, then program code will be stored from the effective address 0000h by
default.
Eg: Org 1000h will start storing the program from the address 1000h.

EQU (EQUATE) DIRECTIVE

This directive tells the assembler to assign a value to the corresponding label.
This directive is used in general when we use some value, multiple times in a program. So when
we want to change the value. We need to go everywhere in the program where we entered the
value.
Instead of that what we will do is that we will use equ directive and assign that value to a label
through this directive as follows:
Count equ 100h
Now the above directive will assign value 100h to count label.
Now we use this count label many times in the main program. If we want to change its value,
we will just change the value above once and not go to every instruction like others.

PROC AND ENDP DIRECTIVE

The subprogram called as subroutine is also called as procedure.


The proc and endp directive are used to indicate the start and of the subprogram or procedure.
Eg. Add proc far/near

30 | U b a i d S a u d a g a r
The above statement identifies the start of a procedure named add and tells the assembler that
the procedure is far (different code segment)

ENDP

The above directive indicates the end of procedure.

EVEN DIRECTIVE

This assembler directive informs the assembler to increment the memory location counter to
the next even address if it is not already at the even address.
As we know that mp 8086 is a 16 bit processor, therefore it can access a word in one machine
cycle.
But the condition is that the word is located at an even address.
Eg:1005h is a word located at 2000h and 2001h
Which means that at:
2000h – 10h
2001h – 05h
Hence in such case we don’t require an even directive.
But if the word is located at 2001h and 2002h
Then it means that:
2001h – 10h
2002h – 05h
Now to access this word it will take 2 machine cycles, hence to reduce the access time we use
even directive

31 | U b a i d S a u d a g a r
CHAPTER 3: INSTRUCTION SET OF 8086 MICROPROCESSOR

3.1 ADDRESSING MODES:


The different addressing modes are as follows:
1. Implicit addressing mode
2. Register addressing mode
3. Immediate addressing mode
4. Direct addressing mode
5. Register Indirect addressing mode
6. Register relative addressing mode
7. Based indexed addressing mode
8. Relative based indexed addressing mode

1.1.1 IMMEDIATE ADDRESSING MODE


In this addressing mode the data (operand) is present in the instruction itself.
E.g. Mov bl, 02h
This instruction when executed will move immediate data 02h into the bl register
Assume bl = 9fh before execution
After execution bl = 02h

1.1.2 DIRECT ADDRESSING MODE


In this addressing mode, the data is present in the memory i.e. data segment, and the
effective address (offset address) where the data is present is given in the instruction.
E.g. Mov al, [1234h]
This instruction moves the data present at the effective address 1234h
Assume data at the address DS: 1234 = 78h; al = 90h
After execution al = 78h

3.1.3 REGISTER ADDRESSING MODE


In this addressing mode the data is present in any of the general-purpose registers.
The name of the register is mentioned in the instruction itself.
Eg: Mov al, bl
When this instruction is executed the contents of bl register is copied to al register

32 | U b a i d S a u d a g a r
If BL = 55h; AL = 00h
After instruction is executed
BL = 55h; AL = 55h

3.1.4 REGISTER INDIRECT ADDRESSING MODE


In this addressing mode the data is present in the memory location and the address of
the memory location is not given directly in the instruction but instead is stored in
some register pair or index register.
For this purpose, three registers are used viz. BX, SI, and DI
E.g. Mov al, [si]
It moves the contents at the memory location whose offset address is given by si
register
Assume al = 00h, si = 145Ah, now square brackets in the instruction indicates address
Let data at the location DS : 145AH = 34h
After execution al = 34h

3.1.5 REGISTER RELATIVE ADDRESSING MODE


In this addressing mode, data is present in the memory and to access this data we
require its address.
The address is not given directly in the instruction but instead is obtained by adding
the contents of the base register and displacement or index register and displacement.
Eg. Mov al, [si+08]
si = 1002h, displacement = 08, therefore effective address = 1002h + 08h = 100Ah
The above instruction will move the content at the memory location 100Ah which is
formed by adding si and displacement in al register.
Assume data at location DS : 100Ah = 56h
Therefore al = 56h after execution

3.1.6 BASED INDEXED ADDRESSING MODE


In this addressing mode the data is present in the memory whose address is not given
directly in the instruction but instead is calculated by adding the contents in the base
register and index register.
E.g. Mov al, [bx][si]
Let bx = 1087h, si = 2004h, si+bx = 308Bh = effective address or offset address

33 | U b a i d S a u d a g a r
This instruction will mov the data present at the memory location 308Bh into al
register. Let’s assume al = 09h before execution and address 308Bh contains 8Ch
data. After execution al = 8Ch

3.1.7 RELATIVE BASED INDEXED ADDRESSING MODE


In this addressing mode the data is present in the memory and to access this data, we
require its address which is not present directly in the instruction.
The address is calculated by adding the contents in the base register and index
register and displacement
E.g. Mov al, [si+01][bx]
Let si = 1000h, bx = 1200h, displacement = 1h, EA = 1000h+1200h+1h = 2201h
When the above instruction is executed the data at the location 2201h will be copied
in the al register.
Assume al = 23h before execution, data at the location 2201h = 6Fh
After execution al = 6Fh

3.1.8 MPLICIT ADDRESSING MODE


In the addressing mode the data is neither present in the instruction, nor any register,
nor at any memory location.
There are some instructions which work on some registers directly such addressing
scheme is called as implicit addressing mode.
E.g.
STI ; set interrupt flag
CLD ; clear direction flag
STD ; set direction flag

3.2 8086 INSTRUCTIONS

3.2.1 DATA TRANSFER INSTRUCTIONS


a. PUSH source, POP destination
PUSH:
• This instruction pushes the content of the specified register or memory location on to the
stack memory.
• The PUSH instruction decrements the stack pointer by 2 and copies a word (16 bit only)
into the stack segment which is pointed by the stack pointer.
• E.g. PUSH BX

34 | U b a i d S a u d a g a r
• The above instruction decrements the SP by 2 and copies the contents of the BX register
onto the stack memory
POP:
• This instruction removes the contents from the stack memory i.e. from the stack segment
and loads into the specified register or memory location
• E.g. POP CX
• The above instruction when executed, increments the stack pointer by 2 and removes a
word from the stack segment and loads in the CX register

b. XCHG D, S (Exchange the content of destination and source)


• This instruction exchanges the content of source and destination. The size of the source
and destination must be equal.
• The possible combinations of source and destinations are as follows:
• It is to be noted that immediate address is also not allowed
• Eg: XCHG cl, [0004h]
• Where 0004h is the offset address, but it is not allowed.

Destination Source

Register Register

Memory Register

c. XLAT
• The XLAT instruction is used to translate a byte from one code to another code. The
byte to be translated is put in the AL register.
• There is a lookup table where a number of bytes are present
E.g.
Data segment
Table db 00h,01h,04h,09h,16h,25h
a db 04
Data ends
Code segment
Mov al, a

35 | U b a i d S a u d a g a r
Lea bx, table
Xlat
Code ends

d. LEA R, EA(OA) (Load effective address of memory into given register R)


This instruction copies the effective address of memory into the given register R.
The register R should be of 16 bits, as EA or OA is always of 16 bits.

e. LDS R, Memory (Load DS and given register {16-bit register} with memory contents)
This instruction copies the contents of four memory locations in the given register and DS
register. The register R should be of 16 bits.
Note: immediate addressing is also not allowed over here

f. LES R, Memory (Load ES and given register {16-bit register} with memory contents)
This instruction copies the contents of four memory locations in the given register and ES
register. The register R should be of 16 bits.

36 | U b a i d S a u d a g a r
g. LAHF (Load AH register with 8 LSB’s of flag register)
This instruction copies the content of 8 LSB’s of flag register into AH register, so the old value
of flag register is not lost but the old content of AH register is lost.

h. SAHF (Store contents of AH register into 8 LSB’s of flag register)


This instruction copies the content of AH register into the 8 LSB’s of flag register, the content
of AH register does not change but the content in the flag register changes.

i. PUSHF and POPF


PUSHF:

37 | U b a i d S a u d a g a r
The above instruction copies the flag register contents i.e. 16 bits into the stack segment pointed
by the stack pointer.
So whenever the above instruction is executed the stack pointer decrements by 2 and the flag
register contents are stored on to the stack.
POPF:
The above instruction copies the contents (16 bits) from the stack segment pointed buy the
stack pointer into the flag register.
So, whenever the above instruction is executed the stack pointer is incremented by 2 and
contents on the stack memory are moved to the flag register.

3.2.2 ARITHMETIC INSTRUCTIONS


a. ADC D, S (Add destination and source data with carry)
This instruction adds the content of source and destination with carry and result is stored in the
same destination.
The size of the source and destination should be same.
The possible combinations are same as add instruction.
Eg: adc ax, bx
adc al, bl

b. Inc d (Increments the contents of destination)


This instruction increments the contents of destination by one and stores the result in the same
destination.
The destination may be a register or any memory location but not any immediate data.
Eg:
Inc ch
Inc si ; where si consist of offset address
c. Dec d (decrements the contents of the destination)
This instruction decrements the contents of the destination by one and stores the result in the
destination again. The destination may be a register or memory, but not an immediate data.
E.g.
Dec ch
Dec si
d. AAA (ASCII adjust after addition)

38 | U b a i d S a u d a g a r
Need for ASCII adjust instructions: numerical data coming into a computer from a terminal
is usually in ASCII code.
In this code numbers 0 to 9 are represented by the ASCII codes 30H to 39H.
The 8086 allows you to add two ASCII code for two decimal digits without masking off 3 in
the upper nibble of each.
After the addition, the AAA instruction is used to make sure that the result is in correct
unpacked BCD form.
Assume AL = 0011 0101 = ASCII 5, BL = 0011 1001 = ASCII 9
Add al, bl = 35h + 39h = 6eh which is incorrect bcd form
AAA when executed – AL = AL AND 0fh = 6eh AND 0fh = 0eh
Since lower nibble of AL > 9, AL = AL + 06 = 14h
AH = AH + 1 = 00h + 01h = 01h (note that AH should be zero to get correct result)
AL = AL AND 0FH = 14h AND 0fh = 04h
Therefore AH = 01h AL = 04h i.e. correct unpacked bcd result

e. AAS (ASCII adjust after subtraction)


Operation:
Sub al, bl ; let bl = 0011 1001 – 39h = ascii 9 ; let al = 0011 0101 – 35h = ascii 5
aas
If lower nibble of AL > 9 or AF = 1, then AL = AL – 06
AH = AH – 1
AF = CF =1
AL = AL AND 0F
This instruction corrects the result in AL register after subtracting two unpacked ASCII
operands. The result is unpacked BCD format.

f. AAM (ASCII adjust for multiplication)


This instruction can be used to convert the result of the multiplication of two valid unpacked
BCD numbers
This instruction must be used after multiplication instruction. The multiplication of two
unpacked BCD numbers is always less than 100, hence the result will be in AL and that number
is finally then converted into unpacked BCD form.

Eg:

39 | U b a i d S a u d a g a r
If AL = 06 (ASCII value is 30), BL=08 (ASCII value is 38)
MUL BL ; [AX = 30H {48 decimal}]
AAM ;[AH = 04, AL = 08]

g. AAD (ASCII adjust before division)


This instruction is used before division instruction.
Eg:
If AX = 0205 and BL = 07
AAD ; after execution AX = 0019 (25 decimal)
DIV BL ; content of AX is divided by BL, after execution AL = 03 (quotient), AH = 04
(Remainder)

h. DAS (Decimal adjust AL after subtraction)


This instruction is used to adjust the result and get the final result in packed bcd form.
Operation performed:
To subtract two 8 bit numbers and obtain the result in AL register.
Then execute DAS instruction
This instruction will check lower nibble of AL register (if > 9 or AC = 1, then it will subtract
06h from AL register)
Also it will check higher nibble of AL register (if > 9 or CF = 1, then it will subtract 60h from
AL register)
If both the nibbles are greater than 9 then AL = AL - 66h
Eg: SUB AL, BL
DAS

i. DAA (decimal adjust AL after addition)


This instruction is used to adjust the result and get the final result in packed bcd form.
Operation performed:
To add two 8 bit numbers and obtain the result in AL register.
Then execute DAA instruction
This instruction will check lower nibble of AL register (if > 9 or AC = 1, then it will add 06h
to AL register)
Also it will check higher nibble of AL register (if > 9 or CF = 1, then it will add 60h to AL
register)
If both the nibbles are greater than 9 then AL = AL + 66h

40 | U b a i d S a u d a g a r
Eg: ADD AL, BL
DAA

j. Mul s (8/16 bits) (unsigned multiplication of source data and accumulator)


This instruction is used multiply two 8/16 bit numbers and store the result in AX if result
is 16 bit or DX-AX in case result is 32 bits
The source may be a memory or register, but not an immediate data.
Eg.
Mul bl
This instruction will multiply content in al (by default) and bl and store the result in ax
register.
Mul bx
This instruction will multiply content in ax (by default) and bx and store the result in dx-ax
register.

k. IMUL S (8/16 bits) (integer multiplication of source data with accumulator)


This instruction is used for signed multiplication which multiplies source operand with
the contents of accumulator and the result is stored in AX if result is 16 bits.
It is stored in DX-AX register if result is 32 bits.

l. DIV S (Unsigned Division)


This instruction is used for division operation.
To understand this instruction let’s take some examples.
Eg1.
DIV BL
The above instruction will divide AX/BL by default (where AX is dividend and BL is
divisor)
In other words, whenever the source operand is a byte, the dividend is a word i.e. AX
register
After division we get a quotient and a remainder, quotient is stored in AL register and
remainder is stored in AH register by default
Eg2.
DIV BX
The above instruction will divide DX-AX/BX by default (where DX-AX is dividend and
BX is divisor)

41 | U b a i d S a u d a g a r
m. IDIV S (Signed Division)
This instruction performs the same operation as DIV instruction but with signed operands.

n. Cbw (convert sign byte into sign word)


This instruction converts the sign byte present in register AL into sign word and stores the
result in register AX.
Eg.
Content of AL = 94h
So after cbw instruction is executed, the result in AX = FF94H
Note: FF is indicating the number is negative

o. Cwd (convert sign word into sign double word)


This instruction converts the sign word present in the ax register into sign double word and
stores the result in register DX – AX
Eg.
Let AX = 1004H
Therefore, after execution of cwd instruction
The result in the DX – AX register after execution = 0000H - 1004 H
Note that 0000h here indicates the number is positive

p. Neg d (negate the contents of destination)


This instruction finds the 2’s complement of the contents of the destination and stores the
result in the same destination.
The destination may be a register or memory but not an immediate data
Eg.
Neg ah
Neg cx
Neg si ; si register contains the memory address

3.2.3 LOGICAL INSTRUCTIONS


a. SHR and SHL
This instruction is used to shift the data bits either right or left.
If its SHR, then the bits are shifted right depending upon the shifting factor
Eg. SHR al,01
Let AL = 14H = 0001 0100

42 | U b a i d S a u d a g a r
After execution AL = 0AH
Here we see when the bits are shifted towards right, the MSB bits are filled with zero.
If its SHL, then the bits are shifted left depending upon the shifting factor
Eg. SHL al,02
Let AL = 14H = 0001 0100
After execution AL = 50H = 0101 0000
Here we see when the bits are shifted towards left, the LSB bits are filled with zero.

b. ROR and ROL


This instruction rotates the data bits in a register from MSB to LSB or LSB to MSB.
When ROR is executed, all the bits shift towards right and the LSB bit gets shifted out and
rotates back to MSB bit of the same register as well as a copy is send to the carry flag.
Eg. ROR AL,01
Let AL = 45H = 0100 0101
After execution AL = A2H = 1010 0010 and C = 1
When ROL is executed, all the bits shift towards left and the MSB bit gets shifted out and
rotates back to LSB bit of the same register as well as a copy is send to the carry flag.
Eg. ROL AL,01
Let AL = A5H = 1010 0101
After execution AL = 4BH = 0100 1011 and C = 1

c. RCR and RCL


This instruction rotates the data bits in a register from MSB to LSB or LSB to MSB.
When RCR is executed, all the bits shift towards right and the LSB bit gets shifted out and
goes to carry flag, the bit which previously stored in the carry flag is placed in the MSB bit
of the same register.
Let AL = 45H = 0100 0101
Eg. RCR AL,01 ;C=0
After execution AL = 22H = 0010 0010 and C = 1
When RCL is executed, all the bits shift towards left and the MSB bit gets shifted out and
goes to carry flag, the bit which previously stored in the carry flag is placed in the LSB bit
of the same register.
Let AL = A5H = 1010 0101
Eg. ROL AL,01 ;C=0

43 | U b a i d S a u d a g a r
After execution AL = 4AH = 0100 1010 and C = 1

d. AND destination, source


Eg AND AL, BL
The above instruction will perform logical AND operation between the registers of AX and
BX and the result in AX register.
Eg AX = 1000 0110, BX = 1110 0101
1001 1110 AND 1110 0101 = 1000 0110, this result goes in AX register

e. TEST D, S (AND destination and source data)


This instruction is used to AND the destination content and source content and the result is
copied in the ALU, so the old value of destination is not lost.

f. OR destination, source
Eg: OR AX, 0098H
Let, AX = 3F0FH
0000 0000 1001 1000 OR 0011 1111 0000 1111 = 0011 1111 1001 1111, this value gets
stored in AX register.

g. NOT destination
Eg: NOT AX ; if the content of AX = 200FH = 0010 0000 0000 1111
After the instruction gets executed, the result is = 1101 1111 1111 0000 which is stored in
AX register.

h. LOOP
assume cs: code

44 | U b a i d S a u d a g a r
code segment
start:
mov al,10h
mov bl,10h
mov cl,05
up: add al, bl
loop up
int 3
code ends
end start
Observation:
CL = 05

1. AL+BL = 20 CL = 04
2. AL+BL = 30 CL = 03
3. AL+BL = 40 CL = 02
4. AL+BL = 50 CL = 01
5. AL+BL = 60 CL = 00

3.2.4 STRING INSTRUCTIONS


a. REP
This is an instruction prefix, which can be used in string instructions.
It can be used with string instructions only.
It causes the instruction to be repeated CX no. of times.
After each execution the SI and DI registers are incremented or decremented by 1.
It is important that before we use the REP instruction prefix the following steps must be
carried out:
3 CX must be initialized to count value.
4 If auto incrementing is required, DF must be set using STD instruction else cleared using
CLD instruction.
b. MOVSB/MOVSW {move string}
It is used to transfer a word/byte from data segment to extra segment
The offset of the source in the data segment is in SI
The offset of the destination in extra segment is in DI
Eg: MOVSB ; {ES:[DI] DS:[SI]…..byte transfer}

45 | U b a i d S a u d a g a r
Eg: MOVSW ; {ES:[DI], ES:[DI+1]  DS:[SI],DS:[SI+1]}

c. LODSB/LODSW {Load string}


It is used to load AL(or AX) register with a byte(or word) from data segment.
The offset of the source in data segment is in SI
Eg: LODSB ;{AL DS:[SI]….byte transfer}
Eg: LODSW ;{AL DS:[SI]; AH DS:[SI+1]}

d. STOSB/STOSW {Store string}


It is used to store AL(or AX) in the extra segment.
The offset of the destination in extra segment is in DI
Eg: STOSB ; {ES:[DI] AL ….byte transfer}
Eg: STOSW ; {ES:[DI] AL; ES:[DI+1] AH}

e. CPMSB/CPMSW {Compare string}


It is used to compare a byte(or word) in the data segment with a byte or a word in the extra
segment
The offset of the byte(or word) in extra segment is in DI, the offset of the byte(or word) in
data segment is in SI
Comparison is done by subtracting the byte(or word) in extra segment from the byte(or
word) in data segment
Flags are affected but result is not stored anywhere
Eg: CMPSB ;{compare DS[SI]with ES:[DI]….byte operation}
Eg: CMPSW ;{compare DS:[SI],DS:[SI+1] and ES:[DI],ES:[DI+1]}

3.2.5 FLAG CONTROLLED INSTRUCTIONS


• STC – Set carry flag
• STD – Set direction flag
• When direction flag sets, it sets the SI and DI flag in auto decrementing mode
• STI – Set interrupt flag
• When interrupt flag is set, the interrupts appearing on the INTR pin are enabled
• CLC – Clear carry flag
• CLD – Clear direction flag
• When direction flag clears, it sets the SI and DI flag in auto incrementing mode

46 | U b a i d S a u d a g a r
• CLI – Clear interrupt flag
• When interrupt flag is cleared, the interrupts appearing on the INTR pin are disabled
• CMC – Complement the carry flag

3.2.6 BRANCH INSTRUCTIONS


a. Call operand (unconditional call)
When the microprocessor executes the CALL instruction, then the microprocessor will
branch from the main program to the subprogram (procedure (subroutine)).
If the main program and the sub program are present in the same code memory segment,
then it is called as intra segment branching in which only the IP is changed i.e. the offset
address changes, but base address remains same.
But if the main program and the sub program are present in different code memory
segments, then it is called as inter segment branching in which the contents of CS and IP
are changed i.e. both base and offset address are changed.
There are two types of call i.e. Near CALL and Far CALL.
Near CALL is associated with intra segment branching and Far CALL is associated with
Inter segment branching.
When the CALL instruction is executed, the control gets transferred to the sub program.
When the control gets transferred to the sub program the contents of the Instruction Pointer
(IP) are changed only during Near CALL
When the control gets transferred to the sub program the contents of the Code Segment
register (CS) and Instruction Pointer (IP) are changed only during Far CALL
When the control gets transferred to the sub program, the sub program gets executed and at
the end of the program there exist a RET instruction which returns the control to the main
program
Near CALL
Main program and Sub program = same code segment
When control transfers to sub program, next instruction’s address (offset present in IP) are
saved on the stack and CS contents remain unchanged
The control gets transferred to subprogram by loading starting address of sub program in
IP register

47 | U b a i d S a u d a g a r
At the end of subprogram RET instruction gets executed due to which the address of the
next instruction in the main program is loaded from the stack into the IP register and
execution of main program continues.
Far CALL
Main program and Sub program = different code segment
When control transfers to sub program, next instruction’s address (offset present in IP and
base address present in CS) are saved on the stack.
The control gets transferred to subprogram by loading base and offset of sub program in
CS and IP register. At the end of subprogram RET instruction gets executed due to which
the base and the offset address of the next instruction in the main program is loaded from
the stack into the CS and IP register and execution of main program continues.

b. JMP (Unconditional Jump) Label


This instruction unconditionally transfers the control of execution to the specified address.

c. JC/JB Label
This instruction jumps to the specified label when the carry flag is in the set state.

d. JNC/JNB Label
This instruction jumps to the specified label when the carry flag is in the reset state.

e. JS Label
This instruction jumps to the specified label when the Sign flag is in the set state.

f. JNS Label
This instruction jumps to the specified label when the sign flag is in the reset state.

48 | U b a i d S a u d a g a r
g. JZ/JE Label
This instruction jumps to the specified label when the zero flag is in the set state.

h. JNZ/JNE Label
This instruction jumps to the specified label when the zero flag is in the reset state.

i. JP Label
This instruction jumps to the specified label when the parity flag is in the set state.

j. JNP Label
This instruction jumps to the specified label when the parity flag is in the reset state.

49 | U b a i d S a u d a g a r
CHAPTER 4: ASSEMBLY LANGUAGE PROGRAMMING

4.1 MODEL OF 8086 ASSEMBLY LANGUAGE PROGRAM

4.1.1 INSTRUCTION FORMAT:

The assembly language program is divided into fields, separated by spaces and tabs. A format
for a typical line from assembly language program can be given as.
Label: Mnemonic Operand1, Operand2 ; Comment
The first field, which is optional, is the label field, used to specify symbolic labels. A label is
an identifier that is assigned to the address of the first byte of the instruction in which it appears.
The second field is mnemonic, which is compulsory. All instructions must contain a mnemonic.
The third and following fields are operands. The presence of the operands depends on the
instruction. Some instructions have no operands, some have one, and some have two. If there
are two operands, they are separated by a comma.
The last field is a comment field. It begins with a semicolon and continues to the end of the
line. The comments are for our benefits, they tell us what the program is trying to accomplish.

4.1.2 ASSEMBLER DIRECTIVES:

There are some instructions in the assembly language program which are not a part of the
processor instruction set. These instructions are instructions to the assembler, linker, and loader.
These assembler directives to not generate any executable machine code.

4.2 PROGRAMMING USING ASSEMBLER:

1. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO ADD TWO 8 BIT


NUMBERS

Program:
assume cs : code
assume ds : data

data segment
a db 25h
b db 34h
sum db ?
data ends

code segment

50 | U b a i d S a u d a g a r
start:
mov ax,data
mov ds,ax
mov ax,0000h

mov al,a
mov bl,b
add al,bl
mov sum,al

int 3
code ends
end start

Observation:

2. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO ADD TWO 16 BIT


NUMBERS

Program:

assume cs : code
assume ds : data

data segment
a dw 1191h
b dw 2272h
sum dw ?
data ends

code segment
start:

51 | U b a i d S a u d a g a r
mov ax,data
mov ds,ax
mov ax,0000h

mov ax,a
mov bx,b
add ax,bx
mov sum,ax

int 3
code ends
end start
Observation:

3. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO SUBTRACT TWO


8 BIT NUMBERS

Program:
assume cs : code
assume ds : data

data segment
a db 37h
b db 20h
dif db ?
data ends

code segment
start:
mov ax,data
mov ds,ax

52 | U b a i d S a u d a g a r
mov ax,0000h

mov bl,a
mov al,b
sub bl,al
mov dif,bl

int 3
code ends
end start
Observation:

4. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO SUBTRACT TWO


16 BIT NUMBERS

Program:
assume cs : code
assume ds : data

data segment
a dw 3400h
b dw 2500h
dif dw ?
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

53 | U b a i d S a u d a g a r
mov bx,a
mov ax,b
sub bx,ax
mov dif,bx

int 3
code ends
end start
Observation:

5. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO FIND THE SUM OF


SERIES OF N HEX NUMBERS.

Program:
assume cs : code
assume ds : data

data segment
series db 10h,20h,40h,90h,87h
res dw ?
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

lea si,series
mov al,[si]
mov cl,04

up: inc si
mov bl,[si]

54 | U b a i d S a u d a g a r
add al,bl
jc down

up1: dec cl
jnz up
jmp exit

down: inc ah
jmp up1

exit: mov res,ax


int 3
code ends
end start
Observation:

6. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO FIND THE SUM OF


SERIES OF BCD NUMBERS

assume cs:code
assume ds:data

data segment
array db 40h,50h,70h,90h,80h
result dw ?
data ends

code segment
start:

55 | U b a i d S a u d a g a r
mov ax,data
mov ds,ax
mov ax,0000h

mov cl,04
lea si,array
mov al,[si]
up:
inc si
mov bl,[si]
add al,bl
daa
jc down
up1:
dec cl
jnz up
jmp exit
down:
inc ah
jmp up1

exit:
mov result,ax
int 3
code ends
end start

56 | U b a i d S a u d a g a r
Observation

7. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO MULTIPLY TWO 8


BIT UNSIGNED NUMBERS

Program:
assume cs : code
assume ds : data

data segment
a db 25h
b db 40h
result dw ?
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

mov al,a
mov bl,b
mul bl
mov result,ax

int 3
code ends
end start

57 | U b a i d S a u d a g a r
Observation:

8. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO MULTIPLY TWO 16


BIT UNSIGNED NUMBERS

Program:

assume cs : code
assume ds : data

data segment
a dw 2525h
b dw 4040h
result dd ?
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

mov ax,a
mov bx,b
mul bx
lea si,result
mov [si],dh
inc si
mov [si],dl
inc si
mov [si],ah
inc si

58 | U b a i d S a u d a g a r
mov [si],al

int 3
code ends
end start
Observation:

9. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO MULTIPLY TWO 8


BIT SIGNED NUMBERS

Program:
assume cs : code
assume ds : data

data segment
a db 15h
b db -18h
result dw ?
data ends

code segment
start:
mov ax, data
mov ds, ax
mov ax,0000h

mov al,a
mov bl,b
imul bl
mov result, ax
int 3

59 | U b a i d S a u d a g a r
code ends
end start
Observation:

10. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO MULTIPLY TWO


16 BIT SIGNED NUMBERS

Program:

assume cs : code
assume ds : data

data segment
a dw 1000h
b dw -2000h
result dd ?
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

mov ax,a
mov bx,b
imul bx
lea si,result
mov [si],dh
inc si
mov [si],dl
inc si

60 | U b a i d S a u d a g a r
mov [si],ah
inc si
mov [si],al

int 3
code ends
end start
Observation:

11. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO DIVIDE TWO


UNSIGNED NUMBERS (8BITS/8BITS)

Program:
assume cs : code
assume ds : data

data segment
a db 64h
b db 31h
quo db ?
rem db ?
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h
mov al,a
mov bl,b
div bl

61 | U b a i d S a u d a g a r
mov quo,al
mov rem,ah

int 3
code ends
end start
Observation:

12. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO DIVIDE TWO


UNSIGNED NUMBERS (16BITS/8BITS)

Program:
assume cs : code
assume ds : data

data segment
a dw 1200h
b db 89h
quo db ?
rem db ?
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h
mov ax,a
mov bl,b
div bl
mov quo,al

62 | U b a i d S a u d a g a r
mov rem,ah

int 3
code ends
end start
Observation:

13. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO DIVIDE TWO


UNSIGNED NUMBERS (16BITS/16BITS)

Program:
assume cs : code
assume ds : data

data segment
a dw 1200h
b dw 1678h
quo dw ?
rem dw ?
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h
mov dx,0000h

mov ax,a
mov bx,b
div bx
mov quo,ax

63 | U b a i d S a u d a g a r
mov rem, dx

int 3
code ends
end start
Observation:

14. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO DIVIDE TWO


UNSIGNED NUMBERS (32BITS/16BITS)

Program:

assume cs : code
assume ds : data

data segment
a dd 10000000h
b dw 9000h
quo dw ?
rem dw ?
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

lea si,a
mov al,[si]
mov ah,[si+1]
mov dl, [si+2]

64 | U b a i d S a u d a g a r
mov dh, [si+3]
mov bx,b
div bx
mov quo, ax
mov rem, dx
int 3
code ends
end start
Observation:

Note: For signed division only instruction will change from div to idiv and if either dividend
or divisor is negative, quotient will also be negative and remainder will be positive. If both
dividend and divisor are negative then remainder and quotient both are positive.

15. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO FIND THE


SMALLEST NUMBER FROM A GIVEN ARRAY OF N NUMBERS

Program:

assume cs : code
assume ds : data

data segment
array db 09,10,8,12
smallest db ?
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

65 | U b a i d S a u d a g a r
lea si,array
mov al,[si]
mov cl,03

up: inc si
mov bl, [si]
cmp al, bl
jc down
mov al, bl

down: dec cl
jnz up
mov smallest,al

int 3
code ends
end start
Observation:

16. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO FIND THE


LARGEST NUMBER FROM ARRAY OF N NUMBERS.

Program:

assume cs : code
assume ds : data

data segment
array db 09,10,11,12
largest db ?
data ends

code segment
66 | U b a i d S a u d a g a r
start:
mov ax,data
mov ds,ax
mov ax,0000h

lea si,array
mov al,[si]
mov cl,03

up: inc si
mov bl, [si]
cmp al, bl
jnc down
mov al,[si]

down: dec cl
jnz up
mov largest,al

int 3
code ends
end start
Observation:

17. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO ARRANGE N


NUMBERS IN AN ARRAY IN ASCENDING ORDER

Program:

assume cs : code
assume ds : data

67 | U b a i d S a u d a g a r
data segment
array db 10h,15h,9h,7h,4h
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

mov cl,04
up2:
mov ch,04
lea si,array
up1:
mov al,[si]
inc si
cmp al,[si]

jc down
xchg [si],al
dec si
xchg [si],al
inc si
down:
dec ch
jnz up1
dec cl
jnz up2

int 3
code ends
end start

Observation:

68 | U b a i d S a u d a g a r
18. WRITE AN ASSEMBLY LANGUAGE PROGRAMMING TO ARRANGE
AN ARRAY OF N NUMBERS IN DESCENDING ORDER

assume cs : code
assume ds : data

data segment
array db 1h,5h,9h,7h,4h
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

mov cl,04
up2: mov ch,04
lea si,array
up1: mov al,[si]
inc si
cmp al,[si]

jnc down
mov dl,[si]
mov [si],al
dec si
mov [si],dl
inc si

down: dec ch
jnz up1

dec cl

69 | U b a i d S a u d a g a r
jnz up2

int 3
code ends
end start
Observation:

19. WRITE A PROGRAM IN ASSEMBLY LANGUAGE TO PERFORM


BLOCK TRANSFER OF DATA WITHOUT USING STRING
INSTRUCTIONS

Program:
assume cs : code
assume ds : data

data segment

block1 db 10h,20h,30h,40h,50h
block2 db 5 dup(0)
data ends

code segment
start:

70 | U b a i d S a u d a g a r
mov ax,data
mov ds,ax
mov ax,0000h

lea si,block1
lea di,block2
mov cl,5

up:
mov al,[si]
mov [di],al
inc si
inc di
dec cl
jnz up

int 3
code ends
end start
Observation:

71 | U b a i d S a u d a g a r
20. WRITE A PROGRAM IN ASSEMBLY LANGUAGE TO PERFORM
BLOCK TRANSFER OF DATA USING STRING INSTRUCTIONS

Program:
assume cs : code
assume ds : data

data segment
block1 db 10h,20h,30h,40h,01010000b
data ends

extra segment
block2 db 5 dup(?)
extra ends

code segment
start:
mov ax,data
mov bx,extra
mov ds,ax
mov es,bx
mov ax,0000h

lea si,block1
lea di, block2
cld
mov cl, 05h
repz movsb
int 3
code ends
end start
Observation:

72 | U b a i d S a u d a g a r
21. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO COMPARE TWO
STRINGS WITHOUT USING STRING INSTRUCTIONS AND DISPLAY
THE RESULT ACCORDINGLY

Program:
assume cs : code
assume ds : data

data segment
string1 db "abcde$"
string2 db "abcde$"
msg1 db "Both the strings are same$"
msg2 db "Both the strings are not same$"
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

lea si,string1
lea di,string2

up: mov al,[si]


cmp al,[di]
jne down
inc si
inc di
cmp al,'$'
jne up
lea dx,msg1
mov ah,09
int 21h
jmp exit

down: lea dx,msg2


mov ah,09
int 21h

exit: mov ah,4ch


int 21h
code ends
end start

73 | U b a i d S a u d a g a r
Observation:

22. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO COMPARE TWO


STRINGS USING STRING INSTRUCTIONS AND DISPLAY THE
RESULT ACCORDINGLY.

Program:
assume cs : code
assume ds : data
assume es : extra

data segment
string1 db "vbcde$"
msg1 db "Both the strings are same$"
msg2 db "Both the strings are not same$"
data ends

extra segment
string2 db "vwxyz$"
extra ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,extra
mov es,ax
mov ax,0000h

mov cl,5

74 | U b a i d S a u d a g a r
cld
lea si,string1
lea di,string2
repz cmpsb
jz msg
lea dx,msg2
mov ah,09
int 21h
jmp exit
msg: lea dx,msg1
mov ah,09
int 21h
exit:
mov ah,4ch
int 21h
code ends
end start
Observation:

23. WRITE A PROGRAM IN ASSEMBLY LANGUAGE TO CALCULATE


THE LENGTH OF THE STRING

Program:
assume cs : code
assume ds : data

data segment
string db "xyz$"
strlength db ?
data ends

75 | U b a i d S a u d a g a r
code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

lea si,string
mov cl,00h
up: mov al,[si]
cmp al,'$'
je down
inc si
inc cl
jmp up

down: mov strlength, cl


int 3
code ends
end start
Observation:

24. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO DISPLAY STRING


IN REVERSE ORDER

Program:
assume cs : code
assume ds : data

data segment
string db “MAHARASHTRA$"
stringreverse db 100 dup(?)
msg db "The reverse of the string is: $"

76 | U b a i d S a u d a g a r
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h
lea si,string
up:
mov al,[si]
cmp al,'$'
je down
inc si
jmp up

down:
lea di,stringreverse
mov cx,si
up1:
dec si
mov al,[si]
mov [di],al
inc di
dec cx
jnz up1
mov al,'$'
mov [di],al

lea dx,msg
mov ah,09
int 21h

lea dx,stringreverse
mov ah,09
int 21h

mov ah,4ch
int 21h

code ends
end start

77 | U b a i d S a u d a g a r
Observation:

25. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO CONCATENATE


TWO STRINGS (TWO WORDS)

Program:
assume cs : code
assume ds : data

data segment
msg1 db "The two strings are :$"
string1 db 10,13, "If you don't study $"
string2 db 10,13, "You will fail in MAP $"
msg2 db 10,13, "After concatenation the strings are as follows :$"
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h ;a general msg is printed to show which are the two strings

lea dx,msg1
mov ah,09
int 21h

lea dx,string1
mov ah,09
int 21h
lea dx,string2
mov ah,09
int 21h ;from here the concatenation procedure starts

78 | U b a i d S a u d a g a r
lea si,string1
lea di,string2

up: inc si
mov al,[si]
cmp al,'$'
jne up

up1: mov al,[di+2]


mov [si],al
inc si
inc di
cmp al,'$'
jne up1
lea dx,msg2
mov ah,09
int 21h

lea dx,string1
mov ah,09
int 21h

mov ah,4ch
int 21h
code ends
end start
Observation:

79 | U b a i d S a u d a g a r
26. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO ADD TWO BCD
NUMBERS

Program:
assume cs : code
assume ds : data

data segment
a db 10010110b
b db 00000111b
result dw ?
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

mov al,a
mov bl,b
add al,bl
daa
jc down
mov result,ax
jmp exit
down: inc ah
mov result,ax
exit: int 3
code ends
end start
Observation:

80 | U b a i d S a u d a g a r
27. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO SUBTRACT TWO
BCD NUMBERS

Program:
assume cs : code
assume ds : data

data segment
a db 00110010b
b db 00010111b
result db ?
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

mov al,a
mov bl,b
sub al,bl
das
mov result,al

int 3
code ends
end start
Observation:

81 | U b a i d S a u d a g a r
28. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO MULTIPLY TWO
BCD NUMBERS

Program:
assume cs : code
assume ds : data

data segment
a db 04
b db 06
result dw ?
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

mov al,a
mov bl,b
mul bl
aam
mov result, ax

int 3
code ends
end start
Observation:

82 | U b a i d S a u d a g a r
29. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO DIVIDE TWO BCD
NUMBERS

Program:
assume cs : code
assume ds : data

data segment
a dw 0600h
b db 04
quo db ?
rem db ?
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

mov ax,a
mov bl,b
aad
div bl
daa
int 3
code ends
end start
Observation:

83 | U b a i d S a u d a g a r
30. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO CONVERT A
DECIMAL NUMBER TO HEXADECIMAL NUMBER.

Program:
assume cs : code
assume ds : data

data segment
deci db 45h
hexi db ?
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

mov al,deci
and al,0fh
mov dl,al
mov al,deci
and al,0f0h
mov cl,04h
rol al,cl
mov dh,0ah
mul dh
add al,dl
mov hexi,al

int 3
code ends
end start
Observation:

84 | U b a i d S a u d a g a r
31. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO FIND WHETHER
GIVEN NUMBER IS ODD OR EVEN

Program:
assume cs:code
assume ds:data

data segment
number db 17h
msg_even db "The given number is an EVEN number $"
msg_odd db "The given Number is an ODD number $"
data ends
code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h
mov al,number
mov bl,02h
div bl
cmp ah,00h
je EVEN_PRINT
ODD:
mov ah,09
lea dx,msg_odd
int 21h
jmp EXIT
EVEN_PRINT:
mov ah,09
lea dx,msg_even
int 21h
EXIT:
mov ah,4ch
int 21h

85 | U b a i d S a u d a g a r
code ends
end start
Observation:

32. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO FIND ODD AND


EVEN NUMBERS IN AN ARRAY

Program:
assume cs:code
assume ds:data
data segment
array db 45h,47h,76h,77h
oddarray db 4 dup(?)
evenarray db 4 dup(?)
data ends
code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h
mov dl,02h
mov cl,04
lea si,array
lea di,evenarray

86 | U b a i d S a u d a g a r
lea bx,oddarray
up:
mov al,[si]
div dl
cmp ah,01
jz ODD
mov al,[si]
mov [di],al
inc di
inc si
dec cl
jnz up
jmp exit
ODD:
mov al,[si]
mov [bx],al
mov ah,00h
inc bx
inc si
dec cl
jnz up
exit:
int 3
code ends
end start

87 | U b a i d S a u d a g a r
Observation:

33. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO FIND WHETHER


GIVEN NUMBER IS POSITIVE OR NEGATIVE

assume cs:code
assume ds:data

data segment
number db 67h
msg_pos db "The given number is Positive $"
msg_neg db "The given number is Negative $"
msg_nei db "The given number is Neither Positive nor Negative $"
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

mov al,number
cmp al,0h
88 | U b a i d S a u d a g a r
jz NEITHER
jns POS

mov ah,09
lea dx,msg_neg
int 21h
jmp EXIT

NEITHER:
mov ah,09
lea dx,msg_nei
int 21h
jmp EXIT

POS:
mov ah,09
lea dx,msg_pos
int 21h

EXIT:
mov ah,4ch
int 21h

code ends
end start

89 | U b a i d S a u d a g a r
Observation

34. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO FIND POSITIVE


AND NEGATIVE NUMBERS IN AN ARRAY

Program:
assume ds:data
assume cs:code

data segment
array db 4,-4,5,-5,6,-6
posarray db 6 dup(?)
negarray db 6 dup(?)
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

mov cl,06
lea si,array

90 | U b a i d S a u d a g a r
lea di,posarray
lea bx,negarray

up:
mov al,[si]
neg al

js positive
mov dl,[si]
mov [bx],dl
inc bx
inc si
dec cl
jnz up
jmp exit

positive:
mov dl,[si]
mov [di],dl
inc di
inc si
dec cl
jnz up

exit:
int 3
code ends
end start

91 | U b a i d S a u d a g a r
Observation:

35. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO FIND THE


NUMBER OF ONES AND ZEROES IN A NUMBER

Program:
assume cs:code
assume ds:data

data segment
number db 10011011b
ones dw ?
zeroes dw ?
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

mov al,number
mov cl,08

92 | U b a i d S a u d a g a r
up:
ror al,1
jc one
inc di
dec cl
jnz up
one:
inc si
dec cl
jnz up
mov ones, si
mov zeroes, di
exit: int 3
code ends
end start

Observation:

93 | U b a i d S a u d a g a r
CHAPTER 5: PROCEDURE AND MACRO

5.1 ABOUT PROCEDURE:

Whenever we need to use a group of instructions several times throughout a program, there are
two ways we can avoid having to write the group of instructions each time we want to use them.
One way is to write the group of instructions as a separate procedure. We can then just CALL
the procedure whenever we need to execute that group of instructions. For calling the procedure
we must store the return address on the stack. If the group of instructions is big enough then
this overhead time is negligible with respect to execution time. But if the group of instructions
is too short then the overhead time and execution time are comparable. In such cases we do not
use procedures but macros instead.

DEFINITION:

Procedure is a group of instructions stored as a separate program in the memory and it is called
from the main program whenever required.

TYPES OF PROCEDURES:

The type of procedure depends on where the procedure is stored in the memory. If it is in the
same code segment where the main program is stored then it is called near procedure else it
is called as far procedure.

DIRECTIVES USED:

PROC AND ENDP DIRECTIVE:


The subprogram called as subroutine is also called as procedure.

The proc and endp directive are used to indicate the start and of the subprogram or
procedure.

Eg.

Add proc far/near

The above statement identifies the start of a procedure named add and tells the
assembler that the procedure is far (different code segment)

Endp

94 | U b a i d S a u d a g a r
The above directive indicates the end of procedure.

CALL AND RET INSTRUCTIONS:


When the microprocessor executes the CALL instruction, then the microprocessor will
branch from the main program to the subprogram (procedure (subroutine)).

If the main program and the sub program are present in the same code memory segment,
then it is called as intra segment branching in which only the IP is changed i.e. the offset
address changes but base address remains same.

But if the main program and the sub program are present in different code memory
segments, then it is called as inter segment branching in which the contents of CS and
IP are changed i.e. both base and offset address are changed.

There are two types of call i.e. Near CALL and Far CALL.

Near CALL is associated with intra segment branching and Far CALL is associated
with Inter segment branching.

When the CALL instruction is executed, the control gets transferred to the sub program.

When the control gets transferred to the sub program the contents of the Instruction
Pointer (IP) are changed only during Near CALL

When the control gets transferred to the sub program the contents of the Code Segment
register (CS) and Instruction Pointer (IP) are changed only during Far CALL

When the control gets transferred to the sub program, the sub program gets executed
and at the end of the program there exist a RET instruction which returns the control to
the main program

NEAR CALL:
Main program and Sub program = same code segment.

When control transfers to sub program, next instruction’s address (offset present in IP)
are saved on the stack and hence Stack pointer (SP) decrements by 2 i.e. (SP- 2) and CS
contents remain unchanged.

The control gets transferred to subprogram by loading starting address of sub program
in IP register.

95 | U b a i d S a u d a g a r
At the end of subprogram RET instruction gets executed due to which the address of
the next instruction in the main program is loaded from the stack into the IP register and
now stack pointer increments by 2 i.e. (SP+2) and execution of main program continues.

FAR CALL:
Main program and Sub program = different code segment

When control transfers to sub program, next instruction’s address (offset present in IP
and base address present in CS) are saved on the stack and hence stack pointer
decrements by 4 i.e. (SP – 4).

The control gets transferred to subprogram by loading base and offset of sub program
in CS and IP register

At the end of subprogram RET instruction gets executed due to which the base and the
offset address of the next instruction in the main program is loaded from the stack into
the CS and IP register and stack pointer increments by 4, i.e. (SP+4) and execution of
main program continues.

RE-ENTRANT PROCEDURE:

In many situations, 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 re-enters in procedure1. This type of procedures are called as Re-
entrant procedures.

96 | U b a i d S a u d a g a r
RECURSIVE PROCEDURE:

A recursive procedure is a procedure which calls itself. Recursive procedures are used to work
with complex data structures called trees. If the procedure is called with N (recursive) = 3. Then
the n decremented by one after each procedure CALL and the procedure is called until n = 0.

PROGRAMS USING PROCEDURES:

97 | U b a i d S a u d a g a r
WRITE AN ASSEMBLY LANGUAGE PROGRAM USING RECURSIVE
PROCEDURE TO FIND FACTORIAL OF A NUMBER.

Program:
assume cs:code
assume ds:data
data segment
n db 04h
res dw ?
data ends
code segment
start:
mov ax,data
mov ds,ax
mov al,n
mov ah,00h
call fact
int 3
fact proc
cmp ax,01 ;if n=1, fact=1 else fact=n*fact(n-1)
jz exit
push ax
dec ax ;n-1
call fact ;fact(n-1)
pop ax
mul res ;n*fact(n-1)
mov res,ax ;res=factorial
ret
exit: mov res,01
ret
fact endp
code ends
end start

98 | U b a i d S a u d a g a r
Observation:

WRITE AN ASSEMBLY LANGUAGE PROGRAM TO MULTIPLY TWO 8 BITS


NUMBERS USING NEAR PROCEDURE

Program:
assume cs:code
assume ds:data
data segment
a db 12h
b db 17h
result dw ?
data ends
code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

CALL multiply
int 3
multiply proc near
mov al,a

99 | U b a i d S a u d a g a r
mov bl,b
mul bl
mov result,ax
ret
endp
code ends
end start
Observation:

5.2 ABOUT MACROS:

Macro is also a group of instructions. Each time we call a macro in our program, the assembler
will insert the defined group of instructions in place of the call.
An important point here is that the assembler generates machine codes for the group of
instructions each time macro is called. So there is not overhead time involved in calling and
returning from procedure.
The disadvantage of macro is that it generates inline code each time when the macro is called
which takes more memory.
It is important to note that macro sequences faster than procedures because there is no CALL
and RET instructions to execute. The assembler places the macro instructions in the program
each time it is invoked.

100 | U b a i d S a u d a g a r
DEFINITION:

Macro is also a group of instructions. Each time we call a macro in our program, the assembler
will insert the defined group of instructions in place of the call.

DIFFERENCE BETWEEN PROCESURE AND MACRO

Procedure Macro

Accessed by CALL and RET instruction Accessed during assembly with name given
during program execution to macro when defined.
Machine code for instructions is put only Machine code is generated for instructions
once in the memory. each time when macro is called.
With procedures less memory is required. With macros more memory is required.
Parameters can be passed in registers, Parameters can be passed as part of
memory locations or stack. statements which calls the macro.

PROGRAMS USING MACROS:

WAP USING MACRO TO ADD TWO NUMBERS

Program:
assume cs:code
assume ds:data

addition macro a,b,result


mov al,a
mov bl,b
add al,bl
mov result,al
endm

data segment
num1 db 20h
num2 db 35h
res db ?
data ends

101 | U b a i d S a u d a g a r
code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

addition num1,num2,res
int 3
code ends

end start
Observation:

WAP TO PRINT TWO STRINGS USING MACRO

Program:
assume cs:code
assume ds:data

print macro msg


mov ah,09
lea dx,msg

102 | U b a i d S a u d a g a r
int 21h
endm

data segment
msg1 db "Microprocessor $"
msg2 db 10,13, "And $"
msg3 db 10,13, "Programming $"
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

print msg1
print msg2
print msg3

mov ah,4ch
int 21h

code end
end start

Observation:

103 | U b a i d S a u d a g a r
WAP TO SOLVE P = X 2 +Y 2 USING MACROS

Program:
assume cs:code
assume ds:data

data segment
x db 2
y db 3
sq1 dw ?
sq2 dw ?
result dw ?
data ends

square macro a,value


mov al,a
mul al
mov value,ax
endm

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

104 | U b a i d S a u d a g a r
square x,sq1
square y,sq2

mov ax,sq1
mov bx,sq2
add ax,bx
mov result,ax
int 3
code ends
end start
Observation:

WAP USING MACRO TO COMPARE TWO STRINGS

Program:
assume cs:code
assume ds:data

print macro msg


mov ah,09
lea dx,msg
int 21h

105 | U b a i d S a u d a g a r
endm

data segment
str1 db "ABCD$"
str2 db "ABCD$"
equal db "Strings are same $"
unequal db "Strings are not same $"
data ends

code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h

lea si,str1
lea di,str2

up: mov al,[si]


cmp al,[di]
jz down

print unequal
jmp exit

down: inc si
inc di
cmp al,'$'
jnz up

print equal

exit: mov ah,4ch


int 21h
106 | U b a i d S a u d a g a r
code ends

end start
Observation:

107 | U b a i d S a u d a g a r

S-ar putea să vă placă și