1. Usage of MASM macro

Source: Internet
Author: User

MASM macro usage Summary
Introduction
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 vc.net. 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 <text>
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>
Dwptr textequ <word PTR>
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 <text>

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:

Talent textequ <genius>
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:

Talent textequ <genius>
Taowen textequ <talent>

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:

Taowen textequ <genius>

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 <great country>
% 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
Valexp textequ <Val>

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
Endm

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
Endm
Clear_eax_p proc
XOR eax, eax
RET
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
Statements
Endm

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

Clear_reg macro Reg
XOR Reg, Reg
Endm

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
Endm
Textvar textequ <Hello>
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
Param textequ Endm
Textvar textequ <Hello>
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
Endm

Or specify a default value:

Clear_reg macro REG: = <eax>
XOR Reg, Reg
Endm

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

Clear_reg macro regs: vararg
For Reg, <regs>
XOR Reg, Reg
Endm
Endm

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
Exitm <taowen>
Endm
% 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
Endm
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
Exitm <taowen>
Endm
WHO () textequ <genius>

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
Localvar textequ <Hello>
Endm

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

Testmacro
Testmacro

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
Endm
Testmacro
Testmacro

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 <Hello>
Testmacro macro
Local localvar
% Echo localvar
Endm
Testmacro

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 <person>, % Index
% Namet textequ <taowen>
% 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 <go >,<!,>, <Go>
% 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 <go >,<!,>, <Go!>
% Echo symbol

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

Symbol catstr <go >,<!,>, <Go!>
% 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
Statements
Endm
Repeat expression
Statements
Endm
For parameter [[: req |: = default], <argument [[, argument]...>
Statements
Endm
Forc parameter, <string>
Statements
Endm

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
Temp textequ % I
% Echo temp
I = I + 1
Endm

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, <Params>
% Echo Param
Endm
Endm
Testmacro arg1, arg2, arg3

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

Forc char, <Hello>
% Echo char
Endm

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.

PS:

Repeat the usage of the MASM macro. By Kiki (published on)

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: info-contact@alibabacloud.com 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.