我的预感是(我刚刚使用g++ -S 和gcc -S 验证了这一点)您自己的标签完全模仿了自动分配给 GCC 中的汇编代码的标签的命名方案 (.L<num>)。
执行以下操作:
# for C:
gcc -S source.c
# for C++
g++ -S source.cpp
...然后cat(或less)生成.s文件(相同的基本名称,.s后缀,例如source.s)。您会发现该方案的许多标签 (.L<num>)。现在,如果您自己创建的内联程序集包含与已经自动创建的标签(来自您的 C 代码)相同的名称,那显然会导致冲突。
所以要点:不要使用.L<num> 作为标签的命名方案,因为它会发生冲突。
一般以.L开头的名字似乎在这里自找麻烦。
示例(test.cpp),使用g++ -S test.cpp编译:
#include <cstdio>
int main(int argc, char**argv)
{
switch(argc)
{
case 0:
printf("test 0\n");
break;
case 1:
printf("test %d\n", argc);
break;
case 2:
printf("test %d -> %s\n", argc, argv[0]);
break;
default:
printf("Default\n");
break;
}
return 0;
}
在 x64 上编译(test.s 的内容):
.file "test.cpp"
.section .rodata
.LC0:
.string "test 0"
.LC1:
.string "test %d\n"
.LC2:
.string "test %d -> %s\n"
.LC3:
.string "Default"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
.cfi_personality 0x3,__gxx_personality_v0
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
subq $16, %rsp
movl %edi, -4(%rbp)
movq %rsi, -16(%rbp)
movl -4(%rbp), %eax
cmpl $1, %eax
je .L4
cmpl $2, %eax
je .L5
testl %eax, %eax
jne .L8
.L3:
movl $.LC0, %edi
call puts
jmp .L6
.L4:
movl -4(%rbp), %eax
movl %eax, %esi
movl $.LC1, %edi
movl $0, %eax
call printf
jmp .L6
.L5:
movq -16(%rbp), %rax
movq (%rax), %rdx
movl -4(%rbp), %eax
movl %eax, %esi
movl $.LC2, %edi
movl $0, %eax
call printf
jmp .L6
.L8:
movl $.LC3, %edi
call puts
.L6:
movl $0, %eax
leave
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Debian 4.4.5-8) 4.4.5"
.section .note.GNU-stack,"",@progbits
观察生成的汇编文件中以.L 开头的名称。