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

问题描述 投票:1回答:1

我需要写很多推动不同角色的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'

如何使用assign创建的变量来解决宏的第n个参数?

assembly macros x86 nasm preprocessor
1个回答
2
投票

使用%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以及首先执行%rotate的相反方向旋转:

%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
© www.soinside.com 2019 - 2024. All rights reserved.