SlideShare a Scribd company logo
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.
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.
2
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.
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
Call
Multiply
Interrupt
Here
Call
Multiply
Main Line
Interrupt
Procedure
Return to
Interrupt
Multiply
procedure
Return to Calling pgm
3
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
4
 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

What's hot

A green solution to solve a race condition problem
A green solution to solve a race condition problemA green solution to solve a race condition problem
A green solution to solve a race condition problem
Kai Zhou
 
Click-Through Example for Flink’s KafkaConsumer Checkpointing
Click-Through Example for Flink’s KafkaConsumer CheckpointingClick-Through Example for Flink’s KafkaConsumer Checkpointing
Click-Through Example for Flink’s KafkaConsumer Checkpointing
Robert Metzger
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
Anul Chaudhary
 
Stephan Ewen - Scaling to large State
Stephan Ewen - Scaling to large StateStephan Ewen - Scaling to large State
Stephan Ewen - Scaling to large State
Flink Forward
 
Smpant Transact09
Smpant Transact09Smpant Transact09
Smpant Transact09
smpant
 
Design
DesignDesign
Buffer overflow attack
Buffer overflow attackBuffer overflow attack
Buffer overflow attack
Prithiviraj Prithiviraj
 
Access to non local names
Access to non local namesAccess to non local names
Access to non local namesVarsha Kumar
 
Chapter Seven(1)
Chapter Seven(1)Chapter Seven(1)
Chapter Seven(1)bolovv
 
Assembly fundamentals
Assembly fundamentalsAssembly fundamentals
Assembly fundamentals
Syed Zaid Irshad
 
Robert Metzger - Connecting Apache Flink to the World - Reviewing the streami...
Robert Metzger - Connecting Apache Flink to the World - Reviewing the streami...Robert Metzger - Connecting Apache Flink to the World - Reviewing the streami...
Robert Metzger - Connecting Apache Flink to the World - Reviewing the streami...
Flink Forward
 
Input-output
Input-outputInput-output
Input-output
neda marie maramo
 
TLPI - 6 Process
TLPI - 6 ProcessTLPI - 6 Process
TLPI - 6 ProcessShu-Yu Fu
 
A Simple Communication System Design Lab #1 with MATLAB Simulink
A Simple Communication System Design Lab #1 with MATLAB Simulink A Simple Communication System Design Lab #1 with MATLAB Simulink
A Simple Communication System Design Lab #1 with MATLAB Simulink
Jaewook. Kang
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutine
milandhara
 
Remote client copy
Remote client copyRemote client copy
Remote client copy
Naveed Bashir
 
eBPF Debugging Infrastructure - Current Techniques
eBPF Debugging Infrastructure - Current TechniqueseBPF Debugging Infrastructure - Current Techniques
eBPF Debugging Infrastructure - Current Techniques
Netronome
 

What's hot (20)

A green solution to solve a race condition problem
A green solution to solve a race condition problemA green solution to solve a race condition problem
A green solution to solve a race condition problem
 
Click-Through Example for Flink’s KafkaConsumer Checkpointing
Click-Through Example for Flink’s KafkaConsumer CheckpointingClick-Through Example for Flink’s KafkaConsumer Checkpointing
Click-Through Example for Flink’s KafkaConsumer Checkpointing
 
Compiler design
Compiler designCompiler design
Compiler design
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
 
Run time
Run timeRun time
Run time
 
Stephan Ewen - Scaling to large State
Stephan Ewen - Scaling to large StateStephan Ewen - Scaling to large State
Stephan Ewen - Scaling to large State
 
Smpant Transact09
Smpant Transact09Smpant Transact09
Smpant Transact09
 
Design
DesignDesign
Design
 
Buffer overflow attack
Buffer overflow attackBuffer overflow attack
Buffer overflow attack
 
Access to non local names
Access to non local namesAccess to non local names
Access to non local names
 
Chapter Seven(1)
Chapter Seven(1)Chapter Seven(1)
Chapter Seven(1)
 
Assembly fundamentals
Assembly fundamentalsAssembly fundamentals
Assembly fundamentals
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Robert Metzger - Connecting Apache Flink to the World - Reviewing the streami...
Robert Metzger - Connecting Apache Flink to the World - Reviewing the streami...Robert Metzger - Connecting Apache Flink to the World - Reviewing the streami...
Robert Metzger - Connecting Apache Flink to the World - Reviewing the streami...
 
Input-output
Input-outputInput-output
Input-output
 
TLPI - 6 Process
TLPI - 6 ProcessTLPI - 6 Process
TLPI - 6 Process
 
A Simple Communication System Design Lab #1 with MATLAB Simulink
A Simple Communication System Design Lab #1 with MATLAB Simulink A Simple Communication System Design Lab #1 with MATLAB Simulink
A Simple Communication System Design Lab #1 with MATLAB Simulink
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutine
 
