0% found this document useful (0 votes)
16 views13 pages

lecturenote_384620204Chapter Four

The document outlines the use of procedures and macros in programming, explaining how procedures are groups of instructions stored in memory that can be called using the CALL instruction, while macros are repetitive instructions defined once and inserted multiple times. It details the types of procedures, methods for passing parameters, and the advantages and disadvantages of both procedures and macros. Additionally, it provides syntax examples for defining and calling procedures and macros, along with a sample program demonstrating their use.

Uploaded by

Andu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views13 pages

lecturenote_384620204Chapter Four

The document outlines the use of procedures and macros in programming, explaining how procedures are groups of instructions stored in memory that can be called using the CALL instruction, while macros are repetitive instructions defined once and inserted multiple times. It details the types of procedures, methods for passing parameters, and the advantages and disadvantages of both procedures and macros. Additionally, it provides syntax examples for defining and calling procedures and macros, along with a sample program demonstrating their use.

Uploaded by

Andu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

PROCEDURES

Procedures and Macros:


 When 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.
1. One way is to write the group of instructions as a separate procedure.
2. Another way we can use macros.
Procedures:
 The procedure is a group of instructions stored as a separate program
in the memory and it is called from the main program whenever
required using CALL instruction.
 For calling the procedure we have to store the return address (next
instruction address followed by CALL) onto the stack.
 At the end of the procedure RET instruction used to return the
execution to the next instruction in the main program by retrieving
the address from the top of the stack.
1
PROCEDURES
 Machine codes for the procedure instructions put only once in
memory.
 The procedure can be defined anywhere in the program using
assembly directives PROC and ENDP.
The four major ways of passing parameters to and from a procedure are:
1. In registers
2. In dedicated memory location accessed by name
3 .With pointers passed in registers
4. With the stack
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 the it is called near procedure otherwise it is referred to
as far procedure.
2
PROCEDURES
 For near procedure CALL instruction pushes only the IP register
contents on the stack, since CS register contents remains unchanged
for main program.
 But for Far procedure CALL instruction pushes both IP and CS on the
stack.
Syntax:
Procedure name PROC near
instruction 1
instruction 2
RET
Procedure name ENDP
Example:
near procedure:
ADD2 PROC near
ADD AX,BX
RET ADD2 ENDP 3
PROCEDURES
far procedure:
Procedures segment
Assume CS : Procedures
ADD2 PROC far
ADD AX,BX
RET ADD2 ENDP
Procedures ends
Depending on the characteristics the procedures are two types
1. Re-entrant Procedures
2. Recursive Procedures
Reentrant Procedures: The procedure which can be interrupted, used and
“reentered” without losing or writing over anything.
Recursive Procedure: A recursive procedure is procedure which calls itself.

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

mov ecx, res


mov edx, 1
mov ebx, 1 ;file descriptor (stdout)
mov eax, 4 ;system call number (sys_write)
int 0x80 ;call kernel

mov eax,1 ;system call number (sys_exit)


int 0x80 ;call kernel
12
PROCEDURE Example
sum:
mov eax, ecx
add eax, edx
add eax, '0'
ret

section .data
msg db "The sum is:", 0xA,0xD
len equ $- msg
segment .bss
res resb 1

13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy