MASM macro usage Summary

Source: Internet
Author: User

MASM (Macro volume ER) is an assembly tool provided by Microsoft. Although it has been around for some years, it still exists in newer tools such as Many compilation textbooks take this as an object and show how to use compilation to design a program as a basic course for students in the Computer Science Department. However, the content is basically in version 5.1 and in the DOS era. Although the compilation under win32 is mentioned, it is not at the top of the list. Another thing that is ignored is the macro which is the biggest characteristic of MASM. It is not detailed about how to view the powerful macros provided in the Assembly tool, and how to use the Macros in what scenarios. This article is a summary of the author's extensive use of the MASM macro after building an OOP system in an assembly environment.

Using macros to reduce repeated coding and build powerful functions is a powerful tool for code reuse and code beautification. Macros are something that should be avoided in advanced languages, which is not necessarily the case in low-level languages.

Macro is preprocessing.
Macros are pre-processed before the code is compiled into an obj file. Because it occurs in the Assembly period (Assembly-Time, and the compilation period in advanced languages), it does not burden the execution period and can be used as a code generation tool, the setting is the same as the template in C ++ and serves as a meta-programming tool. In MASM, macros can be divided into two types: 1. Text Macro 2. Procedure (Function) Macro. The first macro is a macro with simple text replacement such as # define pi 31415926, and the second macro with parameters and local variables, return values can be treated as macros of functions or processes. Next we will start with Text Macro to see how to use simple macros.

Simple text macro
You can specify a symbolic name for a character sequence, and use this name in the rest of the source code to replace this character sequence. The text with the specified name is a text macro. To put it bluntly, replace the text. Use TEXTEQU to define such a macro.

Name TEXTEQU textvar
Name TEXTEQU % numvar

The usage instructions I provide here are not the same as those provided in the MASM Programmer Guide, but they are more informative. I will only explain the first usage here. The subsequent usage will be explained after "Assembly period variables. For example.

Pi TEXTEQU <3.1416>
Arg1 TEXTEQU <[bp + 4]>

In the code, you can use pi to replace 3.1416. <> It indicates that they are strings. If you do not add <>, the string you give will be evaluated as a text variable in the compilation period. In this case, errors will occur.

Variables and constants during assembly
These things actually have their own names. In fact, in terms of usage, they mean constants and variables in the compilation period. For example, Text Macro (pair, that is, the previous Text Macro) is used as a Text constant in the compilation period, and Name Assignment is used as a numerical variable in the compilation period.

Define constants during assembly

What is a constant in the Assembly period? It is actually a constant, because it is static in both the compilation and execution phases, and its value cannot be changed once defined. Recall in C that you use # define to define constants. However, # define can change the value equal to a macro, that is, whether a constant or not requires your maintenance (the compiler will give a warning ). There is a keyword in MASM specifically used to define constants. An error message is displayed when you try to change the value of a constant.

Name EQU expression
Name EQU

The first is used to define a "value" constant, and the second is used to define a "text" constant. In the future, we need to distinguish between the start text and the value.

Define text variables in the compilation phase

In the compilation phase, text variables are another idea of "text macro. In fact, they are the same thing. After you define a text macro, You can regard the macro name as the name of the text variable in the compilation period, and the text content replaced by the macro name as the text value of the variable.

The second usage: name TEXTEQU textvar is easy to understand. Is to assign a text variable to another text variable. For example:

Taowen TEXTEQU talent

The first line defines an assembly text variable named talent, and the second line assigns the value of talent to the variable named taowen. The result shows the sum:


The same is true, but the second approach is to change the talent to genius first because of the function of text macro replacement. The actual effect is as follows:


Show text variable content

Printf is often used in C, and some variable content is displayed during runtime for debugging. In MASM, ECHO is used to display the content of text variables during compilation.

China textequ
% Echo China

In this case, great country appears in the command line during assembly. If you remove the % Number, China is displayed. You should be able to deduce what % is, that is, evaluate a variable.

Define the variable of the Assembly period value

There are two types of constants, so there should be two types of variables. Here we will introduce the usage of the number of variable values in the Assembly.

Name = expression

Expression is a numeric expression, for example:

Val = 3 + 4

In this case, Val is a numerical variable whose value is 7. You can also write:

Valexp textequ <3 + 4>
Val = valexp

It seems that a text variable is assigned to a numeric variable for type conversion (the effect is the same ). Actually, 3 + 4 is written to valexp, because the text macro replaces the text.

Assign a value variable to a text variable

