2017-12-11 17 views
1

異なる文字をプッシュする多くのpush命令を記述する必要があります。私はそのためにマクロを使用したいと思います。Nasmプリプロセッサ - 変数を使用してパラメータをアドレス指定する

%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と結果は次のとおりです:これは私がこれまで何をやったかである私はのn番目の引数に対処するにはどうすればよい

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

push %i 
push %i 
push %i 

私はこれをしたいです変数がassignで作成されたマクロ?

答えて

2

%rotate 1を使用すると、マクロ引数リストを1つ左に回転させることができます。これにより、リストの次の要素が効果的に先頭に配置されます。リストの最初の要素はいつも%1として参照できます。ループを%rep %0ループに入れると、マクロ引数リストのすべての要素を反復処理できます。 %rotateためNASM documentationはこれを言う:

%の回転が(表現かもしれない)、単一の数値引数で呼び出されます。マクロパラメータは、その多くの場所で左に回転します。 %rotateの引数が負の場合、マクロパラメータは右に回転します。あなたは-1と反対方向に回転させることができます逆にリストをしたい場合は

%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 

%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 
+0

完璧!ありがとう! – Bilow

関連する問題