【问题标题】:Get first whitespace-separated macro argument获取第一个空格分隔的宏参数
【发布时间】:2020-07-11 17:39:46
【问题描述】:

我想获得 NASM 中第一个以空格分隔的宏参数。如果参数是逗号分隔的,这很容易做到,例如这会发出 5 个 nop,忽略 blah

%macro foo 1+
times 5 %1
%endmacro
foo nop, blah

如何定义我的 foow 宏以使其作为 foow nop blah more-blah 工作,即空格分隔的参数?我只需要第一个参数。

【问题讨论】:

  • 您可以在 %rep 循环中使用 %defstr 和 %substr 来计算第一个非空格序列的长度。然后使用 %deftok 将其转换回非字符串标记。

标签: assembly nasm preprocessor


【解决方案1】:

您可以在%rep 循环中使用%defstr,然后使用%substr 来计算第一个非空格序列的长度。然后使用%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
$ 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 2022-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多