0% found this document useful (0 votes)
67 views7 pages

Macros and Directive

Macros allow code to be reused by expanding copies of the macro code wherever it is invoked in the assembly code, unlike procedures which use CALL and RET instructions; parameters can be passed to macros to customize the code inserted; macros must be defined before they are used and care must be taken to avoid excessively large macros increasing program size.

Uploaded by

Abdul Moeed
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)
67 views7 pages

Macros and Directive

Macros allow code to be reused by expanding copies of the macro code wherever it is invoked in the assembly code, unlike procedures which use CALL and RET instructions; parameters can be passed to macros to customize the code inserted; macros must be defined before they are used and care must be taken to avoid excessively large macros increasing program size.

Uploaded by

Abdul Moeed
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/ 7

File Handling, Macros and Conditional Assembly Directives

Macros:

• Macros are just like procedures, but they exist only until your code is
compiled, after compilation all macros are replaced with real instructions.
• When you invoke a macro procedure,a copy of its code is inserted directly into
the program at the location where it was invoked. This type of automatic code
insertion is also known as inline expansion.

• Macros are expanded during the assembler’s preprocessing step. In this step,
the preprocessor reads a macro definition and scans the remaining source code
in the program. At every point where the macro is called, the assembler inserts
a copy of the macro’s source code into the program

• Parameters Macro parameters are named placeholders for text arguments


passed to the caller. The arguments may in fact be integers, variable names, or
other values, but the preprocessor treats them as text. Parameters are not typed,
so the preprocessor does not check argument types to see whether they are
correct.
• Macro Definition
o name MACRO [parameters,...]
o <instructions>
o ENDM

Using Macros:

• When you want to use a macro, you can just type its name. For example:

o MyMacro

• Macro is expanded directly in program's code. So if you use the same macro 100
times, the compiler expands the macro 100 times, making the output executable
file larger and larger, each time all instructions of a macro are inserted.

• In general, macros execute more quickly than procedures because procedures have
the extra overhead of CALL and RET instructions. There is, however, one
disadvantage to using macros: repeated use of large macros tends to increase a
program’s size because each call to a macro inserts a new copy of the macro’s
statements in the program.

Passing Arguments to Macro:

• To pass parameters to macro, you can just type them after the macro name. For
example:

o MyMacro 1, 2, 3

• To mark the end of the macro ENDM directive is enough

Example:

• Unlike procedures, macros should be defined above the code that uses it.
• For Example
o .code
o mymacro macro p1,p2,p3
o mov ax,p1
o mov bx,p2
o mov cx,p3
o endm
o main proc
o mymacro 1,2,3
o mov ah,4ch
o int 21h
o main endp
o end

Defining Macros in Separate file:

Write these macros in file and name it mylib.lib and save it


in your main program directory path.
Output

Conditional Assembly Directives:


.Repeat directive :
Lab Tasks:
Q1: Write Assembly program which takes 5 numbers input from user
and write to file.

Q2: Write Assembly program which writes a String to a file. Define


String in program.

Q3: Write a macro code named Chars_Print taking two arguments


startingChar and numOfchars you want to print starting with
startingChar in one line. Save this macro in a file named hw.lib, and
then include it in your code. Try to invoke this macro to print characters
from A to Z.

Q4: Write a Assembly Language program that convert this pseudo code
Using Conditional high level directives (.while .if and .Else).

You might also like