获取第一个以空格分隔的宏参数
Get first whitespace-separated macro argument
我想在 NASM 中获取第一个以空格分隔的宏参数。如果参数以逗号分隔,则很容易做到这一点,例如这会发出 5 个 nop,忽略 blah
:
%macro foo 1+
times 5 %1
%endmacro
foo nop, blah
如何定义我的 foow
宏,使其像 foow nop blah more-blah
一样工作,即以空格分隔的参数?我只需要第一个参数。
您可以在 %rep
循环中使用 %defstr
,然后使用 %substr
来计算第一个非 space 序列的长度。然后使用 %deftok
将其转换回非字符串标记。这是一个例子。 (请注意,%$exit
变量仅对某些在 %exitrep
处理中存在错误的 NASM 的旧版本才需要。)
%macro bar 1.nolist
%push
%defstr %$string %1
%strlen %$length %$string
%assign %$ii 0
%assign %$exit 0
%rep %$length
%substr %$point %$string %$ii + 1, 1
%if %$point == 32 || %$point == 9
%assign %$exit 1
%exitrep
%endif
%ifn %$exit
%assign %$ii %$ii + 1
%endif
%endrep
%substr %$word %$string 1, %$ii
%deftok %$token %$word
%$token
%pop
%endmacro
bar nop quux xyzzy
这是一个测试运行:
$ nasm -E test.asm
%line 22+1 test.asm
nop
$
我想在 NASM 中获取第一个以空格分隔的宏参数。如果参数以逗号分隔,则很容易做到这一点,例如这会发出 5 个 nop,忽略 blah
:
%macro foo 1+
times 5 %1
%endmacro
foo nop, blah
如何定义我的 foow
宏,使其像 foow nop blah more-blah
一样工作,即以空格分隔的参数?我只需要第一个参数。
您可以在 %rep
循环中使用 %defstr
,然后使用 %substr
来计算第一个非 space 序列的长度。然后使用 %deftok
将其转换回非字符串标记。这是一个例子。 (请注意,%$exit
变量仅对某些在 %exitrep
处理中存在错误的 NASM 的旧版本才需要。)
%macro bar 1.nolist
%push
%defstr %$string %1
%strlen %$length %$string
%assign %$ii 0
%assign %$exit 0
%rep %$length
%substr %$point %$string %$ii + 1, 1
%if %$point == 32 || %$point == 9
%assign %$exit 1
%exitrep
%endif
%ifn %$exit
%assign %$ii %$ii + 1
%endif
%endrep
%substr %$word %$string 1, %$ii
%deftok %$token %$word
%$token
%pop
%endmacro
bar nop quux xyzzy
这是一个测试运行:
$ nasm -E test.asm
%line 22+1 test.asm
nop
$