lecturenote_384620204Chapter Four
lecturenote_384620204Chapter Four
4
PROCEDURES
Macros:
• A macro is a group of repetitive instructions in a program which
are codified only once and can be used as many times as
necessary.
• A macro can be defined anywhere in program using the
directives MACRO and ENDM
• Each time we call the macro in a program, the assembler will
insert the defined group of instructions in place of the call.
• The assembler generates machine codes for the group of
instructions each time the macro is called.
• Using a macro avoids the overhead time involved in calling and
returning from a procedure.
Syntax of macro:
macroname MACRO
instruction1 5
PROCEDURES
Example#1:
Read MACRO
mov ah,01h
int 21h
ENDM
Example#2:
Display MACRO
mov dl, al
Mov ah,02h
int 21h
ENDM
6
PROCEDURES
Advantage of Procedure and Macros:
Procedures:
Advantages: The machine codes for the group of instructions in the
procedure only have to be put once.
Disadvantages
• Need for stack
• Overhead time required to call the procedure and return to the
calling program.
Macros:
Advantages: Macro avoids overhead time involving in calling and
returning from a procedure.
Disadvantages: Generating in line code each time a macro is called
is that this will make the program take up more memory than
using a procedure.
7
PROCEDURES
8
PROCEDURES
• Procedures or subroutines are very important in assembly
language, as the assembly language programs tend to be large in
size.
• Procedures are identified by a name.
• Following this name, the body of the procedure is described
which performs a well-defined job.
• End of the procedure is indicated by a return statement.
• Syntax:
proc_name:
procedure body
...
ret
9
PROCEDURES
• The procedure is called from another function by using the
CALL instruction.
• The CALL instruction should have the name of the called
procedure as an argument.
• Syntax:
– CALL proc_name
10
PROCEDURE Example
• A program to Add numbers and return their sum
section .text
global _start
_start:
mov ecx,'4'
sub ecx, '0'
mov edx, '5'
sub edx, '0'
call sum ;call sum procedure
mov [res], eax
mov ecx, msg
11
PROCEDURE Example
mov edx, len
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
section .data
msg db "The sum is:", 0xA,0xD
len equ $- msg
segment .bss
res resb 1
13