【问题标题】:Label in %rep section in NASMNASM 中 %rep 部分中的标签
【发布时间】:2016-05-01 02:54:57
【问题描述】:

我有 %rep 预处理器指令,它创建了一个预处理器循环。 我想在其中声明标签,可能有一些连接,但我无法正确使用语法。

%assign i 0 
%rep    64 
   label_%i: ;this, of course, doesn't work
      inc rax    
%assign i i+1 
%endrep

那么如何强制 NASM 预处理器为每次“迭代”生成label_i

【问题讨论】:

    标签: assembly preprocessor nasm


    【解决方案1】:

    如果您不需要从 %rep 块之外引用标签,则可以使用 within-a-macro local %%label 语法:

    %macro  jmpfwd 0
        times 21 nop
        jmp %%fwd                  ;;;;;   <<<------ This jump
        add ax, 0x1234    ; can this stall decoding?
    ;    lea eax, [ebx+edx+1]
      align 64
      %%fwd:                       ;;;;;   <<<------ jumps here
    %endmacro
    

    然后在 %rep 中使用该宏

        .looptop:
    %rep 4
        jmpfwd
    %endrep
    ; times 4 jmpfwd   nope, TIMES only works on (pseudo)instructions, not macros
    
        dec ecx
        jnz .looptop
    

    (事实证明,Skylake can decode this without LCP stalls every iteration,当add 在无条件jmp 指令的分支预测生效之前命中与jmp 同组的解码器时,只有少数LCP 停止。times 21 nop防止它适合 uop 缓存。)

    【讨论】:

      【解决方案2】:

      这可以通过使用%+ 表示法来完成。以下是文档的摘录:

      4.1.4 连接单行宏标记:%+

      单行宏中的各个标记可以连接起来,以 生成更长的令牌以供以后处理。这可能很有用,如果 有几个类似的宏执行类似的功能。

      请注意,%+ 后需要一个空格,以便 消除它与多行宏中使用的语法 %+1 的歧义。

      有关预处理器中此功能和其他功能的更多信息,请访问here

      【讨论】:

        猜你喜欢
        • 2015-01-20
        • 2015-10-21
        • 1970-01-01
        • 1970-01-01
        • 2020-02-21
        • 1970-01-01
        • 2020-05-13
        • 2021-08-20
        • 1970-01-01
        相关资源
        最近更新 更多