Learning Win32 Assembly [34]-macro assembly (1)

Source: Internet
Author: User
I didn't like macros much when I started to use the C language. Code I also have to understand.

Macros can be defined in the sourceProgramBut is usually placed before. Data.
Some simple macros can be replaced by equ, textequ, OR =, but macros have more complex functions.

The essence of "macro" is "Replacement", but it is like "sub-process ";
Therefore, there are both macro processes and macro functions ).
It can be a constant, variable, register, instruction, or expression, and sometimes needs to be declared like a sub-process.
The macro can specify which parameters are required and the default values of the parameters.

Macros can contain data (. Data) and Code (. Code), and can also be nested.

Use the annotation in the macro; if only used; will be replaced with the Code together.

Macros are very powerful. Currently, commonly used printdec, printhex, printstring, and printtext are macros.
It is only "text replacement ".

"Macro" compared with "subroutine:
1. After the macro scale is opened, the program is executed in sequence. Unlike calling subprograms, the program will be faster ";
2. The macro scale will increase the amount of code, resulting in a "large" program ".

A simple macro:
; Test34_1.asm.386.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. LIB; defines a mexit macro printline retendm. codesum proc V1, V2, V3 mov eax, V1 add eax, V2 add eax, V3 retsum endp; main proc invoke sum, 11, 22, 33 printdec eax; 66; printline; RET mexit; mexit will be replaced with the above two lines of code main endpend main

  

A macro that replaces the summation function:

 
; Test34_2.asm.386.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. libmsum macro V1, V2, V3 mov eax, V1 add eax, V2 add eax, v3endm. codemain proc msum 11, 22, 33 printdec eax; 66 printline msum 11, 22, 33, 44, 55; excessive parameters will be ignored printdec eax; 66 printline retmain endpend main

  

Default macro parameters:

 
; Test34_3.asm.386.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. LIB; Parameters V1 and V2 are required parameters identified by Req. Parameters V3 and V4 provide the default values msum macro V1: req, V2: req, V3: =, V4: = mov eax, V1 add eax, V2 add eax, V3 add eax, v4endm. codemain proc msum 11, 22 printdec eax; 110 printline retmain endpend main

  

Exitm: Exit macro

 
; Test34_4.asm.386.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. libmp RINT macro printtext 'specifies 'text' in the first line and 'exitm printtext' in the second line. codemain proc mprint; only the first two lines of retmain endpend main will be output

  

Purge: Cancel macro

 
; Test34_5.asm.386.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. libmp RINT macro printtext 'specifies the first line of 'printtext', the second line of 'printtext', and the third line of 'printlineendm. codemain proc mprint purge mprint; multiple macros mprint can be canceled by commas. This macro will not expand retmain endpend main

  

Use of Local in macro:

; Test34_6.asm.386.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. LIB; the macro mMax macro V1, V2, V3 local L1, L2 that calculates the maximum number from three. If this statement is not found, the macro name will be duplicated in L1 and L2 when it is expanded multiple times, in this way, the macro will coordinate the label names mov eax, V1 CMP eax, V2 jge L1 mov eax, v2l1: CMP eax, V3 jge L2 mov eax, v3l2: endm. codemain proc mMax 11, 22, 33 printdec eax; 33 retmain endpend main

  

Flexible parameters, used & operator:

; test34_7.asm.386.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. LIB; find the maximum value in the maximum number: mMax macro V1, V2 local L1 mov eax, V1 CMP eax, V2 jge L1 mov eax, v2l1: endm; find the minimum value in the maximum number: mmin macro V1, V2 local L1 mov eax, V1 CMP eax, V2 jle L1 mov eax, v2l1: endm; jge or jle can be used as parameters: mcom1 macro XX, V1, v2 local L1 mov eax, V1 CMP eax, V2 XX L1 mov eax, v2l1: endm; change J * E to jge or jle: mcom2 macro X, V1, v2 local L1 mov eax, V1 CMP eax, V2 J & X & E L1; Special operators & mov eax, v2l1: endm are used here. codemain proc mMax 11, 22 printdec eax; 22 mmin 11, 22 printdec eax; 11 mcom1 jge, 11, 22 printdec eax; 22 mcom1 jle, 11, 22 printdec eax; 11 mcom2 g, 11, 22 printdec eax; 22 mcom2 L, 11, 22 printdec eax; 11 retmain endpend main

   

Special operators: &, % ,!

 
&; Replacement operator; string transfer operator %; expression operator, also used to get the value of a variable or constant! ; Escape Operator

  ; Test34_8.asm.386.model flat, stdcallinclude windows. incinclude kernel32.incinclude masm32.incinclude debug. incincludelib kernel32.libincludelib masm32.libincludelib debug. LIB; custom macro mprint macro text printtext '* & text & *' endm. codemain proc; this macro will directly replace the parameter with mprint 1234; * 1234 *; To ensure that the parameter is complete, use mprint; * 12 * mprint *; % () mprint 34 + 12; * 34 + 12 * mprint % (34 + 12); * 46 *; used &,, % ,! Should be used! Escape mprint 10! % 2 = % (10/2 )!! ; * 10% 2 = 5! * Retmain endpend main

  

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.