SlideShare a Scribd company logo
1 of 6
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.
Concepts related to Procedures:
Stack operations:
A stack is a separate section of memory set aside to store return addresses &
data whenever a subroutine is executed.
3F
48
42
37
:
:
:
21
43
8086 allows the user to set aside an entire 64K byte segment as stack. The
upper 16 bits of starting address of stack are stored in Stack segment (SS) register.
The stack pointer (SP) register is used to hold the 16 bit offset from the start of the
segment to memory location where a word was almost recently stored on stack. The
memory location where a word is most recently stored is called the Top of the stack.
The 20 bit physical address for a stack operation is produced by shifting the
stack segment register (SS) by 4 bit positions and adding the contents of the stack
point (SP) contents to it.
If SS = 3000h, SP=FFFFH, p.a. = 3 0 0 0 0 +
F F F F
-----------
3 F F F F
The stack can be accessed by PUSH & POP instructions.
PUSH instruction:
A PUSH reg. instruction is used to save the 16 bit data of the specified register
on the stack.
Example:
PUSH BX ; BX = 1234H
PUSH AX ; AX = ABCDH
If SP is initialized with 3FFF, whenever a PUSH instruction is encountered,
the SP is decremented by two and the contents of the specified register are saved on
the stack with lower order register contents stored at SP– 2 location and higher order
stored at SP-1 location.
3FFFF  SP
(Top of the stack)
3000  Start of the stack
SP  3FFF
3FFE 12
3FFD 34
3FFC AB
SP  3FFB CD
:
:
:
3001
3002
A POP reg. instruction is used to retrieve data back to the contents of the
specified 16 bit register. Whenever a POP instruction is executed, the contents of the
SP & SP+1 locations are stored in the lower order and higher order register
respectively and stack pointer is incremented by 2. The last word PUSHed in the stack
if POPed out first. Thus stack operation is on LIFO basis.
Example:
POP BX
POP AX
3FFF
3FFE 12
3FFD 34
3FFC AB
SP  3FFB CD
:
:
:
3001
3002
After execution, BX = ABCD H & AX = 1234 H. Thus the contents of the registers
are exchanged due to the order in which the contents are POPed back from the stack.
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
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 addressor 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.
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 :
CODE SEGMENT
START : MOV AX, DATA
MOV DS,AX
MOV AX, STACK
MOV SS, AX
LEA SP, STACK_TOP
MOV AX, NUMBER
PUSH AX
CALL FACTO
:
:
Call
Multiply
Interrupt
Here
Call
Multiply
Main Line
Interrupt
Procedure
Return to
Interrupt
Multiply
procedure
Return to Calling pgm
PROCEDURE FACTO
PUSH AX
:
:
:
;Code for checking the number in AX =1 or not
;If AX <>1
CALL FACTO ; calling FACTO within FACTO.
If AX =1
:
:
POP AX
RET ; ret to calling program
FACTO 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.
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 (20)

Buffer overflow attack
Buffer overflow attackBuffer overflow attack
Buffer overflow attack
 
Chapter 3 programming concepts-ii
Chapter 3  programming concepts-iiChapter 3  programming concepts-ii
Chapter 3 programming concepts-ii
 
Introduction to Assembly Language
Introduction to Assembly LanguageIntroduction to Assembly Language
Introduction to Assembly Language
 
Chapter 2 programming concepts - I
Chapter 2  programming concepts - IChapter 2  programming concepts - I
Chapter 2 programming concepts - I
 
Loader
LoaderLoader
Loader
 
Compilers
CompilersCompilers
Compilers
 
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
 
Programming Assignment Help
Programming Assignment HelpProgramming Assignment Help
Programming Assignment Help
 
TLPI - 6 Process
TLPI - 6 ProcessTLPI - 6 Process
TLPI - 6 Process
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
MASM -UNIT-III
MASM -UNIT-IIIMASM -UNIT-III
MASM -UNIT-III
 
L kernel-logging-apis-pdf
L kernel-logging-apis-pdfL kernel-logging-apis-pdf
L kernel-logging-apis-pdf
 
ARM Architecture Subroutine and Flags
ARM Architecture Subroutine and FlagsARM Architecture Subroutine and Flags
ARM Architecture Subroutine and Flags
 
Compreport
CompreportCompreport
Compreport
 
Computer Science Assignment Help
Computer Science Assignment HelpComputer Science Assignment Help
Computer Science Assignment Help
 
Hardware interfacing basics using AVR
Hardware interfacing basics using AVRHardware interfacing basics using AVR
Hardware interfacing basics using AVR
 
Analysis 2-methods-02p
Analysis 2-methods-02pAnalysis 2-methods-02p
Analysis 2-methods-02p
 
Macro
MacroMacro
Macro
 
Operating System Assignment Help
Operating System Assignment HelpOperating System Assignment Help
Operating System Assignment Help
 
10a log
10a log10a log
10a log
 

Similar to Chapter 5 notes

Chap6 procedures &amp; macros
Chap6 procedures &amp; macrosChap6 procedures &amp; macros
Chap6 procedures &amp; macrosHarshitParkar6677
 
Unit 4 assembly language programming
Unit 4   assembly language programmingUnit 4   assembly language programming
Unit 4 assembly language programmingKartik Sharma
 
Assembly level language
Assembly level languageAssembly level language
Assembly level languagePDFSHARE
 
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 8086Urvashi Singh
 
Chapter Seven(1)
Chapter Seven(1)Chapter Seven(1)
Chapter Seven(1)bolovv
 
Addressing modes (detailed data path)
Addressing modes (detailed data path)Addressing modes (detailed data path)
Addressing modes (detailed data path)Mahesh Kumar Attri
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Bilal Amjad
 
Chapter 4 programming concepts III
Chapter 4  programming concepts IIIChapter 4  programming concepts III
Chapter 4 programming concepts IIISHREEHARI WADAWADAGI
 
Module 2 instruction set
Module 2 instruction set Module 2 instruction set
Module 2 instruction set Deepak John
 

Similar to Chapter 5 notes (20)

Chap6 procedures &amp; macros
Chap6 procedures &amp; macrosChap6 procedures &amp; macros
Chap6 procedures &amp; macros
 
Unit 4 assembly language programming
Unit 4   assembly language programmingUnit 4   assembly language programming
Unit 4 assembly language programming
 
Co&amp;al lecture-07
Co&amp;al lecture-07Co&amp;al lecture-07
Co&amp;al lecture-07
 
[ASM]Lab6
[ASM]Lab6[ASM]Lab6
[ASM]Lab6
 
Assembly level language
Assembly level languageAssembly level language
Assembly level language
 
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
 
Procedure
ProcedureProcedure
Procedure
 
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
 
class-Stacks.pptx
class-Stacks.pptxclass-Stacks.pptx
class-Stacks.pptx
 
Micro overview
Micro overviewMicro overview
Micro overview
 
Mp lab manual
Mp lab manualMp lab manual
Mp lab manual
 
Chapter Seven(1)
Chapter Seven(1)Chapter Seven(1)
Chapter Seven(1)
 
Addressing modes (detailed data path)
Addressing modes (detailed data path)Addressing modes (detailed data path)
Addressing modes (detailed data path)
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
 
Chapter 4 programming concepts III
Chapter 4  programming concepts IIIChapter 4  programming concepts III
Chapter 4 programming concepts III
 
Alp 05
Alp 05Alp 05
Alp 05
 
seminar 4 sem
seminar 4 semseminar 4 sem
seminar 4 sem
 
Module 2 instruction set
Module 2 instruction set Module 2 instruction set
Module 2 instruction set
 

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

HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 

Recently uploaded (20)

HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 

Chapter 5 notes

  • 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. Concepts related to Procedures: Stack operations: A stack is a separate section of memory set aside to store return addresses & data whenever a subroutine is executed. 3F 48 42 37 : : : 21 43 8086 allows the user to set aside an entire 64K byte segment as stack. The upper 16 bits of starting address of stack are stored in Stack segment (SS) register. The stack pointer (SP) register is used to hold the 16 bit offset from the start of the segment to memory location where a word was almost recently stored on stack. The memory location where a word is most recently stored is called the Top of the stack. The 20 bit physical address for a stack operation is produced by shifting the stack segment register (SS) by 4 bit positions and adding the contents of the stack point (SP) contents to it. If SS = 3000h, SP=FFFFH, p.a. = 3 0 0 0 0 + F F F F ----------- 3 F F F F The stack can be accessed by PUSH & POP instructions. PUSH instruction: A PUSH reg. instruction is used to save the 16 bit data of the specified register on the stack. Example: PUSH BX ; BX = 1234H PUSH AX ; AX = ABCDH If SP is initialized with 3FFF, whenever a PUSH instruction is encountered, the SP is decremented by two and the contents of the specified register are saved on the stack with lower order register contents stored at SP– 2 location and higher order stored at SP-1 location. 3FFFF  SP (Top of the stack) 3000  Start of the stack
  • 2. SP  3FFF 3FFE 12 3FFD 34 3FFC AB SP  3FFB CD : : : 3001 3002 A POP reg. instruction is used to retrieve data back to the contents of the specified 16 bit register. Whenever a POP instruction is executed, the contents of the SP & SP+1 locations are stored in the lower order and higher order register respectively and stack pointer is incremented by 2. The last word PUSHed in the stack if POPed out first. Thus stack operation is on LIFO basis. Example: POP BX POP AX 3FFF 3FFE 12 3FFD 34 3FFC AB SP  3FFB CD : : : 3001 3002 After execution, BX = ABCD H & AX = 1234 H. Thus the contents of the registers are exchanged due to the order in which the contents are POPed back from the stack. Declaring PROCEDURES: The syntax for procedure declaration: name PROC [NEAR/FAR] ; Instructions of the procedures ; are written here. RET name ENDP
  • 3. Example: Delay PROC AGAIN: MOV CX,COUNT 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 addressor 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
  • 4. : : 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 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 : CODE SEGMENT START : MOV AX, DATA MOV DS,AX MOV AX, STACK MOV SS, AX LEA SP, STACK_TOP MOV AX, NUMBER PUSH AX CALL FACTO : : Call Multiply Interrupt Here Call Multiply Main Line Interrupt Procedure Return to Interrupt Multiply procedure Return to Calling pgm
  • 5. PROCEDURE FACTO PUSH AX : : : ;Code for checking the number in AX =1 or not ;If AX <>1 CALL FACTO ; calling FACTO within FACTO. If AX =1 : : POP AX RET ; ret to calling program FACTO 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. 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
  • 6. 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.