SlideShare a Scribd company logo
http://www.tutorialspoint.com/assembly_prog ramming /assembly_macros.htm Copyright © tutorialspoint.com
ASSEMBLY - MACROS
Writing a macro is another way of ensuring modular programming inassembly language.
A macro is a sequence of instructions, assigned by a name and could be used anywhere inthe program.
InNASM, macros are defined with%macro and %endmacro directives.
The macro begins withthe %macro directive and ends withthe %endmacro directive.
The Syntax for macro definition:
%macro macro_name number_of_params
<macro body>
%endmacro
Where, number_of_params specifies the number parameters, macro_name specifies the name of the macro.
The macro is invoked by using the macro name along withthe necessary parameters. Whenyouneed to use
some sequence of instructions many times ina program, youcanput those instructions ina macro and use it
instead of writing the instructions allthe time.
For example, a very commonneed for programs is to write a string of characters inthe screen. For displaying a
string of characters, youneed the following sequence of instructions:
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
We have observed that, some instructions like IMUL, IDIV, INT, etc., need some of the informationto be stored
insome particular registers and evenreturns values insome specific register(s). If the programwas already
using those registers for keeping important data, thenthe existing data fromthese registers should be saved in
the stack and restored after the instructionis executed.
Inthe above example of displaying a character string also, the registers EAX, EBX, ECX and EDX have been
used by the INT 80H functioncall. So, eachtime youneed to display onscreen, youneed to save these registers
onthe stack, invoke INT 80H and thenrestore the originalvalue of the registers fromthe stack. So, it could be
usefulto write two macros for saving and restoring data.
Example:
Following example shows defining and using macros:
; A macro with two parameters
; Implements the write system call
%macro write_string 2
mov eax, 4
mov ebx, 1
mov ecx, %1
mov edx, %2
int 80h
%endmacro
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
write_string msg1, len1
write_string msg2, len2
write_string msg3, len3
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg1 db 'Hello, programmers!',0xA,0xD
len1 equ $ - msg1
msg2 db 'Welcome to the world of,', 0xA,0xD
len2 equ $- msg2
msg3 db 'Linux assembly programming! '
len3 equ $- msg3
Whenthe above code is compiled and executed, it produces the following result:
Hello, programmers!
Welcome to the world of,
Linux assembly programming!

More Related Content

PPTX
Macros...presentation
PPT
Mule security
PPTX
Exception
PPTX
Managing stack traces
PPT
SAS Macros part 3
PPTX
It322 intro 3
PPT
Chapter4.7-mikroprocessor
Macros...presentation
Mule security
Exception
Managing stack traces
SAS Macros part 3
It322 intro 3
Chapter4.7-mikroprocessor

Viewers also liked (10)

PPTX
Computer instruction
PDF
instruction set of 8086
PPTX
Time delay programs and assembler directives 8086
PDF
Instruction cycle
PPT
Assembly Language Lecture 5
PPS
Computer instructions
PPT
Types of instructions
PPT
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
PPT
Assembly Language Programming Of 8085
Computer instruction
instruction set of 8086
Time delay programs and assembler directives 8086
Instruction cycle
Assembly Language Lecture 5
Computer instructions
Types of instructions
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Assembly Language Programming Of 8085
Ad

Similar to N_Asm Assembly macros (sol) (20)

PDF
Task Perform addition subtraction division and multiplic.pdf
PDF
handout6.pdf
PDF
Unit 2
DOC
Chapter 5 notes new
DOC
Chapter 6 notes
PPT
PPT
Sas macros part 4.1
PPT
Preprocessors
PPT
SAS Macros part 1
PDF
N_Asm Assembly system calls (sol)
PDF
Module 5.pdf
PDF
N_Asm Assembly basic syntax (sol)
PDF
Handout#04
PPTX
System software - macro expansion,nested macro calls
PDF
Safe Clearing of Private Data
PPTX
Chap6 procedures &amp; macros
PPTX
C programming language tutorial
PDF
Macro-processor
DOCX
LA3-64 -vit assembly language program to Accept string and display its length...
PDF
Handout#05
Task Perform addition subtraction division and multiplic.pdf
handout6.pdf
Unit 2
Chapter 5 notes new
Chapter 6 notes
Sas macros part 4.1
Preprocessors
SAS Macros part 1
N_Asm Assembly system calls (sol)
Module 5.pdf
N_Asm Assembly basic syntax (sol)
Handout#04
System software - macro expansion,nested macro calls
Safe Clearing of Private Data
Chap6 procedures &amp; macros
C programming language tutorial
Macro-processor
LA3-64 -vit assembly language program to Accept string and display its length...
Handout#05
Ad