We have seen how to "assign a text variable to a numeric variable". What is the opposite?

Val = 3 + 4
Valexp textequ Val

The error message is STest. asm (15): error A2051: text item required. The assembler needs text items, so we can add <>.

Val = 3 + 4

Check with % echo valexp and you will find that it is not 7 as you wish, but val. This is because <> the assembler considers val to be a string. Because the numeric variable is not a macro of text replacement, val is not replaced with 7, so of course val is displayed. The correct method is:

Val = 3 + 4
Valexp TEXTEQU % val

% Is used as the evaluate value, just like the previous one. Recall that the third in the usage of the document macro introduced at the beginning is: name TEXTEQU % numvar. This function assigns the value of a numeric variable to a text variable and is often used to display the value of a numeric variable. This is written during debugging. Pi = 1, 3.1415926
Temp TEXTEQU % pi
% Echo temp

This is an important Debugging Technique.

Macro process and macro function
Previously, the constants and variables in the compilation period were derived from a simple text macro. If it is only used in code outside the macro, it is enough to simply replace the text macro. They are mostly used in complex macros, which can be seen as processes and functions. Like variables in the Assembly period, they are used in the assembly period.

Text Macro will not be treated as a macro, but as a text variable. Macros directly refer to macro processes or macro functions. Macro processes are macros without return values, while macro functions are macros with return values. Both of them can contain parameters or local variables. In fact, they can be integrated into macro functions or assembler functions.

Create a simple macro in the following format

Name macro

Statements can be used for judgment or loop, which is a very full function. However, the functions in the compilation period are very different from those in the execution period. One is preprocessing in the compilation period, and the other is to change the execution position in the execution period and return the result after executing a piece of code.

Clear_eax_m macro
XOR eax, eax
Clear_eax_p proc
XOR eax, eax
Clear_eax_p endp

I suppose you have understood the difference between the two. If you do not understand it, you can refer to any compilation textbook with complete assembly code on it to explain why it is different.

Passing parameters to macros
The importance of parameters for functions is self-evident. macro parameters are defined as follows.

Name macro parameterlist

In simple cases, parameterlist is used as the parameter name, with an asterisk (_), for example:

Clear_reg MACRO reg
Xor reg, reg

This format is used for calling:

Clear_reg eax

This is the only calling format for macro processes.

The parameter transfer is also very different from the parameter transfer of the function during the execution period. Parameters are directly replaced. You can perform this experiment:

TestMacro MACRO param
Echo param
% Echo param
TestMacro TextVar

The output result is TextVar and Hello. I don't need to say much about what is going on. You can even further test:

TestMacro MACRO param
TestMacro TextVar
% Echo TextVar

The output result is How are you. The so-called parameter is replaced. The parameter name will be replaced by a number of arguments (the arguments are those passed during the call ). In the macro system of MASM, all the symbols of these variable names are in a common space (haha, how does it sound like a mathematical term ?), Is global.

You can make some restrictions on the parameter. For example, you must pass this parameter when calling it:

Clear_reg MACRO reg: REQ
Xor reg, reg

Or specify a default value:

Clear_reg MACRO reg: = xor reg, reg

You can also set the number of parameters to a variable.

Clear_reg MACRO regs: VARARG
FOR reg,
Xor reg, reg

Note that the VARARG parameter must be the last one in the parameter.

Returns a macro value.
The difference between a macro process and a macro function is whether a return value exists. Of course, the return value here is also very different from the return value of the function during the execution period. Functions in the execution period pass the return value through eax. Here, it is just a direct replacement. The syntax of the returned value is as follows:

EXITM textitem

A macro function can have multiple exitm, just as a function in C can have multiple return. However, the returned values must be consistent. Let's look at a simple example.

Who macro
% Echo who ()

The result is taowen. If () is removed, the WHO is displayed. Therefore, you must add () to macro function calls (). You cannot add () to the macro call process (). Let's look at an interesting example:

Who macro temp
% Echo temp
WHO ()

The result is (). Description () is used as a parameter passed to the macro process.

You can use the return value as needed.

Who macro
WHO () textequ

In this way, the Assembly text variable is defined with the value genius. It can be seen that macro functions can be used in any place where text variables can appear. In many places, the upper-level languages can include those in the number of functions.

Local variable
A macro can have a local variable. It looks like a local variable, but it is actually just a bit of name tips.

There are two facts about local variables: 1. They cannot be accessed outside the function; 2. Their values should not be affected by the previous call in different calls to the function.

TestMacro MACRO
LOCAL LocalVar
% Echo LocalVar

