This is my C code
void user_main(void)
{
u32 asd;
osGetSysCnt(&asd);
}
This is the disassembly
void user_main(void)
{
10040230: b507 push {r0, r1, r2, lr}
u32 asd;
osGetSysCnt(&asd);
10040232: a801 add r0, sp, #4
10040234: f000 f801 bl 1004023a <osGetSysCnt>
}
10040238: bd07 pop {r0, r1, r2, pc}
Why is the stack pointer incremented? Wouldn't that override the already pushed registers?
This is my C code
void user_main(void)
{
u32 asd;
osGetSysCnt(&asd);
}
This is the disassembly
void user_main(void)
{
10040230: b507 push {r0, r1, r2, lr}
u32 asd;
osGetSysCnt(&asd);
10040232: a801 add r0, sp, #4
10040234: f000 f801 bl 1004023a <osGetSysCnt>
}
10040238: bd07 pop {r0, r1, r2, pc}
Why is the stack pointer incremented? Wouldn't that override the already pushed registers?
Share Improve this question edited Nov 19, 2024 at 9:10 0___________ 68.5k4 gold badges38 silver badges86 bronze badges asked Nov 19, 2024 at 8:11 KorsarqKorsarq 7954 silver badges19 bronze badges1 Answer
Reset to default 4Why is the stack pointer incremented? Wouldn't that override the already pushed registers?
No, because sp
is not incremented in the disassembly code.
10040232: a801 add r0, sp, #4
Purpose of add
: The add
instruction calculates the memory address relative to the current value of sp (stack pointer). Here, add r0, sp, #4
computes the address sp + 4
, which points to the location of the variable asd
on the stack.
The resulting address is stored in r0
, which is then passed as a parameter to osGetSysCnt
.
Isn't ARM stack full descending? Meaning sp+4 refers to the already pushed at the start registers.
It is. The push
instruction is creating the stack frame sufficient for the function needs. The compiler does not care about what was there as ABI specifies that those registers do not have to be preserved. So this push
is not preserving anything.
Why does the compiler use push
instruction? It indicates that you are compiling with size optimizations enabled (-Os
). push
is short but time-consuming. If you change the optimization level to optimize for speed (-Ofast
, -O2
, -O3
) the compiler will emit different code:
str lr, [sp, #-4]!
ldr r3, .L4
sub sp, sp, #12
add r0, sp, #4
str r3, [sp, #4]
bl osGetSysCnt
add sp, sp, #12
ldr lr, [sp], #4
bx lr
https://godbolt./z/7GY63r6vv
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745577306a4634044.html
评论列表(0条)