0% found this document useful (0 votes)
25 views20 pages

Procedure 4

This document discusses procedures and macros in assembly language. It covers calling and returning from procedures using the CALL and RET instructions, passing parameters between procedures, and differences between near and far calls. It also discusses writing and calling macros, and compares macros to procedures. Macros allow grouping instructions under a name to avoid overhead of procedure calls, while procedures only need to load code once in memory.

Uploaded by

krishna_cad28
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)
25 views20 pages

Procedure 4

This document discusses procedures and macros in assembly language. It covers calling and returning from procedures using the CALL and RET instructions, passing parameters between procedures, and differences between near and far calls. It also discusses writing and calling macros, and compares macros to procedures. Macros allow grouping instructions under a name to avoid overhead of procedure calls, while procedures only need to load code once in memory.

Uploaded by

krishna_cad28
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/ 20

Chapter 6 - Procedures and Macros

Writing and using procedures

• Avoid writing the same sequence of instruction again and again.

• Write it in a separate subprogram and call that subprogram whenever


necessary.

• For that CALL instruction is used.


The CALL and RET instructions(contd.)

•Stores the address of the next instruction to be executed after the


CALL instruction to stack. This address is called as the return address.

•RET at the end of the procedure, it copies this value from stack back to
the instruction pointer (IP).
The CALL and RET instructions(contd.)

Chart for CALL and


RET instruction 
The CALL and RET instructions(contd.)
Types of CALL :
•WITHIN-SEGMENT NEAR CALL: produce the starting address of the
procedure by adding a 16-bit signed displacement to the contents of the
instruction pointer.

•INTERSEGMENT FAR CALL: used when the called procedure is in different


segment.
Difference between NEAR and Far
NEAR FAR
• Within Same CS • Within Different CS
• Replace old IP with new IP • Replace old pair CS:IP with new
• Value of IP is Pushed on the pair
stack • Value of pair CS:IP is Pushed on
• Also called as Intrasegment call the stack
• Also called as Intersegment call
Using PUSH and POP

• The PUSH register/memory instruction decrements the stack pointer


by 2 and copies he contents of the specified 16-bit register or
memory location to memory at the new top-of-stack location.

• The POP register/memory instruction copies the word on the top-of-


stack to the specified 16-bit register or memory location and
increments the stack pointer by 2.
Passing parameters to and from
procedures
Major ways of passing parameters to and from a procedure:

•In register
•In dedicated memory locations accessed by name
•With the stack
Reentrant and Recursive procedures

• Reentrant procedures: The procedure which can be interrupted, used


and “reentered” without losing or writing over anything.

• Recursive procedure: It is the procedure which call itself.


Writing and using Assembler Macros
Comparison Macros and Procedures
• A big advantage of using procedures is that the machine codes for the
group of instruction in the procedures needs to be loaded in to main
memory only once.
• Disadvantage using the procedures is the need for the stack.
• A macro is the group of instruction we bracket and give a name to at
the start of the program.
• Using macro avoids the overhead time involved in calling and
returning from a procedures.
Defining and calling a Macro without
parameters
Defining and calling a Macro with
parameters

Syntax:

NameMacro MACRO [parameter1, parameter2...]


Code of the macro
ENDM

You might also like