![]() |
#2
xkwy00002013-10-05 15:25
|
现在在深入学习C语言,想在linux用汇编分析一下C语言各种变量的存储位置,用gcc -S hello.c生成的汇编代码却看不大懂,与我在网上查到的AT&T风格的汇编教程有一点不符啊。所以来请教一下各位高手。
我用的环境是ubuntu 12.04 x64
hello.c的代码如下:什么没干只是定义了很多变量。

int a = 100;
short int b = 200;
int c;
short int d;
static int e = 300;
static short int f = 400;
static int g;
static short int h;
const int i = 500;
const short int j = 600;
const int k;
const short int l;
char *str = "hello world!";
int main()
{
int m = 700;
short int n = 800;
int o;
short int p;
static int q = 900;
static short int r = 1000;
const int s = 1100;
const short t = 1200;
const int u;
const short int v;
char *str2 = "hello China!";
return 0;
}
short int b = 200;
int c;
short int d;
static int e = 300;
static short int f = 400;
static int g;
static short int h;
const int i = 500;
const short int j = 600;
const int k;
const short int l;
char *str = "hello world!";
int main()
{
int m = 700;
short int n = 800;
int o;
short int p;
static int q = 900;
static short int r = 1000;
const int s = 1100;
const short t = 1200;
const int u;
const short int v;
char *str2 = "hello China!";
return 0;
}
gcc -S hello.c生成的汇编代码如下:

.file "hello.c"
.globl a
.data
.align 4
.type a, @object
.size a, 4
a: ;不太理解的是a:后面应该只跟a的东西,可是根据对齐方式,发现b的东西也在下面
.long 100
.globl b;这里是b的定义了,为什么不另外抬头,难道只是gcc生成时没考虑到这一点?
.align 2
.type b, @object
.size b, 2
b:
.value 200
.comm c,4,4
.comm d,2,2
.align 4
.type e, @object
.size e, 4
e:
.long 300
.align 2
.type f, @object
.size f, 2
f:
.value 400
.local g
.comm g,4,4
.local h
.comm h,2,2
.globl i
.section .rodata
.align 4
.type i, @object
.size i, 4
i:
.long 500
.globl j
.align 2
.type j, @object
.size j, 2
j:
.value 600
.comm k,4,4
.comm l,2,2
.globl str
.LC0:
.string "hello world!"
.data
.align 8
.type str, @object
.size str, 8
str:
.quad .LC0
.section .rodata
.LC1:
.string "hello China!"
.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
movl $700, -12(%rbp)
movw $800, -4(%rbp)
movl $1100, -8(%rbp)
movw $1200, -2(%rbp)
movq $.LC1, -24(%rbp)
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.data
.align 2
.type r.1604, @object
.size r.1604, 2
r.1604:
.value 1000
.align 4
.type q.1603, @object
.size q.1603, 4
q.1603:
.long 900
.ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
.section .note.GNU-stack,"",@progbits
.globl a
.data
.align 4
.type a, @object
.size a, 4
a: ;不太理解的是a:后面应该只跟a的东西,可是根据对齐方式,发现b的东西也在下面
.long 100
.globl b;这里是b的定义了,为什么不另外抬头,难道只是gcc生成时没考虑到这一点?
.align 2
.type b, @object
.size b, 2
b:
.value 200
.comm c,4,4
.comm d,2,2
.align 4
.type e, @object
.size e, 4
e:
.long 300
.align 2
.type f, @object
.size f, 2
f:
.value 400
.local g
.comm g,4,4
.local h
.comm h,2,2
.globl i
.section .rodata
.align 4
.type i, @object
.size i, 4
i:
.long 500
.globl j
.align 2
.type j, @object
.size j, 2
j:
.value 600
.comm k,4,4
.comm l,2,2
.globl str
.LC0:
.string "hello world!"
.data
.align 8
.type str, @object
.size str, 8
str:
.quad .LC0
.section .rodata
.LC1:
.string "hello China!"
.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
movl $700, -12(%rbp)
movw $800, -4(%rbp)
movl $1100, -8(%rbp)
movw $1200, -2(%rbp)
movq $.LC1, -24(%rbp)
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.data
.align 2
.type r.1604, @object
.size r.1604, 2
r.1604:
.value 1000
.align 4
.type q.1603, @object
.size q.1603, 4
q.1603:
.long 900
.ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
.section .note.GNU-stack,"",@progbits
我不知道这个结构是怎么安排的,网上所linux汇编的基本结构是这样的:
程序基本结构
.data
...
.bss
...
.text
.global _start
_start:
...
1.为什么我没有在hello.s中看到.bss符号?
2.照理说,a和b的地位是平等的,为什么语句.globl a在.data的前面,.data好像把a的定义分隔成了两半,这是怎么回事?a,b不应该一起定义吗?
3..type和.size是什么意思?.type e,@object是说e是个普通变量吗?而.type main,@functon是说main是个函数?.size j,2是说j的占用内存2个字节吗?
4. .value和.long是同一个意思吗?都是表示变量的初始化值吗?
5. .comm又是什么意思?.comm h,2,2是说变量h占用大小为2,按2个字节对齐吗?.comm和.bss有什么关系吗?
6.这种汇编定义变量(也许我说“变量”这个词,用错了术语)好像不是按类型批量定义,而是一个一个的定义,而且看起来好没条理,我学8086的时候都是集中在一个段中定义变量的。
比如b的定义
.globl b ;意思是b是一个全局变量?
.align 2 ;对齐方式?
.type b,@object ;b是一个对象,不是函数?
.size b,2 ;b占用两个字节?
b: ;我最不理解这里,为什么b:不出现在整个b定义的最前面
value 200 ;b的初始化值?
为什么不像下面这样定义呢?
b:
.globl
.align 2
.type @boject
.size 2
value 200
7. .LC0,LC1,LFB0,LFE0是什么意思?
8. r.1604 q.1603只是 r q变量名字的改编吗?
[ 本帖最后由 chaoc 于 2013-10-4 16:51 编辑 ]