由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问个超简单的C问题
相关主题
dll求救一个debug的问题
请问在ASP.net中用Javascript的一个问题不同compiler速度可以差很远吗?
[合集] 关于查内存泄露After build,how to run the program on visual C# 2008
c++的问题C++现在写起来真舒服啊
VC++ release VS debugSQL debug step into a store procedure from another one (转载)
what is the diff between release build and debug build?大家不觉得这篇文章很有道理么?未来语言的趋势?
how to debug mpi?question about structure initializationa and reference
能否给些讲debug经验的文章和书籍 (转载)关于C/C++里的Static variable的memory allocation/initializa
相关话题的讨论汇总
话题: 00000000话题: contents话题: 00话题: readonly话题: debugging
进入Programming版参与讨论
1 (共1页)
r****o
发帖数: 1950
1
void foo()
{
int a=3;
}
int main()
{
foo();
return 0;
}
上面的常数3是不是先放在RODATA 数据段,在main()函数执行foo()的时候再从RODATA读
出来赋给stack上面的a变量。
这样,RODATA和stack都有一个地方存了这个3。
对吗?
b***i
发帖数: 3043
2
不一定啊。我要写编译器,就作为立即数放了。或者优化了。

【在 r****o 的大作中提到】
: void foo()
: {
: int a=3;
: }
: int main()
: {
: foo();
: return 0;
: }
: 上面的常数3是不是先放在RODATA 数据段,在main()函数执行foo()的时候再从RODATA读

j*a
发帖数: 14423
3
gcc编译一下, objdump反编译一下,看看汇编代码

【在 r****o 的大作中提到】
: void foo()
: {
: int a=3;
: }
: int main()
: {
: foo();
: return 0;
: }
: 上面的常数3是不是先放在RODATA 数据段,在main()函数执行foo()的时候再从RODATA读

r****o
发帖数: 1950
4
Thanks,
那要是
void foo
{
int a[100]={1,2,3};
...
}
呢?

【在 b***i 的大作中提到】
: 不一定啊。我要写编译器,就作为立即数放了。或者优化了。
S**I
发帖数: 15689
5
gcc直接能生成汇编,不需要objdump。

【在 j*a 的大作中提到】
: gcc编译一下, objdump反编译一下,看看汇编代码
S**I
发帖数: 15689
6
这得看编译器优化到什么程度。开优化的情况下,foo可能就是一个ret:常量也好,数
组也好,都是对外部毫无影响的局部变量,直接就优化没了。main不调用foo都是有可
能的。

【在 r****o 的大作中提到】
: Thanks,
: 那要是
: void foo
: {
: int a[100]={1,2,3};
: ...
: }
: 呢?

m********2
发帖数: 89
7
It depends on the compiler and architech.
I think, for example, arm-gcc will, in general, try to embed numbers into
instructions as immediate numbers before try the .rodata section and the
numbers you give can be used as immediate numbers.

【在 r****o 的大作中提到】
: Thanks,
: 那要是
: void foo
: {
: int a[100]={1,2,3};
: ...
: }
: 呢?

r****o
发帖数: 1950
8
多谢,
咱先假设这个foo()里面还有很多其他操作用到了这个a[]数组吧,所以不能直接把这个
foo()优化没了。
然后呢?
r****o
发帖数: 1950
9
Thanks.
So in ARM, if the array initiazation is a little large, for example,
void foo()
{
int a[5][100] = {{1,2,...,100},{2,3,...,101},{3,4,...,102},{4,5,...,103},{
5,6,...,104}};
//some operation that will use a[][];
}
Then the initialization
{{1,2,...,100},{2,3,...,101},{3,4,...,102},{4,5,...,103},{5,6,...,104}}
will always be in RODATA. Correct?
And when foo() is executed, the CPU will copy the initialized data above
from RODATA to the stack for a[][] array. Correct?

【在 m********2 的大作中提到】
: It depends on the compiler and architech.
: I think, for example, arm-gcc will, in general, try to embed numbers into
: instructions as immediate numbers before try the .rodata section and the
: numbers you give can be used as immediate numbers.

r****o
发帖数: 1950
10
有人能回答一下吗?
10个包子求准确答案。呵呵。
相关主题
what is the diff between release build and debug build?一个debug的问题
how to debug mpi?不同compiler速度可以差很远吗?
能否给些讲debug经验的文章和书籍 (转载)After build,how to run the program on visual C# 2008
进入Programming版参与讨论
S**I
发帖数: 15689
11
还是很难说,要看编译器怎么干。你可以自己编译一下看看汇编。