If function calls affect each other, call the following method:


For the first time, an error with undefined variables will be generated, and for the second time, Hello will be output. In fact, LocalVar is LOCAL. So both are undefined errors. This reflects the independence of multiple calls to local variables.

Next we will expose the cards of local variables. You don't need to talk about it. You can simply look at it and you will understand it:

TestMacro MACRO
LOCAL LocalVar
Echo LocalVar

The output result is :?? 0000 and ?? 0001. This is the actual name of the local variable. A local variable is called by a strange name to make it inaccessible to the outside (you don't know what it is), and then replaces the local variable name with a different name each time you expand the same macro, so that multiple calls do not affect each other.

In fact, you can test this:

?? 0000 TEXTEQU
Testmacro macro
Local localvar
% Echo localvar

The displayed result is hello, so that local variables are accessed outside a macro process (function.

Text operations
MASM has two built-in text operation functions: Macro function and directive function. The functions are the same, but the presentation flexibility is provided. Name catstr [[textitem1 [[, textitem2]...]
Name instr [[position,] textitem1, textitem2
Name sizestr textitem
Name substr textitem, position [[, length]

This set is Directive, which is used to connect the text, search for the Child text, obtain the text length, and obtain the child text.

@ Catstr (string1 [[, string2. ..])
@ Instr ([[position], string1, string2)
@ Sizestr (string)
@ SubStr (string, position [[, length])

This is a macro function. An example shows that the two sets are actually the same:

Taowen TEXTEQU @ CatStr (,)
% Echo taowen

Taowen CATSTR,
% Echo taowen

We can see that He is genius is output in both examples. Note: 1. A Macro function such as @ CatStr can be used as the left value and assigned a value. 2. macro functions such as @ CatStr do not automatically evaluate parameters. When the behavior is different from what you want, add %.

It is not difficult to use it. You can test it. One point is that the index of the first character is 1, rather than 0 in C.

% And evaluate
% May be the most difficult syntax to use. In general, when the behavior is different from what you think, add % to test it. % And <> and! .

Generally, you do not need the "%" sign. % Can be used to convert numeric variables into text variables. You can use "%" to forcibly retrieve the text value. For example:

Index = 0
NameT CATSTR, % Index
% Echo Person0

First, % is used to convert the value to text, and the second % is used to convert NameT to Person0. This article also demonstrates an important technique for generating variable names. You can build an array of variables by incrementing indexes.

<> You can place the evaluated value to a certain extent. However, in most cases, because the replacement of the text macro is not affected, the replaced value is still obtained .! Used to cancel the original meaning of the symbol. For example:

Symbol CATSTR ,,
% Echo Symbol

The output result is Go and Go. If not! Then, the number will cause an error. Interestingly, if you add it after the second Go! It should have been Go, Go !. The result is indeed an error with the right angle brackets missing. It turns out to be! The original interesting change of> is no longer an end. If you do this:

Symbol CATSTR,>
% Echo Symbol

Go, Go>. Let's see what's going on. To generate !, Write as follows:

Symbol CATSTR ,,
% Echo Symbol

When to use it <> when to use it, I think it is best to use it <> when to use it <>. The specific reason is that the application scope can be increased.

Loop in macros
There are four types of loops: WHILE, REPEAT, FOR, and FORC. Syntax:

WHILE expression
REPEAT expression
FOR parameter [[: REQ |: = default], statements
FORC parameter,

WHILE and REPEAT are the same from usage to effect, at least I think it is the same. Expression requires a value. You can use EQ (equal to), LT (less than) to compare Operator values.

I = 0
While I LT 10
% Echo Temp
I = I + 1

The output result is 0 to 9.

FOR and FORC are special-purpose loops. One is used to obtain parameters in a parameter list, and the other is to extract each character in a string one by one. Let's take two examples to understand:

TestMacro MACRO params: VARARG
FOR param,
% Echo param
TestMacro arg1, arg2, arg3

The displayed result is arg1, then arg2, and then arg3.

FORC char,
% Echo char

H, e, l, and o are displayed respectively.

Macro-based judgment
The judgment is simple, but there are many options such as IF, IFDEF, IFIDN, IFE, IFNDEF, and IFDIF. Nothing worth noting is easy to use. Determine the value, whether the symbol is defined, and whether the two texts are consistent.

Macros are also defined in macros, OPATTR, SIZEOF, LENGTHOF, and many other advanced things. However, I believe that there is a foundation mentioned above. The usage of these things is just a matter of checking the manual.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.