免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: dadaball
打印 上一主题 下一主题

[C] c语言的尴尬:取常量地址 [复制链接]

论坛徽章:
0
41 [报告]
发表于 2004-09-12 18:38 |只看该作者

c语言的尴尬:取常量地址


  1. main()
  2. {int *p,i;
  3. i=100;
  4. p=&
  5. printf("%d\n",i); /*想第一次输出100*/
  6. printf("%d\n",*p);
  7. getch(); /*执行程序2,再按任意键*/
  8. printf("%d\n",i); /*通过程序2输出50*/
  9. }
复制代码

论坛徽章:
0
42 [报告]
发表于 2004-09-12 20:55 |只看该作者

c语言的尴尬:取常量地址

原帖由 "ljttlf" 发表:
另外>;>;但从概念上看,c 与汇编没有直接关系
怎么说呢?c语言是最符合现在使用的计算机体系结构的语言,从最初的c语言实现可以看出来,c代码都是首先翻译成汇编的阿,然后再用as,ld。

原帖由 "The C Programming Language" 发表:

In 1983, the American National Standards Institute (ANSI) established a committee whose  goal was to produce "an unambiguous and machine-independant definition of the language C," while still retaining its spirit.


So C is "machine-independant" and assembly is "machine-dependant". It's all right to use assembly as illustration. But keep in mind that there is a subtile logical problem here. An implementaion should follow the standard, not in reverse. I would ask that is the assembler put an literal 5 on a machine instruction so that the 5 without an address or, because the standard says literal 5 can't own an address so the assembler put it in an machine instruction?

论坛徽章:
0
43 [报告]
发表于 2004-09-12 20:59 |只看该作者

c语言的尴尬:取常量地址

