以下是使用gcc编译的C代码:
char *a="a";
char *d="d";
printf("%d\n", strcmp("a", "d"));
printf("%d\n", strcmp(a, "d"));
printf("%d\n", strcmp(a, d));
当我使用-O
编译时,输出结果为:
-1
-3
-1
当我没有使用
-O
编译时,输出结果为:-1
-3
-3
为什么输出不同,
strcmp
的代码是什么?int
strcmp (p1, p2)
const char *p1;
const char *p2;
{
register const unsigned char *s1 = (const unsigned char *) p1;
register const unsigned char *s2 = (const unsigned char *) p2;
unsigned char c1, c2;
do
{
c1 = (unsigned char) *s1++;
c2 = (unsigned char) *s2++;
if (c1 == '\0')
return c1 - c2;
}
while (c1 == c2);
return c1 - c2;
}
编辑:@AndreyT不相信我,所以这是GCC 4.2为我生成的汇编代码(OS X 10.7.5 64位Intel,默认优化级别-无标志):
.section __TEXT,__text,regular,pure_instructions
.globl _main
.align 4, 0x90
_main:
Leh_func_begin1:
pushq %rbp
Ltmp0:
movq %rsp, %rbp
Ltmp1:
subq $32, %rsp
Ltmp2:
leaq L_.str(%rip), %rax
movq %rax, -16(%rbp)
leaq L_.str1(%rip), %rax
movq %rax, -24(%rbp)
movl $-1, %ecx ; <- THIS!
xorb %dl, %dl
leaq L_.str2(%rip), %rsi
movq %rsi, %rdi
movl %ecx, %esi
movq %rax, -32(%rbp)
movb %dl, %al
callq _printf ; <- no call to `strcmp()` so far!
movq -16(%rbp), %rax
movq %rax, %rdi
movq -32(%rbp), %rsi
callq _strcmp ; <- strcmp()
movl %eax, %ecx
xorb %dl, %dl
leaq L_.str2(%rip), %rdi
movl %ecx, %esi
movb %dl, %al
callq _printf ; <- printf()
movq -16(%rbp), %rax
movq -24(%rbp), %rcx
movq %rax, %rdi
movq %rcx, %rsi
callq _strcmp ; <- strcmp()
movl %eax, %ecx
xorb %dl, %dl
leaq L_.str2(%rip), %rdi
movl %ecx, %esi
movb %dl, %al
callq _printf ; <- printf()
movl $0, -8(%rbp)
movl -8(%rbp), %eax
movl %eax, -4(%rbp)
movl -4(%rbp), %eax
addq $32, %rsp
popq %rbp
ret
Leh_func_end1:
.section __TEXT,__cstring,cstring_literals
L_.str:
.asciz "a"
L_.str1:
.asciz "d"
L_.str2:
.asciz "%d\n"
.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support
EH_frame0:
Lsection_eh_frame:
Leh_frame_common:
Lset0 = Leh_frame_common_end-Leh_frame_common_begin
.long Lset0
Leh_frame_common_begin:
.long 0
.byte 1
.asciz "zR"
.byte 1
.byte 120
.byte 16
.byte 1
.byte 16
.byte 12
.byte 7
.byte 8
.byte 144
.byte 1
.align 3
Leh_frame_common_end:
.globl _main.eh
_main.eh:
Lset1 = Leh_frame_end1-Leh_frame_begin1
.long Lset1
Leh_frame_begin1:
Lset2 = Leh_frame_begin1-Leh_frame_common
.long Lset2
Ltmp3:
.quad Leh_func_begin1-Ltmp3
Lset3 = Leh_func_end1-Leh_func_begin1
.quad Lset3
.byte 0
.byte 4
Lset4 = Ltmp0-Leh_func_begin1
.long Lset4
.byte 14
.byte 16
.byte 134
.byte 2
.byte 4
Lset5 = Ltmp1-Ltmp0
.long Lset5
.byte 13
.byte 6
.align 3
Leh_frame_end1:
.subsections_via_symbols
原始源代码如下:
#include <stdio.h>
#include <string.h>
int main()
{
const char *a = "a";
const char *d = "d";
printf("%d\n", strcmp("a", "d"));
printf("%d\n", strcmp(a, "d"));
printf("%d\n", strcmp(a, d));
return 0;
}
以下是它生成的输出(为了更好的证明,附有屏幕截图):
-1
-3
-3
movl $-1, 4(%esp)
movl $LC2, (%esp)
call _printf
movl $LC1, 4(%esp)
movl 28(%esp), %eax
movl %eax, (%esp)
call _strcmp
movl %eax, 4(%esp)
movl $LC2, (%esp)
call _printf
movl 24(%esp), %eax
movl %eax, 4(%esp)
movl 28(%esp), %eax
movl %eax, (%esp)
call _strcmp
movl %eax, 4(%esp)
正如你所看到的,只有两个strcmp
调用。第一个比较的结果-1是在编译时产生的,因为编译器知道"a"小于"d"。如果我使用-O,它会产生以下代码:
movl $-1, 4(%esp)
movl $LC0, (%esp)
call _printf
movl $-1, 4(%esp)
movl $LC0, (%esp)
call _printf
movl $-1, 4(%esp)
movl $LC0, (%esp)
call _printf
我遇到了问题
-1
-3
-1
以下是在Linux上使用GCC 4.1.2进行优化编译(-O4
)的输出结果。这里是编译器为main
生成的代码。
main:
.LFB25:
subq $8, %rsp
.LCFI0:
movl $-1, %esi
xorl %eax, %eax
movl $.LC0, %edi
call printf
movzbl .LC1(%rip), %edx
movzbl .LC2(%rip), %eax
movl %edx, %esi
subl %eax, %esi
jne .L2
movzbl .LC1+1(%rip), %esi
movzbl .LC2+1(%rip), %eax
subl %eax, %esi
.L2:
movl $.LC0, %edi
xorl %eax, %eax
call printf
movl $-1, %esi
movl $.LC0, %edi
xorl %eax, %eax
call printf
xorl %eax, %eax
addq $8, %rsp
ret
-3
)。我没有看到这种选择性行为中的任何逻辑,所以它可能只是优化器的怪癖。 -1
-1
-1
由于它调用了strcmp
,因此产生了输出。在这个标准库中,strcmp
是这样实现的:
<strcmp> mov (%rdi),%al
<strcmp+2> cmp (%rsi),%al
<strcmp+4> jne <strcmp+19>
<strcmp+6> inc %rdi
<strcmp+9> inc %rsi
<strcmp+12> test %al,%al
<strcmp+14> jne <strcmp>
<strcmp+16> xor %eax,%eax
<strcmp+18> retq
<strcmp+19> mov $0x1,%eax
<strcmp+24> mov $0xffffffff,%ecx
<strcmp+29> cmovb %ecx,%eax
<strcmp+32> retq
-1
,0
或+1
,即使可能被视为次优。strcmp()
实现在起作用:编译器内实现并在编译时完全计算的比较;由编译器提供但在运行时评估的内置函数;以及在运行时评估的标准库函数。你看到的确切输出取决于每个实现对测试输入生成的输出,以及每个比较使用的实现(这可能取决于优化设置)。 - cafstrcmp
返回< 0
表示字符串不相等。
这表明第二个字符串在第一个不匹配的字符上具有更高的值。确切的值是未指定的。
唯一定义的是输出是否为:
strcmp
的行为看起来是如此的不确定性? - AnT stands with Russia
strcmp()
的实际返回值),并将其替换为-1(一个相当随意的负数)。 - user529758strcmp(a, "d")
返回 -3 而另外两个返回 -1,你需要检查优化器。 - Jim Balter