80 × 86 basic macro commands for assembly languages

Source: Internet
Author: User
A macro is a powerful preprocessing command of the assembler. It is similar to other preprocessing commands and starts compilation in the assembler. Code Previously, the macro was processed. Its syntax rules are as follows:

Name macro [parameter [: Tag] [, parameter [: Tag]…]
[Local varlist]
Statements
[Exitm [textitem]
Endm

Name is a unique identifier. Program This identifier will be used in the code to call the macro. parameter is the variable used in the macro process. It is like the parameter in the function. tag can be: req,: = default and: vararg.
If you use req after a variable (parameter), it indicates that the parameter cannot be empty. Otherwise, the assembler reports an error.
If the variable (parameter) is followed by: = default, if the parameter before: = default is not passed when the macro is called, the assembler uses the default content as the default value.
If vararg is used after the variable (parameter), this parameter only works when it is used as the last parameter of the macro keyword.
Statement can be any assembly language process that complies with macro assembly syntax.
Textitem can be used as an optional return value for the entire macro process. The optional meaning here is that textitem can be used or not.
In addition to replacing some strings, macros can also perform complex operations, extend commands, and other functions. The following describes the usage of keywords related to Macros in detail.


Basic syntax Keywords of macros

Macro
This keyword indicates the start of the entire macro. A name must be added before the macro keyword. This is the macro name, during assembler assembly, the content in the macro corresponding to the name will be used in the place where the name appears in the program to replace the name. you can keep up with the required parameters after the macro keyword.

Example:
RGB macro red, green, blue
XOR eax, eax
MoV Al, blue; blue
Rol eax, 8
MoV Al, green; green
Rol eax, 8
MoV Al, red; red
Endm


In the preceding example, the macro name uses an RGB identifier. The macro contains three parameters: Red, green, and blue. the macro is used to put the three parameters in the eax register in order, and the final return value is eax by default. this API function returns the same method.
When the above macro is used in the program, the macro parameter can be three immediate numbers, three memory variables, three byte registers, or any mixed form, as shown below:
RGB 1, 125,175,225
RGB byte1, byte2, byte3
RGB Cl, CH, DL
RGB 125, byte, Al

In macro nesting, process macros and function macros can be nested at 40 layers, and text macros can be nested at 20 layers. A macro can be redefined, but the defined content only affects the subsequent code content. function macros cannot be redefined once used.


Endm
End a macro and match it with the macro keyword.
Exitm
This keyword is used to end a macro. The function is like the endm keyword, and the code between this keyword and endm will not be compiled. Therefore, this keyword is mainly used for condition assembly.

Example: Use of the exitm keyword
Words macro count
If Count EQ 0
Exitm
Endif
Word Count DUP (?)
Endm

. Data
Myword1 DW 8
Words 8
Words 0

In the preceding example, a total of 9 Characters of memory space is defined. The statement words 8 defines the memory space of 8 words, and the statement word 0 does not define any memory space.
In addition, when the exitm keyword is followed by the returned textitem value, the macro is a function macro with the returned value:
. 386
. Model flat, stdcall
@ Retval macro
Local txt
TXT textequ <This Is a function macro>
Exitm txt
Endm
% Echo & retval ()
. Code
A:
End

In the preceding example, a function macro @ retval is defined, a local variable is defined in the macro, and a string is assigned to the variable. At the end of the macro, return the local variable as the return value. echo pseudocommands are used to output information in the command line.


Cyclic command keywords in the macro

For

The keyword format is as follows:
For parameter [: req |: = default], <argument [, argument]…>
Statements
Endm
This keyword uses each argument to replace the parameter in statements and execute the statements process.
The meaning of each part in the preceding format is as follows:
Parameter can be any valid variable name.
If you use req after a variable (parameter), it indicates that the parameter cannot be empty. Otherwise, the assembler reports an error.
If: default is used after the variable (parameter), if the parameter before: = default is not passed when the macro is called, the assembler uses the default content as the default value.
Argument can be a text, identifier, string, or a numerical constant. if a string argument contains spaces or commas, You need to enclose the string argument with Angle brackets (<>.
Statement can be any assembly language process that complies with macro assembly syntax.

Example: Use the for keyword

Fors macro count
For value, <1, 2, 3, 4, 4, 3, 2, 1>
If value GT count
Exitm
Endif
Byte value
Endm
Endm
. Data
Var1 DW 11 h
Fors 3
Var2 dB offh
In the preceding example, A fors macro is defined. This macro is used to define a string of bytes of memory space and give the initial value of the memory space. Obviously, this macro can only be used in. Data data segments.


Forc

The keyword format is as follows:

Forc parameter, <string>
Statements
Endm

This keyword uses each character in string to replace the parameter in statements and executes the statements process.
The meaning of each part in the preceding format is as follows:
Parameter can be any valid variable name. When it is executed in statements, it will be replaced by different characters in the string parameter.
A string can be a string or a string variable. the variable in the string is also executed in statements as a character. end with Angle brackets (<>) on both sides of the string.
Statements can be an assembly process that complies with macro assembly syntax.


Goto

The keyword format is as follows:
Goto macrolabel
The function of this keyword is to let the assembler jump to the place marked by macrolabel. This identifier can only exist in macro, for, Forc repeat, while module.

Use of the goto keyword:
Myname macro var1
XOR eax, eax; ---------- 1
Goto label; ---------- 2
MoV EBX, var1; ------------- 3
: Label; ------------ 4
MoV eax, var1; -------------- 5
Endm

In the preceding example, after the first line is executed, a GOTO statement is found in the second line. Therefore, locate the label and execute the label directly, that is, execute the fifth line.


Repeat

The keyword format is as follows:
Repeat expression
Statements
Endm

The function of this keyword is to allow the assembler to compile the expression in statements.
Expression is a constant, indicating the number of times the assembly is repeated.


While

The keyword format is as follows:

While expression
Statements
Endm
The function of this keyword is to allow the assembler to reassemble the content in statements until the content of the expression is zero (false)
Expression is an expression that changes the value of this expression during assembly.



Conditional test keywords

If, elseif, else, endif

Example:

Retval macro VaR
If var EQ 0
XOR eax, eax
Else
MoV eax, VAR
Endif
Endm \

When the assembler compiles the source program, it finds that the program has the retval keyword, and then judges whether the VaR value is zero Based on the macro content. If so, the assembler will set the XOR eax, put the eax statement in the place where retval appears. note that the conditional expressions in the macro cannot use = ,! =,>, <And other symbols.

These condition test statements can be nested for more than 20 times.


Relational operators in conditional expressions in macro definition
The two expressions of EQ 1 EQ 2 are equal, and true is returned. Otherwise, false is returned.
Ne 1 ne 2 not equal returns true, equal to false
If expression 1 is greater than expression 1, it is true. Otherwise, it is false.
Lt is opposite to above
GE expression 1 is greater than or equal to expression 2, returns true, otherwise false
Expression 1 is less than or equal to expression 2. True is returned. Otherwise, false is returned.




Other keywords in the macro:

<Text>
When using the for keyword, we often use angle brackets (<>) in subsequent parameters, because some parameters contain special symbols such as commas, spaces, and semicolons, when you want to process them as a parameter, you need to use angle brackets, as shown below:
Use of angle brackets:

Mymacro <One, Two, Three>; pass a parameter to mymacro macro
Mymacro One, Two, Three; pass three parameters to mymacro macro

In the above example, mymacro is a defined macro. The first method in the example passes a parameter to mymacro macro, and the second method passes three parameters to mymacro macro.

Local
Sometimes we may want to define some local variables or some local flags in the macro, then we can use the keyword local to define the local variables or local flags in the macro. this keyword is the same as the keyword defining local variables in the Win32 Assembler. the specific definition format is as follows:
Local localname [, localname]…
Localname is the name of the local variable to be defined.


Pushcontext and popcontext
In the macro, when we need to save some status values of the assembler to the stack at that time, we do not need to press the values one by one in the push and pop machine commands to bring up the stack, we can use the pushcontext and popcontext keywords provided by the assembler to conveniently process these tasks. the specific format is as follows:
Pushcontext Context
Popcontext Context
In the above format, the pushcontext keyword puts the content of context into the stack in sequence, while the popcontext keyword restores the content of context from the stack. These two keywords can be nested with 10 layers.

Assumes register status value
Radix current default base
Listing content of all identity registers
Instructions in CPU processor and coprocessor
All content above


& Amp; parameter & amp &

Sometimes we need to insert a variable into a string in the macro. At this time, we can use the & symbol to accomplish this purpose. The format of this symbol is as follows:
String & Parameter & string
Here, string is an arbitrary string, and parameter is the name of the variable we want to insert. If you want to display an & symbol in the string, you need to add an exclamation mark (!) before this symbol (!).

Example:

. 386
. Model flat, stdcall
Guard macro pisonernum: req, prisonername: = <anonymouse>
Local txt
& Echo there are & prisonernum & prisonername & have arrived at @ time.
TXT textequ <prisonernum & prisonernum>
Exitm txt
Endm

% Echo guard (2)
. Code
Strcat:
End strcat

@ Time indicates the reference system time.



Vararg (macro)

When we want to define a macro with an indefinite number of parameters, we can use the vararg keyword to define a variable with the vararg attribute. when using this macro, you only need to separate these parameters with commas. parameters with this attribute can only be placed at the end of the macro parameter, for example:
. 386
. Model flat, stdcall
Argcount macro parmlist: varrg
Local count
Count = 0
For parm, <parmlist>
Count = count + 1
Endm
Exitm <count>
Emdm

. Code
VaR:
MoV eax, argcount (1, 2, 3, 4); eax returns 4
MoV EBX, argcount (a, B); EBX returns 2
End VaR

A macro named argcount is defined above. This macro contains a variable with the attribute vararg. The for loop statement is used in this macro process, counts the number of passed parameters. in the code segment, four parameters are input using the macro statement, so the return value of the macro is 4, that is, 4 is saved in the eax register. Similarly, the second statement is passed to the macro two parameters, therefore, the returned value is 2.


Predefined macros for string operations

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.