Topic: Macros All macros must be specified in NASM format. Show each macro defin
ID: 3720588 • Letter: T
Question
Topic: Macros
All macros must be specified in NASM format.
Show each macro definition and also give an example of the macro call and macro expansion.
Use the C library.
Example:
%macro mWrite 1
;
; Writes a string literal to standard output with newline.
; Receives: a string enclosed in single or double
; quotes (null terminator not required).
; Uses puts
;------------------------------------------------------
segment .data
%%string: db %1,10,0
segment .text
push dword %%string
call puts
add esp,4
%endmacro
Macro call:
mWrite “I love assembly language!”
Expansion:
segment .data
%%string: db “I love assembly language!”,10,0
segment .text
push dword %%string
call puts
add esp,4
(NASM listing doesn't show the generated labels.)
1. Write a macro PrintHxDc that prints the contents of a given memory location, first in hex and then in decimal. Parameter is the memory address (label).
2. Write a macro CopyBlock that copies a block of memory. Parameters are source address, destination address and length.
3. Write a macro PrintTime that prints the current time. No parameters. (See example in Duntemann that was discussed in class.)
4. Write a macro that simulates 3-operand instructions by creating a 3-operand add and subtract in the format add3 dest,source1,source2 and sub3 dest,source1,source2 where source1, source2 and dest can be any legal 32-bit location. The macro should use EAX for temporary storage.
Assume all variables are signed 32-bit quantities, except for when you can tell they are characters.
Explanation / Answer
Ans :
A unit of specification for a program generation is called a macro. It consists of name, set of formal parameters and body of code. When a macro name is used with a set of actual parameters it is replaced by a code generated from its body. This code is called macro expansion. There are two
types of expansions:
1. lexical expansion
2. Semantic expansion
Lexical expansion: It means a replacement of character string by another string during program generation. It is generally used to replace occurrences of formal parameters by corresponding actual ones. Semantic Expansion: It implies generation of instructions build to the requirements of specific usage. It is characterized by the fact that different uses of a macro can lead to codes which differ in the number, sequence and opcodes of instructions. The macro definition is located at the beginning of the program is enclosed between a macro header and macro end statement.
A macro statement contains macro name and parameters < macro name > { < parameters>}
A macro call: A macro is called by writing the macro name in the mnemonic field of
an assembly statement.