主题 : 堆栈变量问题 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 8981
精华: 0
发帖: 10
金钱: 100 两
威望: 50 点
综合积分: 20 分
注册时间: 2009-09-13
最后登录: 2009-10-31
楼主  发表于: 2009-09-14 23:26

 堆栈变量问题

当使用C编程点亮LED时, 发现如下语句会影响运行结果:
        unsigned long mGPBconValue = 0x00014400; (main函数14行)
mGPBconValue 从未被使用,只是加入一个赋值语句而已.
- 屏蔽该语句,运行结果正常 (LED被正常点亮)
- 启用该语句,运行结果错误
哪位大侠指点一下....难道和函数堆栈有关系?

simon@lionteeth:~/work/studycdrom/hardware/led_on_c$ cat -n led_on_c2.c
     1  #define pGPBCON (volatile unsigned long*)(0x56000010)
     2  #define GPIO_OUTPUT 0x01
     3  #define pGPBDATA (volatile unsigned long*)(0x56000014)
     4  #define GET_GPIOCON_OUTPUTBITS(x) (GPIO_OUTPUT << (2*x))
     5
     6  /* Let me light LED1, LED3, LED4
     7   LED1: GPB5
     8   LED3: GPB7
     9  LED4:  GPB8
    10   */
    11  int main()
    12  {
    13
    14          //unsigned long mGPBconValue = 0x00014400;
    15
    16          //mGPBconValue |= GET_GPIOCON_OUTPUTBITS(5);
    17          //mGPBconValue |= GET_GPIOCON_OUTPUTBITS(7);
    18          //mGPBconValue |= GET_GPIOCON_OUTPUTBITS(8);
    19
    20          //*pGPBCON = mGPBconValue;
    21          //*pGPBCON = 0x00015400;
    22          //*pGPBCON = 0x00015400;
    23          //*pGPBCON = 0x00015000;
    24          *pGPBCON = GET_GPIOCON_OUTPUTBITS(5) |
    25                          GET_GPIOCON_OUTPUTBITS(6) |
    26                          GET_GPIOCON_OUTPUTBITS(7) |
    27                          GET_GPIOCON_OUTPUTBITS(8);
    28
    29          *pGPBDATA = 0;
    30
    31          while(1);
    32          return 0;
    33  }

simon@lionteeth:~/work/studycdrom/hardware/led_on_c$ cat simoncrt.S
.text
.global _start
_start:

    mov sp, #1024*4
         bl main
loop:
         b loop


simon@lionteeth:~/work/studycdrom/hardware/led_on_c$ make clean;make
rm -f led_on_c2.dis led_on_c2.bin led_on_c2_elf *.o
arm-linux-gcc -g -c -o simoncrt.o simoncrt.S
arm-linux-gcc -g -c -o led_on_c2.o led_on_c2.c
arm-linux-ld -Ttext 0x0000000 -g  simoncrt.o led_on_c2.o -o led_on_c2_elf
arm-linux-objcopy -O binary -S led_on_c2_elf led_on_c2.bin
arm-linux-objdump -D -m arm  led_on_c2_elf > led_on_c2.dis