Documente Academic
Documente Profesional
Documente Cultură
Editor: ............................................................................................................................... 27
Assembler: ........................................................................................................................ 27
Linker: .............................................................................................................................. 27
Debugger: ......................................................................................................................... 28
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
2|Ubaid Saudagar
4.2 Programming using Assembler: ..................................................................................... 50
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
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
4|Ubaid Saudagar
Definition: ...................................................................................................................... 101
Difference between Procesure and Macro ..................................................................... 101
Programs using macros: ................................................................................................. 101
5|Ubaid Saudagar
CHAPTER 1: 8086 - 16 BIT MICROPROCESSOR
6|Ubaid Saudagar
1.3 REGISTER ORGANIZATION OF 8086
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)
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
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.
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.
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.
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.
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.
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.
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
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.
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.
𝑨𝟏𝟔 /𝑺𝟑 - 𝑨𝟏𝟗 /𝑺𝟔 (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).
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.
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.
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.
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.
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.
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.
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
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.
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.
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.
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}
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}
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}
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}
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:
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
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.
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.
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
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
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
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
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
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
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.
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.
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
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]
40 | U b a i d S a u d a g a r
Eg: ADD AL, BL
DAA
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.
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.
43 | U b a i d S a u d a g a r
After execution AL = 4AH = 0100 1010 and C = 1
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
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]}
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
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.
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
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.
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.
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:
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:
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:
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:
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
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
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:
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:
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:
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:
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:
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:
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:
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.
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:
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:
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:
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
73 | U b a i d S a u d a g a r
Observation:
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:
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
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:
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
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:
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:
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
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:
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
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:
The proc and endp directive are used to indicate the start and of the subprogram or
procedure.
Eg.
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.
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.
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:
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:
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.
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.
Program:
assume cs:code
assume ds:data
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:
Program:
assume cs:code
assume ds:data
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
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:
Program:
assume cs:code
assume ds:data
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
print unequal
jmp exit
down: inc si
inc di
cmp al,'$'
jnz up
print equal
end start
Observation:
107 | U b a i d S a u d a g a r