Assembly - Macros
Assembly - Macros
Assembly - Macros
Writing a macro is another way of ensuring modular programming in assembly
language.
The macro is invoked by using the macro name along with the necessary
parameters. When you need to use some sequence of instructions many times in a
program, you can put those instructions in a macro and use it instead of writing the
instructions all the time.
For example, a very common need for programs is to write a string of characters in
the screen. For displaying a string of characters, you need the following sequence of
instructions −
In the above example of displaying a character string, the registers EAX, EBX, ECX
and EDX have been used by the INT 80H function call. So, each time you need to
display on screen, you need to save these registers on the stack, invoke INT 80H
https://www.tutorialspoint.com/assembly_programming/assembly_macros.htm 1/3
6/15/24, 3:59 PM Assembly - Macros
and then restore the original value of the registers from the stack. So, it could be
useful to write two macros for saving and restoring data.
We have observed that, some instructions like IMUL, IDIV, INT, etc., need some of
the information to be stored in some particular registers and even return values in
some specific register(s). If the program was already using those registers for
keeping important data, then the existing data from these registers should be saved
in the stack and restored after the instruction is executed.
Example
Following example shows defining and using macros −
section .text
global _start ;must be declared for using gcc
section .data
msg1 db 'Hello, programmers!',0xA,0xD
len1 equ $ - msg1
https://www.tutorialspoint.com/assembly_programming/assembly_macros.htm 2/3
6/15/24, 3:59 PM Assembly - Macros
When the above code is compiled and executed, it produces the following result −
Hello, programmers!
Welcome to the world of,
Linux assembly programming!
https://www.tutorialspoint.com/assembly_programming/assembly_macros.htm 3/3