|
发表于 2017-1-20 17:19:06
|
显示全部楼层
本楼为最佳答案
cat test.c
#include <stdio.h>
int c = 10;
int main(void)
{
int i = 10;
while(i--)
{
c++;
}
printf("c = %d\n", c);
return 0;
}
如果你学过汇编语言,这个问题很好理解
gcc test.c -S
cat test.s
.file "test.c"
.globl c
.data
.align 4
.type c, @object
.size c, 4
c:
.long 10
.section .rodata
.LC0:
.string "c = %d\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 $16, %rsp
movl $10, -4(%rbp)
jmp .L2
.L3:
movl c(%rip), %eax
addl $1, %eax
movl %eax, c(%rip)
.L2:
cmpl $0, -4(%rbp)
setne %al
subl $1, -4(%rbp)
testb %al, %al
jne .L3
movl c(%rip), %edx
movl $.LC0, %eax
movl %edx, %esi
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: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-17)"
.section .note.GNU-stack,"",@progbits
c变量放到了data段
C语言中的while 、printf、return
这些已经编译成汇编语言,放到了.text 段(代码段)
text段,data段,bss段,堆和栈
http://www.cnblogs.com/hfww/archive/2011/06/04/2223366.html |
|