Vous êtes sur la page 1sur 12

Implementation Examples

MASM Macro Processor ANSI C Macro language

MASM Macro Processor


The macro processor of MASM is integrated with Pass 1 of the assembler It supports all functions of a macro processor such as definition and invocation of macro instructions. MASM generates the unique names of local labels in the form ??n
n is a hexadecimal number in the range 0000 to FFFF

.ERR: signals to MASM that an error has been detected EXITM: directs MASM to terminate the expansion of the macro The macro parameters in MASM need not begin with & or any special character

MASM Macro Processor


;; is a macro comment, serves only as documentation for the macro definition ignored during expansion ; is an ordinary assembler language comment, included as part of the macro expansion & is a concatenation operator IRP: sets the macro-time variable to a sequence of values specified in <> The statements between the IRP and the matching ENDM are generated once for each value of the variable

Examples of MASM Macro and Conditional Statements

Examples of MASM Macro and Conditional Statements

Examples of MASM Macro and Conditional Statements

Example of MASM Iteration Statement

ANSI C MACRO LANGUAGE


Definitions and invocations of macros are handled by a preprocessor, which is generally not integrated with the rest of the compiler. Example #define NULL 0 #define EOF (-1) #define EQ = = The programmer could write while (I EQ0) The macro processor would convert while(I ==0)

ANSI C MACRO LANGUAGE


#define ABSDIFF (X,Y) ((X)>(Y)?(X)-(Y):(Y)-(X)) Macro name ABSDIFF Parameter X,Y Uses c language (X) >(Y) =>TRUE THEN (X)-(Y) else (Y)-(X) When macro is expanded each occurrence of macro parameter is replaced by corresponding argument.

ANSI C Macro Language


Parameter substitutions are not performed within quoted strings: #define DISPLAY(EXPR) printf(EXPR= %d\n, EXPR)
Example
DISPLAY(I*J+1) ==> printf(EXPR= %d\n, I*J+1)

Stringizing operator, #
Used to perform argument substitution in quoted strings #define DISPLAY(EXPR) printf(#EXPR = %d\n, EXPR)

Example DISPLAY(I*J+1) ==> printf(I*J+1 = %d\n, I*J+1)

ANSI C Macro Language


Recursive macro definitions or invocations After a macro is expanded, the macro processor rescans the text that has been generated, looking for more macro definitions or invocations. Macro cannot invoke or define itself recursively. Example DISPLAY(ABSDIFF(3,8))

printf(ABSDIFF(3,8) = %d\n, ABSDIFF(3,8))

printf(ABSDIFF(3,8) = %d\n, ( (3)>(8) ? (3)-(8) : (8)-(3) ))

ANSI C Macro Language


Conditional compilation statements Example 1
#ifndef BUFFER_SIZE #define BUFFER_SIZE 1024 #endif #define DEBUG 1 : #if DEBUG == 1 printf() /* debugging outout */

Example 2

#endif

Vous aimerez peut-être aussi