solaris 下编译的~


  1.         .section        ".text",#alloc,#execinstr
  2.         .align  8
  3.         .skip   16

  4.         ! block 0

  5.         .global main
  6.         .type   main,2
  7. main:
  8.         save    %sp,-136,%sp

  9.         ! block 1
  10. .L175:
  11. .L176:

  12. ! File a.c:
  13. !    1  #include <stdio.h>;
  14. !    2  #include <stdlib.h>;
  15. !    3
  16. !    4  int main(void) {
  17. !    5          char    tmp[32];
  18. !    6
  19. !    7          printf("%p\n",tmp);

  20.         sethi   %hi(.L178),%o0
  21.         or      %o0,%lo(.L178),%o0
  22.         call    printf
  23.         add     %fp,-36,%o1

  24. !    8          printf("%p\n",&tmp[0]);

  25.         sethi   %hi(.L179),%o0
  26.         or      %o0,%lo(.L179),%o0
  27.         call    printf
  28.         add     %fp,-36,%o1

  29. !    9          printf("%p\n",&tmp);

  30.         sethi   %hi(.L180),%o0
  31.         or      %o0,%lo(.L180),%o0
  32.         call    printf
  33.         add     %fp,-36,%o1

  34. !   11          exit(0);

  35.         call    exit
  36.         mov     %g0,%o0
  37.         jmp     %i7+8
  38.         restore

  39.         ! block 2
  40. .L174:
  41.         jmp     %i7+8
  42.         restore
  43.         .size   main,(.-main)
  44.         .align  8

  45.         .section        ".rodata1",#alloc
  46.         .align  4
  47. .L178:
  48.         .ascii  "%p\n\000"
  49.         .type   .L178,#object
  50.         .size   .L178,4
  51.         .align  4
  52. .L179:
  53.         .ascii  "%p\n\000"
  54.         .type   .L179,#object
  55.         .size   .L179,4
  56.         .align  4
  57. .L180:
  58.         .ascii  "%p\n\000"
  59.         .type   .L180,#object
  60.         .size   .L180,4

  61.         .section        ".bss",#alloc,#write
  62. Bbss.bss:
  63.         .skip   0
  64.         .type   Bbss.bss,#object
  65.         .size   Bbss.bss,0

  66.         .section        ".data",#alloc,#write
  67. Ddata.data:
  68.         .skip   0
  69.         .type   Ddata.data,#object
  70.         .size   Ddata.data,0

  71.         .section        ".rodata",#alloc
  72. Drodata.rodata:
  73.         .skip   0
  74.         .type   Drodata.rodata,#object
  75.         .size   Drodata.rodata,0

  76.         .file   "a.c"
  77.         .xstabs ".stab.index","V=8.0;DBG_GEN=4.0.145;Xa;R=Forte Developer 7 C 5.
  78. 4 Patch 111708-09 2004/02/19",60,0,0,1094993767
  79.         .xstabs ".stab.index","/disks/claudius/home/student/23/s4014623; /opt/SU
  80. NWspro/bin/../prod/bin/cc -S  a.c",52,0,0,0
  81.         .xstabs ".stab.index","main",42,0,0,0
  82.         .ident  "@(#)stdio.h    1.79    01/04/16 SMI"
  83.         .ident  "@(#)stdio_iso.h        1.2     99/10/25 SMI"
  84.         .ident  "@(#)feature_tests.h    1.18    99/07/26 SMI"
  85.         .ident  "@(#)isa_defs.h 1.20    99/05/04 SMI"
  86.         .ident  "@(#)va_list.h  1.13    01/02/08 SMI"
  87.         .ident  "@(#)stdio_tag.h        1.3     98/04/20 SMI"
  88.         .ident  "@(#)stdio_impl.h       1.13    01/11/16 SMI"
  89.         .ident  "@(#)stdlib.h   1.48    00/04/13 SMI"
  90.         .ident  "@(#)stdlib_iso.h       1.3     01/03/09 SMI"
  91.         .ident  "acomp: Forte Developer 7 C 5.4 Patch 111708-09 2004/02/19"

  92.         .global __fsr_init_value
  93. __fsr_init_value = 0x0
复制代码

论坛徽章:
0
44 [报告]
发表于 2004-09-12 21:21 |只看该作者

c语言的尴尬:取常量地址

>;>; So C is "machine-independant" and assembly is "machine-dependant".

谢谢,学到。

>;>; I would ask that is the assembler put an literal 5 on a machine instruction so that the 5 without an address or, because the standard says literal 5 can't own an address so the assembler put it in an machine instruction?

你可以看看x86的硬件手册,基本上如果是立即数寻址方式,都是有直接的机器码相对应。这样可以节约从内存取得操作数的时间,也可以节约空间和指令(没有使用语句读取操作数)。
我看得是比较早的资料了,呵呵,是内部资料,因为国防军工的

论坛徽章:
0
45 [报告]
发表于 2007-01-19 14:03 |只看该作者

thank you all!

至此,我认为最初我问的问题已经完全解释清楚,敬佩各位对真理的不懈追求,同时特别感谢THEBEST, JohnBull,yichun,lixuzhang,ljttlf,他们的见解都很正确。

论坛徽章:
0
46 [报告]
发表于 2007-01-19 19:41 |只看该作者
编译下列代码:
const char global[10]="123456789\0";
const int globalint=5;

int main(void)
{
        const char local[10]="123456789\0";
        const int localint=5;
        printf("global: %x\n", global);
        printf("&global: %x\n", &global);
        printf("&globalint: %x\n", &globalint);
        printf("local: %x\n", local);
        printf("&local: %x\n", &local);
        printf("&localint: %x\n", &localint);
        return 0;
}

运行结果为:
global: 80484e0
&global: 80484e0
&globalint: 80484ec
local: bfcbbb3a
&local: bfcbbb3a
&localint: bfcbbb34

不知道lz说的不能取常量的地址是什么意思

论坛徽章:
0
47 [报告]
发表于 2007-01-19 19:49 |只看该作者
  1. --- independant
  2. +++ independent
复制代码


技术无关,呵呵

------------------------------

我猜楼主说的是类似这样的行为:

&(2)
&(3)
&(1.7320508 )

...

====

[ 本帖最后由 langue 于 2007-1-19 19:50 编辑 ]

论坛徽章:
0
48 [报告]
发表于 2007-01-19 19:52 |只看该作者
原帖由 langue 于 2007-1-19 19:49 发表
  1. --- independant
  2. +++ independent
复制代码


技术无关,呵呵

------------------------------

我猜楼主说的是类似这样的行为:

&(2)
&(3)
&(1.7320508 )

...

====


那就知道了,从头看到尾,我都没看懂这个贴在讨论什么,晕了!

论坛徽章:
0
49 [报告]
发表于 2007-01-19 19:58 |只看该作者
====

另外,我认为 13 楼已经给出了正确答案。

====

论坛徽章:
0
50 [报告]
发表于 2007-01-19 20:27 |只看该作者
原帖由 langue 于 2007-1-19 19:58 发表
====

另外,我认为 13 楼已经给出了正确答案。

====


同意,13楼的解释很正确
只是到后面越扯越远,都不知道在说些什么了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP