我正在裸机 ARM 上编写一个 sdram 测试程序。我用 C 编写了它,但现在我想修改生成的程序集以防止程序使用 sdram,这意味着除其他外,没有堆栈。
我最近开始学习ARM汇编,不明白编译器生成的汇编是如何使用下面代码中的堆栈的(我通过阅读ARM ARM :/也找不到答案)。32位的变量值放在栈上,但是为什么push在函数开头保留3乘以32位呢?有人可以在这里解释堆栈操作吗?
C代码:
/* ugly to have it as global but it reduces stack usage*/
unsigned int const led_port[]= {0,0,1,1,2,2,3,3,4,4};
unsigned int const led_value_on[]={0x90,0x9,0x90,0x9,0x90,0x9,0x90,0x9,0x90,0x9};
unsigned int const masks[] = {0xf0,0xf,0xf0,0xf,0xf0,0xf,0xf0,0xf,0xf0,0xf};
unsigned int const led_value_off[]={0x80,0x8,0x80,0x8,0x80,0x8,0x80,0x8,0x80,0x8};
void gbe_led_on(int i)
{
unsigned int value = 0;
phy_read(led_port[i], 0x10, &value);
value &= ~masks[i];
value |= led_value_on[i];
phy_write(led_port[i], 0x10, value);
}
生成的程序集(来自 gcc-arm-elf):
<gbe_led_off>:
push {r4, r5, r6, lr} /* ;reserve space on the stack for 3 32 bits variables + return address */
ldr r5, [pc, #84] ; ffff1578 <gbe_led_off+0x60> /*r5=led_port (array base address) */
sub sp, sp, #8 /* sp = sp-8 (decimal 8) what does it point to??*/
ldr r4, [r5, r0, lsl #2] /* r4 = *(led_port+i)&0x00ff, (shift from 16 bits) */
add r2, sp, #8 /* r2 = sp+8 (decimal 8) why???*/
mov r6, r0 /* r6 = i */
mov r3, #0 /* r3 = 0 */
mov r0, r4 /* r0 = led_port[i]*/
str r3, [r2, #-4]! /* r3 = *(sp+8-4); update r2, to which value???*/
add r5, r5, r6, lsl #2 /* r5 = led_port[i] & 0x00ff */
mov r1, #16 /* r1 = 16 (decimal) */
bl ffff13f8 <phy_read> /* call phy_read with arguments on r0, r1, r2*/
ldr r1, [r5, #40] ; 0x28 /* r1 = masks[i] */
ldr r3, [sp, #4] /* r3 = *(sp+4) ????*/
ldr r2, [r5, #120] ; 0x78 /* r2 = led_value_on[i] */
bic r3, r3, r1 /* value &= masks[i] */
orr r3, r3, r2 /* value |= led_value_on[i] */
mov r0, r4 /* r0 = led_port[i] */
mov r2, r3 /* r2 = value */
mov r1, #16 /* r1 = 16 */
str r3, [sp, #4] /* *(sp+4) = value; why do we do that???*/
bl ffff13cc <phy_write> /* branch to phy_write with arguments on r0,r1,r2*/
add sp, sp, #8 /* sp = sp+8 restore stack pointer before pop? */
pop {r4, r5, r6, pc} /* remove 4 bytes from the stack and branch to return address */
.word 0xffff1a30