代码之家  ›  专栏  ›  技术社区  ›  Bilow

Nasm预处理器-通过变量的地址参数

  •  1
  • Bilow  · 技术社区  · 7 年前

    我需要写很多 push 推送不同字符的指令。我想用宏来实现这一点。这就是我目前所做的:

    %macro push_multi 1-*       ; Accept between 1 and ∞ arguments
        %assign i 1
        %rep %0                 ; %0 is number of arguments
            push %{i}
            %assign i i+1
        %endrep
    %endmacro
    
    push_multi 'a', 'b', 'c'    ; push 'a' then push 'b' then push 'c'
    

    但结果是 nasm -E 是:

    push %i
    push %i
    push %i
    

    我想要这个:

    push 'a'
    push 'b'
    push 'c'
    

    如何使用创建的变量处理宏的第n个参数 assign ?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Michael Petch    7 年前

    使用 %rotate 1 可以将宏参数列表向左旋转1个元素。这有效地将列表中的下一个元素放在了开头。列表中的第一个元素始终可以引用为 %1 . 把这个放在 %rep %0 循环将允许您遍历宏参数列表中的所有元素。这个 NASM documentation 对于 %rotate 这样说:

    %使用单个数值参数(可能是表达式)调用rotate。宏参数向左旋转了那么多个位置。如果%rotate的参数为负,则宏参数将向右旋转。

    在您的情况下,这应该有效:

    %macro push_multi 1-*       ; Accept 1 or more arguments
        %rep %0                 ; %0 is number of arguments pass to macro
            push %1
            %rotate 1           ; Rotate to the next argument in the list
        %endrep
    %endmacro
    

    如果要反向执行列表,可以使用按相反方向旋转 -1 通过执行 %旋转 第一:

    %macro push_multi 1-*       ; Accept 1 or more arguments
        %rep %0                 ; %0 is number of arguments pass to macro
            %rotate -1          ; Rotate to the prev argument in the list
                                ; If at beginning of list it wraps to end of list 
            push %1
        %endrep
    %endmacro