免费注册 查看新帖 |

Chinaunix

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

高人请分析这段代码:gcc 与arm gcc 的差别 [复制链接]

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
11 [报告]
发表于 2009-06-03 09:55 |只看该作者

回复 #1 guode0724 的帖子

楼主感到奇怪的是, 为什么两段代码输出的结果不同吧?

我觉得是printf的问题。 两段代码中向printf传递的参数不同。

第1段代码中, 传递的是一个 unsigned short, 会被提升到int或者unsigned传递。
第2段代码中, 传递的是一个结构体。 应该如何传递我就不清楚了 ……

楼主可以试着这样改:
unsigned char content[sizeof(t1)];
memcpy(content,&t1,sizeof(content));
    for (i=0;i!=sizeof(content);++i)
        printf("%02X ",content[ i ]);
    printf("\n");

这样来查看t1(两段代码中分别是 unsigned short和一个结构体)中的内容。
剩下的,就真的是大端小端问题了。 6次输出应该是一致的。
我这里没有楼主说的平台 ……  测不了 ……
代码贴这里, 楼主自己试试吧


#include <stdio.h>
#include <string.h>

typedef struct ctst{
    unsigned short  pll_iset :2,
                    pll_inv :2,
                    pll_reset :1,
                    pll_r :9,
                    rfu :2;
}TST;

int main(void)
{
    unsigned short t1;
    unsigned char content[sizeof(t1)];
    size_t i;

    t1 = 0x0000;
    ((TST *)&t1)->pll_r = 0x18;
    ((TST *)&t1)->pll_inv = 1;

    memcpy(content,&t1,sizeof(content));
    for (i=0;i!=sizeof(content);++i)
        printf("%02X ",content[ i ]);
    printf("\n");

    ((TST *)&t1)->pll_inv = 2;

    memcpy(content,&t1,sizeof(t1));
    for (i=0;i!=sizeof(content);++i)
        printf("%02X ",content[ i ]);
    printf("\n");  

    ((TST *)&t1)->pll_inv = 3;

    memcpy(content,&t1,sizeof(t1));
    for (i=0;i!=sizeof(content);++i)
        printf("%02X ",content[ i ]);
    printf("\n");

    return 0;
}

[ 本帖最后由 OwnWaterloo 于 2009-6-3 09:57 编辑 ]

论坛徽章:
0
12 [报告]
发表于 2009-06-03 17:07 |只看该作者
我用gdb调试的:
在arm上

  1. (gdb) c
  2. Continuing.

  3. Breakpoint 1, main () at test.c:15
  4. 15            t1 = 0x0000;
  5. (gdb) x/t &t1
  6. 0xbef3de8e:        00000000000000000000000000000000
  7. (gdb) s

  8. Breakpoint 3, main () at test.c:17
  9. 17            ((TST *)&t1)->pll_r = 0x18;
  10. (gdb) x/t &t1
  11. 0xbef3de8e:        00000000000000000000000000000000
  12. (gdb) s

  13. Breakpoint 4, main () at test.c:18
  14. 18            ((TST *)&t1)->pll_inv = 1;
  15. (gdb) x/t &t1
  16. 0xbef3de8e:        00000000000000001000010001110100
  17. (gdb) s
  18. Cannot access memory at address 0x0
  19. 20            printf("the t1 = 0x%x\n",t1);
  20. (gdb) x/t &t1
  21. 0xbef3de8e:        00000000000000000000001100000000

复制代码

感觉怪怪的,谁来解释下呢
x86上到正常

  1. Breakpoint 1, main () at test.c:15
  2. 15            t1 = 0x0000;
  3. (gdb) x/t &t1
  4. 0xbfcb97e2:        10011000000000001011100000001010
  5. (gdb) s

  6. Breakpoint 2, main () at test.c:17
  7. 17            ((TST *)&t1)->pll_r = 0x18;
  8. (gdb) x/t &t1
  9. 0xbfcb97e2:        10011000000000000000000000000000
  10. (gdb) s

  11. Breakpoint 3, main () at test.c:18
  12. 18            ((TST *)&t1)->pll_inv = 1;
  13. (gdb) x/t &t1
  14. 0xbfcb97e2:        10011000000000000000001100000000
  15. (gdb) s
  16. 20            printf("the t1 = 0x%x\n",t1);
  17. (gdb) x/t &t1
  18. 0xbfcb97e2:        10011000000000000000001100000100
  19. (gdb)
复制代码

论坛徽章:
0
13 [报告]
发表于 2009-06-03 17:42 |只看该作者

回复 #11 OwnWaterloo 的帖子

不是printf打印问题,这个问题是我在相关硬件调试时发现
就是因为这个问题导致我硬件的某些关键参数配置不正确导致硬件无法正常进入状态
在我采用别的方式正确赋值后硬件才正常

论坛徽章:
0
14 [报告]
发表于 2009-06-08 20:57 |只看该作者
ARM芯片是大小端都支持的,你程序中配置ARM芯片的运行模式需要和你编译器编译程序时设置的相同才行

论坛徽章:
0
15 [报告]
发表于 2009-06-08 23:45 |只看该作者
原帖由 eveson 于 2009-6-8 20:57 发表
ARM芯片是大小端都支持的,你程序中配置ARM芯片的运行模式需要和你编译器编译程序时设置的相同才行

总算有人说出来了
omap5910的ARM是默认的小端,而DSP默认是大端

论坛徽章:
0
16 [报告]
发表于 2009-06-12 13:41 |只看该作者
已解决,最终原因是对齐的原因,在arm-linux-gcc 中默认的是4字节对齐

在X86中gcc默认的是字节对齐

程序做如下修改就对了
struct ctst{
        unsigned short pll_iset :2,
                        pll_inv :2,
                        pll_reset :1,
                        pll_r :9,
                        rfu :2;
}__attribute__((packed));
typedef struct ctst TST;

int main(void)
{
        unsigned short t1;
        
        t1 = 0x0000;
        ((TST *)&t1)->pll_r = 0x18;
        ((TST *)&t1)->pll_inv = 1;
        
        printf("the t1 = 0x%x\n",t1);

        ((TST *)&t1)->pll_inv = 2;
        
        printf("the t1 = 0x%x\n",t1);        

        ((TST *)&t1)->pll_inv = 3;
        
        printf("the t1 = 0x%x\n",t1);

        return 0;
}
谢谢大家的热情帮助
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP