PROCEDURES & MACROS
While writing programs, sometimes it may be required to use same set of
instructions repeatedly. If these instructions are actually written again & again, the
program size increases and occupies more memory space. To avoid this, these
instructions can be written as a separate program (subprogram) and whenever this set
of instructions is required, an instruction can be written in the main program CALL
this subprogram. This saves the memory space. Such a subprogram which is called
from the main program to execute certain set of instructions is called subroutine or
procedure.
Declaring PROCEDURES:
The syntax for procedure declaration:
name PROC [NEAR/FAR]
; Instructions of the procedures
; are written here.
RET
name ENDP
Example:
Delay PROC
AGAIN: MOV CX,COUNT
NOP
LOOP AGAIN
RET
Delay ENDP
The above procedure introduces a delay in between the program statements, when it is
called from the main program.
A procedure in 8086 can be accessed with a CALL & RET instruction.
CALL instruction: This performs two operations
1. Saves the return address or the address of the instruction next to the CALL
instruction on the stack. Return address is the address where the program will
return to after the procedure completes execution.
a. If the call to procedure is in the same code segment, i.e. a near CALL,
then only the contents of IP are pushed on the stack.
b. If the call to procedure is in another code segment, i.e. a far CALL,
then the contents of IP as well as CS are pushed on the stack.
2. It loads the IP & CS register with a new starting address of the procedure and
then branches to the procedure.
RET instruction: When 8086 executes a CALL instruction, it stores the return
address of the CALLing routine on the stack. A RET instruction at the end of the
procedure copies the return address stored on the stack back into the CS and IP
registers and then returns execution to the main program.
Passing Parameters to procedures:
Procedures may require input data or constants for their execution. Their data
or constants may be passed to the procedure by the main program or some procedures
may access the readily available data of constants available in memory.
Generally following techniques are used to pass input data/parameter to procedures in
assembly language language programs,
1. Using global declared variable
2. Using registers of CPU architecture
3. Using memory locations
4. Using stack
Example : Using registers
CODE SEGMENT
START: MOV AX, 5555H
MOV BX,7272h
:
:
CALL PROC1
:
:
PROCEDURE PROC1
:
:
ADD AX,BX
:
:
RET
PROC1 ENDP
CODE ENDS
END START
Re-entrant Procedures :
A procedure is said to be re-entrant, if it can be interrupted, used and re-
entered without losing or writing over anything. To be a re-entrant,
 Procedure must first push all the flags and registers used in the procedure.
 It should also use only registers or stack to pass parameters.
The flow of re-entrant procedure for a multiply procedure when interrupt procedure is
executed, as shown below.
Call
Multiply
Interrupt
Here
Call
Multiply
Main Line
Interrupt
Procedure
Return to
Interrupt
Multiply
procedure
Return to Calling pgm
Recursive Procedures:
A recursive procedure is a procedure which calls itself. Here, the program sets
aside a few locations in stack for the storage of the parameters whicha re passed each
time the computation is done and the value is returned. Each value returned is then
obtained by popping back from the stack at every RET instruction when executed at
the end of the procedure.
Example : To calculate factorial using recursion
data segment
n db 04h
res dw ?
data ends
code segment
assume cs:code, ds:data
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) , RECURSION
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
Advantages of Procedures:
1. Simple modular programming
2. Reduced workload and development time
3. Debugging of program and procedure is easier
4. Reduction in the size of the main program
5. Reuse of procedures in the same program many times or in another program.
Macros :
Small sequences of codes of the same pattern repeated frequently at different
places which perform the same operation on the different data of the same data type
are called MACRO.
Macro is also called as an Open subroutine. When Called, the code written
within macro are executed automatically. Macros should be used when it has few
program statements. This simplifies the programming process.
Procedures Vs Macros
 Procedure does occupy minimum memory space than macro.
 In macro machine code is generated for instructions each time when it is
called but in procedure machine code for instruction is put only once in the
memory
 Procedure is accessed by call Instruction whereas Macro is accessed with the
name given.
Advantages
 Simplify and reduce the amount of repetitive coding
 Reduce errors caused by repetitive coding
 Makes program more readable
 Execution time is less as compared to procedures as no extra instructions
required
Defining Macros
 The Directive MACRO indicates the beginning of a MACRO
 Name of the Macro followed by MACRO and arguments if any are specified.
 ENDM is always associated with MACRO which ends the macro.
General Form :
Macro_name MACRO [Arguement1, arguement2…]
:
:
ENDM
Example:
PRINT MACRO MES
MOV AX,09H
LEA DX, MES
INT 21H
ENDM
The above macro is used to display a string specified in the argument MES on
the screen, when evoked by the main program as given below
DATA SEGEMENT
STR DB 0DH,0AH,”Hello World$”
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA, CS:CODE
START: MOV AX, DATA
MOV DS, AX
PRINT STR ; Calls Macro PRINT to display STR
; STR is the parameter passed which is
;taken as MES in the Macro PRINT.
:
:
CODE ENDS
END START
Note : Main difference between Macro and Procedure is that A call to Macro
will be replaced with its body during assembly time, whereas the call to procedure is
explicit transfer of control during run-time.

More Related Content

DOC
Chapter 6 notes
PPTX
Chap6 procedures & macros
DOC
Chapter 5 notes
PDF
Buffer overflow tutorial
PDF
Emu8086
PDF
Implementation - Sample Runs
PPTX
Click-Through Example for Flink’s KafkaConsumer Checkpointing
PPTX
A green solution to solve a race condition problem
Chapter 6 notes
Chap6 procedures & macros
Chapter 5 notes
Buffer overflow tutorial
Emu8086
Implementation - Sample Runs
Click-Through Example for Flink’s KafkaConsumer Checkpointing
A green solution to solve a race condition problem

What's hot (20)

PPTX
Stephan Ewen - Scaling to large State
PPTX
Compiler design
PPTX
Peephole optimization techniques in compiler design
PDF
TLPI - 6 Process
PDF
Buffer overflow attack
PPT
Smpant Transact09
PPTX
Access to non local names
PPTX
Code optimization
PPT
Chapter Seven(1)
DOCX
Remote client copy
PPTX
Robert Metzger - Connecting Apache Flink to the World - Reviewing the streami...
PPTX
Assembly fundamentals
PDF
Declare Your Language: Virtual Machines & Code Generation
PDF
eBPF Debugging Infrastructure - Current Techniques
PPT
Parallel concepts1
PPT
Os4
PDF
FARIS: Fast and Memory-efficient URL Filter by Domain Specific Machine
PDF
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
DOCX
project_2
Stephan Ewen - Scaling to large State
Compiler design
Peephole optimization techniques in compiler design
TLPI - 6 Process
Buffer overflow attack
Smpant Transact09
Access to non local names
Code optimization
Chapter Seven(1)
Remote client copy
Robert Metzger - Connecting Apache Flink to the World - Reviewing the streami...
Assembly fundamentals
Declare Your Language: Virtual Machines & Code Generation
eBPF Debugging Infrastructure - Current Techniques
Parallel concepts1
Os4
FARIS: Fast and Memory-efficient URL Filter by Domain Specific Machine
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
project_2
Ad

Similar to Chapter 5 notes new (20)

PPTX
PPTX
Co&al lecture-07
PDF
Assembly level language
PDF
Assembly_80x86- Assembly languages programming and 80x861.pdf
PDF
Assembler directives and basic steps ALP of 8086
PPTX
[ASM]Lab6
PPTX
Advanced procedures in assembly language Full chapter ppt
PPT
Stack and subroutine
PDF
N_Asm Assembly macros (sol)
PPT
Compiler 2011-8-re1
PPT
Compiler 2011-8-re1
ODP
Introduction to MPI
PDF
Alp 05
PPTX
unit-2.pptx
PDF
Different addressing mode and risc, cisc microprocessor
PDF
Mp lab manual
PPS
Inter Task Communication On Volatile Nodes
DOCX
LA3-64 -vit assembly language program to Accept string and display its length...
PPT
Lec 04 intro assembly
PDF
Embedded web technology makes it possibl
Co&al lecture-07
Assembly level language
Assembly_80x86- Assembly languages programming and 80x861.pdf
Assembler directives and basic steps ALP of 8086
[ASM]Lab6
Advanced procedures in assembly language Full chapter ppt
Stack and subroutine
N_Asm Assembly macros (sol)
Compiler 2011-8-re1
Compiler 2011-8-re1
Introduction to MPI
Alp 05
unit-2.pptx
Different addressing mode and risc, cisc microprocessor
Mp lab manual
Inter Task Communication On Volatile Nodes
LA3-64 -vit assembly language program to Accept string and display its length...
Lec 04 intro assembly
Embedded web technology makes it possibl
Ad

More from HarshitParkar6677 (20)

PPTX
Wi fi hacking
PPT
D dos attack
DOCX
Notes chapter 6
DOC
Interface notes
PPTX
Chapter6 2
PPTX
PPT
8086 cpu 1
DOCX
Notes arithmetic instructions
DOCX
Notes all instructions
DOCX
Notes aaa aa
DOCX
Notes 8086 instruction format
PPTX
Copy of 8086inst logical
PPT
Copy of 8086inst logical
PPTX
Chapter3 program flow control instructions
PPTX
Chapter3 8086inst stringsl
PPTX
Chapter3 8086inst logical 2
PPTX
Chapter 3 8086 ins2 math
PPTX
Chap3 program flow control instructions
PPTX
Chap3 8086 logical
Wi fi hacking
D dos attack
Notes chapter 6
Interface notes
Chapter6 2
8086 cpu 1
Notes arithmetic instructions
Notes all instructions
Notes aaa aa
Notes 8086 instruction format
Copy of 8086inst logical
Copy of 8086inst logical
Chapter3 program flow control instructions
Chapter3 8086inst stringsl
Chapter3 8086inst logical 2
Chapter 3 8086 ins2 math
Chap3 program flow control instructions
Chap3 8086 logical

Recently uploaded (20)

PPTX
Design ,Art Across Digital Realities and eXtended Reality
PDF
Mechanics of materials week 2 rajeshwari
PDF
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
PDF
electrical machines course file-anna university
PPTX
CS6006 - CLOUD COMPUTING - Module - 1.pptx
PDF
Micro 4 New.ppt.pdf a servay of cells and microorganism
PDF
Cryptography and Network Security-Module-I.pdf
PDF
Designing Fault-Tolerant Architectures for Resilient Oracle Cloud ERP and HCM...
PDF
Lesson 3 .pdf
DOCX
ENVIRONMENTAL PROTECTION AND MANAGEMENT (18CVL756)
PPTX
Environmental studies, Moudle 3-Environmental Pollution.pptx
PPTX
WN UNIT-II CH4_MKaruna_BapatlaEngineeringCollege.pptx
PPTX
CT Generations and Image Reconstruction methods
PPTX
Agentic Artificial Intelligence (Agentic AI).pptx
PPTX
CNS - Unit 1 (Introduction To Computer Networks) - PPT (2).pptx
PPTX
Micro1New.ppt.pptx the main themes if micro
PPTX
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
PDF
[jvmmeetup] next-gen integration with apache camel and quarkus.pdf
PPTX
MAD Unit - 3 User Interface and Data Management (Diploma IT)
PDF
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
Design ,Art Across Digital Realities and eXtended Reality
Mechanics of materials week 2 rajeshwari
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
electrical machines course file-anna university
CS6006 - CLOUD COMPUTING - Module - 1.pptx
Micro 4 New.ppt.pdf a servay of cells and microorganism
Cryptography and Network Security-Module-I.pdf
Designing Fault-Tolerant Architectures for Resilient Oracle Cloud ERP and HCM...
Lesson 3 .pdf
ENVIRONMENTAL PROTECTION AND MANAGEMENT (18CVL756)
Environmental studies, Moudle 3-Environmental Pollution.pptx
WN UNIT-II CH4_MKaruna_BapatlaEngineeringCollege.pptx
CT Generations and Image Reconstruction methods
Agentic Artificial Intelligence (Agentic AI).pptx
CNS - Unit 1 (Introduction To Computer Networks) - PPT (2).pptx
Micro1New.ppt.pptx the main themes if micro
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
[jvmmeetup] next-gen integration with apache camel and quarkus.pdf
MAD Unit - 3 User Interface and Data Management (Diploma IT)
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf

Chapter 5 notes new

  • 1. PROCEDURES & MACROS While writing programs, sometimes it may be required to use same set of instructions repeatedly. If these instructions are actually written again & again, the program size increases and occupies more memory space. To avoid this, these instructions can be written as a separate program (subprogram) and whenever this set of instructions is required, an instruction can be written in the main program CALL this subprogram. This saves the memory space. Such a subprogram which is called from the main program to execute certain set of instructions is called subroutine or procedure. Declaring PROCEDURES: The syntax for procedure declaration: name PROC [NEAR/FAR] ; Instructions of the procedures ; are written here. RET name ENDP Example: Delay PROC AGAIN: MOV CX,COUNT NOP LOOP AGAIN RET Delay ENDP The above procedure introduces a delay in between the program statements, when it is called from the main program. A procedure in 8086 can be accessed with a CALL & RET instruction. CALL instruction: This performs two operations 1. Saves the return address or the address of the instruction next to the CALL instruction on the stack. Return address is the address where the program will return to after the procedure completes execution. a. If the call to procedure is in the same code segment, i.e. a near CALL, then only the contents of IP are pushed on the stack. b. If the call to procedure is in another code segment, i.e. a far CALL, then the contents of IP as well as CS are pushed on the stack. 2. It loads the IP & CS register with a new starting address of the procedure and then branches to the procedure. RET instruction: When 8086 executes a CALL instruction, it stores the return address of the CALLing routine on the stack. A RET instruction at the end of the procedure copies the return address stored on the stack back into the CS and IP registers and then returns execution to the main program.
  • 2. Passing Parameters to procedures: Procedures may require input data or constants for their execution. Their data or constants may be passed to the procedure by the main program or some procedures may access the readily available data of constants available in memory. Generally following techniques are used to pass input data/parameter to procedures in assembly language language programs, 1. Using global declared variable 2. Using registers of CPU architecture 3. Using memory locations 4. Using stack Example : Using registers CODE SEGMENT START: MOV AX, 5555H MOV BX,7272h : : CALL PROC1 : : PROCEDURE PROC1 : : ADD AX,BX : : RET PROC1 ENDP CODE ENDS END START Re-entrant Procedures : A procedure is said to be re-entrant, if it can be interrupted, used and re- entered without losing or writing over anything. To be a re-entrant,  Procedure must first push all the flags and registers used in the procedure.  It should also use only registers or stack to pass parameters. The flow of re-entrant procedure for a multiply procedure when interrupt procedure is executed, as shown below. Call Multiply Interrupt Here Call Multiply Main Line Interrupt Procedure Return to Interrupt Multiply procedure Return to Calling pgm
  • 3. Recursive Procedures: A recursive procedure is a procedure which calls itself. Here, the program sets aside a few locations in stack for the storage of the parameters whicha re passed each time the computation is done and the value is returned. Each value returned is then obtained by popping back from the stack at every RET instruction when executed at the end of the procedure. Example : To calculate factorial using recursion data segment n db 04h res dw ? data ends code segment assume cs:code, ds:data 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) , RECURSION 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 Advantages of Procedures: 1. Simple modular programming 2. Reduced workload and development time 3. Debugging of program and procedure is easier 4. Reduction in the size of the main program 5. Reuse of procedures in the same program many times or in another program. Macros : Small sequences of codes of the same pattern repeated frequently at different places which perform the same operation on the different data of the same data type are called MACRO. Macro is also called as an Open subroutine. When Called, the code written within macro are executed automatically. Macros should be used when it has few program statements. This simplifies the programming process.
  • 4. Procedures Vs Macros  Procedure does occupy minimum memory space than macro.  In macro machine code is generated for instructions each time when it is called but in procedure machine code for instruction is put only once in the memory  Procedure is accessed by call Instruction whereas Macro is accessed with the name given. Advantages  Simplify and reduce the amount of repetitive coding  Reduce errors caused by repetitive coding  Makes program more readable  Execution time is less as compared to procedures as no extra instructions required Defining Macros  The Directive MACRO indicates the beginning of a MACRO  Name of the Macro followed by MACRO and arguments if any are specified.  ENDM is always associated with MACRO which ends the macro. General Form : Macro_name MACRO [Arguement1, arguement2…] : : ENDM Example: PRINT MACRO MES MOV AX,09H LEA DX, MES INT 21H ENDM The above macro is used to display a string specified in the argument MES on the screen, when evoked by the main program as given below DATA SEGEMENT STR DB 0DH,0AH,”Hello World$” DATA ENDS CODE SEGMENT ASSUME DS:DATA, CS:CODE START: MOV AX, DATA MOV DS, AX PRINT STR ; Calls Macro PRINT to display STR ; STR is the parameter passed which is ;taken as MES in the Macro PRINT. : : CODE ENDS END START Note : Main difference between Macro and Procedure is that A call to Macro will be replaced with its body during assembly time, whereas the call to procedure is explicit transfer of control during run-time.