gcc -masm=intel 给我小写助记符

gcc -masm=intel gives me lowercase mnemonics

使用 with

编译一个简单的 C 程序
gcc -masm=intel basic.c -o basic.asm

生成的程序集如下所示:

.cfi_startproc
    push    rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    mov rbp, rsp /I expected mov to be MOV
    .cfi_def_cfa_register 6
    mov QWORD PTR [rbp-8], OFFSET FLAT:.LC0
    mov eax, 0
    pop rbp /I expected pop to POP
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc

我使用的是 Intel X86_64 机器,在 Intel 软件开发人员手册中,我知道助记符都是大写的(这意味着他们建议使用它?)。

gcc 有提供大写助记符的选项吗?

仅供参考,删除 gcc asm 输出中的 "noise"(.cfi 指令、未使用的标签等),请参阅 . TL:DR: just put your code on http://gcc.godbolt.org/


In the Intel software developer's manual, the mnemonics are represented all uppercase (which means they suggest using it?).

我认为他们使用全大写作为代码格式的替代方法,因此他们可以毫无混淆地编写诸如“Performs a bitwise AND operation”之类的东西(因为 "bitwise and operation" 也是一个有效的英语短语)。他们本可以选择 "perform a bitwise and operation",但他们没有。

他们使用全部大写的助记符和寄存器名称并不是说这是正确的编码方式。 Intel的教程/文章经常有大块的asm代码(like this one about detecting AVX). In Listing 1 of that, they use lowercase mnemonics and register names for everything except XGETBV, which I think they do to highlight it, because that's the new instruction that they're teaching you about. Their intrinsics guide在字段中也使用小写助记符显示助记符映射到哪条指令(编译器优化可以选择不同的insns...)。

助记符和寄存器名称不区分大小写。大多数人更喜欢小写。良好的风格还包括将您的操作数缩进到一致的列,注释也是如此,这样您的代码就不会显得参差不齐。并且总是缩进你的代码而不是标签,所以你可以快速看到标签(分支目标)。例如the source for a golfed (smallest code-size) Adler-32 in x86-64 machine code这一点说明了我认为还算不错的风格。

; optimized for code-size, not speed
slow_small_addler32:
    xor     eax,eax         ; scratch reg for loading bytes
    cdq                     ; edx: high=0
    lea     edi, [rdx+1]    ; edi: low=1
    ;jrcxz  .end            ; We don't handle len=0.  unlike rep, loop only checks rcx after decrementing
.byteloop:
    lodsb                   ; upper 24b of eax stays zeroed (no partial-register stall on Intel P6/SnB-family CPUs, thanks to the xor-zeroing)
    add     edi, eax        ; low += zero_extend(buf[i])
    add     edx, edi        ; high += low
    loop   .byteloop      ; only use LOOP for small code-size, not speed
.end:
    ;; exit when ecx = 0, eax = last byte of buf
    ;; lodsb at this point would load the terminating 0 byte, conveniently leaving eax=0
 ... more code

另请参阅此 NASM style guide, linked from the 标签 wiki。


Is there an option with gcc that gives me uppercase mnemonics?

没有。如果你愿意,你可以将指令助记符大写并用 sed 或其他东西注册名称。 (但你必须小心,不要混淆区分大小写的符号名称)。

支持 http://gcc.godbolt.org/ 的脚本中的模式匹配可能对此有用,但我没怎么看 js 源代码。