Remote client copy
Remote client copyRemote client copy
Remote client copy
 
eBPF Debugging Infrastructure - Current Techniques
eBPF Debugging Infrastructure - Current TechniqueseBPF Debugging Infrastructure - Current Techniques
eBPF Debugging Infrastructure - Current Techniques
 

Similar to Chapter 6 notes

Procedure
ProcedureProcedure
Assembly level language
Assembly level languageAssembly level language
Assembly level language
PDFSHARE
 
Co&al lecture-07
Co&al lecture-07Co&al lecture-07
Co&al lecture-07
AbdulKarim563520
 
Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086
Urvashi Singh
 
Compiler 2011-8-re1
Compiler 2011-8-re1Compiler 2011-8-re1
Compiler 2011-8-re1
Ganesh Amirineni
 
Compiler 2011-8-re1
Compiler 2011-8-re1Compiler 2011-8-re1
Compiler 2011-8-re1
Ganesh Amirineni
 
N_Asm Assembly macros (sol)
N_Asm Assembly macros (sol)N_Asm Assembly macros (sol)
N_Asm Assembly macros (sol)
Selomon birhane
 
[ASM]Lab6
[ASM]Lab6[ASM]Lab6
[ASM]Lab6
Nora Youssef
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutineAshim Saha
 
Advanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptAdvanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter ppt
Muhammad Sikandar Mustafa
 
Unit 4 assembly language programming
Unit 4   assembly language programmingUnit 4   assembly language programming
Unit 4 assembly language programming
Kartik Sharma
 
Different addressing mode and risc, cisc microprocessor
Different addressing mode and risc, cisc microprocessorDifferent addressing mode and risc, cisc microprocessor
Different addressing mode and risc, cisc microprocessor
Daffodil International University
 
Introduction to MPI
Introduction to MPIIntroduction to MPI
Introduction to MPI
yaman dua
 
Intermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdfIntermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdf
Himanshu883663
 
unit-2.pptx
unit-2.pptxunit-2.pptx
unit-2.pptx
norajobai
 
Lec 04 intro assembly
Lec 04 intro assemblyLec 04 intro assembly
Lec 04 intro assemblyAbdul Khan
 
OSTEP Chapter2 Introduction
OSTEP Chapter2 IntroductionOSTEP Chapter2 Introduction
OSTEP Chapter2 Introduction
Shuya Osaki
 

Similar to Chapter 6 notes (20)

Procedure
ProcedureProcedure
Procedure
 
Assembly level language
Assembly level languageAssembly level language
Assembly level language
 
Co&al lecture-07
Co&al lecture-07Co&al lecture-07
Co&al lecture-07
 
Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086
 
Compiler 2011-8-re1
Compiler 2011-8-re1Compiler 2011-8-re1
Compiler 2011-8-re1
 
Compiler 2011-8-re1
Compiler 2011-8-re1Compiler 2011-8-re1
Compiler 2011-8-re1
 
N_Asm Assembly macros (sol)
N_Asm Assembly macros (sol)N_Asm Assembly macros (sol)
N_Asm Assembly macros (sol)
 
[ASM]Lab6
[ASM]Lab6[ASM]Lab6
[ASM]Lab6
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutine
 
Advanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptAdvanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter ppt
 
Unit 4 assembly language programming
Unit 4   assembly language programmingUnit 4   assembly language programming
Unit 4 assembly language programming
 
Different addressing mode and risc, cisc microprocessor
Different addressing mode and risc, cisc microprocessorDifferent addressing mode and risc, cisc microprocessor
Different addressing mode and risc, cisc microprocessor
 
Mp lab manual
Mp lab manualMp lab manual
Mp lab manual
 
Alp 05
Alp 05Alp 05
Alp 05
 
Introduction to MPI
Introduction to MPIIntroduction to MPI
Introduction to MPI
 
Intermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdfIntermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdf
 
unit-2.pptx
unit-2.pptxunit-2.pptx
unit-2.pptx
 
Lec 04 intro assembly
Lec 04 intro assemblyLec 04 intro assembly
Lec 04 intro assembly
 
OSTEP Chapter2 Introduction
OSTEP Chapter2 IntroductionOSTEP Chapter2 Introduction
OSTEP Chapter2 Introduction
 
Bc0040
Bc0040Bc0040
Bc0040
 

More from HarshitParkar6677

Wi fi hacking
Wi fi hackingWi fi hacking
Wi fi hacking
HarshitParkar6677
 
D dos attack
D dos attackD dos attack
D dos attack
HarshitParkar6677
 
Notes chapter 6
Notes chapter  6Notes chapter  6
Notes chapter 6
HarshitParkar6677
 