,{

【在 r****o 的大作中提到】
: Thanks.
: So in ARM, if the array initiazation is a little large, for example,
: void foo()
: {
: int a[5][100] = {{1,2,...,100},{2,3,...,101},{3,4,...,102},{4,5,...,103},{
: 5,6,...,104}};
: //some operation that will use a[][];
: }
: Then the initialization
: {{1,2,...,100},{2,3,...,101},{3,4,...,102},{4,5,...,103},{5,6,...,104}}

d****n
发帖数: 12461
12
这也可能优化没了。循环都可以直接变成不循环呢。

【在 r****o 的大作中提到】
: 多谢,
: 咱先假设这个foo()里面还有很多其他操作用到了这个a[]数组吧,所以不能直接把这个
: foo()优化没了。
: 然后呢?

k****e
发帖数: 126
13
If a[][] is declared as static storage duration then it will certainly go to
.data segment. If not, the compiler will probably use immediate value
instead.
If you compare the following, (gcc version 4.5.3 -O0)
(1) array a[] has "auto" storage duration.
#include
#include
int foo()
{
int a[] = {3, 4, 5};
return a[2];
}
int main(void)
{
int temp;
temp = foo();
printf("%d", temp);
return 0;
}
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 00000054 00000000 00000000 0000021c 2**2
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
1 .data 00000000 00000000 00000000 00000000 2**2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ALLOC, LOAD, DATA
2 .bss 00000000 00000000 00000000 00000000 2**2
ALLOC
3 .debug_abbrev 00000070 00000000 00000000 00000270 2**0
CONTENTS, READONLY, DEBUGGING
4 .debug_info 00000192 00000000 00000000 000002e0 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
5 .debug_line 00000246 00000000 00000000 00000472 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
6 .debug_macinfo 00006233 00000000 00000000 000006b8 2**0
CONTENTS, READONLY, DEBUGGING
7 .rdata 00000004 00000000 00000000 000068eb 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
8 .debug_frame 00000054 00000000 00000000 000068ef 2**2
CONTENTS, RELOC, READONLY, DEBUGGING
9 .debug_loc 00000070 00000000 00000000 00006943 2**0
CONTENTS, READONLY, DEBUGGING
10 .debug_pubnames 00000023 00000000 00000000 000069b3 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
11 .debug_pubtypes 00000012 00000000 00000000 000069d6 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
12 .debug_aranges 00000020 00000000 00000000 000069e8 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
Disassembly of section .text:
00000000 :
#include
#include
int foo()
{
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 83 ec 10 sub $0x10,%esp
int a[] = {3, 4, 5};
6: c7 45 f4 03 00 00 00 movl $0x3,-0xc(%ebp)
d: c7 45 f8 04 00 00 00 movl $0x4,-0x8(%ebp)
14: c7 45 fc 05 00 00 00 movl $0x5,-0x4(%ebp)
return a[2];
1b: 8b 45 fc mov -0x4(%ebp),%eax
}
1e: c9 leave
1f: c3 ret
00000020
:
int main(void)
{
20: 55 push %ebp
21: 89 e5 mov %esp,%ebp
23: 83 e4 f0 and $0xfffffff0,%esp
26: 83 ec 20 sub $0x20,%esp
29: e8 00 00 00 00 call 2e
2a: DISP32 __main
int temp;
temp = foo();
2e: e8 cd ff ff ff call 0
33: 89 44 24 1c mov %eax,0x1c(%esp)
printf("%d", temp);
37: 8b 44 24 1c mov 0x1c(%esp),%eax
3b: 89 44 24 04 mov %eax,0x4(%esp)
3f: c7 04 24 00 00 00 00 movl $0x0,(%esp)
42: dir32 .rdata
46: e8 00 00 00 00 call 4b
47: DISP32 printf
return 0;
4b: b8 00 00 00 00 mov $0x0,%eax
}
50: c9 leave
51: c3 ret
52: 90 nop
53: 90 nop
(2) array a[] has "static" storage duration.
#include
#include
int foo()
{
static int a[] = {3, 4, 5};
return a[2];
}
int main(void)
{
int temp;
temp = foo();
printf("%d", temp);
return 0;
}
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 0000003c 00000000 00000000 0000021c 2**2
CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
1 .data 0000000c 00000000 00000000 00000258 2**2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
CONTENTS, ALLOC, LOAD, DATA
2 .bss 00000000 00000000 00000000 00000000 2**2
ALLOC
3 .debug_abbrev 00000070 00000000 00000000 00000264 2**0
CONTENTS, READONLY, DEBUGGING
4 .debug_info 00000195 00000000 00000000 000002d4 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
5 .debug_line 00000244 00000000 00000000 00000469 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
6 .debug_macinfo 00006233 00000000 00000000 000006ad 2**0
CONTENTS, READONLY, DEBUGGING
7 .rdata 00000004 00000000 00000000 000068e0 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
8 .debug_frame 00000054 00000000 00000000 000068e4 2**2
CONTENTS, RELOC, READONLY, DEBUGGING
9 .debug_loc 00000070 00000000 00000000 00006938 2**0
CONTENTS, READONLY, DEBUGGING
10 .debug_pubnames 00000023 00000000 00000000 000069a8 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
11 .debug_pubtypes 00000012 00000000 00000000 000069cb 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
12 .debug_aranges 00000020 00000000 00000000 000069dd 2**0
CONTENTS, RELOC, READONLY, DEBUGGING
isassembly of section .text:
00000000 :
#include
#include
int foo()
{
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
static int a[] = {3, 4, 5};
return a[2];
3: a1 08 00 00 00 mov 0x8,%eax
4: dir32 .data
}
8: 5d pop %ebp
9: c3 ret
0000000a
:
int main(void)
{
a: 55 push %ebp
b: 89 e5 mov %esp,%ebp
d: 83 e4 f0 and $0xfffffff0,%esp
10: 83 ec 20 sub $0x20,%esp
13: e8 00 00 00 00 call 18
14: DISP32 __main
int temp;
temp = foo();
18: e8 e3 ff ff ff call 0
1d: 89 44 24 1c mov %eax,0x1c(%esp)
printf("%d", temp);
21: 8b 44 24 1c mov 0x1c(%esp),%eax
25: 89 44 24 04 mov %eax,0x4(%esp)
29: c7 04 24 00 00 00 00 movl $0x0,(%esp)
2c: dir32 .rdata
30: e8 00 00 00 00 call 35
31: DISP32 printf
return 0;
35: b8 00 00 00 00 mov $0x0,%eax
}
3a: c9 leave
3b: c3 ret

,{

【在 r****o 的大作中提到】
: Thanks.
: So in ARM, if the array initiazation is a little large, for example,
: void foo()
: {
: int a[5][100] = {{1,2,...,100},{2,3,...,101},{3,4,...,102},{4,5,...,103},{
: 5,6,...,104}};
: //some operation that will use a[][];
: }
: Then the initialization
: {{1,2,...,100},{2,3,...,101},{3,4,...,102},{4,5,...,103},{5,6,...,104}}

u**h
发帖数: 509
14
Only initialized global data will be stored
in data section;
const and string literals are normally in
RO data section;
uninitialized global data are in bss section;
The data variables in your examples are dynamically
allocated and initialized on stack.

【在 r****o 的大作中提到】
: 有人能回答一下吗?
: 10个包子求准确答案。呵呵。

m********2
发帖数: 89
15
It make sense for a large array of initialization data to be stored in .
rodata section to be read later with ldm instructions. But I am not sure. I
need to try some cases and get back to you.

,{

【在 r****o 的大作中提到】
: Thanks.
: So in ARM, if the array initiazation is a little large, for example,
: void foo()
: {
: int a[5][100] = {{1,2,...,100},{2,3,...,101},{3,4,...,102},{4,5,...,103},{
: 5,6,...,104}};
: //some operation that will use a[][];
: }
: Then the initialization
: {{1,2,...,100},{2,3,...,101},{3,4,...,102},{4,5,...,103},{5,6,...,104}}

r****o
发帖数: 1950
16
谢谢,
我比较了一下当initilization data是large array和short array的两种情况下的ARM
assembly code,发现确实前者会被stored 到RODATA,并且function执行的时候RODATA的
数据会被copy到stack。而short array就是直接在stack上用数值初始化了。

I

【在 m********2 的大作中提到】
: It make sense for a large array of initialization data to be stored in .
: rodata section to be read later with ldm instructions. But I am not sure. I
: need to try some cases and get back to you.
:
: ,{

1 (共1页)
进入Programming版参与讨论
相关主题
关于C/C++里的Static variable的memory allocation/initializaVC++ release VS debug
thread or processwhat is the diff between release build and debug build?
Marshal C++ struct to C# structhow to debug mpi?
C++一个string的小问题能否给些讲debug经验的文章和书籍 (转载)
dll求救一个debug的问题
请问在ASP.net中用Javascript的一个问题不同compiler速度可以差很远吗?
[合集] 关于查内存泄露After build,how to run the program on visual C# 2008
c++的问题C++现在写起来真舒服啊
相关话题的讨论汇总
话题: 00000000话题: contents话题: 00话题: readonly话题: debugging