NASM %rep 部分中的标签

Label in %rep section in NASM

我有 %rep 预处理器指令,它创建一个预处理器循环。 我想在其中声明标签,可能有一些串联,但我的语法不正确。

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

那么如何强制 NASM 预处理器为每个 "iteration" 生成 label_i

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

4.1.4 Concatenating Single Line Macro Tokens: %+

Individual tokens in single line macros can be concatenated, to produce longer tokens for later processing. This can be useful if there are several similar macros that perform similar functions.

Please note that a space is required after %+, in order to disambiguate it from the syntax %+1 used in multiline macros.

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

如果您不需要从 %rep 块外部引用标签,宏内局部 %%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 ,当 add 在无条件 [=16 的分支预测之前命中与 jmp 相同组中的解码器时,只有少数 LCP 停顿=] 指令生效。times 21 nop 阻止它放入 uop 缓存。)