Interface notes
Interface notesInterface notes
Interface notes
HarshitParkar6677
 
Chapter6 2
Chapter6 2Chapter6 2
Chapter6 2
HarshitParkar6677
 
Chapter6
Chapter6Chapter6
8086 cpu 1
8086 cpu 18086 cpu 1
8086 cpu 1
HarshitParkar6677
 
Notes arithmetic instructions
Notes arithmetic instructionsNotes arithmetic instructions
Notes arithmetic instructions
HarshitParkar6677
 
Notes all instructions
Notes all instructionsNotes all instructions
Notes all instructions
HarshitParkar6677
 
Notes aaa aa
Notes aaa aaNotes aaa aa
Notes aaa aa
HarshitParkar6677
 
Notes 8086 instruction format
Notes 8086 instruction formatNotes 8086 instruction format
Notes 8086 instruction format
HarshitParkar6677
 
Misc
MiscMisc
Copy of 8086inst logical
Copy of 8086inst logicalCopy of 8086inst logical
Copy of 8086inst logical
HarshitParkar6677
 
Copy of 8086inst logical
Copy of 8086inst logicalCopy of 8086inst logical
Copy of 8086inst logical
HarshitParkar6677
 
Chapter3 program flow control instructions
Chapter3 program flow control instructionsChapter3 program flow control instructions
Chapter3 program flow control instructions
HarshitParkar6677
 
Chapter3 8086inst stringsl
Chapter3 8086inst stringslChapter3 8086inst stringsl
Chapter3 8086inst stringsl
HarshitParkar6677
 
Chapter3 8086inst logical 2
Chapter3 8086inst logical 2Chapter3 8086inst logical 2
Chapter3 8086inst logical 2
HarshitParkar6677
 
Chapter 3 8086 ins2 math
Chapter 3 8086 ins2 mathChapter 3 8086 ins2 math
Chapter 3 8086 ins2 math
HarshitParkar6677
 
Chap3 program flow control instructions
Chap3 program flow control instructionsChap3 program flow control instructions
Chap3 program flow control instructions
HarshitParkar6677
 
Chap3 8086 logical
Chap3 8086 logicalChap3 8086 logical
Chap3 8086 logical
HarshitParkar6677
 

More from HarshitParkar6677 (20)

Wi fi hacking
Wi fi hackingWi fi hacking
Wi fi hacking
 
D dos attack
D dos attackD dos attack
D dos attack
 
Notes chapter 6
Notes chapter  6Notes chapter  6
Notes chapter 6
 
Interface notes
Interface notesInterface notes
Interface notes
 
Chapter6 2
Chapter6 2Chapter6 2
Chapter6 2
 
Chapter6
Chapter6Chapter6
Chapter6
 
8086 cpu 1
8086 cpu 18086 cpu 1
8086 cpu 1
 
Notes arithmetic instructions
Notes arithmetic instructionsNotes arithmetic instructions
Notes arithmetic instructions
 
Notes all instructions
Notes all instructionsNotes all instructions
Notes all instructions
 
Notes aaa aa
Notes aaa aaNotes aaa aa
Notes aaa aa
 
Notes 8086 instruction format
Notes 8086 instruction formatNotes 8086 instruction format
Notes 8086 instruction format
 
Misc
MiscMisc
Misc
 
Copy of 8086inst logical
Copy of 8086inst logicalCopy of 8086inst logical
Copy of 8086inst logical
 
Copy of 8086inst logical
Copy of 8086inst logicalCopy of 8086inst logical
Copy of 8086inst logical
 
Chapter3 program flow control instructions
Chapter3 program flow control instructionsChapter3 program flow control instructions
Chapter3 program flow control instructions
 
Chapter3 8086inst stringsl
Chapter3 8086inst stringslChapter3 8086inst stringsl
Chapter3 8086inst stringsl
 
Chapter3 8086inst logical 2
Chapter3 8086inst logical 2Chapter3 8086inst logical 2
Chapter3 8086inst logical 2
 
Chapter 3 8086 ins2 math
Chapter 3 8086 ins2 mathChapter 3 8086 ins2 math
Chapter 3 8086 ins2 math
 
Chap3 program flow control instructions
Chap3 program flow control instructionsChap3 program flow control instructions
Chap3 program flow control instructions
 
Chap3 8086 logical
Chap3 8086 logicalChap3 8086 logical
Chap3 8086 logical
 

Recently uploaded

Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 

Recently uploaded (20)

Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 

Chapter 6 notes

  • 1. 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. 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.
  • 2. 2 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. 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 Call Multiply Interrupt Here Call Multiply Main Line Interrupt Procedure Return to Interrupt Multiply procedure Return to Calling pgm
  • 3. 3 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
  • 4. 4  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.