Macro compilation macro

Source: Internet
Author: User

I. macro assembly
Macro definition is implemented by a set of pseudo operations. The format is:
Macro_name macro [dumny_parameter_list]
... (Macro definition)
Endm
Macro and endm are a pair of pseudo operations. this is a macro definition between pseudo operations-a group of independent functional program code. the macro command name (macro_name) indicates the macro definition name. When called, the macro command name is used to call the macro definition. the dummy element table (dumny_parameter_list) provides the formal parameters (or virtual parameters) used in the macro definition. Each dummy element is separated by commas.
Macro commands defined by macros can be called in the source program. Macro calls are called. Macro calls are in the following format:
Macro name [actual parameter list]
Each item in an actual_parameter_list is an object, which is separated by commas.
1. macro definition can be non-Variable
Macro definition:
Savereg macro
PUSH AX
Push BX
Push CX
Push DX
Push Si
Push di
Endm
Macro call:
Savereg
2. The variable can be an operation code.
Macro definition:
Foo macro P1, P2, P3
MoV ax, p1
P2 p3
Endm
Macro call:
Foo word_var, Inc, ax
Macro expansion:
+ Mov ax, word_var
+ Inc ax
3. The variable can be part of the operation code, but the & separator must be used in the macro definition.
Macro definition:
Leap macro cond, lab
J & cond lab
Endm
Macro call:
...
Leap Z, there
...
Leap NZ, here
...
Macro expansion:
...
+ JZ there
...
+ Jnz here
...
4. & is an operator. It can be used as the prefix of the dummy element in the macro definition body. When it is expanded, it can combine the & symbol before and after to form a symbol. This symbol can be an operation code, the operand or a string.
Macro definition:
Po macro p1
Jmp ta & p1
Endm
Macro call:
Fo word_var
Macro expansion:
+ JMP taword_var
5. The entity is an ASCII string.
Macro definition:
Msggen macro lab, num, XYZ
Lab & num dB 'Hello mr. & xyz'
Endm
Macro call:
Msggen MSG, 1, Taylor
Macro expansion:
+ Msg1 dB 'Hello mr. Taylor'
6. the macro command name can be the same as the command mnemonic or pseudo operation name. In this case, the macro command has the highest priority, and the command or pseudo operation with the same name becomes invalid. the pseudo-operation purge can be used to cancel the macro definition when appropriate to restore the original meaning of the command.
Macro definition:
Add macro opr1, opr2, result
...
Endm
Macro call:
...
Add XX, YY, ZZ
Purge add
...
After a macro is called, use the purge pseudo operation to cancel the definition so as to restore the original meaning of the add command. The add command used after the purge add command is subject to the definition of the machine command.
The purge pseudo operation can cancel multiple macro operations at the same time. In this case, macro commands are separated by commas.
7. Use of Local pseudo operations. The macro defines the label that can be used in the body, for example:
Macro definition:
Absol macro labels
CMP protocol, O
Jge next
Neg Subnet
Next:
Endm
If the macro definition is called multiple times in the program, Multiple labels are displayed after expansion. For this reason, the system provides local pseudo operations. The format is
Local List of local labels
The labels in the local label table are separated by commas (,). The assembler creates a unique symbol for each of the local labels in the local pseudo-operation table ?? 0000 ~?? FFFF) to replace each local label that exists in the expansion. note that the local pseudo operation can only be used in the macro definition body. It must be the first statement after the macro pseudo operation. Comments and semicolons are not allowed between the macro and local pseudo operations.
In this example, the absol macro is defined:
Absol macro labels
Local next
CMP protocol, 0
Jge next
Neg Subnet
Next:
Endm
Macro call:
...
Absol VaR
...
Absol BX
...
Macro expansion:
...
+ CMP var, 0
+ Jge ?? 0000
+ Neg VaR
+ ?? 0000:
...
+ Cmp bx, 0
+ Jge ?? 0001
+ Neg BX
+ ?? 0001:
...
8. Macro calling is allowed in macro definition. The conditions are as follows:
Macro definition:
DIF macro X, Y
MoV ax, X
Sub ax, y
Endm
Difsqr macro opr1, opr2, result
Push DX
PUSH AX
DIF opr1, opr2
Imul ax
MoV result, ax
Pop ax
Pop DX
Endm
Macro call:
Difsqr var1, var2, var3
9. In the macro definition body, you can use both macro calls and macro definitions.
Macro definition:
Defmac macro macnam, Operator
Macnam macro x, y, z
PUSH AX
MoV ax, X
Operator ax, y
MoV Z, ax
Pop ax
Endm
Endm
Macnam is the macro definition name of the inner layer, but it is also the dummy element defined by the outer macro. Therefore, when defmac is called, a macro definition is formed.
Macro call:
Defmac addition, add
Macro expansion:
+ Addition macro x, y, z
PUSH AX
MoV ax, X
Add ax, y
MoV Z, ax
Pop ax
Endm
Form the Add macro definition addition. Similarly, macro calls
Defmac subtract, sub
Form the macro definition of subtraction. Of course, after these macro definitions are formed, macro calls can be used.
Addition var1, var2, var3
Expand:
+ PUSH AX
+ Mov ax, var1
+ Add ax, var2
+ Mov var3, ax
+ Pop ax
10. Here we will introduce the pseudo operation % used in a macro-defined variable. Its format is:
% Expression
The assembler converts the value of the expression following % to the number under the current base. During expansion, this number is used to replace the dummy element.
Macro definition:
MSG macro count, string
MSG & COUNT dB string
Endm
Errmsg macro text
Cntr = cntr + 1
MSG % cntr, text
Endm
Macro call:
...
Cntr = 0
Errmsg 'syntax error'
...
Errmsg 'invalid operand'
...
Macro expansion:
...
+ Msg1 dB 'syntax error'
...
+ Msg2 dB 'invalidoperand'

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.