C语言中有单最常见的代码在ARM的汇编中是如何实现的呢?
下面以for循环语句为例,处理器为LPC2368,在Keil μVision4中编译的到的结果:
两个全局数组的赋值操作
BYTE Memory[MSC_MemorySize]; /* MSC Memory in RAM */
const unsigned char DiskImage[MSC_ImageSize] = {
0xEB,0x3C,0x90,0x4D,0x53,0x44,0x4F,
0x53,0x35,0x2E,0x30, 0x00,0x02,0x01,
0x01,0x00,0x01,0x10,0x00,0x20...
}
for (n = 0; n < MSC_ImageSize; n++) { /* Copy Initial Disk Image */
Memory[n] = DiskImage[n]; /* from Flash to RAM */
}
汇编:
0x00001DC8 E3A04000 MOV R4,#0x00000000
0x00001DCC EA000004 B 0x00001DE4
0x00001DD0 E59F0034 LDR R0,[PC,#0x0034]
0x00001DD4 E7D00004 LDRB R0,[R0,R4]
0x00001DD8 E59F1030 LDR R1,[PC,#0x0030]
0x00001DDC E7C10004 STRB R0,[R1,R4]
0x00001DE0 E2844001 ADD R4,R4,#0x00000001
0x00001DE4 E3540A01 CMP R4,#0x00001000
0x00001DE8 3AFFFFF8 BCC 0x00001DD0
Memory中的地址:
0x00001E00: 5C FB FF EB 00 00 A0 E1 FE FF FF EA A4 28 00 00
0x00001E10: 74 00 00 40 00 C0 ...
......
0x000028A0: 00 00 00 00 EB 3C 90 4D 53 44 4F 53 35 2E 30 00
0x000028B0: 02 01 01 00 01 10 00 20 00 F8 02 00 01 00 01 00
理解时需要注意ARM7的一些特征:
1. LDR R0,[PC,#0x0034] ;读取PC+0x34地址上的存储单元的内容,放入R0
2. 采用三级流水线,PC=当前执行位置+8;
3. 支持大小端模式,一般默认小端模式。