More from Selomon birhane (10)

PDF
N_Asm Assembly addressing modes (sol)
PDF
N_Asm Assembly arrays (sol)
PDF
N_Asm Assembly arithmetic instructions (sol)
PDF
N_Asm Assembly recursion (sol)
PDF
N_Asm Assembly strings (sol)
PDF
N_Asm Assembly numbers (sol)
PDF
N_Asm Assembly registers (sol)
PDF
N_Asm Assembly variables (sol)
PDF
Control system(smarajit ghosh) By sol
PDF
Two ports
N_Asm Assembly addressing modes (sol)
N_Asm Assembly arrays (sol)
N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly recursion (sol)
N_Asm Assembly strings (sol)
N_Asm Assembly numbers (sol)
N_Asm Assembly registers (sol)
N_Asm Assembly variables (sol)
Control system(smarajit ghosh) By sol
Two ports

Recently uploaded (20)

PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
top salesforce developer skills in 2025.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
System and Network Administraation Chapter 3
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
System and Network Administration Chapter 2
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
history of c programming in notes for students .pptx
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
ai tools demonstartion for schools and inter college
Reimagine Home Health with the Power of Agentic AI​
Odoo POS Development Services by CandidRoot Solutions
top salesforce developer skills in 2025.pdf
Operating system designcfffgfgggggggvggggggggg
System and Network Administraation Chapter 3
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
System and Network Administration Chapter 2
How Creative Agencies Leverage Project Management Software.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
wealthsignaloriginal-com-DS-text-... (1).pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Softaken Excel to vCard Converter Software.pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Adobe Illustrator 28.6 Crack My Vision of Vector Design
history of c programming in notes for students .pptx
CHAPTER 2 - PM Management and IT Context
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
ai tools demonstartion for schools and inter college

N_Asm Assembly macros (sol)

  • 1. http://www.tutorialspoint.com/assembly_prog ramming /assembly_macros.htm Copyright © tutorialspoint.com ASSEMBLY - MACROS Writing a macro is another way of ensuring modular programming inassembly language. A macro is a sequence of instructions, assigned by a name and could be used anywhere inthe program. InNASM, macros are defined with%macro and %endmacro directives. The macro begins withthe %macro directive and ends withthe %endmacro directive. The Syntax for macro definition: %macro macro_name number_of_params <macro body> %endmacro Where, number_of_params specifies the number parameters, macro_name specifies the name of the macro. The macro is invoked by using the macro name along withthe necessary parameters. Whenyouneed to use some sequence of instructions many times ina program, youcanput those instructions ina macro and use it instead of writing the instructions allthe time. For example, a very commonneed for programs is to write a string of characters inthe screen. For displaying a string of characters, youneed the following sequence of instructions: mov edx,len ;message length mov ecx,msg ;message to write mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel We have observed that, some instructions like IMUL, IDIV, INT, etc., need some of the informationto be stored insome particular registers and evenreturns values insome specific register(s). If the programwas already using those registers for keeping important data, thenthe existing data fromthese registers should be saved in the stack and restored after the instructionis executed. Inthe above example of displaying a character string also, the registers EAX, EBX, ECX and EDX have been used by the INT 80H functioncall. So, eachtime youneed to display onscreen, youneed to save these registers onthe stack, invoke INT 80H and thenrestore the originalvalue of the registers fromthe stack. So, it could be usefulto write two macros for saving and restoring data. Example: Following example shows defining and using macros: ; A macro with two parameters ; Implements the write system call %macro write_string 2 mov eax, 4 mov ebx, 1 mov ecx, %1 mov edx, %2 int 80h %endmacro section .text global _start ;must be declared for using gcc _start: ;tell linker entry point write_string msg1, len1 write_string msg2, len2 write_string msg3, len3 mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel
  • 2. section .data msg1 db 'Hello, programmers!',0xA,0xD len1 equ $ - msg1 msg2 db 'Welcome to the world of,', 0xA,0xD len2 equ $- msg2 msg3 db 'Linux assembly programming! ' len3 equ $- msg3 Whenthe above code is compiled and executed, it produces the following result: Hello, programmers! Welcome to the world of, Linux assembly programming!