gcc生成汇编程序问题
/* 优化生成的汇编代码很容易就能够看出max对应的值以及相关循环控制
* 而未优化生成的汇编代码没有对应的max值?```cmpq -8(%rbp), %rax```
* 看了半天都感觉这个是无限循环啊,%rax(这个也没溢出啊)总是大于-8(%rbp)(为0)的,jl .L3就
* 一直循环。*/
#include <stdio.h>
#include <math.h>
int main(int argc, const char *argv[])
{
long i, max;
long sum = 0;
max = (long)pow(2,32);
for (i = 0; i < max; i++) {
sum += i;
}
printf("%ld\n", sum);
return 0;
}
用
gcc -S main.c
生成的汇编代码为:
.file "main.c"
.section .rodata
.LC0:
.string "%ld\n"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $48, %rsp
movl %edi, -36(%rbp)
movq %rsi, -48(%rbp)
movq $0, -16(%rbp)
movl $0, -8(%rbp)
movl $1, -4(%rbp)
movq $0, -24(%rbp)
jmp .L2
.L3:
movq -24(%rbp), %rax
addq %rax, -16(%rbp)
addq $1, -24(%rbp)
.L2:
movq -24(%rbp), %rax
cmpq -8(%rbp), %rax
jl .L3
movl $.LC0, %eax
movq -16(%rbp), %rdx
movq %rdx, %rsi
movq %rax, %rdi
movl $0, %eax
call printf
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
.section .note.GNU-stack,"",@progbits
而优化
gcc -S -O1 main.c
后的汇编代码为:
.file "main.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "%ld\n"
.text
.globl main
.type main, @function
main:
.LFB29:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $0, %eax
movabsq $4294967296, %rdx
.L2:
addq $1, %rax
cmpq %rdx, %rax
jne .L2
movabsq $9223372034707292160, %rdx
movl $.LC0, %esi
movl $1, %edi
movl $0, %eax
call __printf_chk
movl $0, %eax
addq $8, %rsp
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE29:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
.section .note.GNU-stack,"",@progbits
nigel
11 years, 9 months ago