先列出原程序:在Liunx下编程
led_c.S
@----------------------------------------
@function:leghten led for c and assembler
@----------------------------------------
.text
.global _start
_start:
@close wacthdog
ldr r0,=0x53000000
mov r1,#0x0
str r1,[r0]
@to c run
ldr sp,=1024*4
bl main
wite_loop:
b wite_loop
下面为ledc.c
#define gpfcon (*(volatile unsigned long *)0x56000050)
#define gpfdat (*(volatile unsigned long *)0x56000054)
void delay2(volatile unsigned long x){
for(;x>0;x--);
}
int main(viod)
{
while(1){
gpfcon = 0x55;
gpfdat = 0x0;
delay2(524288);
gpfcon = 0x55;
gpfdat = 0xff;
delay2(524288);
}
return 0;
}
以下为Makefile
CFLAGS := -Wall -Wstrict-prototypes -fomit-frame-pointer -ffreestanding
all : led_c.S ledc.c
arm-linux-gcc $(CFLAGS) -c -o led_c.o led_c.S
arm-linux-gcc $(CFLAGS) -c -o ledc.o ledc.c
arm-linux-ld -Ttext 0x00000000 led_c.o ledc.o -o ledc_elf
# arm-linux-ld -Tledc.lds led_c.o ledc.o -o ledc_elf
arm-linux-objcopy -O binary -S ledc_elf ledc.bin
arm-linux-objdump -D -m arm ledc_elf > ledc.dis
clean:
rm -f *.o ledc_elf ledc.dis ledc.bin
以上程序可以正常运行
这以下是在没有修改之前的Makefile
CFLAGS := -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -ffreestanding
all : led_c.S ledc.c
arm-linux-gcc $(CFLAGS) -c -o led_c.o led_c.S
arm-linux-gcc $(CFLAGS) -c -o ledc.o ledc.c
arm-linux-ld -Ttext 0x00000000 led_c.o ledc.o -o ledc_elf
# arm-linux-ld -Tledc.lds led_c.o ledc.o -o ledc_elf
arm-linux-objcopy -O binary -S ledc_elf ledc.bin
arm-linux-objdump -D -m arm ledc_elf > ledc.dis
clean:
rm -f *.o ledc_elf ledc.dis ledc.bin
用这个Makefile编译后下载到开发板时程序不能正常运行,要分析.S和.C之后确定程序是正确的,为什么不能正常运行呢?原因是在于优化上
先给出这两个的返汇编代码:
这个是正常的:
00000000 <_start>:
0: e3a00453 mov r0, #1392508928 ; 0x53000000
4: e3a01000 mov r1, #0 ; 0x0
8: e5801000 str r1, [r0]
c: e3a0da01 mov sp, #4096 ; 0x1000
10: eb00000b bl 44
00000014 :
14: eafffffe b 14
00000018 :
18: e24dd008 sub sp, sp, #8 ; 0x8
1c: e58d0004 str r0, [sp, #4]
20: ea000002 b 30
24: e59d3004 ldr r3, [sp, #4]
28: e2433001 sub r3, r3, #1 ; 0x1
2c: e58d3004 str r3, [sp, #4]
30: e59d3004 ldr r3, [sp, #4]
34: e3530000 cmp r3, #0 ; 0x0
38: 1afffff9 bne 24
3c: e28dd008 add sp, sp, #8 ; 0x8
40: e12fff1e bx lr
00000044 :
44: e52de004 push {lr} ; (str lr, [sp, #-4]!)
48: e24dd00c sub sp, sp, #12 ; 0xc
4c: e58d0004 str r0, [sp, #4]
50: e3a03456 mov r3, #1442840576 ; 0x56000000
54: e2833050 add r3, r3, #80 ; 0x50
58: e3a02055 mov r2, #85 ; 0x55
5c: e5832000 str r2, [r3]
60: e3a03456 mov r3, #1442840576 ; 0x56000000
64: e2833054 add r3, r3, #84 ; 0x54
68: e3a02000 mov r2, #0 ; 0x0
6c: e5832000 str r2, [r3]
70: e3a00702 mov r0, #524288 ; 0x80000
74: ebffffe7 bl 18
78: e3a03456 mov r3, #1442840576 ; 0x56000000
7c: e2833050 add r3, r3, #80 ; 0x50
80: e3a02055 mov r2, #85 ; 0x55
84: e5832000 str r2, [r3]
88: e3a03456 mov r3, #1442840576 ; 0x56000000
8c: e2833054 add r3, r3, #84 ; 0x54
90: e3a020ff mov r2, #255 ; 0xff
94: e5832000 str r2, [r3]
98: e3a00702 mov r0, #524288 ; 0x80000
9c: ebffffdd bl 18
a0: eaffffea b 50
Disassembly of section .comment
下面这是不正常的
00000000 <_start>:
0: e3a00453 mov r0, #1392508928 ; 0x53000000
4: e3a01000 mov r1, #0 ; 0x0
8: e5801000 str r1, [r0]
c: e3a0da01 mov sp, #4096 ; 0x1000
10: eb00000d bl 4c
00000014 :
14: eafffffe b 14
00000018 :
18: e24dd008 sub sp, sp, #8 ; 0x8
1c: e58d0004 str r0, [sp, #4]
20: e59d3004 ldr r3, [sp, #4]
24: e3530000 cmp r3, #0 ; 0x0
28: 0a000005 beq 44
2c: e59d3004 ldr r3, [sp, #4]
30: e2433001 sub r3, r3, #1 ; 0x1
34: e58d3004 str r3, [sp, #4]
38: e59d2004 ldr r2, [sp, #4]
3c: e3520000 cmp r2, #0 ; 0x0
40: 1afffff9 bne 2c
44: e28dd008 add sp, sp, #8 ; 0x8
48: e12fff1e bx lr
0000004c :
4c: e3a02456 mov r2, #1442840576 ; 0x56000000
50: e3a01055 mov r1, #85 ; 0x55
54: e3a0c000 mov ip, #0 ; 0x0
58: e3a000ff mov r0, #255 ; 0xff
5c: e5821050 str r1, [r2, #80]
60: e582c054 str ip, [r2, #84]
64: e3a03702 mov r3, #524288 ; 0x80000
68: e2533001 subs r3, r3, #1 ; 0x1
6c: 1afffffd bne 68
70: e5821050 str r1, [r2, #80]
74: e5820054 str r0, [r2, #84]
78: e3a03702 mov r3, #524288 ; 0x80000
7c: e2533001 subs r3, r3, #1 ; 0x1
80: 0afffff5 beq 5c
84: eafffffc b 7c
Disassembly of section .comment:
观察后才发现main数中没有调用 delay这个函数,原因是在makefile中我把它优化了(加了一个O2的选项),最后终于找出了问题,所以循环延时不起作用,-O2 除了涉及空间和速度交换的优化。几乎执行所有的优化工作,所以在编译时这些问题是隐含着的,优化时要格外小心才是