Está en la página 1de 11

MAP notes

Chapter 6: Procedure and Macro in Assembly Language Program Part 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 have to 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

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

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

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.

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.

Write an assembly language program using recursive procedure to find factorial of a


number.
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
Write an assembly language program to multiply two 8 bits numbers using NEAR
procedure
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
mov bl,b
mul bl
mov result,ax
ret
endp

code ends
end start

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.
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
Accessed by CALL and RET instruction during

Macro
Accessed during assembly with name given to

program execution
Machine code for instructions is put only once

macro when defined.


Machine code is generated for instructions each

in the memory.
With procedures less memory is required.
Parameters can be passed in registers, memory

time when macro is called.


With macros more memory is required.
Parameters can be passed as part of statements

locations or stack.

which calls the macro.

Programs using macros:


WAP using macro to add two numbers
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
code segment
start:
mov ax,data
mov ds,ax
mov ax,0000h
addition num1,num2,res
int 3
code ends
end start
WAP to print two strings using macro
assume cs:code
assume ds:data
print macro msg
mov ah,09
lea dx,msg
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 ends
end start
WAP to solve P = x2+y2 using macros

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

square x,sq1
square y,sq2

mov ax,sq1
mov bx,sq2
add ax,bx
mov result,ax
int 3
code ends
end start
WAP using macro to compare two strings
assume cs:code
assume ds:data
print macro msg
mov ah,09
lea dx,msg
int 21h
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
code ends
end start

También podría gustarte