I haven't posted 9cbs for a long time, Gigix left 9CBS ... (Silent)
Recently used MASM macro has an alternative thing, the OOP object model in the assembly environment. Most of the functionality of the C object model is made. Based on the existing things that have existing online, it is far more than. Waiting for me to completely write it, I am sending it out to everyone. But everyone can come from here:
Participate in this alternative discussion
Thanks to AllaboutProgram.com to give me such a good look.
My personal home page is: New version of the personal homepage
This article is a summary of me spending a night. From a relatively essential perspective, some macro behavior is not the level of the textbook setting is the extent of Programmer Guide. It seems that there is no taste, but if you want to use the macro, you will find that I have almost no nonsense. Not to teach you how to use the macro, but you don't have to learn after you have seen it, you can write it yourself. Although there are not a few guys are still using MASM, fewer people use Macro, which may be this style of my alternative.
MASM Macro Summary
Introduction
MASM (Macro Assembler) is a compilation tool provided by Microsoft, although some years, but still exist in Vc.Net to compare new tools. There are many compilation textbooks to describe how to design a program with compilation, as the basic class of computer science students. However, the content that is described is generally in version 5.1, and stays in the era of DOS. Although the compilation of Win32 is mentioned, it is not placed in the primary position. Another neglected is a macro that is MASM's biggest feature, how to look at the powerful macro provided in the assembly tool, and how to use the macro in any site, the language is unknown. This article is a summary of the author after a large number of MASM macros, built an OOP system in a compilation environment.
Good use macro, can reduce duplicate coding, and build a powerful function, a powerful tool for reusing code, beautifying code. Macro in advanced language is something that should be avoided, and it is not necessary in the low language.
Macro is preprocessing
The macro is preprocessing before the code is compiled as an OBJ file. Since the occurrence period (Assembly-Time, and the compile period in the senior language is one meaning), it will not bring a burden on the execution period, which can be used as a code generation tool, settings, and the template in C , used as META -Programming tool. Macro in Masm can be divided into two: 1, text macro 2, procedure (function) macro. The first macro is a simple text of #define pi 31415926, the second is the kind of parameter, which can have a local variable, which can return the value such as a function or process. Let's start with Text Macro to see how to use a simple macro.
Simple text macro
You can specify a symbolic name to a character sequence, then use this name in the rest of the source code instead of this character sequence. This designation of the name is text macro. To put it bluntly, it is a text replacement. Use TeXtequ to define a macro.
Name TextEqu
Name Textequ Textvar
Name Textequ% Numvar
The instructions I have given here are not a lot given in the Masm Programmer Guide, but this more can explain the problem. I only explained the first usage here, and the next usage will talk about the "assembly period variable". Some examples of use.
Pi Textequ <3.1416>
DWPTR TEXTEQU
Arg1 TextEqu <[BP 4]> Then use PIs in the code to replace 3.1416. <> Means that they are strings. If it does not add <>, you will give you a text variable you give as a text variable of a assembly period, and this will be wrong.
Compalance period variables and constants
This several things have actually have their own name, in fact, in accordance with the usage, the constant and variables of the assembly period. For example, Text Macro is used as a text constant of a compilation, name assignment acts as a numerical variable of a compilation.
Define assembly period constants What is a constant constant? In fact, it means constant, because whether it is static in the compile period or executing, once it is defined, its value cannot be changed. Recalling in C, you use #define to define constants. However, #define can change a value equal to a macro, which means that constants need your maintenance (the compiler gives a warning). There is a keyword specifically used in Masm to define constants, and try changing the constant to get an error prompt.
Name Equ EXPRESSION
Name EQU
The first is to use as a "value" constant, and the second is used as a constant that defines "text". The text and values are subsequently separate from the special area. Defining Assembly During Text Variable Assembly Text Variables is another view of "Text Macro". In fact, they are the same thing. When you define a text macro, you can regard the macro name as the name of the assembly period text variable, the text content replaced by the macro name as the text value of the variable. Then, the second usage of the previous: Name Textequ textvar is very understanding. It is to make a text variable to another text variable. such as:
Talent Textequ
Taowen Textequ talent
The first line defines a quotation period text variable name Trent, and the second line assigns Talent's value to TAOWEN's variable. See this and:
Talent Textequ
Taowen Textequ
Yes, but the second approach is to turn Talent to Genius because the role of text macro is replaced. The actual effect is this:
Taowen Textequ
The content of the display text variable is often used in C, and the content of some variables is displayed in the runtime. In MASM, echo is used to display text variables during the assembly period.
China TextEqu
% echo china
This will appear Great Country in the command line of the assembly. If you remove the% number, it will be displayed. You should be able to speculate% to what is done, is a variable for evaluating. There are two types of variables that define the numerical variable constant of the assembly period. Here is the usage of assembly period numerical variables.
Name = expression
Expression is a numerical expression, such as:
VAL = 3 4
At this time, Val is a numeric variable whose value is 7. You can also write this:
ValExp Textequ <3 4>
VAL = VALEXP
It seems that I have given a text variable to a numeric variable, and the type conversion is made (huh, the effect is the same). In fact, it is actually written 3 4 to Valexp because the text macro has been replaced. We have seen how the numerical variables have been assigned to the text variables How to "assign the text variable to numeric variables", then?
VAL = 3 4
Valexp Textequ Val
The result is a prompt error: STEST.ASM (15): Error A2051: Text item Required. The assembler says the text item, then we add <> just fine.
VAL = 3 4
ValExp Textequ
Check you with% echo valexp You will find that it is not as you wish to display 7, but Val. This is because <> makes the assembler that VAL is a string, because the numerical variable is not a macro that is replaced with a text, does not replace the VAL to 7, so of course, VAL is displayed. The correct approach is:
VAL = 3 4
ValExp Textequ% VAL
Yes, like the previous usage, is used as a value. The third article in the usage of the text macro to recalls: name textequ% Numvar. This method is to enable a value of a numerical variable to a text variable, often used as a value displayed a numerical variable. Write this time when debugging.
Pi = 3.1415926
Temp textequ% PI
% echo temp
This is a very important commissioning skill.
Macro process and macro function
The front is from a simple text macro to the constant and variables of the assembly period. If you are just used in the macro code, some simple replacement of a text macro is enough. They are more in complex macros, which can be seen as process and functions. Like the variables of the same contract, they are used in the assembly period.
The following will no longer treat Text Macro as a macro and treat it as a text variable. Macro directly refers to a macro process or macro function. The macro process is a macro without a return value, while the macro function is a macro with a return value. They can have parameters, and they can have local variables. In fact, it can be unified to become a macro function, or the compilation period function
Create a simple macro with the following format
Name Macro
Statements
ENDM
It can be judged or loop in Statements, which can be said to be a very full function. However, the function of the assembly period and the execution period is very different, one is the pretreatment of the assembly period, the other is to change the execution position of the execution period, and return it after performing a code.
CLEAR_EAX_M MACRO
XOR EAX, EAX
ENDM
CLEAR_EAX_P PROC
XOR EAX, EAX
RET
CLEAR_EAX_P ENDP
Regarding this difference, I assume that you have already understood, if you don't understand any of the compilation textbooks, the above has a complete assembly code explanation.
Give macro delivery parameters
Parameters are self-evident for the importance of functions, and for the parameters of the macro are defined below.
Name Macro Parameterlist
Statements
ENDM
In the simple case, for parameterlist is the parameter name, the number is opened, for example:
Clear_reg Macro REG
Xor REG, REG
ENDM
When calling, use this format:
Clear_reg EAX
For the macro process, this is the only call format.
The parameters pass the parameters of the parameters of the parameters are also very different. The parameter is replaced directly. You can make such a test:
Testmacro Macro Param
Echo param
% echo param
ENDM
TextVar Textequ
Testmacro Textvar
The result of the output is TextVar and Hello. I don't have to say more. You can even further test:
Testmacro Macro Param
Param Textequ
ENDM
TextVar Textequ
Testmacro Textvar% Echo Textvar
The result of the output is how are you. It can be seen that the so-called parameters is just a replacement. The parameter name will be replaced by the number of quotes (quota is the parameters passing the past). Due to MASM in this system, all of these variable names are in a common space (huh, how to sound like mathematical terms?) Is a global thing.
For parameters, some qualified modifications can be made, for example, when you call, you must pass this parameter:
CLEAR_REG MACRO REG: REQ
Xor REG, REG
ENDM
Or specify a default value:
CLEAR_REG Macro REG: =
Xor REG, REG
ENDM
Or let the number of parameters become a variable.
Clear_reg Macro Regs: VARARG
For REG,
Xor REG, REG
ENDM
ENDM
However, it is important to note that the parameters of vararg must be the last one in the parameter.
Let the macro return a value
The difference between the macro process and the macro function is whether there is a return value. Of course, the return value of the return value and the function of the execution period is also very different. The function of the execution period is passed through EAX to pass the return value. Here, it is just direct replacement. The syntax of the return value is this:
EXITM TEXTITEM
A macro function can have multiple exitm, just like a function in C which can have multiple returnes. However, it must be returned to the value. Look at a simple example
WHO Macro
EXITM
ENDM
% echo who ()
The result is shown in Taowen. If () is removed, it is displayed by WHO. It can be seen that the call to the macro function must be added (). The call macro process cannot be added (). Look at an interesting example:
Who macro temp
% echo temp
ENDM
WHO ()
The result of the display is (). The description () is used as a parameter that is transmitted to the macro.
You can compare the return value, you can use the return value.
WHO Macro
EXITM
ENDM
WHO () TextEqu
This defines the assembly period text variable and the value is Genius. The visible macro can be used in any place where any text variables can occur, and many places can push those classes in the high-level language.
Local variable
There can be partial variables in the macro, which looks like local, actually just some tips on some name.
There are two facts for local variables: 1. Unacceptable outside the function, 2, its value should be not affected by the previous call on the different calls of the function.
Testmacro Macro
Local Localvar
% Echo Localvar
Localvar Textequ
ENDM
This call is called each time the function is called each time.
Testmacro
Testmacro
The first time, an error not defined, and the second time will output Hello. In fact, because Localvar is LOCAL. So both have undefined errors. This reflects the independence of multiple calls of local variables.
Let's unveiled the top card of local variables. Don't use me more, directly look at this, you will understand:
Testmacro Macro
Local Localvar
Echo Localvar
ENDM
Testmacro
Testmacro
The result of the output is: ?? 0000 and ?? 0001. This is the actual name of local variables. Local variables are unable to access the outside by weird names (you don't know what the name it is), then replace the name of local variables with different names when the same macro is expanded, so that the multiple calls are not Influence each other. In fact, you can test this:
? 0000 TEXTEQU
Testmacro Macro
Local Localvar
% Echo Localvar
ENDM
Testmacro
The result of the display is Hello, so that local variables are accessed externally in an macro process (function).
Text operation
MASM built-in two sets of text operation functions, one is the macro function, the other is Directive. The function is the same, but it provides flexibility in expression.
Name catstr [[textitem1 [[[, textitem2]] ...]]]]
Name Instr [[Position,]] TextItem1, TextItem2
Name Sizestr TextItem
Name Substr TextItem, Position [[, Length]]]
This set is Directive, the role is: Connect text, find child text, get text length, taking a child text.
@Catstr (string1 [[[[, string2 ...]])
@Instr ([Position]], String1, String2)
@Sizestr (String)
@SUBSTR (String, Position [[, Length]])
This set is a macro function. It is actually the same as an example.
TAOWEN TEXTEQU @catstr (
% echo taowen
Taowen Catstr
% echo taowen
It can be seen that both example output is He is Genius. There are two points to note: 1. The macro function such as @ catstr can be used as a left value, and is used to assign a value. 2, @ catstr This macro function is not automatically evaluated for parameters. When the behavior is different from yourself, add%.
It is not very difficult for specific use, and you can know it. One is the index of the first character is 1, not 0 in C.
% And evaluation
% May be the most difficult syntax. Generally, the behavior is not the same as you think, plus% test. % And <>!, Etc., constitute a mess.
Under normal circumstances, you don't have to be allowed. Use% can convert numerical variables to text variables. You can use% to force the value of the text. such as:
INDEX = 0
Namet catstr
% Namet Textequ
% Echo Person0
First use% by%, the value is made into text, and the second% is to turn Namet into Person0. It also demonstrates a very important trick that generates a variable name. As long as Index is incremented, an array of variables can be constructed.
<> Can be placed to a certain extent, but most of the case due to the replacement of the text macro is not affected, the replacement value is still taken. ! Used to cancel the meaning of the symbol. such as:
Symbol Catstr
% echo symbol
The result of the output is GO, Go. If it is not added!, The number will cause an error. Interesting is if you add it behind the second Go! It should be Go, Go !. The result is indeed a mistake in the right pointed parentheses, it turns out that the original meaning of>, no longer expanded. If you are like this: Symbol catstr
% echo symbol
The result is GO, Go>. It's going to see how it is. To produce!, Just write:
Symbol catstr
% echo symbol
What is the best use of <> <>. Specifically, it is because it can increase the scope of application.
Circulation in the macro
There are four kinds of cycles: while, repeat, for, forc. The syntax is as follows:
While Expression
Statements
ENDM
REPEAT Expression
Statements
ENDM
For parameter [[: req |: = default]],
Statements
ENDM
FORC parameter,
Statements
ENDM
While and Repeat are the same as the usage, at least I think is the same. Expression requires a value of one value, which can be used to determine the Operator to compare the value with EQ (equal to), LT (less than).
I = 0
While i lt 10
Temp textequ% i
% echo temp
I = i 1
ENDM
The result of the output is 0 until 9.
FOR and FORC are specialized loops, one is used to obtain each parameter in a parameter list, and the other is to remove each character in a string one by one. Each two examples can be understood:
Testmacro Macro Params: Vararg
For Param,
% echo param
ENDM
ENDM
Testmacro Arg1, Arg2, Arg3
The result of the display is Arg1 and then Arg2 then ARG3.
FORC Char,
% echo char
ENDM
The H and E and L and O O are shown separately.
Judgment in the macro
It is very simple to judge, but there are many kinds of IF, IFDEF, IFIDN, IFE, IFNDEF, IFDIF. There is nothing worth noting in use. It is the judgment value, and it is determined whether the symbol is defined, and it is determined whether the two texts are consistent.
About macros also define macros in the macro, Opattr, SizeOf, Lengthof, and so on. Many relatively advanced things. However, I believe that there is a basis for the previous story, and these things are just a checkbox.
Good luck, That is all.