📄 preproc.html
字号:
to
<blockquote class="code"><pre>
db <font color=#339933>11</font><font color=#333399>,</font><font color=#339933>12</font><font color=#333399>,</font><font color=#339933>13</font>
</pre></blockquote>
<br><br>
<a name="restore"></a>
<b>3.2. Directive <code>restore</code></b><br>
You can also tell preprocessor to stop replacing particular equate. This is
done with <code>restore</code> operator:
<blockquote class="code"><pre>
restore <font color=#333399><</font>name<font color=#333399>></font>
</pre></blockquote>
Where <name> is some equation. Behind this command <name> will
no longer be replaced as specified by <code>equ</code>
<br><br>
Example:
<blockquote class="code"><pre>
mov eax<font color=#333399>,</font>count
count equ <font color=#339933>10</font>
mov eax<font color=#333399>,</font>count
restore count
mov eax<font color=#333399>,</font>count
</pre></blockquote>
will become
<blockquote class="code"><pre>
mov eax<font color=#333399>,</font>count
mov eax<font color=#333399>,</font><font color=#339933>10</font>
mov eax<font color=#333399>,</font>count
</pre></blockquote>
Note that replacements are "stacked" that means if you define two equates
for one symbol, and then restore it (once), the first one will be used.
<br><br>
Example:
<blockquote class="code"><pre>
mov eax<font color=#333399>,</font>count
count equ <font color=#339933>1</font>
mov eax<font color=#333399>,</font>count
count equ <font color=#339933>2</font>
mov eax<font color=#333399>,</font>count
count equ <font color=#339933>3</font>
mov eax<font color=#333399>,</font>count
restore count
mov eax<font color=#333399>,</font>count
restore count
mov eax<font color=#333399>,</font>count
restore count
mov eax<font color=#333399>,</font>count
</pre></blockquote>
to
<blockquote class="code"><pre>
mov eax<font color=#333399>,</font>count
mov eax<font color=#333399>,</font><font color=#339933>1</font>
mov eax<font color=#333399>,</font><font color=#339933>2</font>
mov eax<font color=#333399>,</font><font color=#339933>3</font>
mov eax<font color=#333399>,</font><font color=#339933>2</font>
mov eax<font color=#333399>,</font><font color=#339933>1</font>
mov eax<font color=#333399>,</font>count
</pre></blockquote>
<br>
If you try to restore non-existing equation nothing will happen.
<br><br>
Example:
<blockquote class="code"><pre>
mov eax<font color=#333399>,</font>count
restore count
mov eax<font color=#333399>,</font>count
</pre></blockquote>
to
<blockquote class="code"><pre>
mov eax<font color=#333399>,</font>count
mov eax<font color=#333399>,</font>count
</pre></blockquote>
<br><br>
<a name="simple_mac"></a>
<b>4. Simple macros without arguments</b>
<br><br><br>
<a name="simple_def"></a>
<b>4.1. Simple macro definition</b><br>
You can create your own instruction / directive using "macro".
<blockquote class="code"><pre>
macro <font color=#333399><</font>name<font color=#333399>></font>
<font color=#333399>{</font>
<font color=#333399><</font>body<font color=#333399>></font>
<font color=#333399>}</font>
</pre></blockquote>
After preprocessor finds <code>macro</code> directive, it defines macro, which means
each following occurence of line starting with <name> will be replaced by
<body>. <name> can be one symbol, <body> can be anything except
<code>}</code> which denotes end of macro body.
<br><br>
Example:
<blockquote class="code"><pre>
macro a
<font color=#333399>{</font>
push eax
<font color=#333399>}</font>
xor eax<font color=#333399>,</font>eax
a
</pre></blockquote>
to
<blockquote class="code"><pre>
xor eax<font color=#333399>,</font>eax
push eax
</pre></blockquote>
<br>
Example:
<blockquote class="code"><pre>
macro a
<font color=#333399>{</font>
push eax
<font color=#333399>}</font>
macro b
<font color=#333399>{</font>
push ebx
<font color=#333399>}</font>
b
a
</pre></blockquote>
to
<blockquote class="code"><pre>
push ebx
push eax
</pre></blockquote>
<br>
Of course, macro doesn't have to be indented like in my example, you can use
this too:
<blockquote class="code"><pre>
macro push5 <font color=#333399>{</font>push dword <font color=#339933>5</font><font color=#333399>}</font>
push5
</pre></blockquote>
to
<pre><blockquote class="code">
push dword <font color=#339933>5</font>
</blockquote></pre>
<br>
Or
<blockquote class="code"><pre>
macro push5 <font color=#333399>{</font>push dword <font color=#339933>5</font>
<font color=#333399>}</font>
</pre></blockquote>
to same result. You are free about indenting macros.<br><br><br>
<a name="nested"></a>
<b>4.2. Nested macros</b><br>
You can nest macros. That means, if you redefine macro, then the last one is
used. But if you use original macro in last one, it will work. Look at
example:
<blockquote class="code"><pre>
macro a <font color=#333399>{</font>mov ax<font color=#333399>,</font><font color=#339933>5</font><font color=#333399>}</font>
macro a
<font color=#333399>{</font>
a
mov bx<font color=#333399>,</font><font color=#339933>5</font>
<font color=#333399>}</font>
macro a
<font color=#333399>{</font>
a
mov cx<font color=#333399>,</font><font color=#339933>5</font>
<font color=#333399>}</font>
a
</pre></blockquote>
to
<blockquote class="code"><pre>
mov ax<font color=#333399>,</font><font color=#339933>5</font>
mov bx<font color=#333399>,</font><font color=#339933>5</font>
mov cx<font color=#333399>,</font><font color=#339933>5</font>
</pre></blockquote>
<br>
Or this example:
<blockquote class="code"><pre>
macro a <font color=#333399>{</font><font color=#339933>1</font><font color=#333399>}</font>
a
macro a <font color=#333399>{</font>
a
<font color=#339933>2</font><font color=#333399>}</font>
a
macro a <font color=#333399>{</font>
a
<font color=#339933>3</font><font color=#333399>}</font>
a
</pre></blockquote>
to
<blockquote class="code"><pre>
<font color=#339933>1</font>
<font color=#339933>1</font>
<font color=#339933>2</font>
<font color=#339933>1</font>
<font color=#339933>2</font>
<font color=#339933>3</font>
</pre></blockquote>
<br><br>
<a name="purge"></a>
<b>4.3. Directive <code>purge</code> (macro undefinition)</b><br>
You can also undefine macro, like you undefined equate. This is done by
<code>purge</code> directive followed by macro name:
<blockquote class="code"><pre>
a
macro a <font color=#333399>{</font><font color=#339933>1</font><font color=#333399>}</font>
a
macro a <font color=#333399>{</font><font color=#339933>2</font><font color=#333399>}</font>
a
purge a
a
purge a
a
</pre></blockquote>
to
<blockquote class="code"><pre>
a
<font color=#339933>1</font>
<font color=#339933>2</font>
<font color=#339933>1</font>
a
</pre></blockquote>
If you try to <code>purge</code> non-existing macro nothing will happen.
<br><br><br>
<a name="behave"></a>
<b>4.4. Macros behaviour</b><br>
Macro name will be replaced by macro body not only if line is starting with
macro, but everywhere where instruction mnemonics (like <code>add</code>,
<code>mov</code>) is accepted. It is because main purpose of macro is to
simulate instructions. Only exception is instruction prefix, macro is not
accepted after instruction prefix.
<br><br>
Example:
<blockquote class="code"><pre>
macro CheckErr
<font color=#333399>{</font>
cmp eax<font color=#333399>,-</font><font color=#339933>1</font>
jz error
<font color=#333399>}</font>
call Something
a<font color=#333399>:</font> CheckErr <font color=#777777>;here macro name is preceded by label definition, but it</font>
<font color=#777777>;will be replaced</font>
</pre></blockquote>
to
<blockquote class="code"><pre>
call Something
a<font color=#333399>:</font>
cmp eax<font color=#333399>,-</font><font color=#339933>1</font>
jz error
</pre></blockquote>
<br>
Example:
<blockquote class="code"><pre>
macro stos0
<font color=#333399>{</font>
mov al<font color=#333399>,</font><font color=#339933>0</font>
stosb
<font color=#333399>}</font>
stos0 <font color=#777777>;this is place for instruction, will be rplaced</font>
here<font color=#333399>:</font> stos0 <font color=#777777>;this is place for instruciton too</font>
db stos0 <font color=#777777>;this in not place for instruction and so won't be replaced</font>
</pre></blockquote>
to
<blockquote class="code"><pre>
mov al<font color=#333399>,</font><font color=#339933>0</font>
stosb
here<font color=#333399>:</font>
mov al<font color=#333399>,</font><font color=#339933>0</font>
stosb
db stos0
</pre></blockquote>
<br>
You can also "overload" instruction with macro, because preprocessor doesnt'
know about instructions, it allows macro name to be instruction mnemonics.
<blockquote class="code"><pre>
macro pusha
<font color=#333399>{</font>
push eax ebx ecx edx ebp esi edi
<font color=#333399>}</font>
macro popa
<font color=#333399>{</font>
pop edi esi ebp edx ecx ebx eax
<font color=#333399>}</font>
</pre></blockquote>
these 2 save 4 bytes for every pusha because they don't push ESP. But
overloading istruction isn't very good, because someone reading your code
may get fooled if he don't knows instruction is overloaded.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -